Skip to content

Commit

Permalink
fix: Add verifiableUrl support and make it default on encode.
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Dec 5, 2023
1 parent 90f735c commit 907320f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 257 deletions.
4 changes: 2 additions & 2 deletions examples/src/decodeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const decodedDataOneKey = myERC725.decodeData([
{
keyName: 'LSP3Profile',
value:
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
'0x00006f357c6a0020820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
},
]);
/**
Expand All @@ -31,7 +31,7 @@ const decodedDataManyKeys = myERC725.decodeData([
{
keyName: 'LSP3Profile',
value:
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
'0x00006f357c6a0020820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
},
{
keyName: 'LSP3IssuedAssets[]',
Expand Down
4 changes: 2 additions & 2 deletions examples/src/encodeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const encodedDataOneKeyV2 = myERC725.encodeData({
'0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5'
],
values: [
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178'
'0x00006f357c6a0020820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178'
]
}
*/
Expand Down Expand Up @@ -76,7 +76,7 @@ const encodedDataManyKeys = myERC725.encodeData([
'0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47'
],
values: [
'0x6f357c6a820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
'0x00006f357c6a0020820464ddfac1bec070cc14a8daf04129871d458f2ca94368aae8391311af6361697066733a2f2f516d597231564a4c776572673670456f73636468564775676f3339706136727963455a4c6a7452504466573834554178',
'0x0000000000000000000000000000000000000000000000000000000000000002',
'0xd94353d9b005b3c0a9da169b768a31c57844e490',
'0xdaea594e385fc724449e3118b2db7e86dfba1826',
Expand Down
55 changes: 45 additions & 10 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,58 @@ const encodeDataSourceWithHash = (
const verificationMethod = getVerificationMethod(
verification?.method || UNKNOWN_VERIFICATION_METHOD,
);
return (
(verificationMethod
? keccak256(verificationMethod.name).slice(0, 10)
: padLeft(0, 8)) +
stripHexPrefix(verification ? verification.data : padLeft(0, 64)) +
stripHexPrefix(utf8ToHex(dataSource))
);
return [
'0x',
stripHexPrefix(
verificationMethod
? padLeft(keccak256(verificationMethod.name).slice(0, 10), 12)
: padLeft(0, 12),
),
stripHexPrefix(
verification?.data
? padLeft(verification.data.slice(2).length / 2, 4)
: padLeft(0, 4),
),
stripHexPrefix(
verification?.data ? stripHexPrefix(verification?.data) : '',
),
stripHexPrefix(utf8ToHex(dataSource)),
].join('');
};

const decodeDataSourceWithHash = (value: string): URLDataWithHash => {
if (value.slice(0, 6) === '0x0000') {
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
0x0000 code
6f357c6a hash fn [6]
0020 data len [14]
820464ddfac1be...[18 + data len]
[18 + data len]...696670733a2f2...[...rest]
*/
const verificationMethodSig = `0x${value.slice(6, 14)}`;
const verificationMethod = getVerificationMethod(verificationMethodSig);
if (verificationMethod !== undefined) {
const encodedLength = `0x${value.slice(14, 18)}`; // Rest of data string after function hash
const dataLength = hexToNumber(encodedLength, false) as number;
const dataHash = `0x${value.slice(18, 18 + dataLength * 2)}`; // Get jsonHash 32 bytes
const dataSource = hexToUtf8('0x' + value.slice(18 + dataLength * 2)); // Get remainder as URI
return {
verification: {
method: verificationMethod.name,
data: dataHash,
},
url: dataSource,
};
}
}

const verificationMethodSig = value.slice(0, 10);
const verificationMethod = getVerificationMethod(verificationMethodSig);

const encodedData = value.replace('0x', '').slice(8); // Rest of data string after function hash
const encodedData = value.slice(10); // Rest of data string after function hash
const dataHash = '0x' + encodedData.slice(0, 64); // Get jsonHash 32 bytes
const dataSource = hexToUtf8('0x' + encodedData.slice(64)); // Get remainder as URI

return {
verification: {
method: verificationMethod?.name || UNKNOWN_VERIFICATION_METHOD,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/getDataFromExternalSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export const getDataFromExternalSources = (
}

if (
!['jsonurl', 'asseturl'].includes(
!['jsonurl', 'asseturl', 'verifiableurl'].includes(
schemaElement.valueContent.toLowerCase(),
)
) {
return dataEntry;
}

// At this stage, value should be of type jsonurl or asseturl
// At this stage, value should be of type jsonurl, verifiableurl or asseturl
if (typeof dataEntry.value === 'string') {
console.error(
`Value of key: ${dataEntry.name} (${dataEntry.value}) is string but valueContent is: ${schemaElement.valueContent}. Expected type should be object with url key.`,
Expand Down
Loading

0 comments on commit 907320f

Please sign in to comment.