Skip to content

Commit

Permalink
refactor: improve code for Literal hex for valueContent
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Apr 25, 2024
1 parent 1db6f2f commit 3e6213a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ import {
countNumberOfBytes,
isValidUintSize,
countSignificantBits,
isValidBytesSize,
isValidByteSize,
isValueContentLiteralHex,
} from './utils';
import { ERC725JSONSchemaValueType } from '../types/ERC725JSONSchema';

Expand Down Expand Up @@ -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.`,
);
Expand Down Expand Up @@ -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.`,
);
Expand All @@ -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.`,
);
Expand Down Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ describe('utils', () => {
valueContent: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab',
valueType: 'bytes',
decodedValue: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab',
encodedValue: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab',
encodedValue: '0xc9aaae3201f40fd0ff04d9c885769d8256a456ab', // encoded hex is always lower case
},
];

Expand Down
16 changes: 11 additions & 5 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
checkAddressChecksum,
hexToBytes,
isAddress,
isHexStrict,
leftPad,
numberToHex,
padLeft,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

0 comments on commit 3e6213a

Please sign in to comment.