From 5146358f19c0874afd268f7bf8705ff3c6ac392b Mon Sep 17 00:00:00 2001 From: CJ42 Date: Mon, 27 May 2024 12:25:24 +0200 Subject: [PATCH] fix: allow to decode `0x` as `0` for keyType `Array` --- src/lib/Untitled-1 | 23 +++++++++++++++++++++++ src/lib/decodeData.test.ts | 21 +++++++++++++++++++++ src/lib/decodeData.ts | 6 ++++-- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/lib/Untitled-1 diff --git a/src/lib/Untitled-1 b/src/lib/Untitled-1 new file mode 100644 index 00000000..4ec309a2 --- /dev/null +++ b/src/lib/Untitled-1 @@ -0,0 +1,23 @@ +import { ERC725 } from '@erc725/erc725.js'; + +const schema = [ + { + name: 'LSP3Profile', + key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5', + keyType: 'Singleton', + valueType: 'bytes', + valueContent: 'VerifiableURI', + }, +]; + +const erc725 = new ERC725(schema); +const encodedVerifiableURI = + '0x00008019f9b10020ed9808e44f28535d4673424413c06ce26210077628eddf95da6acba96f0b9e25697066733a2f2f6261666b72656967706176746475357035796d6a637a62346a35696d3434326f6e76336735693767747a6b6c3234336e3763616e71796d646e6334'; + +const result = erc725.decodeData([ + { + keyName: 'LSP3Profile', + value: encodedVerifiableURI, + }, +]); +console.log(JSON.stringify(result, null, 4)); diff --git a/src/lib/decodeData.test.ts b/src/lib/decodeData.test.ts index 39545ebf..2dcc8430 100644 --- a/src/lib/decodeData.test.ts +++ b/src/lib/decodeData.test.ts @@ -284,6 +284,27 @@ describe('decodeData', () => { expect(decodedData.value).to.eql(3); }); + it('parses type Array correctly, just the array length but return 0 if value is 0x', () => { + const decodedData = decodeData( + { + keyName: 'LSP12IssuedAssets[]', + value: '0x', + }, + [ + { + name: 'LSP12IssuedAssets[]', + key: '0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd', + keyType: 'Array', + valueContent: 'Address', + valueType: 'address', + }, + ], + ); + + expect(decodedData.name).to.eql('LSP12IssuedAssets[]'); + expect(decodedData.value).to.eql(0); + }); + it('decodes dynamic keys', () => { const decodedData = decodeData( [ diff --git a/src/lib/decodeData.ts b/src/lib/decodeData.ts index 05050361..850f936f 100644 --- a/src/lib/decodeData.ts +++ b/src/lib/decodeData.ts @@ -201,8 +201,10 @@ export function decodeKey(schema: ERC725JSONSchema, value) { } // Decode as a Number when when the encoded value is to set the Array length only - if (typeof value === 'string' && countNumberOfBytes(value) === 16) { - return decodeKeyValue('Number', 'uint128', value, schema.name) || 0; + if (typeof value === 'string') { + if (value === '0x' || countNumberOfBytes(value) === 16) { + return decodeKeyValue('Number', 'uint128', value, schema.name) || 0; + } } const valueElement = value.find((e) => e.key === schema.key);