Skip to content

Commit

Permalink
abi encode helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Aug 21, 2023
1 parent f7d6544 commit 700a97b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/ensjs/src/errors/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
46 changes: 45 additions & 1 deletion packages/ensjs/src/utils/encoders/encodeAbi.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { encodeAbi } from './encodeAbi.js'
import {
contentTypeToEncodeAs,
encodeAbi,
encodeAsToContentType,
} from './encodeAbi.js'

describe('encodeAbi', () => {
it('encodes data as JSON', async () => {
Expand Down Expand Up @@ -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',
)
})
})
41 changes: 35 additions & 6 deletions packages/ensjs/src/utils/encoders/encodeAbi.ts
Original file line number Diff line number Diff line change
@@ -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<TEncodeAs extends AbiEncodeAs> = AbiEncodeMap[TEncodeAs]

Expand All @@ -33,6 +35,33 @@ export type EncodedAbi<TContentType extends AbiContentType = AbiContentType> = {
export type EncodeAbiReturnType<TContentType extends AbiContentType> =
EncodedAbi<TContentType>

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<TEncodeAs>,
Expand Down
2 changes: 2 additions & 0 deletions packages/ensjs/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export {
contentTypeToEncodeAs,
encodeAbi,
encodeAsToContentType,
type EncodeAbiParameters,
type EncodeAbiReturnType,
type EncodedAbi,
Expand Down

0 comments on commit 700a97b

Please sign in to comment.