1e41f4b71Sopenharmony_ci# Adding an Asset (ArkTS) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Available APIs 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can use [add(attributes: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetadd), an asynchronous API, or [addSync(attributes: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetaddsync12), a synchronous API, to add an asset. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe following table describes the attributes of **AssetMap** for adding an asset. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci>**NOTE** 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci>In the following table, the attributes starting with **DATA_LABEL** are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put personal data in these attributes. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci| Attribute Name (Tag) | Value | Mandatory | Description | 14e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ | 15e41f4b71Sopenharmony_ci| SECRET | Type: Uint8Array<br>Length: 1-1024 bytes | Yes | Asset in plaintext. | 16e41f4b71Sopenharmony_ci| ALIAS | Type: Uint8Array<br>Length: 1-256 bytes | Yes | Asset alias, which uniquely identifies an asset. | 17e41f4b71Sopenharmony_ci| ACCESSIBILITY | Type: number<br>Value range: see [Accessibility](../../reference/apis-asset-store-kit/js-apis-asset.md#accessibility)| No | Access control based on the lock screen status. | 18e41f4b71Sopenharmony_ci| REQUIRE_PASSWORD_SET | Type: bool | No | Whether the asset is accessible only when a lock screen password is set. | 19e41f4b71Sopenharmony_ci| AUTH_TYPE | Type: number<br>Value range: see [AuthType](../../reference/apis-asset-store-kit/js-apis-asset.md#authtype)| No | Type of user authentication required for accessing the asset. | 20e41f4b71Sopenharmony_ci| SYNC_TYPE | Type: number<br>Value range: see [SyncType](../../reference/apis-asset-store-kit/js-apis-asset.md#synctype)| No | Type of sync supported by the asset. | 21e41f4b71Sopenharmony_ci| IS_PERSISTENT | Type: bool | No | Whether to retain the asset when the application is uninstalled.<br>**NOTE**: If this parameter is set, the application must [apply for](../AccessToken/declare-permissions.md) the ohos.permission.STORE_PERSISTENT_DATA permission.| 22e41f4b71Sopenharmony_ci| DATA_LABEL_CRITICAL_1 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 23e41f4b71Sopenharmony_ci| DATA_LABEL_CRITICAL_2 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 24e41f4b71Sopenharmony_ci| DATA_LABEL_CRITICAL_3 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 25e41f4b71Sopenharmony_ci| DATA_LABEL_CRITICAL_4 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 26e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_1 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 27e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_2 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 28e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_3 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 29e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_4 | Type: Uint8Array<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 30e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: Uint8Array<br>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.| 31e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: Uint8Array<br>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.| 32e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: Uint8Array<br>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.| 33e41f4b71Sopenharmony_ci| DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: Uint8Array<br>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.| 34e41f4b71Sopenharmony_ci| CONFLICT_RESOLUTION | Type: number<br>Value range: see [ConflictResolution](../../reference/apis-asset-store-kit/js-apis-asset.md#conflictresolution)| No | Policy for resolving the conflict (for example, duplicate alias). | 35e41f4b71Sopenharmony_ci| REQUIRE_ATTR_ENCRYPTED<sup>13+</sup> | Type: bool| No| Whether to encrypt the customized asset attribute information. By default, the information does not need to be encrypted.| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci## Constraints 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci* Alias-based access 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci Assets are stored in the ASSET database in ciphertext and uniquely identified by the service identity and alias. The alias of each asset must be unique. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci* Custom service data storage 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci ASSET provides 12 custom asset attributes starting with **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. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ASSET protects the integrity of the attributes starting with **DATA_LABEL_CRITICAL**. These attributes cannot be changed once written. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci## Example 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci> **NOTE** 53e41f4b71Sopenharmony_ci> 54e41f4b71Sopenharmony_ci> The **asset** module provides an asynchronous API and a synchronous API for adding an asset. The following uses the asynchronous API as an example. For more information about the APIs, see [Asset Store Service](../../reference/apis-asset-store-kit/js-apis-asset.md). 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciAdd an asset with password of **demo_pwd**, alias of **demo_alias**, and additional information of **demo_label** and allow the asset to be accessed after the device is unlocked for the first time. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci```typescript 59e41f4b71Sopenharmony_ciimport { asset } from '@kit.AssetStoreKit'; 60e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS'; 61e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_cifunction stringToArray(str: string): Uint8Array { 64e41f4b71Sopenharmony_ci let textEncoder = new util.TextEncoder(); 65e41f4b71Sopenharmony_ci return textEncoder.encodeInto(str); 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_cilet attr: asset.AssetMap = new Map(); 69e41f4b71Sopenharmony_ciattr.set(asset.Tag.SECRET, stringToArray('demo_pwd')); 70e41f4b71Sopenharmony_ciattr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 71e41f4b71Sopenharmony_ciattr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED); 72e41f4b71Sopenharmony_ciattr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 73e41f4b71Sopenharmony_citry { 74e41f4b71Sopenharmony_ci asset.add(attr).then(() => { 75e41f4b71Sopenharmony_ci console.info(`Asset added successfully.`); 76e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 77e41f4b71Sopenharmony_ci console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 78e41f4b71Sopenharmony_ci }) 79e41f4b71Sopenharmony_ci} catch (error) { 80e41f4b71Sopenharmony_ci let err = error as BusinessError; 81e41f4b71Sopenharmony_ci console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 82e41f4b71Sopenharmony_ci} 83e41f4b71Sopenharmony_ci``` 84