Skip to content

Commit

Permalink
fix(core): fromMetadatum now uses asset_name as utf8 when name field …
Browse files Browse the repository at this point in the history
…is missing
  • Loading branch information
AngelCastilloB committed Jan 20, 2024
1 parent a9316a4 commit 0bc80cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
14 changes: 12 additions & 2 deletions packages/core/src/Asset/NftMetadata/fromMetadatum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AssetInfo } from '../types';
import { AssetName } from '../../Cardano';
import { Cardano } from '../..';
import { ImageMediaType, MediaType, NftMetadata, NftMetadataFile, Uri } from './types';
import { InvalidFileError } from './errors';
Expand Down Expand Up @@ -98,7 +99,9 @@ const getAssetMetadata = (policy: Cardano.MetadatumMap, assetName: Cardano.Asset
/**
* @param asset try to parse NftMetadata for this asset
* @param metadata transaction metadata (see CIP-0025)
* @param logger logger to use
*/
// eslint-disable-next-line complexity
export const fromMetadatum = (
asset: Pick<AssetInfo, 'policyId' | 'name'>,
metadata: Cardano.TxMetadata | undefined,
Expand All @@ -112,13 +115,20 @@ export const fromMetadatum = (
if (!policy) return null;
const assetMetadata = getAssetMetadata(policy, asset.name);
if (!assetMetadata) return null;
const name = asString(assetMetadata.get('name'));
const version = asString(policy.get('version')) || '1.0';
let name = asString(assetMetadata.get('name'));
const image = metadatumToString(assetMetadata.get('image'));
const assetId = Cardano.AssetId.fromParts(asset.policyId, asset.name);

if (version === '1.0' && !name) {
name = AssetName.toUTF8(asset.name);
}

if (!name || !image) {
logger.warn(missingFieldLogMessage(!name ? 'name' : 'image', assetId, true));
return null;
}

const mediaType = asString(assetMetadata.get('mediaType'));
const files = asMetadatumArray(assetMetadata.get('files'));

Expand All @@ -130,7 +140,7 @@ export const fromMetadatum = (
mediaType: mediaType ? ImageMediaType(mediaType) : undefined,
name,
otherProperties: mapOtherProperties(assetMetadata, ['name', 'image', 'mediaType', 'description', 'files']),
version: asString(policy.get('version')) || '1.0'
version
};
} catch (error: unknown) {
// Any error here means metadata was invalid
Expand Down
35 changes: 33 additions & 2 deletions packages/core/test/Asset/NftMetadata/fromMetadatum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ describe('NftMetadata.fromMetadatum', () => {
const policyIdString = 'b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a7';
const assetImageIPFS = 'ipfs://QmWS6DgF8Ma8oooBn7CtD3ChHyzzMw5NXWfnDbVFTip8af';
const assetImageHTTPS = 'https://tokens.cardano.org';
const ipfsUrl = 'ipfs://image';

const asset = {
name: AssetName(assetNameString),
policyId: PolicyId(policyIdString)
} as Asset.AssetInfo;

const minimalMetadata = new Map([
['image', 'ipfs://image'],
['image', ipfsUrl],
['name', 'test nft']
]);

const minimalConvertedMetadata = {
image: 'ipfs://image',
image: ipfsUrl,
name: 'test nft',
version: '1.0'
};
Expand Down Expand Up @@ -251,6 +252,36 @@ describe('NftMetadata.fromMetadatum', () => {
expect(result?.image).toEqual(base64DecodedImage.join(''));
});

it('replaces the name field with the asset_name when name is missing', () => {
// Arrange
const assetNameUtf8 = 'Meld Bank Manager v1 1589';
const assetNameHex = Buffer.from(assetNameUtf8).toString('hex');
const missingNameMetadata = new Map([['image', ipfsUrl]]);

const metadata = {
image: ipfsUrl,
name: 'Meld Bank Manager v1 1589',
version: '1.0'
};

const metadatum: TxMetadata = new Map([
[721n, new Map([[policyIdString, new Map([[assetNameUtf8, missingNameMetadata]])]])]
]);

// Act
const result = Asset.NftMetadata.fromMetadatum(
{
name: AssetName(assetNameHex),
policyId: PolicyId(policyIdString)
},
metadatum,
logger
);

// Assert
expect(result).toEqual(metadata);
});

it('supports CIP-0025 v2', () => {
const metadatum: TxMetadata = new Map([
[
Expand Down

0 comments on commit 0bc80cb

Please sign in to comment.