You can use OH_Asset_Add to add an asset.
The following table describes the attributes for adding an asset.
In the following table, the attributes ASSET_TAG_ALIAS and those starting with ASSET_TAG_DATA_LABEL are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put sensitive personal data in these attributes.
| Attribute Name (Asset_Tag) | Attribute Content (Asset_Value) | Mandatory | Description |
|---|---|---|---|
| ASSET_TAG_SECRET | Type: uint8[] Length: 1-1024 bytes | Yes | Asset in plaintext. |
| ASSET_TAG_ALIAS | Type: uint8[] Length: 1-256 bytes | Yes | Asset alias, which uniquely identifies an asset. |
| ASSET_TAG_ACCESSIBILITY | Type: uint32_t Value range: see Asset_Accessibility. | No | Access control based on whether the screen is locked. The default value is ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED, indicating that the asset can be accessed after the device is unlocked for the first time. |
| ASSET_TAG_REQUIRE_PASSWORD_SET | Type: bool | No | Whether the asset is accessible only when a lock screen password is set. The value true means that the asset can be accessed only when the user sets a lock screen password. The value false means that the asset can be accessed regardless of whether the user sets a lock screen password. The default value is false. |
| ASSET_TAG_AUTH_TYPE | Type: uint32_t Value range: see Asset_AuthType. | No | User authentication type required for accessing an asset. The default value is ASSET_AUTH_TYPE_NONE, indicating that user authentication is not required before the user accesses an asset. |
| ASSET_TAG_SYNC_TYPE | Type: uint32_t Value range: see Asset_SyncType. | No | Sync type supported by the asset. The default value is ASSET_SYNC_TYPE_NEVER, indicating that the asset cannot be synced. |
| ASSET_TAG_IS_PERSISTENT | Type: bool | No | Whether to retain the asset when the application is uninstalled. The value true means to retain the asset when the application is uninstalled; the value false means the opposite. Default value: false. NOTE: If this parameter is set, the application must apply for the ohos.permission.STORE_PERSISTENT_DATA permission. |
| ASSET_TAG_DATA_LABEL_CRITICAL_1 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_CRITICAL_2 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_CRITICAL_3 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_CRITICAL_4 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_1 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_2 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_3 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_4 | Type is uint8[], length is 1-2048 bytes. | Optional | Attached information of an asset. The content is service-defined and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_112+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_212+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_312+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_412+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_CONFLICT_RESOLUTION | Type: uint32_t Value range: see Asset_ConflictResolution. | No | Conflict (for example, duplicate aliases) resolution when an asset is added. The default value is ASSET_CONFLICT_THROW_ERROR, indicating that an exception is thrown for subsequent processing by the service. |
| ASSET_TAG_REQUIRE_ATTR_ENCRYPTED14+ | Type: bool | No | Whether to encrypt the additional asset information customized by the service. The value true means to encrypt the additional asset information customized by the service; the value false means the opposite. Default value: false. |
| ASSET_TAG_GROUP_ID18+ | Type: Uint8[] Length: 7-127 bytes | No | Group to which the asset to be added belongs. By default, the newly added asset does not belong to any group. |
| ASSET_TAG_WRAP_TYPE18+ | Type: uint32_t Value range: see Asset_WrapType. | No | Encrypted import/export type supported by the asset. The default value is ASSET_WRAP_TYPE_NEVER, indicating that encrypted import/export is not supported. |
Alias-based access
Assets are stored in the ASSET database in ciphertext. The service identity and alias are used as the unique index. The alias of each asset must be unique.
Custom service data storage
Asset Store Kit provides 12 custom asset attributes starting with ASSET_TAG_DATA_LABEL for services. If the 12 custom attributes are used, you can combine multiple data segments in a certain format (for example, JSON) into an ASSET attribute.
Asset Store Kit protects the integrity of the attributes starting with ASSET_TAG_DATA_LABEL_CRITICAL. These attributes cannot be changed once written.
Add an asset with the password demo_pwd, alias demo_alias, and additional attribute demo_label. The asset can be accessed after the device is unlocked for the first time.
For details about how to add an asset to a group, see Adding an Asset to a Group.
Link the dynamic libraries in the CMake script.
target_link_libraries(entry PUBLIC libasset_ndk.z.so)
Include header files.
#include "napi/native_api.h" #include <string.h> #include "asset/asset_api.h"
Add an asset.
static napi_value AddAsset(napi_env env, napi_callback_info info)
{
const char *secretStr = "demo_pwd";
const char *aliasStr = "demo_alias";
const char *labelStr = "demo_label";
Asset_Blob secret = {(uint32_t)(strlen(secretStr)), (uint8_t *)secretStr};
Asset_Blob alias = {(uint32_t)(strlen(aliasStr)), (uint8_t *)aliasStr};
Asset_Blob label = {(uint32_t)(strlen(labelStr)), (uint8_t *)labelStr};
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 addResult = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0]));
napi_value ret;
napi_create_int32(env, addResult, &ret);
return ret;
}