Managing Assets in a Group (ArkTS)

Before managing assets in a group, ensure that you are familiar with the following operations:

Prerequisites

  1. Set the group ID, such as demo_group_id, in the app.json5 file. You can set multiple group IDs for a group.

    {
      "app": {
        // Other configuration items are omitted here.
        "assetAccessGroups": [
          "demo_group_id",
          // "another_group_id",
          // ...
        ]
      }
    }
    
  2. Include the header file and define the tool function.

    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();
      return textEncoder.encodeInto(str);
    }
    
    function arrayToString(arr: Uint8Array): string {
      let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
      let str = textDecoder.decodeToString(arr, { stream: false });
      return str;
    }
    

Adding an Asset to a Group

Add an asset to the group, with the password demo_pwd, alias demo_alias, and additional attribute demo_label.

let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray('demo_pwd'));
attr.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
attr.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
  asset.add(attr).then(() => {
    console.info(`Succeeded in adding Asset to the group.`);
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`);
    // ...
  })
} catch (error) {
  let err = error as BusinessError;
  console.error(`Failed to add Asset to the group. Code is ${err?.code}, message is ${err?.message}`);
  // ...
}

Removing an Asset from a Group

Remove asset demo_alias from group demo_group_id.

let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias to delete a single asset from the group. You can also delete multiple assets without specifying the alias.
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
  asset.remove(query).then(() => {
    console.info(`Succeeded in removing Asset from the group.`);
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`);
    // ...
  });
} catch (err) {
  console.error(`Failed to remove Asset from the group. Code is ${err?.code}, message is ${err?.message}`);
  // ...
}

Updating an asset in a Group

Update asset demo_alias in group demo_group_id as follows: change the asset plaintext to demo_pwd_new and the additional attribute to demo_label_new.

let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
let attrsToUpdate: asset.AssetMap = new Map();
attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new'));
attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new'));
try {
  asset.update(query, attrsToUpdate).then(() => {
    console.info(`Succeeded in updating Asset in the group.`);
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`);
    // ...
  });
} catch (err) {
  console.error(`Failed to update Asset in the group. Code is ${err?.code}, message is ${err?.message}`);
  // ...
}

Querying the Plaintext of an Asset in a Group

Query the plaintext of asset demo_alias in group demo_group_id.

let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query. At most one asset will meet the condition.
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // Return all asset information, including attributes and asset plaintext.
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
  asset.query(query).then((res: Array<asset.AssetMap>) => {
    for (let i = 0; i < res.length; i++) {
      // Parse the secret.
      let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
      // Convert Uint8Array into the string type.
      let secretStr: string = arrayToString(secret);
    }
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to query Asset plaintext from the group. Code is ${err.code}, message is ${err.message}`);
    // ...
  });
} catch (err) {
  console.error(`Failed to query Asset plaintext from the group. Code is ${err?.code}, message is ${err?.message}`);
  // ...
}

Querying the Attributes of an Asset in a Group

Query the attributes of asset demo_alias in group demo_group_id.

let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query. At most one asset will meet the condition.
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext.
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
  asset.query(query).then((res: Array<asset.AssetMap>) => {
    for (let i = 0; i < res.length; i++) {
      // Parse the attributes.
      let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
      console.info(`Succeeded in getting accessibility, which is: ${accessibility}.`);
    }
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to query Asset attribute from the group. Code is ${err.code}, message is ${err.message}`);
    // ...
  });
} catch (err) {
  console.error(`Failed to query Asset attribute from the group. Code is ${err?.code}, message is ${err?.message}`);
  // ...
}