From 8e217413d4967115d02f4075c936d43d56d2d858 Mon Sep 17 00:00:00 2001 From: Rhys Bartels-Waller Date: Wed, 21 Aug 2024 00:16:21 +0100 Subject: [PATCH] fixup! feat(core): adds CIP20 TxMetadata helper functions --- packages/core/src/TxMetadata/cip20.ts | 9 +++-- packages/core/test/TxMetadata/cip20.test.ts | 44 +++++++++++++++------ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/packages/core/src/TxMetadata/cip20.ts b/packages/core/src/TxMetadata/cip20.ts index b077c4af651..7fdc84fb07e 100644 --- a/packages/core/src/TxMetadata/cip20.ts +++ b/packages/core/src/TxMetadata/cip20.ts @@ -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]])]]); }; diff --git a/packages/core/test/TxMetadata/cip20.test.ts b/packages/core/test/TxMetadata/cip20.test.ts index e8729ec809e..d03a207d29c 100644 --- a/packages/core/test/TxMetadata/cip20.test.ts +++ b/packages/core/test/TxMetadata/cip20.test.ts @@ -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); + }); }); }); });