Skip to content

Commit

Permalink
fix: Add workaround to read (bytes4,URI), repair none vs unknown sign…
Browse files Browse the repository at this point in the history
…ature.
  • Loading branch information
richtera committed Jan 5, 2024
1 parent 330c604 commit 204409d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const METHODS: Record<Method, MethodData> = {
},
};

export const UNKNOWN_VERIFICATION_METHOD = 'unknown';
export const NONE_VERIFICATION_METHOD = '0x00000000';

export enum SUPPORTED_VERIFICATION_METHOD_STRINGS {
KECCAK256_UTF8 = 'keccak256(utf8)',
Expand Down
20 changes: 19 additions & 1 deletion src/lib/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
decodeValueContent,
} from './encoder';
import {
NONE_VERIFICATION_METHOD,
SUPPORTED_VERIFICATION_METHOD_HASHES,
SUPPORTED_VERIFICATION_METHOD_STRINGS,
} from '../constants/constants';
Expand Down Expand Up @@ -924,6 +925,20 @@ describe('encoder', () => {
encodedValue:
'0x00006f357c6a0020027547537d35728a741470df1ccf65de10b454ca0def7c5c20b257b7b8d16168687474703a2f2f746573742e636f6d2f61737365742e676c62',
},
{
valueContent: 'VerifiableURI', // Actual content is (bytes4,URI)
decodedValue: {
verification: {
method: NONE_VERIFICATION_METHOD,
data: '0x',
},
url: 'https://name.universal.page/',
},
encodedValue:
'0x6f357c6a68747470733a2f2f6e616d652e756e6976657273616c2e706167652f',
reencodedValue:
'0x000000000000000068747470733a2f2f6e616d652e756e6976657273616c2e706167652f',
},
{
valueContent: 'BitArray',
encodedValue:
Expand Down Expand Up @@ -952,7 +967,10 @@ describe('encoder', () => {

encodeValueContent(testCase.valueContent, testCase.decodedValue);

assert.deepStrictEqual(encodedValue, testCase.encodedValue);
assert.deepStrictEqual(
encodedValue,
testCase.reencodedValue || testCase.encodedValue,
);
assert.deepStrictEqual(
encodedValue !== false
? decodeValueContent(testCase.valueContent, encodedValue)
Expand Down
37 changes: 31 additions & 6 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { AssetURLEncode } from '../types/encodeData';

import {
SUPPORTED_VERIFICATION_METHOD_STRINGS,
UNKNOWN_VERIFICATION_METHOD,
NONE_VERIFICATION_METHOD,
} from '../constants/constants';
import {
getVerificationMethod,
Expand All @@ -72,7 +72,7 @@ const encodeDataSourceWithHash = (
dataSource: string,
): string => {
const verificationMethod = getVerificationMethod(
verification?.method || UNKNOWN_VERIFICATION_METHOD,
verification?.method || '0x00000000',
);
return [
'0x0000',
Expand Down Expand Up @@ -119,7 +119,11 @@ const decodeDataSourceWithHash = (value: string): URLDataWithHash => {

return {
verification: {
method: verificationMethod?.name || UNKNOWN_VERIFICATION_METHOD,
method:
verificationMethod?.name ||
(verificationMethodSignature === '0x00000000'
? NONE_VERIFICATION_METHOD
: verificationMethodSignature),
data: dataHash,
},
url: dataSource,
Expand All @@ -129,12 +133,33 @@ const decodeDataSourceWithHash = (value: string): URLDataWithHash => {
const verificationMethodSignature = value.slice(0, 10);
const verificationMethod = getVerificationMethod(verificationMethodSignature);
const encodedData = value.slice(10); // Rest of data string after function hash

try {
const dataSource = hexToUtf8('0x' + encodedData); // Get as URI
if (encodedData.length < 64 || /^(https?|ipfs):\/\//.test(dataSource)) {
console.log(dataSource);
return {
verification: {
method: NONE_VERIFICATION_METHOD,
data: '0x',
},
url: dataSource,
};
}
} catch {
// ignore
}

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,
method:
verificationMethod?.name ||
(verificationMethodSignature === '0x00000000'
? NONE_VERIFICATION_METHOD
: verificationMethodSignature),
data: dataHash,
},
url: dataSource,
Expand Down Expand Up @@ -707,7 +732,7 @@ export const valueContentEncodingMap = (
);
}

if (!hashedJson) {
if (!hashedJson && method !== NONE_VERIFICATION_METHOD) {
throw new Error(
'You have to provide either the verification.data or the json via the respective properties',
);
Expand All @@ -718,7 +743,7 @@ export const valueContentEncodingMap = (
method:
(method as SUPPORTED_VERIFICATION_METHOD_STRINGS) ||
SUPPORTED_VERIFICATION_METHOD_STRINGS.KECCAK256_UTF8,
data: hashedJson,
data: hashedJson || '',
},
url,
);
Expand Down

0 comments on commit 204409d

Please sign in to comment.