From dd1c996a4afa4c4f772a144d4e5155a814193491 Mon Sep 17 00:00:00 2001 From: Leon Talbert Date: Wed, 12 Jul 2023 13:50:44 +0800 Subject: [PATCH 1/3] utils tests --- packages/ensjs/package.json | 2 +- .../src/utils/encoders/encodeAbi.test.ts | 31 ++++++++++ .../src/utils/encoders/encodeSetAbi.test.ts | 31 ++++++++++ .../src/utils/encoders/encodeSetAddr.test.ts | 59 +++++++++++++++++++ .../encoders/encodeSetContentHash.test.ts | 37 ++++++++++++ .../utils/encoders/encodeSetContentHash.ts | 1 + .../src/utils/encoders/encodeSetText.test.ts | 20 +++++++ packages/ensjs/src/utils/format.test.ts | 17 ++++++ packages/ensjs/src/utils/format.ts | 6 -- packages/ensjs/src/utils/index.ts | 2 +- 10 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 packages/ensjs/src/utils/encoders/encodeAbi.test.ts create mode 100644 packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts create mode 100644 packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts create mode 100644 packages/ensjs/src/utils/encoders/encodeSetContentHash.test.ts create mode 100644 packages/ensjs/src/utils/encoders/encodeSetText.test.ts create mode 100644 packages/ensjs/src/utils/format.test.ts diff --git a/packages/ensjs/package.json b/packages/ensjs/package.json index eaba6061..1407c06c 100644 --- a/packages/ensjs/package.json +++ b/packages/ensjs/package.json @@ -83,7 +83,7 @@ "start": "ts-node --files src/index.test.ts", "test": "jest --runInBand", "test:watch": "jest --watch --run-in-band", - "test:specific": "ts-node --files ", + "test:specific": "jest --testPathPattern=jest --testPathPattern=src/utils/encoders/encodeSetText.test.ts", "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..af4343d7 --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts @@ -0,0 +1,31 @@ +import { encodeAbi } from './encodeAbi' + +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') + }) +}) \ No newline at end of file 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..5929b593 --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts @@ -0,0 +1,31 @@ +import { encodeSetAbi, EncodeSetAbiParameters } from './encodeSetAbi' + +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', () => { + const encodedData = '0xabcdef123456' + const parametersWithEncodedData: EncodeSetAbiParameters = { + namehash, + contentType, + encodedData, + } + const expected = '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000006abcdef1234560000000000000000000000000000000000000000000000000000' + const result = encodeSetAbi(parametersWithEncodedData) + expect(result).toEqual(expected) + }) +}) \ No newline at end of file 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..d24c4d5f --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts @@ -0,0 +1,59 @@ +import { encodeSetAddr, EncodeSetAddrParameters } from './encodeSetAddr' + +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) + }) + }) +}) \ No newline at end of file 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..9d84b71f --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetContentHash.test.ts @@ -0,0 +1,37 @@ +import { + encodeSetContentHash, + EncodeSetContentHashParameters, +} from './encodeSetContentHash' + +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)).toThrowError( + 'Failed to encocde contentHash', + ) + }) +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetContentHash.ts b/packages/ensjs/src/utils/encoders/encodeSetContentHash.ts index ecbad557..a0b45269 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetContentHash.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetContentHash.ts @@ -17,6 +17,7 @@ export const encodeSetContentHash = ({ if (contentHash) { const encodedObject = encodeContenthash(contentHash) if (encodedObject.error) throw new Error(encodedObject.error) + if (!encodedObject.encoded) throw new Error('Failed to encocde contentHash') encodedHash = encodedObject.encoded as Hex } return encodeFunctionData({ 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..ae92d76c --- /dev/null +++ b/packages/ensjs/src/utils/encoders/encodeSetText.test.ts @@ -0,0 +1,20 @@ +import { encodeSetText, EncodeSetTextParameters } from './encodeSetText' + +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 8ce56800..7943adb8 100644 --- a/packages/ensjs/src/utils/index.ts +++ b/packages/ensjs/src/utils/index.ts @@ -42,7 +42,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, From 07164d7507e46abc165f8ce2014e88ec17d7fe72 Mon Sep 17 00:00:00 2001 From: Leon Talbert Date: Thu, 13 Jul 2023 09:42:38 +0800 Subject: [PATCH 2/3] format --- .../ensjs/src/utils/encoders/encodeAbi.test.ts | 6 ++++-- .../ensjs/src/utils/encoders/encodeSetAbi.test.ts | 14 ++++++++------ .../ensjs/src/utils/encoders/encodeSetAddr.test.ts | 5 +++-- .../ensjs/src/utils/encoders/encodeSetText.test.ts | 3 ++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts index af4343d7..cd83c823 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts @@ -12,7 +12,9 @@ describe('encodeAbi', () => { const data = { foo: 'bar' } const result = await encodeAbi({ encodeAs: 'zlib', data }) expect(result.contentType).toEqual(2) - expect(result.encodedData).toEqual('0x789cab564acbcf57b2524a4a2c52aa05001d7a0434') + expect(result.encodedData).toEqual( + '0x789cab564acbcf57b2524a4a2c52aa05001d7a0434', + ) }) it('encodes data as cbor', async () => { @@ -28,4 +30,4 @@ describe('encodeAbi', () => { expect(result.contentType).toEqual(8) expect(result.encodedData).toEqual('0x666f6f3d626172') }) -}) \ No newline at end of file +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts index 5929b593..d23f7e56 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts @@ -1,7 +1,8 @@ import { encodeSetAbi, EncodeSetAbiParameters } from './encodeSetAbi' describe('encodeSetAbi', () => { - const namehash = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' + const namehash = + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' const contentType = 0 const encodedData = null @@ -12,20 +13,21 @@ describe('encodeSetAbi', () => { } it('encodes the setABI function data correctly', () => { - const expected = '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' + const expected = + '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' const result = encodeSetAbi(parameters) expect(result).toEqual(expected) }) it('encodes the setABI function data correctly with encodedData', () => { - const encodedData = '0xabcdef123456' const parametersWithEncodedData: EncodeSetAbiParameters = { namehash, contentType, - encodedData, + encodedData: '0xabcdef123456', } - const expected = '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000006abcdef1234560000000000000000000000000000000000000000000000000000' + const expected = + '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000006abcdef1234560000000000000000000000000000000000000000000000000000' const result = encodeSetAbi(parametersWithEncodedData) expect(result).toEqual(expected) }) -}) \ No newline at end of file +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts index d24c4d5f..22739f23 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts @@ -1,6 +1,7 @@ import { encodeSetAddr, EncodeSetAddrParameters } from './encodeSetAddr' -const namehash = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' +const namehash = + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' describe('encodeSetAddr', () => { const testCases: Array<[string, EncodeSetAddrParameters, string]> = [ @@ -56,4 +57,4 @@ describe('encodeSetAddr', () => { expect(encodeSetAddr(parameters)).toEqual(expectedOutput) }) }) -}) \ No newline at end of file +}) diff --git a/packages/ensjs/src/utils/encoders/encodeSetText.test.ts b/packages/ensjs/src/utils/encoders/encodeSetText.test.ts index ae92d76c..609b2eb6 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetText.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetText.test.ts @@ -13,7 +13,8 @@ describe('encodeSetText', () => { } it('encodes the function data correctly', () => { - const expected = '0x10f13a8c1234567890123456789012345678901234567890123456789012345678901234000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005656d61696c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001074657374406578616d706c652e636f6d00000000000000000000000000000000' + const expected = + '0x10f13a8c1234567890123456789012345678901234567890123456789012345678901234000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005656d61696c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001074657374406578616d706c652e636f6d00000000000000000000000000000000' const result = encodeSetText(parameters) expect(result).toEqual(expected) }) From f4223ce4703200700c34268984af4b7e7409c3ca Mon Sep 17 00:00:00 2001 From: tate Date: Wed, 9 Aug 2023 10:36:37 +1000 Subject: [PATCH 3/3] remove script --- packages/ensjs/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ensjs/package.json b/packages/ensjs/package.json index e5721fe8..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": "jest --testPathPattern=jest --testPathPattern=src/utils/encoders/encodeSetText.test.ts", "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\"}'",