Skip to content

Commit

Permalink
feat: allow to use decodeData to decode Array length only
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed May 3, 2024
1 parent 00b468b commit fbacfeb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
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
18 changes: 12 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 Expand Up @@ -315,6 +320,7 @@ export function decodeData(
data: DecodeDataInput | DecodeDataInput[],
schema: ERC725JSONSchema[],
): DecodeDataOutput | DecodeDataOutput[] {
debugger;
const processDataInput = (
{ keyName, dynamicKeyParts, value }: DecodeDataInput,
throwException = true,
Expand Down

0 comments on commit fbacfeb

Please sign in to comment.