Skip to content

Commit

Permalink
feat(core): add util to create AssetId from PolicyId and AssetName
Browse files Browse the repository at this point in the history
  • Loading branch information
lgobbi-atix committed May 27, 2022
1 parent adeb02c commit aef3345
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/core/src/Asset/util/assetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/Asset/util/assetId.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
});

0 comments on commit aef3345

Please sign in to comment.