Skip to content

Commit

Permalink
Merge pull request #462 from ERC725Alliance/feat/tests
Browse files Browse the repository at this point in the history
fix: Try to work on tests on top of upgrades
  • Loading branch information
richtera authored Jul 9, 2024
2 parents 036f73f + b0ffa5b commit a99f8c5
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 189 deletions.
40 changes: 32 additions & 8 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,
dynamicName: 'ThisKeyDoesNotExist',
};

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

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

Expand Down Expand Up @@ -359,6 +365,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
value: '0x36e4Eb6Ee168EF54B1E8e850ACBE51045214B313',
dynamicName: '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>',
dynamicName:
'AddressPermissions:Permissions:0x9139def55c73c12bcda9c44f12326686e3948634',
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>',
dynamicName:
'LSP4CreatorsMap:0x9139def55c73c12bcda9c44f12326686e3948634',
value: ['0x24871b3d', 0],
});
});
Expand Down Expand Up @@ -472,6 +483,7 @@ describe('Running @erc725/erc725.js tests...', () => {
{
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
dynamicName: 'LSP3Profile',
value: {
verification: {
method: 'keccak256(utf8)',
Expand All @@ -484,6 +496,7 @@ describe('Running @erc725/erc725.js tests...', () => {
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
value: '0x36e4Eb6Ee168EF54B1E8e850ACBE51045214B313',
dynamicName: 'LSP1UniversalReceiverDelegate',
},
];

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

Expand All @@ -546,7 +560,7 @@ describe('Running @erc725/erc725.js tests...', () => {
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000035697066733a2f2f516d6245724b6833466a73415236596a73546a485a4e6d364d6344703661527438324674637639414a4a765a62640000000000000000000000',
},
{
key: '0x74ac2555c10b9349e78f0000b74a88c43bcf691bd7a851f6603cb1868f6fc147', // LSP12IssuedAssetsMap:b74a88C43BCf691bd7A851f6603cb1868f6fc147
key: '0x74ac2555c10b9349e78f0000b74a88c43bcf691bd7a851f6603cb1868f6fc147', // LSP12IssuedAssetsMap:0xb74a88C43BCf691bd7A851f6603cb1868f6fc147
value:
'0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000141098603b193d276f5fa176cc02007b609f9dae6b000000000000000000000000',
},
Expand Down Expand Up @@ -599,16 +613,20 @@ describe('Running @erc725/erc725.js tests...', () => {
{
key: '0x48643a15ac5407a175674ab0f8c92df5ae90694dac72ebf0a058fb2599e3b06a',
name: 'MyURL',
dynamicName: 'MyURL',
value: 'ipfs://QmbErKh3FjsAR6YjsTjHZNm6McDp6aRt82Ftcv9AJJvZbd',
},
{
key: '0x74ac2555c10b9349e78f0000b74a88c43bcf691bd7a851f6603cb1868f6fc147',
name: 'LSP12IssuedAssetsMap:b74a88C43BCf691bd7A851f6603cb1868f6fc147',
name: 'LSP12IssuedAssetsMap:<address>',
dynamicName:
'LSP12IssuedAssetsMap:0xb74a88C43BCf691bd7A851f6603cb1868f6fc147',
value: '0x1098603B193d276f5fA176CC02007B609F9DAE6b',
},
{
key: '0xeafec4d89fa9619884b600005ef83ad9559033e6e941db7d7c495acdce616347',
name: 'SupportedStandards:LSP3Profile',
dynamicName: '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),
dynamicName: 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>',
dynamicName:
'JSONForAddress:0xcafecafecafecafecafecafecafecafecafecafe',
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,
dynamicName: schemaElement.dynamicName,
});
});

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,
dynamicName: schemaElement.dynamicName,
});
});
});
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
11 changes: 3 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { encodeKeyName, isDynamicKeyName } from './lib/encodeKeyName';
import { ERC725Config, ERC725Options } from './types/Config';
import { Permissions } from './types/Method';
import {
DynamicNameSchema,
ERC725JSONSchema,
ERC725JSONSchemaKeyType,
ERC725JSONSchemaValueContent,
Expand Down Expand Up @@ -381,19 +380,15 @@ export class ERC725 {
getSchema(
keyOrKeys: string[],
providedSchemas?: ERC725JSONSchema[],
): Record<string, ERC725JSONSchema | DynamicNameSchema | null>;
): Record<string, ERC725JSONSchema | null>;
getSchema(
keyOrKeys: string,
providedSchemas?: ERC725JSONSchema[],
): ERC725JSONSchema | DynamicNameSchema | null;
): ERC725JSONSchema | null;
getSchema(
keyOrKeys: string | string[],
providedSchemas?: ERC725JSONSchema[],
):
| ERC725JSONSchema
| DynamicNameSchema
| null
| Record<string, ERC725JSONSchema | DynamicNameSchema | null> {
): ERC725JSONSchema | null | Record<string, ERC725JSONSchema | null> {
return getSchema(
keyOrKeys,
this.options.schemas.concat(providedSchemas || []),
Expand Down
6 changes: 3 additions & 3 deletions src/lib/decodeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ describe('decodeData', () => {
schemas,
);

expect(decodedData.map(({ name }) => name)).to.eql([
'MyKeyName2:aaaabbbbccccddddeeeeffff111122223333444455556666777788889999aaaa:true',
'MyDynamicKey2:cafecafecafecafecafecafecafecafecafecafe',
expect(decodedData.map(({ dynamicName }) => dynamicName)).to.eql([
'MyKeyName2:0xaaaabbbbccccddddeeeeffff111122223333444455556666777788889999aaaa:true',
'MyDynamicKey2:0xcafecafecafecafecafecafecafecafecafecafe',
'KeyTwo',
]);
});
Expand Down
7 changes: 5 additions & 2 deletions src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,12 @@ export function decodeData(
}
console.error(error);
}
const { key, name, dynamicName } = schemaElement;
return {
key: schemaElement.key,
name: schemaElement.name,
key,
name,
...(dynamicName ? { dynamicName } : { dynamicName: name }),
...(dynamicKeyParts ? { dynamicKeyParts } : {}),
value: decodedValue,
};
};
Expand Down
20 changes: 13 additions & 7 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,10 +95,10 @@ describe('decodeDynamicKeyParts', () => {
key: {
name: 'MyKeyName:<bytes2>:<uint32>',
encoded:
'0x35e6950bc8d20000ffff000000000000000000000000000000000000f342d33d',
'0x35e6950bc8d2ffff0000000000000000000000000000000000000000f342d33d',
},
dynamicKeyParts: [
{ type: 'bytes2', value: 'ffff' },
{ type: 'bytes2', value: '0xffff' },
{ type: 'uint32', value: 4081242941 },
],
},
Expand Down Expand Up @@ -127,7 +128,8 @@ describe('decodeDynamicKeyParts', () => {
dynamicKeyParts: [
{
type: 'bytes32',
value: 'aaaabbbbccccddddeeeeffff1111222233334444',
value:
'0xaaaabbbbccccddddeeeeffff1111222233334444000000000000000000000000',
},
],
},
Expand All @@ -138,7 +140,11 @@ describe('decodeDynamicKeyParts', () => {
'0x35e6950bc8d2aaaabbbb00000000000000000000000000000000000000000001',
},
dynamicKeyParts: [
{ type: 'bytes32', value: 'aaaabbbb' },
{
type: 'bytes32',
value:
'0xaaaabbbb00000000000000000000000000000000000000000000000000000000',
},
{ type: 'bool', value: true },
],
},
Expand Down
18 changes: 8 additions & 10 deletions src/lib/decodeMappingKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @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';
Expand All @@ -45,18 +45,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
Loading

0 comments on commit a99f8c5

Please sign in to comment.