Skip to content

Commit

Permalink
feat(wallet): add types for NFT metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Jan 18, 2022
1 parent 9b2fa94 commit 913c217
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/wallet/src/NftMetadata/types.ts
@@ -0,0 +1,59 @@
/* eslint-disable wrap-regex */
import { Cardano, InvalidStringError } from '@cardano-sdk/core';

export type Uri = Cardano.util.OpaqueString<'Uri'>;
export const Uri = (uri: string) => {
if (/^[a-z]+:\/\/.+/.test(uri)) {
return uri as unknown as Uri;
}
throw new InvalidStringError(
'Expected Uri to start with "[protocol]://", where protocol is usually "https" or "ipfs"'
);
};

export type ImageMediaType = Cardano.util.OpaqueString<'ImageMediaType'>;
export const ImageMediaType = (mediaType: string) => {
if (/^image\/.+$/.test(mediaType)) {
return mediaType as unknown as ImageMediaType;
}
throw new InvalidStringError('Expected media type to be "image/*"');
};

export type MediaType = Cardano.util.OpaqueString<'MediaType'>;
export const MediaType = (mediaType: string) => {
if (/^[a-z]+\/.+$/.test(mediaType)) {
return mediaType as unknown as MediaType;
}
throw new InvalidStringError('Expected media type to be "*/*"');
};

/**
* https://cips.cardano.org/cips/cip25/
*/
export interface NftMetadataFile {
name: string;
mediaType: MediaType;
src: Uri | Uri[];
otherProperties?: {
[key: string]: Cardano.Metadatum;
};
}

/**
* https://cips.cardano.org/cips/cip25/
*/
export interface NftMetadata {
name: string;
image: Uri | Uri[];
version: string;
mediaType?: ImageMediaType;
files?: NftMetadataFile[];
description?: string | string[];
otherProperties?: {
[key: string]: Cardano.Metadatum;
};
}

export interface NftMetadataProvider {
(asset: Cardano.Asset): Promise<NftMetadata | undefined>;
}
34 changes: 34 additions & 0 deletions packages/wallet/test/NftMetadata/types.test.ts
@@ -0,0 +1,34 @@
import * as NftMetadata from '../../src/NftMetadata';
import { InvalidStringError } from '@cardano-sdk/core';

describe('NftMetadata/types', () => {
describe('Uri', () => {
it('accepts a string starting with protocol://', () => {
expect(() => NftMetadata.Uri('http://some.url')).not.toThrow();
expect(() => NftMetadata.Uri('ipfs://abc123')).not.toThrow();
});
it('throws for string without protocol:// prefix', () => {
expect(() => NftMetadata.Uri('abc123')).toThrowError(InvalidStringError);
});
});

describe('ImageMediaType', () => {
it('accepts a string starting with image/', () => {
expect(() => NftMetadata.ImageMediaType('image/svg+xml')).not.toThrow();
});
it('throws for non-image media type', () => {
expect(() => NftMetadata.ImageMediaType('video/webm')).toThrowError(InvalidStringError);
});
});

describe('MediaType', () => {
it('accepts any media type in format "type/subtype"', () => {
expect(() => NftMetadata.MediaType('image/svg+xml')).not.toThrow();
expect(() => NftMetadata.MediaType('video/mp4')).not.toThrow();
expect(() => NftMetadata.MediaType('audio/x-wav')).not.toThrow();
});
it('throws for incorrectly formatted media type', () => {
expect(() => NftMetadata.MediaType('videomp4')).toThrowError(InvalidStringError);
});
});
});

0 comments on commit 913c217

Please sign in to comment.