diff --git a/zh-cn/application-dev/reference/apis-asset-store-kit/js-apis-asset.md b/zh-cn/application-dev/reference/apis-asset-store-kit/js-apis-asset.md index b63506988ec79082c0b2f05674c7b8a2fe616b2c..a3fbcfefa5f4eb0220c4f84809b1cd9935548de1 100755 --- a/zh-cn/application-dev/reference/apis-asset-store-kit/js-apis-asset.md +++ b/zh-cn/application-dev/reference/apis-asset-store-kit/js-apis-asset.md @@ -57,6 +57,7 @@ add(attributes: AssetMap): Promise\ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -71,11 +72,12 @@ attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); try { asset.add(attr).then(() => { console.info(`Asset added successfully.`); - }).catch(() => { - console.error(`Failed to add Asset.`); + }).catch((err: BusinessError) => { + console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); }) } catch (error) { - console.error(`Failed to add Asset.`); + let err = error as BusinessError; + console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -118,6 +120,7 @@ addSync(attributes: AssetMap): void ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -132,7 +135,8 @@ attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); try { asset.addSync(attr); } catch (error) { - console.error(`Failed to add Asset.`); + let err = error as BusinessError; + console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -177,6 +181,7 @@ remove(query: AssetMap): Promise\ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -188,11 +193,12 @@ query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); try { asset.remove(query).then(() => { console.info(`Asset removed successfully.`); - }).catch(() => { - console.error(`Failed to remove Asset.`); + }).catch((err: BusinessError) => { + console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to remove Asset.`); + let err = error as BusinessError; + console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -231,6 +237,7 @@ removeSync(query: AssetMap): void ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -242,7 +249,8 @@ query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); try { asset.removeSync(query); } catch (error) { - console.error(`Failed to remove Asset.`); + let err = error as BusinessError; + console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -290,6 +298,7 @@ update(query: AssetMap, attributesToUpdate: AssetMap): Promise\ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -303,11 +312,12 @@ attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new')); try { asset.update(query, attrsToUpdate).then(() => { console.info(`Asset updated successfully.`); - }).catch(() => { - console.error(`Failed to update Asset.`); + }).catch((err: BusinessError) => { + console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to update Asset.`); + let err = error as BusinessError; + console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -349,6 +359,7 @@ updateSync(query: AssetMap, attributesToUpdate: AssetMap): void ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -362,7 +373,8 @@ attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new')); try { asset.updateSync(query, attrsToUpdate); } catch (error) { - console.error(`Failed to update Asset.`); + let err = error as BusinessError; + console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -410,6 +422,7 @@ preQuery(query: AssetMap): Promise\ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -421,11 +434,12 @@ query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); try { asset.preQuery(query).then((challenge: Uint8Array) => { console.info(`Succeeded in pre-querying Asset.`); - }).catch (() => { - console.error(`Failed to pre-query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to pre-query Asset.`); + let err = error as BusinessError; + console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -473,6 +487,7 @@ preQuerySync(query: AssetMap): Uint8Array ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -484,7 +499,8 @@ query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); try { let challenge: Uint8Array = asset.preQuerySync(query); } catch (error) { - console.error(`Failed to pre-query Asset.`); + let err = error as BusinessError; + console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -532,6 +548,7 @@ query(query: AssetMap): Promise\> ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -547,11 +564,12 @@ try { let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number; } console.info(`Asset query succeeded.`); - }).catch (() => { - console.error(`Failed to query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to query Asset.`); + let err = error as BusinessError; + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -599,6 +617,7 @@ querySync(query: AssetMap): Array\ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -617,7 +636,8 @@ try { } } } catch (error) { - console.error(`Failed to query Asset.`); + let err = error as BusinessError; + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -657,6 +677,7 @@ postQuery(handle: AssetMap): Promise\ ```typescript import { asset } from '@kit.AssetStoreKit'; +import { BusinessError } from '@kit.BasicServicesKit'; let handle: asset.AssetMap = new Map(); // 此处传入的new Uint8Array(32)仅作为示例,实际应传入asset.preQuery执行成功返回的挑战值 @@ -664,11 +685,12 @@ handle.set(asset.Tag.AUTH_CHALLENGE, new Uint8Array(32)); try { asset.postQuery(handle).then(() => { console.info(`Succeeded in post-querying Asset.`); - }).catch (() => { - console.error(`Failed to post-query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to post-query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to post-query Asset.`); + let err = error as BusinessError; + console.error(`Failed to post-query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -702,6 +724,7 @@ postQuerySync(handle: AssetMap): void ```typescript import { asset } from '@kit.AssetStoreKit'; +import { BusinessError } from '@kit.BasicServicesKit'; let handle: asset.AssetMap = new Map(); // 此处传入的new Uint8Array(32)仅作为示例,实际应传入asset.preQuerySync执行成功返回的挑战值 @@ -709,7 +732,8 @@ handle.set(asset.Tag.AUTH_CHALLENGE, new Uint8Array(32)); try { asset.postQuerySync(handle) } catch (error) { - console.error(`Failed to post-query Asset.`); + let err = error as BusinessError; + console.error(`Failed to post-query Asset. Code is ${err.code}, message is ${err.message}`); } ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-js-add.md b/zh-cn/application-dev/security/AssetStoreKit/asset-js-add.md index 4a862fdc27a492ad77323ca07abc56f4ab97225d..c1d652e164d43acbe14a63f2bde5893f702ee74f 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-js-add.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-js-add.md @@ -49,6 +49,7 @@ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -63,10 +64,11 @@ attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); try { asset.add(attr).then(() => { console.info(`Asset added successfully.`); - }).catch(() => { - console.error(`Failed to add Asset.`); + }).catch((err: BusinessError) => { + console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); }) } catch (error) { - console.error(`Failed to add Asset.`); + let err = error as BusinessError; + console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); } ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-js-query-auth.md b/zh-cn/application-dev/security/AssetStoreKit/asset-js-query-auth.md index 72934c0aa49a56e480491ec39e550812836846bb..52bf779b1fab9c015bcf8178cd8ad856b2f9ae92 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-js-query-auth.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-js-query-auth.md @@ -72,6 +72,7 @@ import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; import userAuth from '@ohos.userIAM.userAuth'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -107,7 +108,8 @@ async function userAuthenticate(challenge: Uint8Array): Promise { }); userAuthInstance.start(); } catch (error) { - console.error(`User identity authentication failed.`); + let err = error as BusinessError; + console.error(`User identity authentication failed. Code is ${err.code}, message is ${err.message}`); reject(); } }) @@ -124,7 +126,8 @@ function preQueryAsset(): Promise { reject(); }) } catch (error) { - console.error(`Failed to pre-query Asset.`); + let err = error as BusinessError; + console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`); reject(); } }); @@ -137,7 +140,8 @@ async function postQueryAsset(challenge: Uint8Array) { await asset.postQuery(handle); console.info(`Succeeded in post-querying Asset.`); } catch (error) { - console.error(`Failed to post-query Asset.`); + let err = error as BusinessError; + console.error(`Failed to post-query Asset. Code is ${err.code}, message is ${err.message}`); } } @@ -166,8 +170,8 @@ async function queryAsset() { // step5. preQuery成功,后续操作失败,也需要调用asset.postQuery进行查询的后置处理。 postQueryAsset(challenge); } - }).catch (() => { - console.error(`Failed to pre-query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`); }) } ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-js-query.md b/zh-cn/application-dev/security/AssetStoreKit/asset-js-query.md index c6536c96377de007dbfc998f9ef0951af54f19ff..278fb0b11e3df9bc380c5cbb5dcfeb8f4fd904a3 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-js-query.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-js-query.md @@ -44,6 +44,7 @@ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -67,11 +68,12 @@ try { // parse uint8array to string let secretStr: string = arrayToString(secret); } - }).catch (() => { - console.error(`Failed to query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to query Asset.`); + let err = error as BusinessError; + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -82,6 +84,7 @@ try { ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -97,11 +100,12 @@ try { // parse the attribute. let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number; } - }).catch (() => { - console.error(`Failed to query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to query Asset.`); + let err = error as BusinessError; + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); } ``` @@ -112,6 +116,7 @@ try { ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -130,10 +135,11 @@ try { // parse the attribute. let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number; } - }).catch (() => { - console.error(`Failed to query Asset.`); + }).catch ((err: BusinessError) => { + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to query Asset.`); + let err = error as BusinessError; + console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); } ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-js-remove.md b/zh-cn/application-dev/security/AssetStoreKit/asset-js-remove.md index a92f55ab35093059e08f8da7f14d1c7df2b66ed9..345e43fc5075ffda9915c8ca9a4f9cb4fb9a3986 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-js-remove.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-js-remove.md @@ -34,6 +34,7 @@ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -45,10 +46,11 @@ query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 此处指定别名 try { asset.remove(query).then(() => { console.info(`Asset removed successfully.`); - }).catch(() => { - console.error(`Failed to remove Asset.`); + }).catch((err: BusinessError) => { + console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to remove Asset.`); + let err = error as BusinessError; + console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`); } ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-js-update.md b/zh-cn/application-dev/security/AssetStoreKit/asset-js-update.md index 64936577112812b021bc7c0079f395d609eeede5..7a2e30c4088fe96a3dea4fc312cf1b57efa391bb 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-js-update.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-js-update.md @@ -46,6 +46,7 @@ ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); @@ -60,10 +61,11 @@ attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new') try { asset.update(query, attrsToUpdate).then(() => { console.info(`Asset updated successfully.`); - }).catch(() => { - console.error(`Failed to update Asset.`); + }).catch((err: BusinessError) => { + console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { - console.error(`Failed to update Asset.`); + let err = error as BusinessError; + console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`); } ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-native-add.md b/zh-cn/application-dev/security/AssetStoreKit/asset-native-add.md index 0c21857abb480a30ef59b520aec3fb4f1a0a11ac..6b47674f35bcc26558cfd0cb2ba2f7950359b19f 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-native-add.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-native-add.md @@ -41,31 +41,37 @@ 新增一条密码是demo_pwd,别名是demo_alias,附属信息是demo_label的数据,该数据在用户首次解锁设备后可被访问。 -```c -#include - -#include "asset/asset_api.h" - -void AddAsset() { - static const char *SECRET = "demo_pwd"; - static const char *ALIAS = "demo_alias"; - static const char *LABEL = "demo_label"; - - Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; - Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; - Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; - Asset_Attr attr[] = { - { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED }, - { .tag = ASSET_TAG_SECRET, .value.blob = secret }, - { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, - { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, - }; - - int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0])); - if (ret == ASSET_SUCCESS) { - // Asset added successfully. - } else { - // Failed to add Asset. - } -} -``` +1. 在CMake脚本中链接相关动态库 + ```txt + target_link_libraries(entry PUBLIC libasset_ndk.z.so) + ``` + +2. 参考如下示例代码,进行业务功能开发 + ```c + #include + + #include "asset/asset_api.h" + + void AddAsset() { + static const char *SECRET = "demo_pwd"; + static const char *ALIAS = "demo_alias"; + static const char *LABEL = "demo_label"; + + Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; + Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; + Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; + Asset_Attr attr[] = { + { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED }, + { .tag = ASSET_TAG_SECRET, .value.blob = secret }, + { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, + { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, + }; + + int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0])); + if (ret == ASSET_SUCCESS) { + // Asset added successfully. + } else { + // Failed to add Asset. + } + } + ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-native-query.md b/zh-cn/application-dev/security/AssetStoreKit/asset-native-query.md index aa9cba6e4b6f21a779bdcca0f6797c63fea3bed7..14707676894b234c64d2735ac64afb84d10e6420 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-native-query.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-native-query.md @@ -38,92 +38,110 @@ 查询别名是demo_alias的关键资产明文。 -```c -#include - -#include "asset/asset_api.h" - -void QueryAsset() { - static const char *ALIAS = "demo_alias"; - Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; - Asset_Attr attr[] = { - { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 指定了关键资产别名,最多查询到一条满足条件的关键资产 - { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // 此处表示需要返回关键资产的所有信息,即属性+明文 - }; - - Asset_ResultSet resultSet = {0}; - int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); - if (ret == ASSET_SUCCESS) { - // Parse the resultSet. - for (uint32_t i = 0; i < resultSet.count; i++) { - // Parse the secret: the data is secret->blob.data, the size is secret->blob.size. - Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET); - } - } - OH_Asset_FreeResultSet(&resultSet); -} -``` +1. 在CMake脚本中链接相关动态库 + ```txt + target_link_libraries(entry PUBLIC libasset_ndk.z.so) + ``` + +2. 参考如下示例代码,进行业务功能开发 + ```c + #include + + #include "asset/asset_api.h" + + void QueryAsset() { + static const char *ALIAS = "demo_alias"; + Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; + Asset_Attr attr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 指定了关键资产别名,最多查询到一条满足条件的关键资产 + { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // 此处表示需要返回关键资产的所有信息,即属性+明文 + }; + + Asset_ResultSet resultSet = {0}; + int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); + if (ret == ASSET_SUCCESS) { + // Parse the resultSet. + for (uint32_t i = 0; i < resultSet.count; i++) { + // Parse the secret: the data is secret->blob.data, the size is secret->blob.size. + Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET); + } + } + OH_Asset_FreeResultSet(&resultSet); + } + ``` ### 查询单条关键资产属性 查询别名是demo_alias的关键资产属性。 -```c -#include - -#include "asset/asset_api.h" - -void QueryAttributes() { - static const char *ALIAS = "demo_alias"; - Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; - Asset_Attr attr[] = { - { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 指定了关键资产别名,最多查询到一条满足条件的关键资产 - { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // 此处表示仅返回关键资产属性,不包含关键资产明文 - }; - - Asset_ResultSet resultSet = {0}; - int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); - if (ret == ASSET_SUCCESS) { - // Parse the result. - for (uint32_t i = 0; i < resultSet.count; i++) { - // Parse the data label: the data is label->blob.data, the size is label->blob.size. - Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1); - } - } - OH_Asset_FreeResultSet(&resultSet); -} -``` +1. 在CMake脚本中链接相关动态库 + ```txt + target_link_libraries(entry PUBLIC libasset_ndk.z.so) + ``` + +2. 参考如下示例代码,进行业务功能开发 + ```c + #include + + #include "asset/asset_api.h" + + void QueryAttributes() { + static const char *ALIAS = "demo_alias"; + Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; + Asset_Attr attr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 指定了关键资产别名,最多查询到一条满足条件的关键资产 + { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // 此处表示仅返回关键资产属性,不包含关键资产明文 + }; + + Asset_ResultSet resultSet = {0}; + int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); + if (ret == ASSET_SUCCESS) { + // Parse the result. + for (uint32_t i = 0; i < resultSet.count; i++) { + // Parse the data label: the data is label->blob.data, the size is label->blob.size. + Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1); + } + } + OH_Asset_FreeResultSet(&resultSet); + } + ``` ### 批量查询关键资产属性 批量查询附属信息是demo_label的关键资产属性,从第5条满足条件的结果开始返回,一共返回10条,且返回结果以DATA_LABEL_NORMAL_1属性内容排序。 -```c -#include - -#include "asset/asset_api.h" - -void BatchQuery() { - static const char *LABEL = "demo_label"; - Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; - - Asset_Attr attr[] = { - { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, - { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, - { .tag = ASSET_TAG_RETURN_OFFSET, .value.u32 = 5 }, - { .tag = ASSET_TAG_RETURN_LIMIT, .value.u32 = 10 }, - { .tag = ASSET_TAG_RETURN_ORDERED_BY, .value.u32 = ASSET_TAG_DATA_LABEL_NORMAL_1 }, - }; - - Asset_ResultSet resultSet = { 0 }; - int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); - if (ret == ASSET_SUCCESS) { - // Parse the result. - for (uint32_t i = 0; i < resultSet.count; i++) { - // Parse the data alias: the data is alias->blob.data, the size is alias->blob.size.. - Asset_Attr *alias = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_ALIAS); - } - } - OH_Asset_FreeResultSet(&resultSet); -} -``` +1. 在CMake脚本中链接相关动态库 + ```txt + target_link_libraries(entry PUBLIC libasset_ndk.z.so) + ``` + +2. 参考如下示例代码,进行业务功能开发 + ```c + #include + + #include "asset/asset_api.h" + + void BatchQuery() { + static const char *LABEL = "demo_label"; + Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; + + Asset_Attr attr[] = { + { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, + { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, + { .tag = ASSET_TAG_RETURN_OFFSET, .value.u32 = 5 }, + { .tag = ASSET_TAG_RETURN_LIMIT, .value.u32 = 10 }, + { .tag = ASSET_TAG_RETURN_ORDERED_BY, .value.u32 = ASSET_TAG_DATA_LABEL_NORMAL_1 }, + }; + + Asset_ResultSet resultSet = { 0 }; + int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); + if (ret == ASSET_SUCCESS) { + // Parse the result. + for (uint32_t i = 0; i < resultSet.count; i++) { + // Parse the data alias: the data is alias->blob.data, the size is alias->blob.size.. + Asset_Attr *alias = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_ALIAS); + } + } + OH_Asset_FreeResultSet(&resultSet); + } + ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-native-remove.md b/zh-cn/application-dev/security/AssetStoreKit/asset-native-remove.md index 32ae47a5e3b9322da8db2103853f3e3cb22142b4..057c4ba89f71da09d842061e34a929e00673dba9 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-native-remove.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-native-remove.md @@ -27,24 +27,30 @@ 删除别名是demo_alias的关键资产。 -```c -#include - -#include "asset/asset_api.h" - -void RemoveAsset() { - static const char *ALIAS = "demo_alias"; - Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; - - Asset_Attr attr[] = { - { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 此处指定别名删除,也可不指定别名删除多条数据 - }; - - int32_t ret = OH_Asset_Remove(attr, sizeof(attr) / sizeof(attr[0])); - if (ret == ASSET_SUCCESS) { - // Asset removed successfully. - } else { - // Failed to remove Asset. - } -} -``` +1. 在CMake脚本中链接相关动态库 + ```txt + target_link_libraries(entry PUBLIC libasset_ndk.z.so) + ``` + +2. 参考如下示例代码,进行业务功能开发 + ```c + #include + + #include "asset/asset_api.h" + + void RemoveAsset() { + static const char *ALIAS = "demo_alias"; + Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; + + Asset_Attr attr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 此处指定别名删除,也可不指定别名删除多条数据 + }; + + int32_t ret = OH_Asset_Remove(attr, sizeof(attr) / sizeof(attr[0])); + if (ret == ASSET_SUCCESS) { + // Asset removed successfully. + } else { + // Failed to remove Asset. + } + } + ``` diff --git a/zh-cn/application-dev/security/AssetStoreKit/asset-native-update.md b/zh-cn/application-dev/security/AssetStoreKit/asset-native-update.md index fcef9d4c348a3079d8700c8d02908fad65854e3c..ce34dac4cddd8887c4efe218b408010940fb25d3 100755 --- a/zh-cn/application-dev/security/AssetStoreKit/asset-native-update.md +++ b/zh-cn/application-dev/security/AssetStoreKit/asset-native-update.md @@ -41,31 +41,37 @@ 更新别名是demo_alias的关键资产,将关键资产明文更新为demo_pwd_new,附属信息更新成demo_label_new。 -```c -#include - -#include "asset/asset_api.h" - -void UpdateAsset() { - static const char *ALIAS = "demo_alias"; - static const char *SECRET = "demo_pwd_new"; - static const char *LABEL = "demo_label_new"; - - Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; - Asset_Blob new_secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; - Asset_Blob new_label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; - Asset_Attr query[] = { { .tag = ASSET_TAG_ALIAS, .value.blob = alias } }; - Asset_Attr attributesToUpdate[] = { - { .tag = ASSET_TAG_SECRET, .value.blob = new_secret }, - { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = new_label }, - }; - - int32_t ret = OH_Asset_Update(query, sizeof(query) / sizeof(query[0]), attributesToUpdate, - sizeof(attributesToUpdate) / sizeof(attributesToUpdate[0])); - if (ret == ASSET_SUCCESS) { - // Asset updated successfully. - } else { - // Failed to update Asset. - } -} -``` +1. 在CMake脚本中链接相关动态库 + ```txt + target_link_libraries(entry PUBLIC libasset_ndk.z.so) + ``` + +2. 参考如下示例代码,进行业务功能开发 + ```c + #include + + #include "asset/asset_api.h" + + void UpdateAsset() { + static const char *ALIAS = "demo_alias"; + static const char *SECRET = "demo_pwd_new"; + static const char *LABEL = "demo_label_new"; + + Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; + Asset_Blob new_secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; + Asset_Blob new_label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; + Asset_Attr query[] = { { .tag = ASSET_TAG_ALIAS, .value.blob = alias } }; + Asset_Attr attributesToUpdate[] = { + { .tag = ASSET_TAG_SECRET, .value.blob = new_secret }, + { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = new_label }, + }; + + int32_t ret = OH_Asset_Update(query, sizeof(query) / sizeof(query[0]), attributesToUpdate, + sizeof(attributesToUpdate) / sizeof(attributesToUpdate[0])); + if (ret == ASSET_SUCCESS) { + // Asset updated successfully. + } else { + // Failed to update Asset. + } + } + ```