Skip to content

Commit

Permalink
fix: Implement tests and changes to make them work
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Jun 29, 2024
1 parent fd57a08 commit 49ede8d
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 105 deletions.
38 changes: 31 additions & 7 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'ThisKeyDoesNotExist',
key: '0xb12a0af5f83066646eb63c96bf29dcb827024d9a33189f5a61652a03951d1fbe',
value: null,
nonDynamicName: 'ThisKeyDoesNotExist',
};

assert.deepStrictEqual(data, expectedResult);
Expand All @@ -240,6 +241,7 @@ describe('Running @erc725/erc725.js tests...', () => {
keyType: 'Array',
valueContent: 'Address',
valueType: 'address',
nonDynamicName: 'NonExistingArray[]',
},
],
ERC725_CONTRACT_ADDRESS,
Expand All @@ -251,6 +253,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'NonExistingArray[]',
key: '0xd6cbdbfc8d25c9ce4720b5fe6fa8fc536803944271617bf5425b4bd579195840',
value: [],
nonDynamicName: 'NonExistingArray[]',
});

const dataArray = await erc725.getData(['NonExistingArray[]']);
Expand All @@ -259,6 +262,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'NonExistingArray[]',
key: '0xd6cbdbfc8d25c9ce4720b5fe6fa8fc536803944271617bf5425b4bd579195840',
value: [],
nonDynamicName: 'NonExistingArray[]',
},
]);
});
Expand All @@ -284,6 +288,7 @@ describe('Running @erc725/erc725.js tests...', () => {
{
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
nonDynamicName: 'LSP3Profile',
value: {
verification: {
method: 'keccak256(utf8)',
Expand All @@ -296,6 +301,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
value: '0x36e4Eb6Ee168EF54B1E8e850ACBE51045214B313',
nonDynamicName: 'LSP1UniversalReceiverDelegate',
},
];

Expand Down Expand Up @@ -359,6 +365,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
value: '0x36e4Eb6Ee168EF54B1E8e850ACBE51045214B313',
nonDynamicName: 'LSP1UniversalReceiverDelegate',
});
});
});
Expand Down Expand Up @@ -400,7 +407,9 @@ describe('Running @erc725/erc725.js tests...', () => {
]);
assert.deepStrictEqual(data[0], {
key: '0x4b80742de2bf82acb36300009139def55c73c12bcda9c44f12326686e3948634',
name: 'AddressPermissions:Permissions:9139def55c73c12bcda9c44f12326686e3948634',
name: 'AddressPermissions:Permissions:<address>',
nonDynamicName:
'AddressPermissions:Permissions:9139def55c73c12bcda9c44f12326686e3948634',
value:
'0x0000000000000000000000000000000000000000000000000000000000000002',
});
Expand Down Expand Up @@ -444,7 +453,9 @@ describe('Running @erc725/erc725.js tests...', () => {
]);
assert.deepStrictEqual(data[0], {
key: '0x6de85eaf5d982b4e5da000009139def55c73c12bcda9c44f12326686e3948634',
name: 'LSP4CreatorsMap:9139def55c73c12bcda9c44f12326686e3948634',
name: 'LSP4CreatorsMap:<address>',
nonDynamicName:
'LSP4CreatorsMap:9139def55c73c12bcda9c44f12326686e3948634',
value: ['0x24871b3d', 0],
});
});
Expand Down Expand Up @@ -472,6 +483,7 @@ describe('Running @erc725/erc725.js tests...', () => {
{
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
nonDynamicName: 'LSP3Profile',
value: {
verification: {
method: 'keccak256(utf8)',
Expand All @@ -484,6 +496,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
value: '0x36e4Eb6Ee168EF54B1E8e850ACBE51045214B313',
nonDynamicName: 'LSP1UniversalReceiverDelegate',
},
];

Expand Down Expand Up @@ -532,6 +545,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: null,
nonDynamicName: 'LSP3Profile',
});
});

