diff --git a/packages/ensjs/src/errors/utils.ts b/packages/ensjs/src/errors/utils.ts index 9b423aba..6f28eaf9 100644 --- a/packages/ensjs/src/errors/utils.ts +++ b/packages/ensjs/src/errors/utils.ts @@ -162,3 +162,11 @@ export class InvalidContentHashError extends BaseError { super('Invalid content hash') } } + +export class UnknownContentTypeError extends BaseError { + override name = 'UnknownContentTypeError' + + constructor({ contentType }: { contentType: string }) { + super(`Unknown content type: ${contentType}`) + } +} diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts index 544b71a1..dd0a3c3f 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts @@ -1,4 +1,8 @@ -import { encodeAbi } from './encodeAbi.js' +import { + contentTypeToEncodeAs, + encodeAbi, + encodeAsToContentType, +} from './encodeAbi.js' describe('encodeAbi', () => { it('encodes data as JSON', async () => { @@ -31,3 +35,43 @@ describe('encodeAbi', () => { expect(result.encodedData).toEqual('0x666f6f3d626172') }) }) + +describe('encodeAsToContentType', () => { + it('returns the correct content type for json', () => { + expect(encodeAsToContentType('json')).toEqual(1) + }) + it('returns the correct content type for zlib', () => { + expect(encodeAsToContentType('zlib')).toEqual(2) + }) + it('returns the correct content type for cbor', () => { + expect(encodeAsToContentType('cbor')).toEqual(4) + }) + it('returns the correct content type for uri', () => { + expect(encodeAsToContentType('uri')).toEqual(8) + }) + it('throws an error for an unknown content type', () => { + expect(() => encodeAsToContentType('foo' as any)).toThrow( + 'Unknown content type: foo', + ) + }) +}) + +describe('contentTypeToEncodeAs', () => { + it('returns the correct encodeAs for json', () => { + expect(contentTypeToEncodeAs(1)).toEqual('json') + }) + it('returns the correct encodeAs for zlib', () => { + expect(contentTypeToEncodeAs(2)).toEqual('zlib') + }) + it('returns the correct encodeAs for cbor', () => { + expect(contentTypeToEncodeAs(4)).toEqual('cbor') + }) + it('returns the correct encodeAs for uri', () => { + expect(contentTypeToEncodeAs(8)).toEqual('uri') + }) + it('throws an error for an unknown content type', () => { + expect(() => contentTypeToEncodeAs(3 as any)).toThrow( + 'Unknown content type: 3', + ) + }) +}) diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.ts b/packages/ensjs/src/utils/encoders/encodeAbi.ts index 2880ba10..35f3d1f2 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.ts @@ -1,16 +1,18 @@ import { bytesToHex, stringToHex, type Hex } from 'viem' +import { UnknownContentTypeError } from '../../errors/utils.js' import type { Prettify } from '../../types.js' type AbiEncodeAs = 'json' | 'zlib' | 'cbor' | 'uri' type AbiContentType = 1 | 2 | 4 | 8 -type AbiEncodeMap = { - json: 1 - zlib: 2 - cbor: 4 - uri: 8 -} +const abiEncodeMap = { + json: 1, + zlib: 2, + cbor: 4, + uri: 8, +} as const +type AbiEncodeMap = typeof abiEncodeMap type GetAbiContentType = AbiEncodeMap[TEncodeAs] @@ -33,6 +35,33 @@ export type EncodedAbi = { export type EncodeAbiReturnType = EncodedAbi +export const contentTypeToEncodeAs = ( + contentType: AbiContentType, +): AbiEncodeAs => { + switch (contentType) { + case 1: + return 'json' + case 2: + return 'zlib' + case 4: + return 'cbor' + case 8: + return 'uri' + default: + throw new UnknownContentTypeError({ contentType }) + } +} + +export const encodeAsToContentType = ( + encodeAs: AbiEncodeAs, +): AbiContentType => { + const contentType = abiEncodeMap[encodeAs] + if (contentType === undefined) { + throw new UnknownContentTypeError({ contentType: encodeAs }) + } + return contentType +} + export const encodeAbi = async < TEncodeAs extends AbiEncodeAs, TContentType extends GetAbiContentType, diff --git a/packages/ensjs/src/utils/index.ts b/packages/ensjs/src/utils/index.ts index 4955aaaf..5d4f351b 100644 --- a/packages/ensjs/src/utils/index.ts +++ b/packages/ensjs/src/utils/index.ts @@ -1,5 +1,7 @@ export { + contentTypeToEncodeAs, encodeAbi, + encodeAsToContentType, type EncodeAbiParameters, type EncodeAbiReturnType, type EncodedAbi,