Skip to content

Commit

Permalink
refactor(core)!: move parseAssetId and createAssetId from util.assetI…
Browse files Browse the repository at this point in the history
…d to coreToCml and cmlToCore

BREAKING CHANGE:
- parseAssetId was moved from util.assetId.ts to coreToCml.ts
- createAssetId was moved from util.assetId.ts to cmlToCore.ts
  • Loading branch information
AngelCastilloB committed Mar 15, 2023
1 parent c7e64d0 commit d649e02
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
17 changes: 0 additions & 17 deletions packages/core/src/Asset/util/assetId.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { AssetId, AssetName, PolicyId } from '../../Cardano/types/Asset';
import { CML } from '../../CML/CML';
import { bytesToHex } from '../../util/misc/bytesToHex';

export const policyIdFromAssetId = (assetId: AssetId): PolicyId => PolicyId(assetId.slice(0, 56));
export const assetNameFromAssetId = (assetId: AssetId): AssetName => AssetName(assetId.slice(56));

/**
* @returns {string} concatenated hex-encoded policy id and asset name
*/
export const createAssetId = (scriptHash: CML.ScriptHash, assetName: CML.AssetName): AssetId =>
AssetId(bytesToHex(scriptHash.to_bytes()) + bytesToHex(assetName.name()));

/**
* @returns {AssetId} concatenated policy id and asset name
*/
export const assetIdFromPolicyAndName = (policyId: PolicyId, assetName: AssetName): AssetId =>
AssetId(policyId + assetName);

export const parseAssetId = (assetId: AssetId) => {
const policyId = policyIdFromAssetId(assetId);
const assetName = assetNameFromAssetId(assetId);
return {
assetName: CML.AssetName.new(Buffer.from(assetName, 'hex')),
scriptHash: CML.ScriptHash.from_bytes(Buffer.from(policyId, 'hex'))
};
};
9 changes: 7 additions & 2 deletions packages/core/src/CML/cmlToCore/cmlToCore.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import * as Cardano from '../../Cardano';
import * as Crypto from '@cardano-sdk/crypto';
import { AssetId, PlutusLanguageVersion, ScriptType } from '../../Cardano';
import { Base64Blob, HexBlob, ManagedFreeableScope, usingAutoFree } from '@cardano-sdk/util';
import { CML } from '../CML';
import { PlutusLanguageVersion, ScriptType } from '../../Cardano';
import { ScriptKind } from '@dcspark/cardano-multiplatform-lib-nodejs';
import { SerializationError, SerializationFailure } from '../../errors';
import { bytesToHex } from '../../util/misc';
import { createAssetId } from '../../Asset/util';
import { createCertificate } from './certificate';

/**
* @returns {string} concatenated hex-encoded policy id and asset name
*/
export const createAssetId = (scriptHash: CML.ScriptHash, assetName: CML.AssetName): AssetId =>
AssetId(bytesToHex(scriptHash.to_bytes()) + bytesToHex(assetName.name()));

export const txRequiredExtraSignatures = (
signatures: CML.Ed25519KeyHashes | undefined
): Crypto.Ed25519KeyHashHex[] | undefined =>
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/CML/coreToCml/coreToCml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,20 @@ import {
} from '@dcspark/cardano-multiplatform-lib-nodejs';

import * as certificate from './certificate';
import { AssetId, RedeemerPurpose } from '../../Cardano';
import { CML } from '../CML';
import { ManagedFreeableScope } from '@cardano-sdk/util';
import { RedeemerPurpose } from '../../Cardano';
import { SerializationError, SerializationFailure } from '../../errors';
import { assetNameFromAssetId, parseAssetId, policyIdFromAssetId } from '../../Asset/util';
import { assetNameFromAssetId, policyIdFromAssetId } from '../../Asset/util';

export const parseAssetId = (assetId: AssetId) => {
const policyId = policyIdFromAssetId(assetId);
const assetName = assetNameFromAssetId(assetId);
return {
assetName: CML.AssetName.new(Buffer.from(assetName, 'hex')),
scriptHash: CML.ScriptHash.from_bytes(Buffer.from(policyId, 'hex'))
};
};

