Skip to content

Commit

Permalink
refactor!: move CIP-20 utils to tx-construction package
Browse files Browse the repository at this point in the history
A circular reference was the motivator for this move, but it's better suited here anyway.
  • Loading branch information
rhyslbw committed Aug 22, 2024
1 parent b7ec9fb commit 33422e4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * as Asset from './Asset';
export * as Cardano from './Cardano';
export * as Serialization from './Serialization';
export * as TxMetadata from './TxMetadata';
export * from './Provider';
export * from './util';
export * from './errors';
Expand Down
1 change: 1 addition & 0 deletions packages/tx-construction/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './fees';
export * from './input-selection';
export * from './output-validation';
export * from './tx-builder';
export * from './tx-metadata';
export * from './ensureValidityInterval';
export * from './types';
export * from './computeScriptDataHash';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Cardano } from '@cardano-sdk/core';
import { CustomError } from 'ts-custom-error';
import { StringUtils } from '@cardano-sdk/util';
import { TxMetadata } from '../Cardano';

export const CIP_20_METADATUM_LABEL = 674n;
const MAX_BYTES = 64;
Expand Down Expand Up @@ -53,7 +53,7 @@ export const validateMessage = (entry: unknown): MessageValidationResult => {
* @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 | string): TxMetadata => {
export const toCIP20Metadata = (args: CIP20TxMetadataArgs | string): Cardano.TxMetadata => {
const messages = typeof args === 'string' ? StringUtils.chunkByBytes(args, MAX_BYTES) : args.messages;
const invalidMessages: ValidationResultMap = new Map();
for (const message of messages) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {
MessageValidationFailure,
toCIP20Metadata,
validateMessage
} from '../../src/TxMetadata';
import { Cardano } from '../../src';
import { TxMetadata } from '../../src/Cardano';
} from '../../src';
import { Cardano } from '@cardano-sdk/core';

describe('TxMetadata.cip20', () => {
describe('CIP-20 helper functions', () => {
const compliantShortMessage = 'Lorem ipsum dolor';
const compliantMaxMessage = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean';
const oversizeMessage = `${compliantMaxMessage}1`;
Expand Down Expand Up @@ -44,7 +43,7 @@ describe('TxMetadata.cip20', () => {
describe('toCIP20Metadata', () => {
describe('args object', () => {
it('produces a CIP-20-compliant TxMetadata map', () => {
const metadata = toCIP20Metadata({ messages: [compliantShortMessage] }) as TxMetadata;
const metadata = toCIP20Metadata({ messages: [compliantShortMessage] }) as Cardano.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]);
Expand All @@ -59,19 +58,19 @@ describe('TxMetadata.cip20', () => {
});
describe('producing a CIP-20-compliant TxMetadata map with a string arg', () => {
test('larger than 64 bytes', () => {
const metadata = toCIP20Metadata(oversizeMessage) as TxMetadata;
const metadata = toCIP20Metadata(oversizeMessage) as Cardano.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;
const metadata = toCIP20Metadata(compliantMaxMessage) as Cardano.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;
const metadata = toCIP20Metadata(compliantShortMessage) as Cardano.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);
Expand Down

0 comments on commit 33422e4

Please sign in to comment.