Skip to content

Commit

Permalink
refactor: throw error when verificationMethod is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Dec 13, 2023
1 parent 26c9988 commit e1dc33e
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,33 @@ const decodeDataSourceWithHash = (value: string): URLDataWithHash => {
*/
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,
};

if (verificationMethod === undefined) {
throw new Error(
"Couldn't decode DataSourceWithHash: `verificationMethod` is 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.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

0 comments on commit e1dc33e

Please sign in to comment.