From d649e023ab98598ff093afcb3b36d052969335bb Mon Sep 17 00:00:00 2001 From: Angel Castillo Date: Wed, 15 Mar 2023 12:28:57 +0800 Subject: [PATCH] refactor(core)!: move parseAssetId and createAssetId from util.assetId 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 --- packages/core/src/Asset/util/assetId.ts | 17 --------------- packages/core/src/CML/cmlToCore/cmlToCore.ts | 9 ++++++-- packages/core/src/CML/coreToCml/coreToCml.ts | 13 ++++++++++-- packages/core/test/Asset/util/assetId.test.ts | 17 +-------------- packages/core/test/CML/cmlToCore.test.ts | 13 ++++++++++++ packages/core/test/CML/coreToCml.test.ts | 21 ++++++++++++++++--- 6 files changed, 50 insertions(+), 40 deletions(-) diff --git a/packages/core/src/Asset/util/assetId.ts b/packages/core/src/Asset/util/assetId.ts index 4d1102149cd..b81e2d089e5 100644 --- a/packages/core/src/Asset/util/assetId.ts +++ b/packages/core/src/Asset/util/assetId.ts @@ -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')) - }; -}; diff --git a/packages/core/src/CML/cmlToCore/cmlToCore.ts b/packages/core/src/CML/cmlToCore/cmlToCore.ts index 54477bba6ab..344e74b4735 100644 --- a/packages/core/src/CML/cmlToCore/cmlToCore.ts +++ b/packages/core/src/CML/cmlToCore/cmlToCore.ts @@ -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 => diff --git a/packages/core/src/CML/coreToCml/coreToCml.ts b/packages/core/src/CML/coreToCml/coreToCml.ts index 4fd0d541041..c0304222c99 100644 --- a/packages/core/src/CML/coreToCml/coreToCml.ts +++ b/packages/core/src/CML/coreToCml/coreToCml.ts @@ -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()); diff --git a/packages/core/test/Asset/util/assetId.test.ts b/packages/core/test/Asset/util/assetId.test.ts index b79d3985fa8..3b33fe13251 100644 --- a/packages/core/test/Asset/util/assetId.test.ts +++ b/packages/core/test/Asset/util/assetId.test.ts @@ -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)]; diff --git a/packages/core/test/CML/cmlToCore.test.ts b/packages/core/test/CML/cmlToCore.test.ts index 23b78d26404..3d05b0d11c9 100644 --- a/packages/core/test/CML/cmlToCore.test.ts +++ b/packages/core/test/CML/cmlToCore.test.ts @@ -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'; @@ -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; @@ -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); diff --git a/packages/core/test/CML/coreToCml.test.ts b/packages/core/test/CML/coreToCml.test.ts index 62180efaf08..ee15f75c309 100644 --- a/packages/core/test/CML/coreToCml.test.ts +++ b/packages/core/test/CML/coreToCml.test.ts @@ -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, @@ -19,6 +20,7 @@ import { valueWithAssets, vkey } from './testData'; +import { parseAssetId } from '../../src/CML/coreToCml'; const txOutByron = { ...txOut, @@ -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); @@ -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); @@ -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);