Skip to content

Commit f33bc2c

Browse files
authored
Merge pull request #1442 from input-output-hk/refactor/move-cip20-to-tx-construction
Refactor/ Move CIP20 to tx-construction
2 parents b7ec9fb + 09aeb2d commit f33bc2c

File tree

7 files changed

+85
-92
lines changed

7 files changed

+85
-92
lines changed

packages/core/src/TxMetadata/index.ts

-1
This file was deleted.

packages/core/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * as Asset from './Asset';
22
export * as Cardano from './Cardano';
33
export * as Serialization from './Serialization';
4-
export * as TxMetadata from './TxMetadata';
54
export * from './Provider';
65
export * from './util';
76
export * from './errors';

packages/core/test/TxMetadata/cip20.test.ts

-81
This file was deleted.

packages/tx-construction/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './fees';
33
export * from './input-selection';
44
export * from './output-validation';
55
export * from './tx-builder';
6+
export * from './tx-metadata';
67
export * from './ensureValidityInterval';
78
export * from './types';
89
export * from './computeScriptDataHash';

packages/core/src/TxMetadata/cip20.ts packages/tx-construction/src/tx-metadata/cip20.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { Cardano } from '@cardano-sdk/core';
12
import { CustomError } from 'ts-custom-error';
23
import { StringUtils } from '@cardano-sdk/util';
3-
import { TxMetadata } from '../Cardano';
44

5-
export const CIP_20_METADATUM_LABEL = 674n;
5+
export const METADATUM_LABEL = 674n;
66
const MAX_BYTES = 64;
77

