ERC725
checkPermissions
myErc725.checkPermissions(requiredPermissions, grantedPermissions);
ERC725.checkPermissions(requiredPermissions, grantedPermissions);
Check if the required permissions are included in the granted permissions as defined by the LSP6 KeyManager Standard.
checkPermissions
is available as either a static or non-static method so can be called without instantiating an ERC725 object.
Parameters
1. requiredPermissions
- String[] | String
An array of required permissions or a single required permission. (32bytes hex or the official name of the permission).
2. grantedPermissions
- String
The granted permissions. (32bytes hex).
Returns
Type | Description |
---|---|
boolean | A boolean value indicating whether the required permissions are included in the granted permissions as defined by the LSP6 KeyManager Standard |
Permission-Name Example
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
ERC725.checkPermissions(requiredPermissions, grantedPermissions);
// true
// This method is also available on the instance:
const requiredPermissions = ['CHANGEOWNER', 'CALL'];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
myErc725.checkPermissions(requiredPermissions, grantedPermissions);
// false
32bytes hex Example
const requiredPermissions = [
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
ERC725.checkPermissions(requiredPermissions, grantedPermissions);
// false
// This method is also available on the instance:
const requiredPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000001';
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
myErc725.checkPermissions(requiredPermissions, grantedPermissions);
// true
decodeData
myErc725.decodeData(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.
If you want total convenience, it is recommended to use the fetchData
function, which automatically decodes
and fetches
external references.
Parameters
1. data
- Object or array of Objects
An object or array of objects containing the following properties:
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... ). |
dynamicKeyParts (optional) | string or string[ ] | If keyName is a dynamic key, the dynamic parts of the keyName that will be used for encoding the key. |
value | string or string[ ] | The value that should be decoded. Can be a string, an array of string or a JSON... |
The keyName
also supports dynamic keys for Mapping
and MappingWithGrouping
. 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[ ] which holds the variables that needs to be encoded.
2. schemas
- Array of Objects (optional)
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
Name | Type | Description |
---|---|---|
decodedData | Object or Array | The decoded data as defined and expected in the following schemas. |
- If the input is an array of objects, the values will be returned in an array.
- If the input is a single object, the output will be the decodedData object directly.
Single-Key Example
myErc725.decodeData([
{
keyName: 'LSP3Profile',
value:
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361696670733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
},
]);
/**
[
{
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
verification: {
method: 'keccak256(utf8)',
data: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361'
},
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
]
*/
myErc725.decodeData({
keyName: 'LSP3Profile',
value:
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361696670733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
});
/**
{
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
verification: {
method: 'keccak256(utf8)',
data: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361'
},
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
}
*/
Multi-Key Example
myErc725.decodeData([
{
keyName: 'LSP3Profile',
value:
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361696670733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
},
{
keyName: 'LSP12IssuedAssets[]',
value: [
{
key: '0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
value: '0x00000000000000000000000000000002',
},
{
key: '0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000000',
value: '0xd94353d9b005b3c0a9da169b768a31c57844e490',
},
{
key: '0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000001',
value: '0xdaea594e385fc724449e3118b2db7e86dfba1826',
},
],
},
]);
/**
[
{
name: 'LSP3Profile',
key: '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
verification: {
data: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
method: 'keccak256(utf8)'
},
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
{
name: 'LSP12IssuedAssets[]',
key: '0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
value: [
'0xD94353D9B005B3c0A9Da169b768a31C57844e490',
'0xDaea594E385Fc724449E3118B2Db7E86dFBa1826',
],
},
];
*/
Dynamic-Key Example
const schemas = [
{
name: 'MyKeyName:<bytes32>:<bool>',
key: '0x...',
keyType: 'Singleton',
valueType: 'bytes',
valueContent: 'VerifiableURI',
},
];
myErc725.decodeData(
[
{
keyName: 'MyKeyName:<bytes32>:<bool>',
dynamicKeyParts: [
'0xaaaabbbbccccddddeeeeffff111122223333444455556666777788889999aaaa',
'true',
],
value:
'0x00006f357c6a0020820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361696670733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
},
],
schemas,
);
/**
[
{
name: 'MyKeyName:aaaabbbbccccddddeeeeffff111122223333444455556666777788889999aaaa:true',
key: '0x35e6950bc8d2aaaabbbb00000000000000000000000000000000000000000001',
value: {
verification: {
method: 'keccak256(utf8)',
data: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361'
},
url: 'ifps://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
];
*/
decodePermissions
ERC725.decodePermissions(permission);
Decodes permissions from hexadecimal defined by the LSP6 KeyManager Standard.
decodePermissions
is available as either a static or non-static method so can be called without instantiating an ERC725 object.
Parameters
1. permission
- String
The encoded permission (32bytes hex).
Returns
Name | Type | Description |
---|---|---|
decodedPermissions | Object | An object specifying whether default LSP6 permissions are included in provided hexademical string. |
Example
ERC725.decodePermissions('0x0000000000000000000000000000000000000000000000000000000000000110'),
/**
{
CHANGEOWNER: false,
EDITPERMISSIONS: false,
ADDCONTROLLER: false,
SETDATA: false,
CALL: true,
STATICCALL: false,
DELEGATECALL: false,
DEPLOY: false,
TRANSFERVALUE: true,
SIGN: false,
}
*/
ERC725.decodePermissions('0x000000000000000000000000000000000000000000000000000000000000000a'),
/**
{
CHANGEOWNER: false,
EDITPERMISSIONS: true,
ADDCONTROLLER: false,
SETDATA: true,
CALL: false,
STATICCALL: false,
DELEGATECALL: false,
DEPLOY: false,
TRANSFERVALUE: false,
SIGN: false,
}
*/
// This method is also available on the instance:
myErc725.decodePermissions('0x0000000000000000000000000000000000000000000000000000000000000110'),
decodeValueType
myErc725.decodeValueType(type, data);
ERC725.decodeValueType(type, data);
Decode some data according to a provided value type.
Parameters
Name | Type | Description |
---|---|---|
type | string | The value type to decode the data (i.e. uint256 , bool , bytes4 , etc...). |
data | string | A hex encoded string starting with 0x to decode |
Returns
Name | Type | Description |
---|---|---|
decodedValue | string or number | A value decoded according to type . |
Examples
myErc725.decodeValueType('uint128', '0x0000000000000000000000000000000a');
// 10
myErc725.decodeValueType('bool', '0x01');
// true
myErc725.decodeValueType('string', '0x48656c6c6f21');
// 'Hello!';
// also available for ABI encoded array + CompactBytesArray
myErc725.decodeValueType(
'uint256[]',
'0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001e',
);
// [ 10, 20, 30 ]
myErc725.decodeValueType(
'uint256[CompactBytesArray]'',
'0x0020000000000000000000000000000000000000000000000000000000000000000500200000000000000000000000000000000000000000000000000000000000000008'
)
// [ 5, 8 ]
This method is also available as a static method:
ERC725.decodeValueType(
'uint256',
'0x000000000000000000000000000000000000000000000000000000000000002a',
);
// 42
encodeData
myErc725.encodeData(data [, schemas]);
ERC725.encodeData(data, schemas);
Encode the data of a smart contract according to your ERC725JSONSchema
so that you can store the information in smart contracts.
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
.
When encoding some values using specific string
or bytesN
as valueType
, if the data passed is a non-hex value, erc725.js will convert the value
to its utf8-hex representation for you. For instance:
- If
valueType
isstring
and you provide anumber
as input.
Example: input 42
--> will encode as 0x3432
(utf-8 hex code for 4
= 0x34
, for 2
= 0x32
).
- If
valueType
isbytes32
orbytes4
, it will convert as follow:
Example 1: input week
encoded as bytes4
--> will encode as 0x7765656b
.
Example 2: input 1122334455
encoded as bytes4
--> will encode as 0x42e576f7
.
Parameters
1. data
- Array of Objects
An array of objects containing the following properties:
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... ). |
dynamicKeyParts (optional) | string or string[ ] | The dynamic parts of the keyName that will be used for encoding the key. |
value | string or string[ ] JSON todo | The value that should be encoded. Can be a string, an array of string or a JSON... |
startingIndex (optional) | number | Starting index for Array types to encode a subset of elements. Defaults t 0 . |
totalArrayLength (optional) | number | Parameter for Array types, specifying the total length when encoding a subset of elements. Defaults to the number of elements in the value field. |
The keyName
also supports dynamic keys for Mapping
and MappingWithGrouping
. 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[ ] which holds the variables that needs to be encoded.
The totalArrayLength
parameter must be explicitly provided to ensure integrity when encoding subsets or modifying existing array elements. Its value specifies the total length of the array after the operation is completed, not just the size of the encoded subset.
When to Use totalArrayLength
- Adding Elements: When adding new elements to an array,
totalArrayLength
should equal the sum of the current array's length plus the number of new elements added. - Modifying Elements: If modifying elements within an existing array without changing the total number of elements,
totalArrayLength
should match the previous length of the array. - Removing Elements: In cases where elements are removed,
totalArrayLength
should reflect the number of elements left.
Please be careful when updating existing contract data. Incorrect usage of startingIndex
and totalArrayLength
can lead to improperly encoded data that changes the intended structure of the data field.
2. schemas
- Array of Objects (optional)
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
Name | Type | Description |
---|---|---|
encodedData | Object | An object containing the encoded keys and values according to the LSP2 ERC725Y JSON Schema of the data which was passed |
After the data
is encoded, the object is ready to be stored in smart contracts.
Examples
Encode a VerifiableURI
with JSON and uploaded URL
myErc725.encodeData([
{
keyName: 'LSP3Profile',
value: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
values: ['0x6f357c6a2404a2866f05e53e141eb61382a045e53c2fc54831daca9d9e1e039a11f739e1696670733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d'],
}
*/
// You can also use the hashed key (with or without 0x prefix)
myErc725.encodeData([
{
keyName:
'0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
values: ['0x6f357c6a2404a2866f05e53e141eb61382a045e53c2fc54831daca9d9e1e039a11f739e1696670733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d'],
}
*/
myErc725.encodeData([
{
keyName: '5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
value: {
json: profileJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
values: ['0x6f357c6a2404a2866f05e53e141eb61382a045e53c2fc54831daca9d9e1e039a11f739e1696670733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d'],
}
*/
myErc725.encodeData([
{
keyName: 'LSP1UniversalReceiverDelegate',
value: '0x1183790f29BE3cDfD0A102862fEA1a4a30b3AdAb',
},
]);
/**
{
keys: ['0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47'],
values: ['0x1183790f29be3cdfd0a102862fea1a4a30b3adab'],
}
*/
Encode a VerifiableURI
with hash function, hash and uploaded URL
myErc725.encodeData([
{
keyName: 'LSP3Profile',
value: {
verification: {
method: 'keccak256(utf8)',
data: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
},
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
]);
/**
{
keys: ['0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'],
values: ['0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361696670733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178'],
}
*/
Encode dynamic keys
const schemas = [
{
name: 'DynamicKey:<address>',
key: '0x0fb367364e1852abc5f20000<address>',
keyType: 'Mapping',
valueType: 'bytes',
valueContent: 'Address',
},
];
myErc725.encodeData(
[
{
keyName: 'DynamicKey:<address>',
dynamicKeyParts: ['0xbedbedbedbedbedbedbedbedbedbedbedbedbedb'],
value: '0xcafecafecafecafecafecafecafecafecafecafe',
},
],
schemas,
);
/**
{
keys: ['0x0fb367364e1852abc5f20000bedbedbedbedbedbedbedbedbedbedbedbedbedb'],
values: ['0xcafecafecafecafecafecafecafecafecafecafe]
}
*/
const schemas = [
{
name: 'DynamicKey:<bytes4>:<string>',
key: '0xForDynamicKeysThisFieldIsIrrelevantAndWillBeOverwriten',
keyType: 'Mapping',
valueType: 'bytes',
valueContent: 'Address',
},
];
myErc725.encodeData(
[
{
keyName: 'DynamicKey:<bytes4>:<string>',
dynamicKeyParts: ['0x11223344', 'Summer'],
value: '0xcafecafecafecafecafecafecafecafecafecafe',
},
],
schemas,
);
/**
{
keys: ['0x0fb367364e1852abc5f2000078c964cd805233eb39f2db152340079088809725'],
values: ['0xcafecafecafecafecafecafecafecafecafecafe']
}
*/
Encode multiple keys at once
myErc725.encodeData([
{
keyName: 'LSP3Profile',
value: {
verification: {
method: 'keccak256(utf8)',
data: '0x820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361',
},
url: 'ipfs://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx',
},
},
{
keyName: 'LSP12IssuedAssets[]',
value: [
'0xD94353D9B005B3c0A9Da169b768a31C57844e490',
'0xDaea594E385Fc724449E3118B2Db7E86dFBa1826',
],
},
{
keyName: 'LSP1UniversalReceiverDelegate',
value: '0x1183790f29BE3cDfD0A102862fEA1a4a30b3AdAb',
},
]);
/**
{
keys: [
'0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5',
'0x7c8c3416d6cda87cd42c71ea1843df28ac4850354f988d55ee2eaa47b6dc05cd',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000000',
'0x7c8c3416d6cda87cd42c71ea1843df2800000000000000000000000000000001',
'0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
],
values: [
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361696670733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
'0x00000000000000000000000000000002',
'0xd94353d9b005b3c0a9da169b768a31c57844e490',
'0xdaea594e385fc724449e3118b2db7e86dfba1826',
'0x1183790f29be3cdfd0a102862fea1a4a30b3adab',
],
}
*/
Encode array length
If the key is of type Array and you pass an integer as a value (for instance, the array length), it will be encoded accordingly.
myErc725.encodeData([
{
keyName: 'LSP3IssuedAssets[]',
value: 5,
},
]);
/**
{
keys: [
'0x3a47ab5bd3a594c3a8995f8fa58d0876c96819ca4516bd76100c92462f2f9dc0',
],
values: ['0x00000000000000000000000000000005'],
}
*/
Encode a subset of array elements
const schemas = [
{
name: 'AddressPermissions[]',
key: '0xdf30dba06db6a30e65354d9a64c609861f089545ca58c6b4dbe31a5f338cb0e3',
keyType: 'Array',
valueType: 'address',
valueContent: 'Address',
},
];
myErc725.encodeData(
[
{
keyName: 'AddressPermissions[]',
value: [
'0x983abc616f2442bab7a917e6bb8660df8b01f3bf',
'0x56ecbc104136d00eb37aa0dce60e075f10292d81',
],
totalArrayLength: 23,
startingIndex: 21,
},
],
schemas,
);
/**
{
keys: [
'0xdf30dba06db6a30e65354d9a64c609861f089545ca58c6b4dbe31a5f338cb0e3',
'0xdf30dba06db6a30e65354d9a64c6098600000000000000000000000000000015',
'0xdf30dba06db6a30e65354d9a64c6098600000000000000000000000000000016',
],
values: [
'0x00000000000000000000000000000017',
'0x983abc616f2442bab7a917e6bb8660df8b01f3bf',
'0x56ecbc104136d00eb37aa0dce60e075f10292d81',
],
}
*/
encodePermissions
ERC725.encodePermissions(permissions);
Encodes permissions into a hexadecimal string as defined by the LSP6 KeyManager Standard.
encodePermissions
is available as either a static or non-static method so can be called without instantiating an ERC725 object.
Parameters
1. permissions
- Object
An object with LSP6 KeyManager Permissions as keys and a boolean
as value. Any ommited permissions will default to false
.
Returns
Type | Description |
---|---|
string | The permissions encoded as a hexadecimal string defined by the LSP6 KeyManager Standard |
Example
ERC725.encodePermissions({
CHANGEOWNER: false,
ADDCONTROLLER: false,
EDITPERMISSIONS: false,
ADDEXTENSIONS: false,
CHANGEEXTENSIONS: true,
ADDUNIVERSALRECEIVERDELEGATE: false,
CHANGEUNIVERSALRECEIVERDELEGATE: false,
REENTRANCY: false,
SUPER_TRANSFERVALUE: true,
TRANSFERVALUE: true,
SUPER_CALL: false,
CALL: true,
SUPER_STATICCALL: false,
STATICCALL: false,
SUPER_DELEGATECALL: false,
DELEGATECALL: false,
DEPLOY: false,
SUPER_SETDATA: false,
SETDATA: false,
ENCRYPT: false,
DECRYPT: false,
SIGN: false,
EXECUTE_RELAY_CALL: false,
ERC4337_PERMISSION: false
}),
// '0x0000000000000000000000000000000000000000000000000000000000000110'
// Any ommited Permissions will default to false
ERC725.encodePermissions({
ADDCONTROLLER: true,
ADDEXTENSIONS: true,
}),
// '0x000000000000000000000000000000000000000000000000000000000000000a'
ERC725.encodePermissions({
EDITPERMISSIONS: true,
CHANGEEXTENSIONS: true,
CHANGEUNIVERSALRECEIVERDELEGATE: true,
SETDATA: true,
}),
// '0x0000000000000000000000000000000000000000000000000000000000040054'
// This method is also available on the instance:
myErc725.encodePermissions({
EDITPERMISSIONS: true,
SETDATA: true,
}),
encodeValueType
myErc725.encodeValueType(type, value);
ERC725.encodeValueType(type, value);
Parameters
Name | Type | Description |
---|---|---|
type | string | The value type to encode the value (i.e. uint256 , bool , bytes4 , etc...). |
value | string or string[ ] or number or number[ ] or boolean or boolean[] | The value that should be encoded as type |
Returns
Name | Type | Description |
---|---|---|
encodedValueType | string | A hex string representing the value encoded as type . |
After the value
is encoded, the hex string can be used to be stored inside the ERC725Y smart contract.
Examples
myErc725.encodeValueType('uint256', 5);
// '0x0000000000000000000000000000000000000000000000000000000000000005'
myErc725.encodeValueType('bool', true);
// '0x01'
// the word `boolean` (Name of the Typescript type) is also available
myErc725.encodeValueType('boolean', true);
// '0x01'
// `bytesN` type will pad on the right if the value contains less than N bytes
myErc725.encodeValueType('bytes4', '0xcafe');
// '0xcafe0000'
myErc725.encodeValueType('bytes32', '0xcafe');
// '0xcafe000000000000000000000000000000000000000000000000000000000000'
// `bytesN` type will throw an error if the value contains more than N bytes
myERC725.encodeValueType('bytes4', '0xcafecafebeef');
// Error: Can't convert 0xcafecafebeef to bytes4. Too many bytes, expected at most 4 bytes, received 6.
// Can also be used to encode arrays as `CompactBytesArray`
myERC725.encodeValueType('uint256[CompactBytesArray]', [1, 2, 3]);
// '0x002000000000000000000000000000000000000000000000000000000000000000010020000000000000000000000000000000000000000000000000000000000000000200200000000000000000000000000000000000000000000000000000000000000003'
myERC725.encodeValueType('bytes[CompactBytesArray]', [
'0xaaaaaaaa',
'0xbbbbbbbbbbbbbbbbbb',
]);
// '0x0004aaaaaaaa0009bbbbbbbbbbbbbbbbbb'
This method is also available as a static method.
ERC725.encodeValueType('string', 'Hello');
// '0x48656c6c6f'
encodeKeyName
ERC725.encodeKeyName(keyName [, dynamicKeyParts]);
Hashes a key name for use on an ERC725Y contract according to the LSP2 ERC725Y JSON Schema Standard.
encodeKeyName
is available as either a static or non-static method so can be called without instantiating an ERC725 object.
Parameters
1. keyName
- String
The key name you want to encode, for instance: LSP3Profile
.
2. dynamicKeyParts
- String or array of Strings (optional)
The variables used to encode the key name, if the key name is a dynamic (i.e.: MyKey:<address>
...)
Returns
Name | Type | Description |
---|---|---|
encodedKeyName | string | The keccak256 hash of the provided key name. |
The hash must be retrievable from the ERC725Y contract via the getData function.
Example
ERC725.encodeKeyName('LSP3Profile');
// '0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'
ERC725.encodeKeyName('SupportedStandards:LSP3Profile');
// '0xeafec4d89fa9619884b600005ef83ad9559033e6e941db7d7c495acdce616347'
ERC725.encodeKeyName(
'AddressPermissions:Permissions:cafecafecafecafecafecafecafecafecafecafe',
);
// '0x4b80742de2bf82acb3630000cafecafecafecafecafecafecafecafecafecafe'
ERC725.encodeKeyName('MyKeyName:<bool>', 'true');
// '0x35e6950bc8d21a1699e500000000000000000000000000000000000000000001'
ERC725.encodeKeyName('MyKeyName:<bytes2>:<uint32>', ['ffff', '4081242941']);
// 0x35e6950bc8d20000ffff000000000000000000000000000000000000f342d33d
ERC725.encodeKeyName('MyKeyName:<uint32>', ['4081242941']);
// 0x35e6950bc8d21a1699e5000000000000000000000000000000000000f342d33d
// This method is also available on the instance:
myErc725.encodeKeyName('LSP3Profile');
decodeMappingKey
ERC725.decodeMappingKey(keyNameOrSchema, keyHash);
Decode the values of the dynamic parts of a hashed key used on an ERC725Y contract according to the LSP2 ERC725Y JSON Schema Standard.
decodeMappingKey
is available as either a static or non-static method so can be called without instantiating an ERC725 object.
Parameters
1. keyHash
- String
The bytes32 key hash that needs to be decoded
2. keyNameOrSchema
- String or ERC725JSONSchema
The key name or erc725y json schema associated to the key hash to decode, for instance: MySuperKey:<address>
.
Returns
Name | Type | Description |
---|---|---|
dynamicKeyParts | {type: string, value: string OR number OR boolean}[] | The dynamic key parts decoded from the key hash. |
Example
ERC725.decodeMappingKey(
'0x35e6950bc8d21a1699e50000cafecafecafecafecafecafecafecafecafecafe',
'MyKeyName:<address>',
);
// Decoding will checksum addresses
// [{
// type: 'address',
// value: '0xCAfEcAfeCAfECaFeCaFecaFecaFECafECafeCaFe'
// }]
ERC725.decodeMappingKey(
'0x35e6950bc8d21a1699e5000000000000000000000000000000000000f342d33d',
'MyKeyName:<uint32>',
);
// [{ type: 'uint32', value: 4081242941 }]
ERC725.decodeMappingKey(
'0x35e6950bc8d21a1699e5000000000000000000000000000000000000abcd1234',
'MyKeyName:<bytes4>',
);
// [{ type: 'bytes4', value: 'abcd1234' }]
ERC725.decodeMappingKey(
'0x35e6950bc8d21a1699e50000aaaabbbbccccddddeeeeffff1111222233334444',
'MyKeyName:<bytes32>',
);
// [{
// type: 'bytes32',
// value: 'aaaabbbbccccddddeeeeffff1111222233334444'
// }]
ERC725.decodeMappingKey(
'0x35e6950bc8d21a1699e500000000000000000000000000000000000000000001',
'MyKeyName:<bool>',
);
// [{ type: 'bool', value: true }]
ERC725.decodeMappingKey(
'0x35e6950bc8d20000ffff000000000000000000000000000000000000f342d33d',
'MyKeyName:<bytes2>:<uint32>',
);
// [
// { type: 'bytes2', value: 'ffff' },
// { type: 'uint32', value: 4081242941 }
// ]
// This method is also available on the instance:
myErc725.decodeMappingKey(
'0x35e6950bc8d20000ffff000000000000000000000000000000000000f342d33d',
'MyKeyName:<bytes2>:<uint32>',
);
fetchData
myErc725.fetchData([keys]);
The fetchData
function fetches smart contract data and can additionally return VerifiableURI
data from IPFS, HTTP, or HTTPS endpoints.
To ensure data authenticity fetchData
compares the hash
of the fetched JSON with the hash
stored on the blockchain.
If you get the ReferenceError: fetch is not defined
error, you need to install and import isomorphic-fetch
.
Parameters
1. keys
- String, Object or array of Strings or/and Objects (optional)
The name(s) (or the encoded name(s) as schema key) of the element(s) in the smart contract's schema. If no keys are set, it will fetch all the non dynamic schema keys given at instantiation. For dynamic keys, you can use the object below:
Name | Type | Description |
---|---|---|
keyName | string | The dynamic key name, such as MyKey:<address> |
dynamicKeyParts | string or string[ ] | The dynamic parts of the keyName that will be used for encoding the key. |