Skip to content

Commit

Permalink
test: add tests for encoding array index
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed May 17, 2024
1 parent 5217fba commit a36f694
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,17 @@ describe('encodeKeyName', () => {
'0x31145577efe228036af40000a4fbbfe353124e6fa6bb7f8e088a9269df552ea2',
);
});

it('works for Array keys with index as `dynamicKeyParts', () => {
assert.deepStrictEqual(
encodeKeyName('MusicPlaylist[]', 2),
'0x03573598507fc76d82171baa336b7fd700000000000000000000000000000002',
);
assert.deepStrictEqual(
erc725Instance.encodeKeyName('MusicPlaylist[]', 2),
'0x03573598507fc76d82171baa336b7fd700000000000000000000000000000002',
);
});
});

describe('supportsInterface', () => {
Expand Down
16 changes: 13 additions & 3 deletions src/lib/encodeKeyName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
padLeft,
} from 'web3-utils';

import { guessKeyTypeFromKeyName } from './utils';
import { encodeArrayKey, guessKeyTypeFromKeyName } from './utils';
import { DynamicKeyParts } from '../types/dynamicKeys';

// https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-2-ERC725YJSONSchema.md#mapping
Expand Down Expand Up @@ -248,9 +248,12 @@ function encodeDynamicKeyName(

switch (keyType) {
case 'Mapping':
return encodeDynamicMapping(name, dynamicKeyPartsArray);
return encodeDynamicMapping(name, dynamicKeyPartsArray as string[]);
case 'MappingWithGrouping':
return encodeDynamicMappingWithGrouping(name, dynamicKeyPartsArray);
return encodeDynamicMappingWithGrouping(
name,
dynamicKeyPartsArray as string[],
);
default:
throw new Error(
`Could not encode dynamic key: ${name} of type: ${keyType}`,
Expand Down Expand Up @@ -295,6 +298,13 @@ export function encodeKeyName(name: string, dynamicKeyParts?: DynamicKeyParts) {
]);
}
case 'Array': // Warning: this can not correctly encode subsequent keys of array, only the initial Array key will work
// encode for array index
if (dynamicKeyParts && typeof dynamicKeyParts == 'number') {
return encodeArrayKey(keccak256(name), dynamicKeyParts);
}

// encode for array length
return keccak256(name);
case 'Singleton':
return keccak256(name);
default:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/schemaParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('schemaParser getSchema', () => {
});
});

describe.only('Array', () => {
describe('Array', () => {
it('finds initial key of type Array correctly', () => {
const schema = getSchema(
'0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
Expand Down
32 changes: 16 additions & 16 deletions src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ describe('utils', () => {
value: '0x00000000000000000000000000000002',
},
{
key: '0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000000',
key: '0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000000',
value: '0xc444009d38d3046bb0cf81fa2cd295ce46a67c78',
},
{
key: '0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000001',
key: '0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000001',
value: '0x4febc3491230571f6e1829e46602e3b110215a2e',
},
],
Expand Down Expand Up @@ -560,7 +560,7 @@ describe('utils', () => {
assert.deepStrictEqual(encodedDataWithMultipleKeys, {
keys: [
'0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000000',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000000',
],
values: [
'0x00000000000000000000000000000001',
Expand Down Expand Up @@ -596,17 +596,17 @@ describe('utils', () => {
assert.deepStrictEqual(encodedDataWithMultipleKeys, {
keys: [
'0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000000',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000001',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000002',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000003',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000004',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000005',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000006',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000007',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000008',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000009',
'0x3a47ab5bd3a594c3a8995f8fa58d08760000000000000000000000000000000a',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000000',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000001',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000002',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000003',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000004',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000005',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000006',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000007',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000008',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000009',
'0x7c8c3416d6cda87cd42c71ea1843df280000000000000000000000000000000a',
],
values: ['0x0000000000000000000000000000000b', ...addressArray],
});
Expand Down Expand Up @@ -665,8 +665,8 @@ describe('utils', () => {
keys: [
'0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
'0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000000',
'0x3a47ab5bd3a594c3a8995f8fa58d087600000000000000000000000000000001',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000000',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000001',
'0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
],
values: [
Expand Down
2 changes: 1 addition & 1 deletion src/types/dynamicKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { EncodeDataType } from './encodeData/JSONURL';

export type DynamicKeyParts = string | string[];
export type DynamicKeyParts = string | string[] | number;

export interface DynamicKeyPartInput {
dynamicKeyParts: DynamicKeyParts;
Expand Down

0 comments on commit a36f694

Please sign in to comment.