Skip to content

Commit

Permalink
fix(core): add support for version specified as bigint in cip25 metadata
Browse files Browse the repository at this point in the history
example in current spec uses a number

it might have changed since initial parser implementation
  • Loading branch information
mkazlauskas committed May 8, 2024
1 parent 012e6cc commit 92a35a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
15 changes: 14 additions & 1 deletion packages/core/src/Asset/NftMetadata/fromMetadatum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isNotNil } from '@cardano-sdk/util';
import difference from 'lodash/difference';

const isString = (obj: unknown): obj is string => typeof obj === 'string';
const VersionRegExp = /^\d+\.?\d?$/;

const metadatumToString = (metadatum: Cardano.Metadatum | undefined): string | undefined => {
if (Array.isArray(metadatum)) {
Expand Down Expand Up @@ -109,6 +110,17 @@ const getName = (assetMetadata: Cardano.MetadatumMap, version: string, asset: As
}
};

const parseVersion = (version: Cardano.Metadatum | undefined) => {
if (!version) return '1.0';
if (typeof version === 'bigint') {
return `${version}.0`;
}
const stringVersion = asString(version);
if (stringVersion && VersionRegExp.test(stringVersion)) {
return `${Number(stringVersion)}.0`;
}
};

/**
* @param asset try to parse NftMetadata for this asset
* @param metadata transaction metadata (see CIP-0025)
Expand All @@ -127,7 +139,8 @@ export const fromMetadatum = (
if (!cip25MetadatumMap) return null;
const policy = getPolicyMetadata(cip25MetadatumMap, asset.policyId);
if (!policy) return null;
const version = asString(policy.get('version')) || '1.0';
const version = parseVersion(policy.get('version'));
if (!version) return null;
const assetMetadata = getAssetMetadata(policy, asset.name);
if (!assetMetadata) return null;
const name = getName(assetMetadata, version, asset, logger);
Expand Down
29 changes: 24 additions & 5 deletions packages/core/test/Asset/NftMetadata/fromMetadatum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('NftMetadata.fromMetadatum', () => {
new Map<bigint, Metadatum>([
[721n, new Map([[policyIdString, new Map<Metadatum, Metadatum>([[assetNameStringUtf8, assetMetadatum]])]])]
]);
const createMetadataV2 = (assetMetadatum: Map<Metadatum, Metadatum>): TxMetadata =>
const createMetadataV2 = (assetMetadatum: Map<Metadatum, Metadatum>, version: Metadatum = 2n): TxMetadata =>
new Map<bigint, Metadatum>([
[
721n,
Expand All @@ -54,7 +54,7 @@ describe('NftMetadata.fromMetadatum', () => {
policyIdString,
new Map<Metadatum, Metadatum>([
[assetNameString, assetMetadatum],
['version', '2.0']
['version', version]
])
]
])
Expand Down Expand Up @@ -331,11 +331,30 @@ describe('NftMetadata.fromMetadatum', () => {
expect(Asset.NftMetadata.fromMetadatum(asset, metadatum, logger)).toEqual(minimalConvertedMetadataV1);
});

it('converts version', () => {
const metadatum: TxMetadata = createMetadataV2(minimalMetadata);
expect(Asset.NftMetadata.fromMetadatum(asset, metadatum, logger)).toEqual({
describe('version', () => {
const expectedMetadata = {
...minimalConvertedMetadataV1,
version: '2.0'
};

it('can parse version as string that is a floating point number', () => {
const metadatum: TxMetadata = createMetadataV2(minimalMetadata, '2.0');
expect(Asset.NftMetadata.fromMetadatum(asset, metadatum, logger)).toEqual(expectedMetadata);
});

it('can parse version as string that is an integer number', () => {
const metadatum: TxMetadata = createMetadataV2(minimalMetadata, '2');
expect(Asset.NftMetadata.fromMetadatum(asset, metadatum, logger)).toEqual(expectedMetadata);
});

it('returns null if version is a string that has non-number characters', () => {
const metadatum: TxMetadata = createMetadataV2(minimalMetadata, '1a');
expect(Asset.NftMetadata.fromMetadatum(asset, metadatum, logger)).toBeNull();
});

it('can parse version as bigint', () => {
const metadatum: TxMetadata = createMetadataV2(minimalMetadata, 2n);
expect(Asset.NftMetadata.fromMetadatum(asset, metadatum, logger)).toEqual(expectedMetadata);
});
});

Expand Down

0 comments on commit 92a35a6

Please sign in to comment.