1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import Constants from '../constant'; 17import { HiLog } from '../HiLog'; 18import { decodeByBase64, getSha256, isInvalidStr, uint8ArrayToHexString } from '../FileUtils/utils'; 19 20const TAG: string = 'AssocUtil'; 21 22export default class AccountAssociationUtil { 23 public static async getAccountAssociationFileName(ownerAccountId: string): Promise<string | null> { 24 HiLog.info(TAG, 'Start getting account association file name.'); 25 if (isInvalidStr(ownerAccountId)) { 26 HiLog.error(TAG, 'Owner account ID is invalid'); 27 return null; 28 } 29 let accountIdUint8: Uint8Array | null = decodeByBase64(ownerAccountId); 30 if (!accountIdUint8) { 31 HiLog.error(TAG, 'Get accountIdUint8 failed.'); 32 return null; 33 } 34 let accountIdSha256: Uint8Array | null = await getSha256(accountIdUint8); 35 if (!accountIdSha256) { 36 HiLog.error(TAG, 'Get accountIdSha256 failed.'); 37 return null; 38 } 39 const accountIdSha256Str: string | null = uint8ArrayToHexString(accountIdSha256); 40 if (isInvalidStr(accountIdSha256Str)) { 41 HiLog.error(TAG, 'Uint8Array to hex string failed.'); 42 return null; 43 } 44 return Constants.ASSOCIATION_FILE_PREFIX + accountIdSha256Str; 45 } 46}