From 3e6213a3acf3b5d16b79447f87a7029f1020d7b3 Mon Sep 17 00:00:00 2001 From: CJ42 Date: Thu, 25 Apr 2024 18:02:04 +0100 Subject: [PATCH] refactor: improve code for `Literal` hex for `valueContent` --- src/lib/encoder.ts | 18 ++++++++++++------ src/lib/utils.test.ts | 2 +- src/lib/utils.ts | 16 +++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/lib/encoder.ts b/src/lib/encoder.ts index 74f30891..bed0761d 100644 --- a/src/lib/encoder.ts +++ b/src/lib/encoder.ts @@ -57,7 +57,8 @@ import { countNumberOfBytes, isValidUintSize, countSignificantBits, - isValidBytesSize, + isValidByteSize, + isValueContentLiteralHex, } from './utils'; import { ERC725JSONSchemaValueType } from '../types/ERC725JSONSchema'; @@ -602,7 +603,7 @@ const valueTypeEncodingMap = ( case `bytes${bytesLength}`: return { encode: (value: string | number) => { - if (!isValidBytesSize(bytesLength as number)) { + if (!isValidByteSize(bytesLength as number)) { throw new Error( `Can't encode ${value} as ${type}. Invalid \`bytesN\` provided. Expected a \`N\` value for bytesN between 1 and 32.`, ); @@ -822,7 +823,7 @@ export const valueContentEncodingMap = ( throw new Error(`Value: ${value} is not hex.`); } - if (bytesLength && !isValidBytesSize(bytesLength)) { + if (bytesLength && !isValidByteSize(bytesLength)) { throw new Error( `Provided bytes length: ${bytesLength} for encoding valueContent: ${valueContent} is not valid.`, ); @@ -844,7 +845,7 @@ export const valueContentEncodingMap = ( return null; } - if (bytesLength && !isValidBytesSize(bytesLength)) { + if (bytesLength && !isValidByteSize(bytesLength)) { console.error( `Provided bytes length: ${bytesLength} for encoding valueContent: ${valueContent} is not valid.`, ); @@ -981,8 +982,13 @@ export function decodeValueContent( valueContent: string, value: string, ): string | URLDataWithHash | number | boolean | null { - if (valueContent.slice(0, 2) === '0x') { - return valueContent === value ? value : null; + if (isValueContentLiteralHex(valueContent)) { + if (valueContent.toLowerCase() != value) { + throw new Error( + `Could not decode value content: the value ${value} does not match the Hex Literal ${valueContent} defined in the \`valueContent\` part of the schema`, + ); + } + return valueContent; } if (value == null || value === '0x') { diff --git a/src/lib/utils.test.ts b/src/lib/utils.test.ts index 088dfe19..3f994707 100644 --- a/src/lib/utils.test.ts +++ b/src/lib/utils.test.ts @@ -378,7 +378,7 @@ describe('utils', () => { valueContent: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab', valueType: 'bytes', decodedValue: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab', - encodedValue: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab', + encodedValue: '0xc9aaae3201f40fd0ff04d9c885769d8256a456ab', // encoded hex is always lower case }, ]; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e22545a2..e8e49baf 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -23,6 +23,7 @@ import { checkAddressChecksum, hexToBytes, isAddress, + isHexStrict, leftPad, numberToHex, padLeft, @@ -61,10 +62,6 @@ import { EncodeDataInput } from '../types/decodeData'; import { GetDataDynamicKey } from '../types/GetData'; import { isValidTuple } from './decodeData'; -function isValueContentLiteralHex(valueContent: string): boolean { - return valueContent.slice(0, 2) === '0x'; -} - /** * * @param {string} valueContent as per ERC725Schema definition @@ -823,6 +820,15 @@ export function isValidUintSize(bitSize: number) { * * @param bytesSize the size of the fixed size bytes value */ -export function isValidBytesSize(bytesSize: number) { +export function isValidByteSize(bytesSize: number) { return bytesSize >= 1 && bytesSize <= 32; } + +/** + * @dev Check if the `valueContent` in a schema is defined as an hex literal string + * @param valueContent The valueContent part of a schema + * @returns true or false + */ +export function isValueContentLiteralHex(valueContent: string): boolean { + return isHexStrict(valueContent); +}