export const tokenMap = (scope: ManagedFreeableScope, map: Cardano.TokenMap) => {
const multiasset = scope.manage(MultiAsset.new());
Expand Down
17 changes: 1 addition & 16 deletions packages/core/test/Asset/util/assetId.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import { AssetId } from '../../../src/Cardano';
import {
assetIdFromPolicyAndName,
assetNameFromAssetId,
createAssetId,
parseAssetId,
policyIdFromAssetId
} from '../../../src/Asset/util';
import { assetIdFromPolicyAndName, assetNameFromAssetId, policyIdFromAssetId } from '../../../src/Asset/util';

describe('Asset', () => {
describe('util', () => {
it('createAssetId and parseAssetId', async () => {
const assetId = AssetId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41');
const tsla = parseAssetId(assetId);
expect(new TextDecoder().decode(tsla.assetName.name())).toEqual('TSLA');
expect(Buffer.from(tsla.scriptHash.to_bytes()).toString('hex')).toEqual(
'659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82'
);
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)];
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/CML/cmlToCore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Crypto from '@cardano-sdk/crypto';
import { AssetId } from '../../src/Cardano';
import { Base64Blob, HexBlob, ManagedFreeableScope } from '@cardano-sdk/util';
import { Cardano, cmlToCore, coreToCml } from '../../src';
import { NativeScript } from '@dcspark/cardano-multiplatform-lib-nodejs';
Expand All @@ -17,6 +18,8 @@ import {
valueCoinOnly,
valueWithAssets
} from './testData';
import { createAssetId } from '../../src/CML/cmlToCore';
import { parseAssetId } from '../../src/CML/coreToCml';

describe('cmlToCore', () => {
let scope: ManagedFreeableScope;
Expand All @@ -28,6 +31,16 @@ describe('cmlToCore', () => {
afterEach(() => {
scope.dispose();
});

describe('AssetId', () => {
it('createAssetId', async () => {
const assetId = AssetId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41');
const cmlAssetId = parseAssetId(assetId);

expect(createAssetId(cmlAssetId.scriptHash, cmlAssetId.assetName)).toEqual(assetId);
});
});

describe('value', () => {
it('coin only', () => {
expect(cmlToCore.value(coreToCml.value(scope, valueCoinOnly))).toEqual(valueCoinOnly);
Expand Down
21 changes: 18 additions & 3 deletions packages/core/test/CML/coreToCml.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-len */
import { Asset, CML, Cardano, SerializationFailure, coreToCml } from '../../src';
import { AssetId } from '../../src/Cardano';
import { BigNum } from '@dcspark/cardano-multiplatform-lib-nodejs';
import { CML, Cardano, SerializationFailure, coreToCml } from '../../src';
import { ManagedFreeableScope } from '@cardano-sdk/util';
import {
babbageTxBody,
Expand All @@ -19,6 +20,7 @@ import {
valueWithAssets,
vkey
} from './testData';
import { parseAssetId } from '../../src/CML/coreToCml';

const txOutByron = {
...txOut,
Expand All @@ -38,9 +40,22 @@ describe('coreToCml', () => {
scope.dispose();
});

describe('AssetId', () => {
it('parseAssetId', async () => {
const assetId = AssetId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41');
const cmlAssetId = parseAssetId(assetId);

expect(new TextDecoder().decode(cmlAssetId.assetName.name())).toEqual('TSLA');
expect(Buffer.from(cmlAssetId.scriptHash.to_bytes()).toString('hex')).toEqual(
'659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82'
);
});
});

it('txIn', () => {
expect(coreToCml.txIn(scope, txIn)).toBeInstanceOf(CML.TransactionInput);
});

describe('txOut', () => {
it('converts to CML.TransactionOutput', () => {
expect(coreToCml.txOut(scope, txOut)).toBeInstanceOf(CML.TransactionOutput);
Expand Down Expand Up @@ -80,7 +95,7 @@ describe('coreToCml', () => {
const multiasset = scope.manage(value.multiasset())!;
expect(multiasset.len()).toBe(3);
for (const [assetId, expectedAssetQuantity] of valueWithAssets.assets!.entries()) {
const { scriptHash, assetName } = Asset.util.parseAssetId(assetId);
const { scriptHash, assetName } = parseAssetId(assetId);
const multiAsset = scope.manage(multiasset.get(scriptHash)!);
const assetQuantity = BigInt(scope.manage(multiAsset.get(assetName)!).to_str());
expect(assetQuantity).toBe(expectedAssetQuantity);
Expand All @@ -95,7 +110,7 @@ describe('coreToCml', () => {
const scriptHashes = scope.manage(multiasset.keys());
expect(scriptHashes.len()).toBe(3);
for (const [assetId, expectedAssetQuantity] of txOut.value.assets!.entries()) {
const { scriptHash, assetName } = Asset.util.parseAssetId(assetId);
const { scriptHash, assetName } = parseAssetId(assetId);
const multiAsset = scope.manage(multiasset.get(scriptHash)!);
const assetQuantity = BigInt(scope.manage(multiAsset.get(assetName)!).to_str());
expect(assetQuantity).toBe(expectedAssetQuantity);
Expand Down

0 comments on commit d649e02

Please sign in to comment.