Skip to content

Commit

Permalink
fix: allow to decode 0x as 0 for keyType Array
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed May 27, 2024
1 parent 622675d commit fde9f5d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/lib/Untitled-1
Original file line number Diff line number Diff line change
@@ -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));
21 changes: 21 additions & 0 deletions src/lib/decodeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ describe('decodeData', () => {
expect(decodedData.value).to.eql(3);
});

it('parses type Array correctly, return empty array when 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([]);
});

it('decodes dynamic keys', () => {
const decodedData = decodeData(
[
Expand Down
2 changes: 1 addition & 1 deletion src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function decodeKey(schema: ERC725JSONSchema, value) {
switch (lowerCaseKeyType) {
case 'array': {
// If user has requested a key which does not exist in the contract, value will be: 0x and value.find() will fail.
if (!value) {
if (!value || value == '0x') {
return [];
}

Expand Down

0 comments on commit fde9f5d

Please sign in to comment.