From 114f324cc829f5a214d13ea2738b4c3ffc0ad032 Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Fri, 19 Jan 2024 00:02:42 +0800 Subject: [PATCH 1/5] fix setting abi and add tests --- .../src/functions/wallet/setAbiRecord.test.ts | 81 ++++++++++++++++++- .../src/functions/wallet/setAbiRecord.ts | 5 +- .../src/functions/wallet/setRecords.test.ts | 57 +++++++++++++ .../ensjs/src/utils/encoders/encodeAbi.ts | 12 +-- .../ensjs/src/utils/encoders/encodeSetAbi.ts | 4 +- .../src/utils/generateRecordCallArray.ts | 5 +- 6 files changed, 146 insertions(+), 18 deletions(-) diff --git a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts index 0b095497..10c7af42 100644 --- a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts +++ b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts @@ -136,12 +136,16 @@ it('should allow an abi record to be set with uri content type', async () => { expect(response!.decoded).toBe(true) }) -it('should allow an abi record to be set to blank', async () => { +it('should allow an abi record to be set to null with json content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'json', + data: null, + }) const tx = await setAbiRecord(walletClient, { name: 'with-type-1-abi.eth', - encodedAbi: null, + encodedAbi, resolverAddress: (await getResolver(publicClient, { - name: 'test123.eth', + name: 'with-type-1-abi.eth', }))!, account: accounts[1], }) @@ -150,7 +154,76 @@ it('should allow an abi record to be set to blank', async () => { expect(receipt.status).toBe('success') const response = await getAbiRecord(publicClient, { - name: 'test123.eth', + name: 'with-type-1-abi.eth', + }) + expect(response).toBeNull() +}) + +it('should allow an abi record to be set to null with zlib content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'zlib', + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name: 'with-type-2-abi.eth', + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name: 'with-type-2-abi.eth', + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name: 'with-type-2-abi.eth', + }) + expect(response).toBeNull() +}) + +it('should allow an abi record to be set to null with cbor content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'cbor', + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name: 'with-type-4-abi.eth', + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name: 'with-type-4-abi.eth', + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name: 'with-type-4-abi.eth', + }) + expect(response).toBeNull() +}) + +it('should allow an abi record to be set to null with uri content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'uri', + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name: 'with-type-8-abi.eth', + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name: 'with-type-8-abi.eth', + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name: 'with-type-8-abi.eth', }) expect(response).toBeNull() }) diff --git a/packages/ensjs/src/functions/wallet/setAbiRecord.ts b/packages/ensjs/src/functions/wallet/setAbiRecord.ts index 59f096c6..2f4bbd1e 100644 --- a/packages/ensjs/src/functions/wallet/setAbiRecord.ts +++ b/packages/ensjs/src/functions/wallet/setAbiRecord.ts @@ -22,7 +22,7 @@ export type SetAbiRecordDataParameters = { /** Name to set ABI for */ name: string /** Encoded ABI data to set */ - encodedAbi: EncodedAbi | null + encodedAbi: EncodedAbi /** Resolver address to set ABI on */ resolverAddress: Address } @@ -47,12 +47,11 @@ export const makeFunctionData = < _wallet: WalletWithEns, { name, encodedAbi, resolverAddress }: SetAbiRecordDataParameters, ): SetAbiRecordDataReturnType => { - const encodedAbi_ = encodedAbi || { contentType: 0, encodedData: null } return { to: resolverAddress, data: encodeSetAbi({ namehash: namehash(name), - ...encodedAbi_, + ...encodedAbi, } as EncodeSetAbiParameters), } } diff --git a/packages/ensjs/src/functions/wallet/setRecords.test.ts b/packages/ensjs/src/functions/wallet/setRecords.test.ts index a5e76a56..d9525d47 100644 --- a/packages/ensjs/src/functions/wallet/setRecords.test.ts +++ b/packages/ensjs/src/functions/wallet/setRecords.test.ts @@ -93,6 +93,63 @@ it('should return a transaction to the resolver and set successfully', async () ] `) }) +it('should return a transaction to the resolver and delete successfully', async () => { + const setupTx = await setRecords(walletClient, { + name: 'test123.eth', + resolverAddress: (await getResolver(publicClient, { + name: 'test123.eth', + }))!, + coins: [ + { + coin: 'etcLegacy', + value: '0x42D63ae25990889E35F215bC95884039Ba354115', + }, + ], + texts: [{ key: 'foo', value: 'bar' }], + abi: await encodeAbi({ encodeAs: 'json', data: dummyABI }), + account: accounts[1], + }) + await waitForTransaction(setupTx) + const checkRecords = await getRecords(publicClient, { + name: 'test123.eth', + records: { + coins: ['etcLegacy'], + texts: ['foo'], + abi: true, + }, + }) + expect(checkRecords.abi!.abi).not.toBeNull() + expect(checkRecords.coins).toHaveLength(1) + expect(checkRecords.texts).toHaveLength(1) + const tx = await setRecords(walletClient, { + name: 'test123.eth', + resolverAddress: (await getResolver(publicClient, { + name: 'test123.eth', + }))!, + coins: [ + { + coin: 'etcLegacy', + value: '', + }, + ], + texts: [{ key: 'foo', value: '' }], + abi: await encodeAbi({ encodeAs: 'json', data: null }), + account: accounts[1], + }) + await waitForTransaction(tx) + + const records = await getRecords(publicClient, { + name: 'test123.eth', + records: { + coins: ['etcLegacy'], + texts: ['foo'], + abi: true, + }, + }) + expect(records.abi).toBeNull() + expect(records.coins).toHaveLength(0) + expect(records.texts).toHaveLength(0) +}) it('should error if there are no records to set', async () => { await expect( setRecords(walletClient, { diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.ts b/packages/ensjs/src/utils/encoders/encodeAbi.ts index 2cd13c3e..16d54e37 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.ts @@ -20,11 +20,11 @@ export type EncodeAbiParameters = TEncodeAs extends 'uri' ? { encodeAs: TEncodeAs - data: string + data: string | null } : { encodeAs: TEncodeAs - data: Record + data: Record | null } export type EncodedAbi = { @@ -76,23 +76,23 @@ export const encodeAbi = async < switch (encodeAs) { case 'json': contentType = 1 - encodedData = stringToHex(JSON.stringify(data)) + encodedData = data ? stringToHex(JSON.stringify(data)) : '0x' break case 'zlib': { contentType = 2 const { deflate } = await import('pako/dist/pako_deflate.min.js') - encodedData = bytesToHex(deflate(JSON.stringify(data))) + encodedData = data ? bytesToHex(deflate(JSON.stringify(data))) : '0x' break } case 'cbor': { contentType = 4 const { cborEncode } = await import('@ensdomains/address-encoder/utils') - encodedData = bytesToHex(new Uint8Array(cborEncode(data))) + encodedData = data ? bytesToHex(new Uint8Array(cborEncode(data))) : '0x' break } default: { contentType = 8 - encodedData = stringToHex(data as string) + encodedData = data ? stringToHex(data as string) : '0x' break } } diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.ts index ea263e85..460583ed 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAbi.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.ts @@ -4,7 +4,7 @@ import type { EncodedAbi } from './encodeAbi.js' export type EncodeSetAbiParameters = { namehash: Hex -} & (EncodedAbi | { contentType: 0; encodedData: null }) +} & EncodedAbi export type EncodeSetAbiReturnType = Hex @@ -16,6 +16,6 @@ export const encodeSetAbi = ({ return encodeFunctionData({ abi: publicResolverSetAbiSnippet, functionName: 'setABI', - args: [namehash, BigInt(contentType), encodedData ?? '0x'], + args: [namehash, BigInt(contentType), encodedData], }) } diff --git a/packages/ensjs/src/utils/generateRecordCallArray.ts b/packages/ensjs/src/utils/generateRecordCallArray.ts index 83b0ec6d..61656375 100644 --- a/packages/ensjs/src/utils/generateRecordCallArray.ts +++ b/packages/ensjs/src/utils/generateRecordCallArray.ts @@ -26,7 +26,7 @@ export type RecordOptions = Prettify<{ /** Array of coin records */ coins?: Omit[] /** ABI value */ - abi?: EncodedAbi | null + abi?: EncodedAbi }> export const generateRecordCallArray = ({ @@ -49,8 +49,7 @@ export const generateRecordCallArray = ({ } if (abi !== undefined) { - const abi_ = abi ?? { contentType: 0, encodedData: null } - const data = encodeSetAbi({ namehash, ...abi_ } as EncodeSetAbiParameters) + const data = encodeSetAbi({ namehash, ...abi } as EncodeSetAbiParameters) if (data) calls.push(data) } From 483f0951012b695208902c7269fb51a5758cd78c Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Fri, 19 Jan 2024 00:16:46 +0800 Subject: [PATCH 2/5] fix unit tests --- .../ensjs/src/utils/encoders/encodeSetAbi.test.ts | 6 +++--- .../ensjs/src/utils/generateRecordCallArray.test.ts | 12 ------------ 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts index 044f5774..65ddd4ec 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts @@ -4,8 +4,8 @@ import { encodeSetAbi, type EncodeSetAbiParameters } from './encodeSetAbi.js' describe('encodeSetAbi', () => { const namehash = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' - const contentType = 0 - const encodedData = null + const contentType = 1 + const encodedData = '0x' const parameters: EncodeSetAbiParameters = { namehash, @@ -15,7 +15,7 @@ describe('encodeSetAbi', () => { it('encodes the setAbi function data correctly', () => { const expected = - '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' + '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' const result = encodeSetAbi(parameters) expect(result).toEqual(expected) }) diff --git a/packages/ensjs/src/utils/generateRecordCallArray.test.ts b/packages/ensjs/src/utils/generateRecordCallArray.test.ts index 5dd3cd54..bfc5b31e 100644 --- a/packages/ensjs/src/utils/generateRecordCallArray.test.ts +++ b/packages/ensjs/src/utils/generateRecordCallArray.test.ts @@ -44,18 +44,6 @@ it('adds contentHash call when contentHash is defined', () => { ] `) }) -it('adds abi call when abi is null', () => { - expect( - generateRecordCallArray({ - namehash: namehash('test.eth'), - abi: null, - }), - ).toMatchInlineSnapshot(` - [ - "0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000", - ] - `) -}) it('does not add abi call when abi is undefined', () => { expect( generateRecordCallArray({ From de7ad28cd6e82477f06e512ac9c541280e55ecca Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Fri, 19 Jan 2024 00:34:37 +0800 Subject: [PATCH 3/5] refactor to fix sonarcloud warnings --- .../src/functions/wallet/setAbiRecord.test.ts | 113 +++++------------- 1 file changed, 33 insertions(+), 80 deletions(-) diff --git a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts index 10c7af42..34825275 100644 --- a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts +++ b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts @@ -136,94 +136,47 @@ it('should allow an abi record to be set with uri content type', async () => { expect(response!.decoded).toBe(true) }) -it('should allow an abi record to be set to null with json content type', async () => { - const encodedAbi = await encodeAbi({ +type EncodeAs = Parameters[0]['encodeAs'] +const ABI_TEST_CASES: { encodeAs: EncodeAs; name: string }[] = [ + { encodeAs: 'json', - data: null, - }) - const tx = await setAbiRecord(walletClient, { name: 'with-type-1-abi.eth', - encodedAbi, - resolverAddress: (await getResolver(publicClient, { - name: 'with-type-1-abi.eth', - }))!, - account: accounts[1], - }) - expect(tx).toBeTruthy() - const receipt = await waitForTransaction(tx) - expect(receipt.status).toBe('success') - - const response = await getAbiRecord(publicClient, { - name: 'with-type-1-abi.eth', - }) - expect(response).toBeNull() -}) - -it('should allow an abi record to be set to null with zlib content type', async () => { - const encodedAbi = await encodeAbi({ + }, + { encodeAs: 'zlib', - data: null, - }) - const tx = await setAbiRecord(walletClient, { - name: 'with-type-2-abi.eth', - encodedAbi, - resolverAddress: (await getResolver(publicClient, { - name: 'with-type-2-abi.eth', - }))!, - account: accounts[1], - }) - expect(tx).toBeTruthy() - const receipt = await waitForTransaction(tx) - expect(receipt.status).toBe('success') - - const response = await getAbiRecord(publicClient, { name: 'with-type-2-abi.eth', - }) - expect(response).toBeNull() -}) - -it('should allow an abi record to be set to null with cbor content type', async () => { - const encodedAbi = await encodeAbi({ + }, + { encodeAs: 'cbor', - data: null, - }) - const tx = await setAbiRecord(walletClient, { - name: 'with-type-4-abi.eth', - encodedAbi, - resolverAddress: (await getResolver(publicClient, { - name: 'with-type-4-abi.eth', - }))!, - account: accounts[1], - }) - expect(tx).toBeTruthy() - const receipt = await waitForTransaction(tx) - expect(receipt.status).toBe('success') - - const response = await getAbiRecord(publicClient, { name: 'with-type-4-abi.eth', - }) - expect(response).toBeNull() -}) - -it('should allow an abi record to be set to null with uri content type', async () => { - const encodedAbi = await encodeAbi({ + }, + { encodeAs: 'uri', - data: null, - }) - const tx = await setAbiRecord(walletClient, { name: 'with-type-8-abi.eth', - encodedAbi, - resolverAddress: (await getResolver(publicClient, { - name: 'with-type-8-abi.eth', - }))!, - account: accounts[1], - }) - expect(tx).toBeTruthy() - const receipt = await waitForTransaction(tx) - expect(receipt.status).toBe('success') + }, +] - const response = await getAbiRecord(publicClient, { - name: 'with-type-8-abi.eth', +ABI_TEST_CASES.forEach(({ encodeAs, name }) => { + it(`should allow an abi record to be set to null with ${encodeAs} content type`, async () => { + const encodedAbi = await encodeAbi({ + encodeAs, + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name, + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name, + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name, + }) + expect(response).toBeNull() }) - expect(response).toBeNull() }) From a02c47b09fc38a9c7b5c3df9895fa98bb5a225d5 Mon Sep 17 00:00:00 2001 From: sugh01 <19183308+sugh01@users.noreply.github.com> Date: Fri, 19 Jan 2024 00:39:21 +0100 Subject: [PATCH 4/5] added few encodeAbi tests --- .../src/utils/encoders/encodeAbi.test.ts | 28 +++++++++++++++++++ .../src/utils/encoders/encodeSetAbi.test.ts | 2 +- .../src/utils/generateRecordCallArray.test.ts | 27 ++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts index dd0a3c3f..b4e6a246 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts @@ -12,6 +12,13 @@ describe('encodeAbi', () => { expect(result.encodedData).toEqual('0x7b22666f6f223a22626172227d') }) + // Null JSON data + it('encodes null JSON data', async () => { + const result = await encodeAbi({ encodeAs: 'json', data: null }) + expect(result.contentType).toEqual(1) + expect(result.encodedData).toEqual('0x') + }) + it('encodes data as zlib', async () => { const data = { foo: 'bar' } const result = await encodeAbi({ encodeAs: 'zlib', data }) @@ -21,6 +28,13 @@ describe('encodeAbi', () => { ) }) + // Null zlib data + it('encodes null zlib data', async () => { + const result = await encodeAbi({ encodeAs: 'zlib', data: null }) + expect(result.contentType).toEqual(2) + expect(result.encodedData).toEqual('0x') + }) + it('encodes data as cbor', async () => { const data = { foo: 'bar' } const result = await encodeAbi({ encodeAs: 'cbor', data }) @@ -28,12 +42,26 @@ describe('encodeAbi', () => { expect(result.encodedData).toEqual('0xa163666f6f63626172') }) + // Null CBOR data + it('encodes null CBOR data', async () => { + const result = await encodeAbi({ encodeAs: 'cbor', data: null }) + expect(result.contentType).toEqual(4) + expect(result.encodedData).toEqual('0x') + }) + 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') }) + + // Null URI data + it('encodes null URI data', async () => { + const result = await encodeAbi({ encodeAs: 'uri', data: null }) + expect(result.contentType).toEqual(8) + expect(result.encodedData).toEqual('0x') + }) }) describe('encodeAsToContentType', () => { diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts index 65ddd4ec..046d95f8 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts @@ -13,7 +13,7 @@ describe('encodeSetAbi', () => { encodedData, } - it('encodes the setAbi function data correctly', () => { + it('encodes the setAbi function data correctly with null encodedData', async () => { const expected = '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' const result = encodeSetAbi(parameters) diff --git a/packages/ensjs/src/utils/generateRecordCallArray.test.ts b/packages/ensjs/src/utils/generateRecordCallArray.test.ts index bfc5b31e..e50f21ef 100644 --- a/packages/ensjs/src/utils/generateRecordCallArray.test.ts +++ b/packages/ensjs/src/utils/generateRecordCallArray.test.ts @@ -1,4 +1,5 @@ import { generateRecordCallArray } from './generateRecordCallArray.js' +import { encodeAbi } from './index.js' import { namehash } from './normalise.js' it('generates a record call array', () => { @@ -78,3 +79,29 @@ it('adds coin calls when coins array is defined and not empty', () => { ] `) }) +it('adds abi call when data is null', async () => { + const result = await encodeAbi({ encodeAs: 'uri', data: null }) + expect( + generateRecordCallArray({ + namehash: namehash('test.eth'), + abi: result, + }), + ).toMatchInlineSnapshot(` + [ + "0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f1000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000", + ] + `) +}) +it('adds abi call when data is not empty', async () => { + const result = await encodeAbi({ encodeAs: 'json', data: { foo: 'bar' } }) + expect( + generateRecordCallArray({ + namehash: namehash('test.eth'), + abi: result, + }), + ).toMatchInlineSnapshot(` + [ + "0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d7b22666f6f223a22626172227d00000000000000000000000000000000000000", + ] + `) +}) From 7f3b1225f730251978409abde8b07053d5af61b2 Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Fri, 19 Jan 2024 17:35:07 +0800 Subject: [PATCH 5/5] simplify encodeAbi + setAbitRecord.test --- .../src/functions/wallet/setAbiRecord.test.ts | 33 ++++++------------- .../src/functions/wallet/setRecords.test.ts | 16 ++++----- .../ensjs/src/utils/encoders/encodeAbi.ts | 18 ++++++---- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts index 34825275..34701364 100644 --- a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts +++ b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts @@ -137,27 +137,14 @@ it('should allow an abi record to be set with uri content type', async () => { }) type EncodeAs = Parameters[0]['encodeAs'] -const ABI_TEST_CASES: { encodeAs: EncodeAs; name: string }[] = [ - { - encodeAs: 'json', - name: 'with-type-1-abi.eth', - }, - { - encodeAs: 'zlib', - name: 'with-type-2-abi.eth', - }, - { - encodeAs: 'cbor', - name: 'with-type-4-abi.eth', - }, - { - encodeAs: 'uri', - name: 'with-type-8-abi.eth', - }, -] - -ABI_TEST_CASES.forEach(({ encodeAs, name }) => { - it(`should allow an abi record to be set to null with ${encodeAs} content type`, async () => { +it.each([ + ['json', 'with-type-1-abi.eth'], + ['zlib', 'with-type-2-abi.eth'], + ['cbor', 'with-type-4-abi.eth'], + ['uri', 'with-type-8-abi.eth'], +] as [EncodeAs, string][])( + `should allow an abi record to be set to null with %s content type`, + async (encodeAs, name) => { const encodedAbi = await encodeAbi({ encodeAs, data: null, @@ -178,5 +165,5 @@ ABI_TEST_CASES.forEach(({ encodeAs, name }) => { name, }) expect(response).toBeNull() - }) -}) + }, +) diff --git a/packages/ensjs/src/functions/wallet/setRecords.test.ts b/packages/ensjs/src/functions/wallet/setRecords.test.ts index c94b3208..e4e0c6ea 100644 --- a/packages/ensjs/src/functions/wallet/setRecords.test.ts +++ b/packages/ensjs/src/functions/wallet/setRecords.test.ts @@ -110,11 +110,9 @@ it('should return a transaction to the resolver and delete successfully', async await waitForTransaction(setupTx) const checkRecords = await getRecords(publicClient, { name: 'test123.eth', - records: { - coins: ['etcLegacy'], - texts: ['foo'], - abi: true, - }, + coins: ['etcLegacy'], + texts: ['foo'], + abi: true, }) expect(checkRecords.abi!.abi).not.toBeNull() expect(checkRecords.coins).toHaveLength(1) @@ -138,11 +136,9 @@ it('should return a transaction to the resolver and delete successfully', async const records = await getRecords(publicClient, { name: 'test123.eth', - records: { - coins: ['etcLegacy'], - texts: ['foo'], - abi: true, - }, + coins: ['etcLegacy'], + texts: ['foo'], + abi: true, }) expect(records.abi).toBeNull() expect(records.coins).toHaveLength(0) diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.ts b/packages/ensjs/src/utils/encoders/encodeAbi.ts index 16d54e37..a976e69e 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.ts @@ -72,27 +72,31 @@ export const encodeAbi = async < Prettify> > => { let contentType: AbiContentType - let encodedData: Hex + let encodedData: Hex = '0x' switch (encodeAs) { case 'json': contentType = 1 - encodedData = data ? stringToHex(JSON.stringify(data)) : '0x' + if (data) encodedData = stringToHex(JSON.stringify(data)) break case 'zlib': { contentType = 2 - const { deflate } = await import('pako/dist/pako_deflate.min.js') - encodedData = data ? bytesToHex(deflate(JSON.stringify(data))) : '0x' + if (data) { + const { deflate } = await import('pako/dist/pako_deflate.min.js') + encodedData = bytesToHex(deflate(JSON.stringify(data))) + } break } case 'cbor': { contentType = 4 - const { cborEncode } = await import('@ensdomains/address-encoder/utils') - encodedData = data ? bytesToHex(new Uint8Array(cborEncode(data))) : '0x' + if (data) { + const { cborEncode } = await import('@ensdomains/address-encoder/utils') + encodedData = bytesToHex(new Uint8Array(cborEncode(data))) + } break } default: { contentType = 8 - encodedData = data ? stringToHex(data as string) : '0x' + if (data) encodedData = stringToHex(data as string) break } }