88
export enum MessageValidationFailure {
@@ -15,14 +15,14 @@ export type MessageValidationResult = {
1515
failure?: MessageValidationFailure;
1616
};
1717

18-
export type CIP20TxMetadataMessage = string;
18+
export type TxMetadataMessage = string;
1919

20-
export type CIP20TxMetadataArgs = {
20+
export type TxMetadataArgs = {
2121
// An array of message strings, limited to 64 bytes each
22-
messages: CIP20TxMetadataMessage[];
22+
messages: TxMetadataMessage[];
2323
};
2424

25-
export type ValidationResultMap = Map<CIP20TxMetadataMessage, MessageValidationResult>;
25+
export type ValidationResultMap = Map<TxMetadataMessage, MessageValidationResult>;
2626

2727
export class MessageValidationError extends CustomError {
2828
public constructor(failures: ValidationResultMap) {
@@ -49,17 +49,17 @@ export const validateMessage = (entry: unknown): MessageValidationResult => {
4949
* Converts an object containing an array of individual messages into https://cips.cardano.org/cip/CIP-20 compliant
5050
* transaction metadata
5151
*
52-
* @param args CIP20TxMetadataArgs or a string to be transformed into an array
52+
* @param args Object containing a message property or a string to be transformed into an array
5353
* @returns CIP20-compliant transaction metadata
5454
* @throws Message validation error containing details. Use validateMessage to independently check each message before calling this function
5555
*/
56-
export const toCIP20Metadata = (args: CIP20TxMetadataArgs | string): TxMetadata => {
56+
export const toTxMetadata = (args: TxMetadataArgs | string): Cardano.TxMetadata => {
5757
const messages = typeof args === 'string' ? StringUtils.chunkByBytes(args, MAX_BYTES) : args.messages;
5858
const invalidMessages: ValidationResultMap = new Map();
5959
for (const message of messages) {
6060
const result = validateMessage(message);
6161
if (!result.valid) invalidMessages.set(message, result);
6262
}
6363
if (invalidMessages.size > 0) throw new MessageValidationError(invalidMessages);
64-
return new Map([[CIP_20_METADATUM_LABEL, new Map([['msg', messages]])]]);
64+
return new Map([[METADATUM_LABEL, new Map([['msg', messages]])]]);
6565
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as CIP20 from './cip20';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { CIP20 } from '../../src';
2+
import { Cardano } from '@cardano-sdk/core';
3+
4+
describe('CIP20', () => {
5+
const compliantShortMessage = 'Lorem ipsum dolor';
6+
const compliantMaxMessage = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean';
7+
const oversizeMessage = `${compliantMaxMessage}1`;
8+
describe('validateMessage', () => {
9+
it('validates a CIP-20 message if a string and less than or equal to 64 bytes', () => {
10+
expect(CIP20.validateMessage(compliantShortMessage)).toStrictEqual({ valid: true });
11+
expect(CIP20.validateMessage(compliantMaxMessage)).toStrictEqual({ valid: true });
12+
});
13+
it('invalidates a CIP-20 message if a string but over 64 bytes', () => {
14+
expect(CIP20.validateMessage(oversizeMessage)).toStrictEqual({
15+
failure: CIP20.MessageValidationFailure.oversize,
16+
valid: false
17+
});
18+
});
19+
it('invalidates a CIP-20 message if wrong type', () => {
20+
expect(CIP20.validateMessage(1 as unknown as string)).toStrictEqual({
21+
failure: CIP20.MessageValidationFailure.wrongType,
22+
valid: false
23+
});
24+
expect(CIP20.validateMessage({ message: compliantShortMessage } as unknown as string)).toStrictEqual({
25+
failure: CIP20.MessageValidationFailure.wrongType,
26+
valid: false
27+
});
28+
expect(CIP20.validateMessage([compliantShortMessage] as unknown as string)).toStrictEqual({
29+
failure: CIP20.MessageValidationFailure.wrongType,
30+
valid: false
31+
});
32+
expect(
33+
CIP20.validateMessage(new Map([[CIP20.METADATUM_LABEL, compliantShortMessage]]) as unknown as string)
34+
).toStrictEqual({ failure: CIP20.MessageValidationFailure.wrongType, valid: false });
35+
});
36+
});
37+
describe('toTxMetadata', () => {
38+
describe('args object', () => {
39+
it('produces a CIP20-compliant TxMetadata map', () => {
40+
const metadata = CIP20.toTxMetadata({ messages: [compliantShortMessage] }) as Cardano.TxMetadata;
41+
expect(metadata.has(CIP20.METADATUM_LABEL)).toBe(true);
42+
const cip20Metadata = metadata.get(CIP20.METADATUM_LABEL) as Cardano.MetadatumMap;
43+
expect(cip20Metadata.get('msg')).toStrictEqual([compliantShortMessage]);
44+
});
45+
it('throws an error if any messages are invalid', () => {
46+
expect(() =>
47+
CIP20.toTxMetadata({
48+
messages: [compliantShortMessage, compliantMaxMessage, oversizeMessage]
49+
})
50+
).toThrowError(CIP20.MessageValidationError);
51+
});
52+
});
53+
describe('produces a CIP20-compliant TxMetadata map with a string arg', () => {
54+
test('larger than 64 bytes', () => {
55+
const metadata = CIP20.toTxMetadata(oversizeMessage) as Cardano.TxMetadata;
56+
expect(metadata.has(CIP20.METADATUM_LABEL)).toBe(true);
57+
const cip20Metadata = metadata.get(CIP20.METADATUM_LABEL) as Cardano.MetadatumMap;
58+
expect((cip20Metadata.get('msg') as string[]).length).toBe(2);
59+
});
60+
test('equal to 64 bytes', () => {
61+
const metadata = CIP20.toTxMetadata(compliantMaxMessage) as Cardano.TxMetadata;
62+
expect(metadata.has(CIP20.METADATUM_LABEL)).toBe(true);
63+
const cip20Metadata = metadata.get(CIP20.METADATUM_LABEL) as Cardano.MetadatumMap;
64+
expect((cip20Metadata.get('msg') as string[]).length).toBe(1);
65+
});
66+
test('smaller than to 64 bytes', () => {
67+
const metadata = CIP20.toTxMetadata(compliantShortMessage) as Cardano.TxMetadata;
68+
expect(metadata.has(CIP20.METADATUM_LABEL)).toBe(true);
69+
const cip20Metadata = metadata.get(CIP20.METADATUM_LABEL) as Cardano.MetadatumMap;
70+
expect((cip20Metadata.get('msg') as string[]).length).toBe(1);
71+
});
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)