Skip to content

Commit

Permalink
fix(core): add option to remove invisible characters from asset name
Browse files Browse the repository at this point in the history
  • Loading branch information
mchappell committed May 9, 2024
1 parent 898a821 commit a47beba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/Asset/NftMetadata/fromMetadatum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ const getAssetMetadata = (policy: Cardano.MetadatumMap, assetName: Cardano.Asset
);

type AssetIdParts = Pick<AssetInfo, 'policyId' | 'name'>;
const getName = (assetMetadata: Cardano.MetadatumMap, version: string, asset: AssetIdParts, logger: Logger) => {
const getName = (
assetMetadata: Cardano.MetadatumMap,
version: string,
asset: AssetIdParts,
logger: Logger,
stripInvisibleCharacters = false
) => {
const name = asString(assetMetadata.get('name'));
if (name) return name;
if (version === '1.0') {
try {
return AssetName.toUTF8(asset.name);
return AssetName.toUTF8(asset.name, stripInvisibleCharacters);
} catch (error) {
logger.warn(error);
}
Expand Down Expand Up @@ -143,7 +149,7 @@ export const fromMetadatum = (
if (!version) return null;
const assetMetadata = getAssetMetadata(policy, asset.name);
if (!assetMetadata) return null;
const name = getName(assetMetadata, version, asset, logger);
const name = getName(assetMetadata, version, asset, logger, true);
const image = metadatumToString(assetMetadata.get('image'));
const assetId = Cardano.AssetId.fromParts(asset.policyId, asset.name);

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/Cardano/types/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ export const AssetName = (value: string): AssetName => {
};

const utf8Decoder = new TextDecoder('utf8', { fatal: true });
AssetName.toUTF8 = (assetName: AssetName) => {
AssetName.toUTF8 = (assetName: AssetName, stripInvisibleCharacters = false) => {
const assetNameBuffer = Buffer.from(assetName, 'hex');
try {
return utf8Decoder.decode(Buffer.from(assetName, 'hex'));
if (stripInvisibleCharacters) {
// 'invisible' control characters are valid utf8, but we don't want to use them, strip them out
return utf8Decoder.decode(assetNameBuffer.filter((v) => v > 31));
}
return utf8Decoder.decode(assetNameBuffer);
} catch (error) {
throw new InvalidStringError(`Cannot convert AssetName '${assetName}' to UTF8`, error);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/test/Cardano/types/Asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ describe('Cardano/types/Asset', () => {
expect(() => AssetName('0dbe461fb5f981c0d01615332b8666340eb1a692b3034f46bcb5f5e0dbe461abc')).toThrow();
});

describe('fromUTF8', () => {
describe('toUTF8', () => {
it('decodes hex string (bytes) to utf8 string', () => {
expect(AssetName.toUTF8(AssetName('737472'))).toEqual('str');
});
it('throws InvalidStringError when it cannot be decoded to utf8', () => {
expect(() => AssetName.toUTF8(AssetName('e8'))).toThrowError(InvalidStringError);
});
it('strips invisible characters when requested', () => {
expect(AssetName.toUTF8(AssetName('100000'), true)).toEqual('');
});
});
});

Expand Down

0 comments on commit a47beba

Please sign in to comment.