Skip to content

Commit

Permalink
Merge pull request #458 from ERC725Alliance/revert/decode-value-conte…
Browse files Browse the repository at this point in the history
…nt-literal

Revert "refactor: improve code for `Literal` hex for `valueContent`" to return `null` when decoding
  • Loading branch information
CJ42 authored Jun 24, 2024
2 parents 2a3f80f + a443fe2 commit 8396676
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
13 changes: 5 additions & 8 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,10 @@ export function encodeValueContent(
valueContent: string,
value: string | number | AssetURLEncode | URLDataToEncode | boolean,
): string | false {
if (valueContent.slice(0, 2) === '0x') {
return valueContent === value ? value : false;
if (isValueContentLiteralHex(valueContent)) {
// hex characters are always lower case, even if the schema define some hex words uppercase
// e.g: 0xAabbcCddeE -> encoded as 0xaabbccddee
return valueContent === value ? value.toLowerCase() : false;
}

const valueContentEncodingMethods = valueContentEncodingMap(valueContent);
Expand Down Expand Up @@ -983,12 +985,7 @@ export function decodeValueContent(
value: string,
): string | URLDataWithHash | number | boolean | 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;
return valueContent.toLowerCase() === value ? valueContent : null;
}

if (value == null || value === '0x') {
Expand Down
11 changes: 5 additions & 6 deletions src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { keccak256, utf8ToHex } from 'web3-utils';
import {
ERC725JSONSchema,
ERC725JSONSchemaKeyType,
ERC725JSONSchemaValueType,
} from '../types/ERC725JSONSchema';
import { GetDataDynamicKey } from '../types/GetData';

Expand Down Expand Up @@ -226,12 +225,12 @@ describe('utils', () => {
testCases.forEach((testCase) => {
it(`encodes/decodes keyType Array / tuples (valueContent: ${testCase.schema.valueContent}, valueType: ${testCase.schema.valueType}`, () => {
assert.deepStrictEqual(
encodeKey(testCase.schema as ERC725JSONSchema, testCase.decodedValue),
encodeKey(testCase.schema, testCase.decodedValue),
testCase.encodedValue,
);

assert.deepStrictEqual(
decodeKey(testCase.schema as ERC725JSONSchema, testCase.encodedValue),
decodeKey(testCase.schema, testCase.encodedValue),
testCase.decodedValue,
);
});
Expand Down Expand Up @@ -378,7 +377,7 @@ describe('utils', () => {
valueContent: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab',
valueType: 'bytes',
decodedValue: '0xc9aaAE3201F40fd0fF04D9c885769d8256A456ab',
encodedValue: '0xc9aaae3201f40fd0ff04d9c885769d8256a456ab', // encoded hex is always lower case
encodedValue: '0xc9aaae3201f40fd0ff04d9c885769d8256a456ab',
},
];

Expand All @@ -387,7 +386,7 @@ describe('utils', () => {
assert.strictEqual(
encodeKeyValue(
testCase.valueContent,
testCase.valueType as ERC725JSONSchemaValueType,
testCase.valueType,
testCase.decodedValue,
),
testCase.encodedValue,
Expand All @@ -397,7 +396,7 @@ describe('utils', () => {
assert.deepStrictEqual(
decodeKeyValue(
testCase.valueContent,
testCase.valueType as ERC725JSONSchemaValueType,
testCase.valueType,
testCase.encodedValue,
),
testCase.decodedValue,
Expand Down

0 comments on commit 8396676

Please sign in to comment.