Skip to content

Commit

Permalink
refactor!: use array for encodeData
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugoo committed Jun 7, 2022
1 parent f386e15 commit a2e6cdd
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 133 deletions.
122 changes: 76 additions & 46 deletions docs/classes/ERC725.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ erc725.decodeData(data [, schemas]);
```

```js
ERC725.encodeData(data, schemas);
ERC725.decodeData(data, schemas);
```

If you are reading the key-value store from an ERC725 smart-contract you can use the `decodeData` function to do the decoding for you.
Expand Down Expand Up @@ -215,17 +215,23 @@ When encoding JSON, it is possible to pass in the JSON object and the URL where

#### Parameters

| Name | Type | Description |
| :-------- | :----------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data` | Object | An object with one or many properties containing the data that needs to be encoded. |
| `schemas` | ERC725JSONSchema[] | An array of extra [LSP-2 ERC725YJSONSchema] objects that can be used to find the schema. If called on an instance, it is optional and it will be concatenated with the schema provided on instantiation. |
##### `data` - Array of `EncodeDataInput`

The key can be either the named key (i.e. `LSP3Profile`) or the hashed key (with or without `0x` prefix, i.e. `0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5` or `5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5`).
An array of objects containing the following properties:

The key also supports dynamic keys for [`Mapping`](https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-2-ERC725YJSONSchema.md#mapping) and [`MappingWithGrouping`](https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-2-ERC725YJSONSchema.md#mapping). Therefore, you can use variables in the key name such as `LSP12IssuedAssetsMap:<address>`. In that case, the value should be an object with two keys:
| Name | Type | Description |
| :--------------------------- | :--------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `keyName` | string | Can be either the named key (i.e. `LSP3Profile`, `LSP12IssuedAssetsMap:<address>`) or the hashed key (with or without `0x` prefix, i.e. `0x5ef...` or `5ef...`). |
| `value` | string or <br/> string[] <br/> JSON todo | The value that should be encoded. Can be a string, an array of string or a JSON... |
| `dynamicKeyParts` (optional) | string or <br/> string[&nbsp;] | The dynamic parts of the `keyName` that will be used for encoding the key. |

The `keyName` also supports dynamic keys for [`Mapping`](https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-2-ERC725YJSONSchema.md#mapping) and [`MappingWithGrouping`](https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-2-ERC725YJSONSchema.md#mapping). Therefore, you can use variables in the key name such as `LSP12IssuedAssetsMap:<address>`. In that case, the value should also set the `dynamicKeyParts` property:

- `dynamicKeyParts`: string or string[&nbsp;] which holds the variables that needs to be encoded.
- `value`: the value that needs to be encoded. Same as for non dynamic keys.

##### `schemas` - Array of `ERC725JSONSchema`

An array of extra [LSP-2 ERC725YJSONSchema] objects that can be used to find the schema. If called on an instance, it is optional and it will be concatenated with the schema provided on instantiation.

#### Returns

Expand All @@ -238,12 +244,15 @@ After the `data` is encoded, the object is ready to be stored in smart contracts
#### Examples

```javascript title="Encode a JSONURL with JSON and uploaded URL"
erc725.encodeData({
LSP3Profile: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
erc725.encodeData([
{
keyName: 'LSP3Profile',
value: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
});
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
Expand All @@ -252,25 +261,32 @@ erc725.encodeData({
*/

// You can also use the hashed key (with or without 0x prefix)
erc725.encodeData({
'0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5': {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
erc725.encodeData([
{
keyName:
'0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
});
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
values: ['0x6f357c6a2404a2866f05e53e141eb61382a045e53c2fc54831daca9d9e1e039a11f739e1696670733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d'],
}
*/

erc725.encodeData({
'5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5': {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
erc725.encodeData([
{
keyName: '5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
});
]);

/**
{
Expand All @@ -281,13 +297,16 @@ erc725.encodeData({
```

```javascript title="Encode a JSONURL with hash function, hash and uploaded URL"
erc725.encodeData({
LSP3Profile: {
hashFunction: 'keccak256(utf8)',
hash: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
erc725.encodeData([
{
keyName: 'LSP3Profile',
value: {
hashFunction: 'keccak256(utf8)',
hash: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
});
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
Expand All @@ -298,12 +317,13 @@ erc725.encodeData({

```javascript title="Encode dynamic keys"
erc725.encodeData(
{
'DynamicKey:<address>': {
[
{
keyName: 'DynamicKey:<address>',
dynamicKeyParts: ['0xbedbedbedbedbedbedbedbedbedbedbedbedbedb'],
value: '0xcafecafecafecafecafecafecafecafecafecafe',
},
},
],
[
{
name: 'DynamicKey:<address>',
Expand All @@ -322,12 +342,13 @@ erc725.encodeData(
*/

erc725.encodeData(
{
'DynamicKey:<bytes4>:<string>': {
[
{
keyName: 'DynamicKey:<bytes4>:<string>',
dynamicKeyParts: ['0x11223344', 'Summer'],
value: '0xcafecafecafecafecafecafecafecafecafecafe',
},
},
],
[
{
name: 'DynamicKey:<bytes4>:<string>',
Expand All @@ -347,18 +368,27 @@ erc725.encodeData(
```

```javascript title="Encode multiple keys at once"
erc725.encodeData({
LSP3Profile: {
hashFunction: 'keccak256(utf8)',
hash: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
erc725.encodeData([
{
keyName: 'LSP3Profile',
value: {
hashFunction: 'keccak256(utf8)',
hash: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
'LSP12IssuedAssets[]': [
'0xD94353D9B005B3c0A9Da169b768a31C57844e490',
'0xDaea594E385Fc724449E3118B2Db7E86dFBa1826',
],
LSP1UniversalReceiverDelegate: '0x1183790f29BE3cDfD0A102862fEA1a4a30b3AdAb',
});
{
keyName: 'LSP12IssuedAssets[]',
value: [
'0xD94353D9B005B3c0A9Da169b768a31C57844e490',
'0xDaea594E385Fc724449E3118B2Db7E86dFBa1826',
],
},
{
keyName: 'LSP1UniversalReceiverDelegate',
value: '0x1183790f29BE3cDfD0A102862fEA1a4a30b3AdAb',
},
]);
/**
{
keys: [
Expand Down
35 changes: 22 additions & 13 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,12 @@ describe('Running @erc725/erc725.js tests...', () => {

const erc725 = new ERC725([schemaElement]);

const results = erc725.encodeData({
[schemaElement.name]: data,
});
const results = erc725.encodeData([
{
keyName: schemaElement.name,
value: data,
},
]);
assert.deepStrictEqual(results, intendedResult);
},
);
Expand Down Expand Up @@ -637,18 +640,21 @@ describe('Running @erc725/erc725.js tests...', () => {
assert.deepStrictEqual(result, schemaElement.expectedResult);
});

it('Encode data value from naked class instance!', async () => {
it('Encode data value from naked class instance', async () => {
const erc725 = new ERC725([schemaElement]);
const result = erc725.encodeData({
[schemaElement.name]: schemaElement.expectedResult,
});
const result = erc725.encodeData([
{
keyName: schemaElement.name,
value: schemaElement.expectedResult,
},
]);
assert.deepStrictEqual(result, {
keys: [schemaElement.key],
values: [schemaElement.returnGraphData],
});
});

it('Decode data value from naked class instance!', async () => {
it('Decode data value from naked class instance', async () => {
const erc725 = new ERC725([schemaElement]);
const result = erc725.decodeData({
[schemaElement.name]: schemaElement.returnGraphData,
Expand Down Expand Up @@ -713,12 +719,15 @@ describe('Running @erc725/erc725.js tests...', () => {
links: [],
};

const encodedData = myERC725.encodeData({
LSP3Profile: {
json,
url: 'ifps://QmbKvCVEePiDKxuouyty9bMsWBAxZDGr2jhxd4pLGLx95D',
const encodedData = myERC725.encodeData([
{
keyName: 'LSP3Profile',
value: {
json,
url: 'ifps://QmbKvCVEePiDKxuouyty9bMsWBAxZDGr2jhxd4pLGLx95D',
},
},
});
]);

const decodedData = myERC725.decodeData({
LSP3Profile: encodedData.values[0],
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
import { encodeKeyName } from './lib/encodeKeyName';

// Types
import { URLDataWithHash, KeyValuePair, EncodeDataInput } from './types';
import { URLDataWithHash, KeyValuePair } from './types';
import { ERC725Config } from './types/Config';
import { Permissions } from './types/Method';
import {
Expand All @@ -54,7 +54,7 @@ import {
ERC725JSONSchemaValueType,
} from './types/ERC725JSONSchema';
import { getSchemaElement } from './lib/getSchemaElement';
import { DecodeDataInput } from './types/decodeData';
import { DecodeDataInput, EncodeDataInput } from './types/decodeData';

export {
ERC725JSONSchema,
Expand Down Expand Up @@ -333,7 +333,7 @@ export class ERC725 {
* When encoding JSON it is possible to pass in the JSON object and the URL where it is available publicly.
* The JSON will be hashed with `keccak256`.
*/
encodeData(data: EncodeDataInput, schemas?: ERC725JSONSchema[]) {
encodeData(data: EncodeDataInput[], schemas?: ERC725JSONSchema[]) {
return encodeData(
data,
Array.prototype.concat(this.options.schemas, schemas),
Expand All @@ -351,7 +351,7 @@ export class ERC725 {
* When encoding JSON it is possible to pass in the JSON object and the URL where it is available publicly.
* The JSON will be hashed with `keccak256`.
*/
static encodeData(data: EncodeDataInput, schemas: ERC725JSONSchema[]) {
static encodeData(data: EncodeDataInput[], schemas: ERC725JSONSchema[]) {
return encodeData(data, schemas);
}

Expand Down
Loading

0 comments on commit a2e6cdd

Please sign in to comment.