Expand Down Expand Up @@ -599,16 +613,20 @@ describe('Running @erc725/erc725.js tests...', () => {
{
key: '0x48643a15ac5407a175674ab0f8c92df5ae90694dac72ebf0a058fb2599e3b06a',
name: 'MyURL',
nonDynamicName: 'MyURL',
value: 'ipfs://QmbErKh3FjsAR6YjsTjHZNm6McDp6aRt82Ftcv9AJJvZbd',
},
{
key: '0x74ac2555c10b9349e78f0000b74a88c43bcf691bd7a851f6603cb1868f6fc147',
name: 'LSP12IssuedAssetsMap:b74a88C43BCf691bd7A851f6603cb1868f6fc147',
name: 'LSP12IssuedAssetsMap:<address>',
nonDynamicName:
'LSP12IssuedAssetsMap:b74a88C43BCf691bd7A851f6603cb1868f6fc147',
value: '0x1098603B193d276f5fA176CC02007B609F9DAE6b',
},
{
key: '0xeafec4d89fa9619884b600005ef83ad9559033e6e941db7d7c495acdce616347',
name: 'SupportedStandards:LSP3Profile',
nonDynamicName: 'SupportedStandards:LSP3Profile',
value: '0x5ef83ad9',
},
]);
Expand Down Expand Up @@ -704,6 +722,7 @@ describe('Running @erc725/erc725.js tests...', () => {
key: testJSONURLSchema.key,
name: testJSONURLSchema.name,
value: JSON.parse(jsonString),
nonDynamicName: testJSONURLSchema.name,
});
});

Expand Down Expand Up @@ -743,7 +762,9 @@ describe('Running @erc725/erc725.js tests...', () => {
});

assert.deepStrictEqual(result, {
name: 'JSONForAddress:cafecafecafecafecafecafecafecafecafecafe',
name: 'JSONForAddress:<address>',
nonDynamicName:
'JSONForAddress:cafecafecafecafecafecafecafecafecafecafe',
key: '0x84b02f6e50a0a0819a4f0000cafecafecafecafecafecafecafecafecafecafe',
value: JSON.parse(jsonString),
});
Expand Down Expand Up @@ -817,7 +838,7 @@ describe('Running @erc725/erc725.js tests...', () => {
const result = await erc725.getData(
schemaElement.dynamicKeyParts
? {
keyName: schemaElement.key,
keyName: schemaElement.name,
dynamicKeyParts: schemaElement.dynamicKeyParts,
}
: schemaElement.key,
Expand All @@ -826,6 +847,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: schemaElement.name,
key: schemaElement.key,
value: schemaElement.expectedResult,
nonDynamicName: schemaElement.nonDynamicName,
});
});

