diff --git a/packages/ensjs/package.json b/packages/ensjs/package.json index 2ee433dd..348cab5b 100644 --- a/packages/ensjs/package.json +++ b/packages/ensjs/package.json @@ -84,7 +84,6 @@ "start": "ts-node --files src/index.test.ts", "test": "jest --runInBand", "test:watch": "jest --watch --run-in-band", - "test:specific": "ts-node --files ", "clean": "rm -rf ./dist ./README.md ./LICENSE", "lint": "eslint ./src/* --no-error-on-unmatched-pattern", "build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && echo > ./dist/cjs/package.json '{\"type\":\"commonjs\"}'", diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts new file mode 100644 index 00000000..544b71a1 --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts @@ -0,0 +1,33 @@ +import { encodeAbi } from './encodeAbi.js' + +describe('encodeAbi', () => { + it('encodes data as JSON', async () => { + const data = { foo: 'bar' } + const result = await encodeAbi({ encodeAs: 'json', data }) + expect(result.contentType).toEqual(1) + expect(result.encodedData).toEqual('0x7b22666f6f223a22626172227d') + }) + + it('encodes data as zlib', async () => { + const data = { foo: 'bar' } + const result = await encodeAbi({ encodeAs: 'zlib', data }) + expect(result.contentType).toEqual(2) + expect(result.encodedData).toEqual( + '0x789cab564acbcf57b2524a4a2c52aa05001d7a0434', + ) + }) + + it('encodes data as cbor', async () => { + const data = { foo: 'bar' } + const result = await encodeAbi({ encodeAs: 'cbor', data }) + expect(result.contentType).toEqual(4) + expect(result.encodedData).toEqual('0xa163666f6f63626172') + }) + + it('encodes data as uri', async () => { + const data = 'foo=bar' + const result = await encodeAbi({ encodeAs: 'uri', data }) + expect(result.contentType).toEqual(8) + expect(result.encodedData).toEqual('0x666f6f3d626172') + }) +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts new file mode 100644 index 00000000..044f5774 --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts @@ -0,0 +1,32 @@ +import { encodeAbi } from './encodeAbi.js' +import { encodeSetAbi, type EncodeSetAbiParameters } from './encodeSetAbi.js' + +describe('encodeSetAbi', () => { + const namehash = + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' + const contentType = 0 + const encodedData = null + + const parameters: EncodeSetAbiParameters = { + namehash, + contentType, + encodedData, + } + + it('encodes the setAbi function data correctly', () => { + const expected = + '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' + const result = encodeSetAbi(parameters) + expect(result).toEqual(expected) + }) + + it('encodes the setAbi function data correctly with encodedData', async () => { + const result = encodeSetAbi({ + namehash, + ...(await encodeAbi({ encodeAs: 'json', data: { foo: 'bar' } })), + }) + expect(result).toEqual( + '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d7b22666f6f223a22626172227d00000000000000000000000000000000000000', + ) + }) +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts new file mode 100644 index 00000000..79974174 --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts @@ -0,0 +1,60 @@ +import { encodeSetAddr, type EncodeSetAddrParameters } from './encodeSetAddr.js' + +const namehash = + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' + +describe('encodeSetAddr', () => { + const testCases: Array<[string, EncodeSetAddrParameters, string]> = [ + [ + 'should encode setAddr with ETH address', + { + namehash, + coin: 'ETH', + value: '0x1234567890123456789012345678901234567890', + }, + '0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141234567890123456789012345678901234567890000000000000000000000000', + ], + [ + 'should encode setAddr with BTC address', + { + namehash, + coin: 'BTC', + value: 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq', + }, + '0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000160014e8df018c7e326cc253faac7e46cdc51e68542c4200000000000000000000', + ], + [ + 'should encode setAddr with coin type as number', + { + namehash, + coin: 60, + value: '0x1234567890123456789012345678901234567890', + }, + '0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141234567890123456789012345678901234567890000000000000000000000000', + ], + [ + 'should encode setAddr with coin type as string', + { + namehash, + coin: '60', + value: '0x1234567890123456789012345678901234567890', + }, + '0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141234567890123456789012345678901234567890000000000000000000000000', + ], + [ + 'should encode setAddr with null value', + { + namehash, + coin: 'ETH', + value: null, + }, + '0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000', + ], + ] + + testCases.forEach(([description, parameters, expectedOutput]) => { + it(description, () => { + expect(encodeSetAddr(parameters)).toEqual(expectedOutput) + }) + }) +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetContentHash.test.ts b/packages/ensjs/src/utils/encoders/encodeSetContentHash.test.ts new file mode 100644 index 00000000..b3377d30 --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetContentHash.test.ts @@ -0,0 +1,41 @@ +import { getVersion } from '../../errors/error-utils.js' +import { + encodeSetContentHash, + type EncodeSetContentHashParameters, +} from './encodeSetContentHash.js' + +describe('encodeSetContentHash', () => { + const namehash = + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' + const contentHash = 'ipfs://QmXwMFNjzjRvZuPvzJfYJZ1QqX2QJjzj1YJZ1QqX2QJjzj' + + it('encodes the function data correctly when contentHash is not null', () => { + const expectedEncodedData = + '0x304e6ade1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000026e301017012208e9cc47fde7ff64028480ec671a4ddb8f767a71ff71a73247f51a495a6f296340000000000000000000000000000000000000000000000000000' + const params: EncodeSetContentHashParameters = { namehash, contentHash } + expect(encodeSetContentHash(params)).toEqual(expectedEncodedData) + }) + + it('encodes the function data correctly when contentHash is null', () => { + const expectedEncodedData = + '0x304e6ade1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000' + const params: EncodeSetContentHashParameters = { + namehash, + contentHash: null, + } + expect(encodeSetContentHash(params)).toEqual(expectedEncodedData) + }) + + it('throws an error when contentHash is invalid', () => { + const params: EncodeSetContentHashParameters = { + namehash, + contentHash: 'invalid-content-hash', + } + expect(() => encodeSetContentHash(params)) + .toThrowErrorMatchingInlineSnapshot(` + "Invalid content hash + + Version: ${getVersion()}" + `) + }) +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetText.test.ts b/packages/ensjs/src/utils/encoders/encodeSetText.test.ts new file mode 100644 index 00000000..829b9dce --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetText.test.ts @@ -0,0 +1,21 @@ +import { encodeSetText, type EncodeSetTextParameters } from './encodeSetText.js' + +describe('encodeSetText', () => { + const namehash = + '0x1234567890123456789012345678901234567890123456789012345678901234' + const key = 'email' + const value = 'test@example.com' + + const parameters: EncodeSetTextParameters = { + namehash, + key, + value, + } + + it('encodes the function data correctly', () => { + const expected = + '0x10f13a8c1234567890123456789012345678901234567890123456789012345678901234000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005656d61696c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001074657374406578616d706c652e636f6d00000000000000000000000000000000' + const result = encodeSetText(parameters) + expect(result).toEqual(expected) + }) +}) diff --git a/packages/ensjs/src/utils/format.test.ts b/packages/ensjs/src/utils/format.test.ts new file mode 100644 index 00000000..6cf06da9 --- /dev/null +++ b/packages/ensjs/src/utils/format.test.ts @@ -0,0 +1,17 @@ +import { truncateFormat } from './format.js' + +describe('truncateFormat', () => { + it('should truncate a long name correctly', () => { + const longName = + '[0x12345678901234567890123456789012345678901234567890123456dfssss]' + const expected = '[0x1...sss]' + const result = truncateFormat(longName) + expect(result).toEqual(expected) + }) + + it('should not modify a short name', () => { + const shortName = 'example.eth' + const result = truncateFormat(shortName) + expect(result).toEqual(shortName) + }) +}) diff --git a/packages/ensjs/src/utils/format.ts b/packages/ensjs/src/utils/format.ts index 4c28648e..f8469509 100644 --- a/packages/ensjs/src/utils/format.ts +++ b/packages/ensjs/src/utils/format.ts @@ -1,8 +1,2 @@ -export const formatHashed = (name: string): string => - name.replace(/(\[)(.{64})(\])/g, '0x$2') - export const truncateFormat = (name: string): string => name.replace(/(\[.{3})(.{58})(.{3}\])/g, '$1...$3') - -export const bracketFormat = (name: string): string => - name.replace(/(0x)(.{64})(?=\.)/g, '[$2]') diff --git a/packages/ensjs/src/utils/index.ts b/packages/ensjs/src/utils/index.ts index f6df75c8..5bee0ac8 100644 --- a/packages/ensjs/src/utils/index.ts +++ b/packages/ensjs/src/utils/index.ts @@ -41,7 +41,7 @@ export { type DecodedContentHash, type ProtocolType, } from './contentHash.js' -export { bracketFormat, formatHashed, truncateFormat } from './format.js' +export { truncateFormat } from './format.js' export { generateRecordCallArray, type RecordOptions,