Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to decode array length only as number using decodeData #430

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/lib/decodeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ describe('decodeData', () => {
]);
});

it('parses type Array correctly but just the array length', () => {
const decodedData = decodeData(
{
keyName: 'LSP12IssuedAssets[]',
value: '0x00000000000000000000000000000003',
},
[
{
name: 'LSP12IssuedAssets[]',
key: '0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
keyType: 'Array',
valueContent: 'Address',
valueType: 'address',
},
],
);

expect(decodedData.name).to.eql('LSP12IssuedAssets[]');
expect(decodedData.value).to.eql(3);
});

it('decodes dynamic keys', () => {
const decodedData = decodeData(
[
Expand Down
17 changes: 11 additions & 6 deletions src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { isDynamicKeyName } from './encodeKeyName';
import { valueContentEncodingMap, decodeValueType } from './encoder';
import { getSchemaElement } from './getSchemaElement';
import { decodeKeyValue, encodeArrayKey } from './utils';
import { countNumberOfBytes, decodeKeyValue, encodeArrayKey } from './utils';

const tupleValueTypesRegex = /bytes(\d+)/;
const valueContentsBytesRegex = /Bytes(\d+)/;
Expand Down Expand Up @@ -195,23 +195,28 @@ export function decodeKey(schema: ERC725JSONSchema, value) {

switch (lowerCaseKeyType) {
case 'array': {
const results: any[] = [];

// If user has requested a key which does not exist in the contract, value will be: 0x and value.find() will fail.
if (!value || typeof value === 'string') {
return results;
if (!value) {
return [];
}

// 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;
}

const valueElement = value.find((e) => e.key === schema.key);
// Handle empty/non-existent array
if (!valueElement) {
return results;
return [];
}

const arrayLength =
decodeKeyValue('Number', 'uint128', valueElement.value, schema.name) ||
0;

const results: any[] = [];

// This will not run if no match or arrayLength
for (let index = 0; index < arrayLength; index++) {
const dataElement = value.find(
Expand Down
Loading