Skip to content

Commit

Permalink
fixup! feat(core): adds CIP20 TxMetadata helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed Aug 20, 2024
1 parent 9e72920 commit 8e21741
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
9 changes: 5 additions & 4 deletions packages/core/src/TxMetadata/cip20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ export const validateMessage = (entry: unknown): MessageValidationResult => {
* Converts an object containing an array of individual messages into https://cips.cardano.org/cip/CIP-20 compliant
* transaction metadata
*
* @param args CIP20TxMetadataArgs
* @param args CIP20TxMetadataArgs or a string to be transformed into an array
* @returns CIP20-compliant transaction metadata
* @throws Message validation error containing details. Use validateMessage to independently check each message before calling this function
*/
export const toCIP20Metadata = (args: CIP20TxMetadataArgs): TxMetadata => {
export const toCIP20Metadata = (args: CIP20TxMetadataArgs | string): TxMetadata => {
const messages = typeof args === 'string' ? StringUtils.sliceByBytes(args, MAX_BYTES) : args.messages;
const invalidMessages: ValidationResultMap = new Map();
for (const message of args.messages) {
for (const message of messages) {
const result = validateMessage(message);
if (!result.valid) invalidMessages.set(message, result);
}
if (invalidMessages.size > 0) throw new MessageValidationError(invalidMessages);
return new Map([[CIP_20_METADATUM_LABEL, new Map([['msg', args.messages]])]]);
return new Map([[CIP_20_METADATUM_LABEL, new Map([['msg', messages]])]]);
};
44 changes: 33 additions & 11 deletions packages/core/test/TxMetadata/cip20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,40 @@ describe('TxMetadata.cip20', () => {
});
});
describe('toCIP20Metadata', () => {
it('produces a CIP-20-compliant TxMetadata map', () => {
const metadata = toCIP20Metadata({ messages: [compliantShortMessage] }) as TxMetadata;
expect(metadata.has(CIP_20_METADATUM_LABEL)).toBe(true);
const cip20Metadata = metadata.get(CIP_20_METADATUM_LABEL) as Cardano.MetadatumMap;
expect(cip20Metadata.get('msg')).toStrictEqual([compliantShortMessage]);
describe('args object', () => {
it('produces a CIP-20-compliant TxMetadata map', () => {
const metadata = toCIP20Metadata({ messages: [compliantShortMessage] }) as TxMetadata;
expect(metadata.has(CIP_20_METADATUM_LABEL)).toBe(true);
const cip20Metadata = metadata.get(CIP_20_METADATUM_LABEL) as Cardano.MetadatumMap;
expect(cip20Metadata.get('msg')).toStrictEqual([compliantShortMessage]);
});
it('throws an error if any messages are invalid', () => {
expect(() =>
toCIP20Metadata({
messages: [compliantShortMessage, compliantMaxMessage, oversizeMessage]
})
).toThrowError(MessageValidationError);
});
});
it('throws an error if any messages are invalid', () => {
expect(() =>
toCIP20Metadata({
messages: [compliantShortMessage, compliantMaxMessage, oversizeMessage]
})
).toThrowError(MessageValidationError);
describe('producing a CIP-20-compliant TxMetadata map with a string arg', () => {
test('larger than 64 bytes', () => {
const metadata = toCIP20Metadata(oversizeMessage) as TxMetadata;
expect(metadata.has(CIP_20_METADATUM_LABEL)).toBe(true);
const cip20Metadata = metadata.get(CIP_20_METADATUM_LABEL) as Cardano.MetadatumMap;
expect((cip20Metadata.get('msg') as string[]).length).toBe(2);
});
test('equal to 64 bytes', () => {
const metadata = toCIP20Metadata(compliantMaxMessage) as TxMetadata;
expect(metadata.has(CIP_20_METADATUM_LABEL)).toBe(true);
const cip20Metadata = metadata.get(CIP_20_METADATUM_LABEL) as Cardano.MetadatumMap;
expect((cip20Metadata.get('msg') as string[]).length).toBe(1);
});
test('smaller than to 64 bytes', () => {
const metadata = toCIP20Metadata(compliantShortMessage) as TxMetadata;
expect(metadata.has(CIP_20_METADATUM_LABEL)).toBe(true);
const cip20Metadata = metadata.get(CIP_20_METADATUM_LABEL) as Cardano.MetadatumMap;
expect((cip20Metadata.get('msg') as string[]).length).toBe(1);
});
});
});
});

0 comments on commit 8e21741

Please sign in to comment.