Expand All @@ -838,7 +860,7 @@ describe('Running @erc725/erc725.js tests...', () => {
const result = await erc725.getData(
schemaElement.dynamicKeyParts
? {
keyName: schemaElement.key,
keyName: schemaElement.name,
dynamicKeyParts: schemaElement.dynamicKeyParts,
}
: schemaElement.key,
Expand All @@ -847,6 +869,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: schemaElement.name,
key: schemaElement.key,
value: schemaElement.expectedResult,
nonDynamicName: schemaElement.nonDynamicName,
});
});
});
Expand Down Expand Up @@ -1136,6 +1159,7 @@ describe('Running @erc725/erc725.js tests...', () => {
const result = erc725.encodeData([
{
keyName: schemaElement.name,
dynamicKeyParts: schemaElement.dynamicKeyParts,
value: schemaElement.expectedResult,
},
]);
Expand Down Expand Up @@ -1979,7 +2003,7 @@ describe('decodeMappingKey', () => {
[
{
type: 'bytes16',
value: '00000000000000000000000012345678',
value: '0x12345678000000000000000000000000',
},
],
);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/decodeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('decodeData', () => {
valueContent: 'JSONURL', // Deprecated - We keep it for backward compatibility between v0.21.3 and v0.22.0
},
{
name: 'MyKeyName2:<bytes32>:<bool>',
name: 'MyKeyName2:<bytes20>:<bool>',
key: '0x',
keyType: 'MappingWithGrouping',
valueType: 'bytes',
Expand Down Expand Up @@ -309,9 +309,9 @@ describe('decodeData', () => {
const decodedData = decodeData(
[
{
keyName: 'MyKeyName2:<bytes32>:<bool>',
keyName: 'MyKeyName2:<bytes20>:<bool>',
dynamicKeyParts: [
'0xaaaabbbbccccddddeeeeffff111122223333444455556666777788889999aaaa',
'0xaaaabbbbccccddddeeeeffff1111222233334444',
'true',
],
value:
Expand All @@ -331,8 +331,8 @@ describe('decodeData', () => {
schemas,
);

expect(decodedData.map(({ name }) => name)).to.eql([
'MyKeyName2:aaaabbbbccccddddeeeeffff111122223333444455556666777788889999aaaa:true',
expect(decodedData.map(({ nonDynamicName }) => nonDynamicName)).to.eql([
'MyKeyName2:aaaabbbbccccddddeeeeffff1111222233334444:true',
'MyDynamicKey2:cafecafecafecafecafecafecafecafecafecafe',
'KeyTwo',
]);
Expand Down
10 changes: 8 additions & 2 deletions src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ export function decodeData(
const schemaElement: ERC725JSONSchema = isDynamic
? getSchemaElement(schema, keyName, dynamicKeyParts)
: getSchemaElement(schema, keyName);
// if (isDynamic && !dynamicKeyParts) {
// decodeKey
// }
let decodedValue: any = null;
try {
decodedValue = decodeKey(schemaElement, value);
Expand All @@ -338,9 +341,12 @@ export function decodeData(
}
console.error(error);
}
const { key, name, nonDynamicName } = schemaElement;
return {
key: schemaElement.key,
name: schemaElement.name,
key,
name,
...(nonDynamicName ? { nonDynamicName } : { nonDynamicName: name }),
...(dynamicKeyParts ? { dynamicKeyParts } : {}),
value: decodedValue,
};
};
Expand Down
30 changes: 17 additions & 13 deletions src/lib/decodeMappingKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ describe('decodeDynamicKeyParts', () => {
key: {
name: 'MyKeyName:<bytes4>',
encoded:
'0x35e6950bc8d21a1699e5000000000000000000000000000000000000abcd1234',
'0x35e6950bc8d21a1699e50000abcd123400000000000000000000000000000000',
},
dynamicKeyParts: [{ type: 'bytes4', value: 'abcd1234' }],
dynamicKeyParts: [{ type: 'bytes4', value: '0xabcd1234' }],
},
{
key: {
Expand All @@ -57,7 +57,8 @@ describe('decodeDynamicKeyParts', () => {
dynamicKeyParts: [
{
type: 'bytes32',
value: 'aaaabbbbccccddddeeeeffff1111222233334444',
value:
'0xaaaabbbbccccddddeeeeffff1111222233334444000000000000000000000000',
},
],
},
Expand Down Expand Up @@ -94,23 +95,23 @@ describe('decodeDynamicKeyParts', () => {
key: {
name: 'MyKeyName:<bytes2>:<uint32>',
encoded:
'0x35e6950bc8d20000ffff000000000000000000000000000000000000f342d33d',
'0x35e6950bc8d2ffff0000000000000000000000000000000000000000f342d33d',
},
dynamicKeyParts: [
{ type: 'bytes2', value: 'ffff' },
{ type: 'bytes2', value: '0xffff' },
{ type: 'uint32', value: 4081242941 },
],
},
{
key: {
name: 'MyKeyName:<address>:<address>',
name: 'MyKeyName:<bytes4>:<address>',
encoded:
'0x35e6950bc8d2abcdef110000cafecafecafecafecafecafecafecafecafecafe',
},
dynamicKeyParts: [
{
type: 'address',
value: '0x00000000000000000000000000000000AbCDeF11',
type: 'bytes4',
value: '0xabcdef11',
},
{
type: 'address',
Expand All @@ -120,25 +121,28 @@ describe('decodeDynamicKeyParts', () => {
},
{
key: {
name: 'MyKeyName:MyMapName:<bytes32>',
name: 'MyKeyName:MyMapName:<bytes16>',
encoded:
'0x35e6950bc8d275060e3c0000aaaabbbbccccddddeeeeffff1111222233334444',
},
dynamicKeyParts: [
{
type: 'bytes32',
value: 'aaaabbbbccccddddeeeeffff1111222233334444',
type: 'bytes16',
value: '0xaaaabbbbccccddddeeeeffff11112222',
},
],
},
{
key: {
name: 'MyKeyName:<bytes32>:<bool>',
name: 'MyKeyName:<bytes19>:<bool>',
encoded:
'0x35e6950bc8d2aaaabbbb00000000000000000000000000000000000000000001',
},
dynamicKeyParts: [
{ type: 'bytes32', value: 'aaaabbbb' },
{
type: 'bytes19',
value: '0xaaaabbbb000000000000000000000000000000',
},
{ type: 'bool', value: true },
],
},
Expand Down
34 changes: 21 additions & 13 deletions src/lib/decodeMappingKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
* @date 2022
*/

import { padLeft } from 'web3-utils';
import { padLeft, padRight } from 'web3-utils';
import { isHex } from 'web3-validator';
import { decodeValueType } from './encoder';
import { ERC725JSONSchema } from '../types/ERC725JSONSchema';
import { DynamicKeyPart } from '../types/dynamicKeys';
import { dynamicKeySize } from './encodeKeyName';

function isDynamicKeyPart(keyPartName: string): boolean {
return (
Expand All @@ -45,18 +46,16 @@ function decodeKeyPart(

let decodedKey: string | number | boolean | undefined;
const type = keyPartName.slice(1, keyPartName.length - 1);

if (type === 'bool')
if (type === 'bool') {
decodedKey = encodedKeyPart.slice(encodedKeyPart.length - 1) === '1';
else if (type.includes('uint'))
} else if (type.includes('uint'))
decodedKey = Number.parseInt(encodedKeyPart, 16);
else if (type.includes('bytes')) {
const bytesLength = Number.parseInt(type.replace('bytes', ''), 10) * 2;
const sliceFrom =
encodedKeyPart.length - bytesLength < 0
? 0
: encodedKeyPart.length - bytesLength;
decodedKey = encodedKeyPart.slice(sliceFrom);
const charLength = Number.parseInt(type.replace('bytes', ''), 10) * 2;
decodedKey = padRight(
`0x${encodedKeyPart.slice(0, charLength)}`,
charLength,
);
} else if (type === 'address') {
// this is required if the 2nd word is an address in a MappingWithGrouping
const leftPaddedAddress = padLeft(`0x${encodedKeyPart}`, 40);
Expand Down Expand Up @@ -108,10 +107,19 @@ export function decodeMappingKey(
dynamicParts.push(decodeKeyPart(hashedKey.slice(26), keyParts[1]));
break;

case 3: // MappingWithGrouping
dynamicParts.push(decodeKeyPart(hashedKey.slice(14, 22), keyParts[1]));
dynamicParts.push(decodeKeyPart(hashedKey.slice(26), keyParts[2]));
case 3: {
// MappingWithGrouping
const len = dynamicKeySize(keyParts[1]);
if (keyParts[1].startsWith('<')) {
dynamicParts.push(
decodeKeyPart(hashedKey.slice(14, 14 + len * 2), keyParts[1]),
);
}
dynamicParts.push(
decodeKeyPart(hashedKey.slice(14 + len * 2 + 4), keyParts[2]),
);
break;
}
default:
break;
}
Expand Down
Loading

0 comments on commit 49ede8d

Please sign in to comment.