From aef334546d53be746608129a96fc5032d67cd1c0 Mon Sep 17 00:00:00 2001 From: lgobbi-atix Date: Thu, 5 May 2022 16:30:42 -0300 Subject: [PATCH] feat(core): add util to create AssetId from PolicyId and AssetName --- packages/core/src/Asset/util/assetId.ts | 6 ++++++ packages/core/test/Asset/util/assetId.test.ts | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/core/src/Asset/util/assetId.ts b/packages/core/src/Asset/util/assetId.ts index 6a0ce07b901..c459876656d 100644 --- a/packages/core/src/Asset/util/assetId.ts +++ b/packages/core/src/Asset/util/assetId.ts @@ -10,6 +10,12 @@ export const assetNameFromAssetId = (assetId: AssetId): AssetName => AssetName(a export const createAssetId = (scriptHash: CSL.ScriptHash, assetName: CSL.AssetName): AssetId => AssetId(util.bytesToHex(scriptHash.to_bytes()) + util.bytesToHex(assetName.name()).toString()); +/** + * @returns {AssetId} concatenated policy id and asset name + */ +export const assetIdFromPolicyAndName = (policyId: PolicyId, assetName: AssetName): AssetId => + AssetId(policyId.toString() + assetName.toString()); + export const parseAssetId = (assetId: AssetId) => { const policyId = policyIdFromAssetId(assetId); const assetName = assetNameFromAssetId(assetId); diff --git a/packages/core/test/Asset/util/assetId.test.ts b/packages/core/test/Asset/util/assetId.test.ts index aa2e01e7c66..b79d3985fa8 100644 --- a/packages/core/test/Asset/util/assetId.test.ts +++ b/packages/core/test/Asset/util/assetId.test.ts @@ -1,5 +1,11 @@ import { AssetId } from '../../../src/Cardano'; -import { createAssetId, parseAssetId } from '../../../src/Asset/util'; +import { + assetIdFromPolicyAndName, + assetNameFromAssetId, + createAssetId, + parseAssetId, + policyIdFromAssetId +} from '../../../src/Asset/util'; describe('Asset', () => { describe('util', () => { @@ -12,5 +18,13 @@ describe('Asset', () => { ); expect(createAssetId(tsla.scriptHash, tsla.assetName)).toEqual(assetId); }); + it('policyIdFromAssetId, assetNameFromAssetId and assetIdFromPolicyAndName', async () => { + const assetId = AssetId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41'); + const [policyId, assetName] = [policyIdFromAssetId(assetId), assetNameFromAssetId(assetId)]; + + expect(policyId).toEqual('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82'); + expect(assetName).toEqual('54534c41'); + expect(assetIdFromPolicyAndName(policyId, assetName)).toEqual(assetId); + }); }); });