Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: new encoding for static value types (not arrays []) #288

Merged
merged 7 commits into from
Aug 1, 2023
Merged
17 changes: 17 additions & 0 deletions docs/classes/ERC725.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ When encoding JSON, it is possible to pass in the JSON object and the URL where

:::

:::info

When encoding some values using specific `string` or `bytesN` as `valueType`, if the data passed is a non-hex value, _erc725.js_ will convert the value
to its utf8-hex representation for you. For instance:

- If `valueType` is `string` and you provide a `number` as input.

_Example: input `42` --> will encode as `0x3432` (utf-8 hex code for `4` = `0x34`, for `2` = `0x32`)._

- If `valueType` is `bytes32` or `bytes4`, it will convert as follow:

_Example 1: input `week` encoded as `bytes4` --> will encode as `0x7765656b`._

_Example 2: input `1122334455` encoded as `bytes4` --> will encode as `0x42e576f7`._

:::

#### Parameters

##### 1. `data` - Array of Objects
Expand Down
13 changes: 8 additions & 5 deletions src/lib/decodeMappingKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import { decodeValueType } from './encoder';
import { ERC725JSONSchema } from '../types/ERC725JSONSchema';
import { DynamicKeyPart } from '../types/dynamicKeys';

function make32BytesLong(s: string): string {
return padLeft(s, 64);
}

function isDynamicKeyPart(keyPartName: string): boolean {
return (
keyPartName.slice(0, 1) === '<' &&
Expand Down Expand Up @@ -59,7 +55,14 @@ function decodeKeyPart(
? 0
: encodedKeyPart.length - bytesLength;
decodedKey = encodedKeyPart.slice(sliceFrom);
} else decodedKey = decodeValueType(type, make32BytesLong(encodedKeyPart));
} else if (type === 'address') {
// this is required if the 2nd word is an address in a MappingWithGrouping
CJ42 marked this conversation as resolved.
Show resolved Hide resolved
const leftPaddedAddress = padLeft('0x' + encodedKeyPart, 40);

decodedKey = decodeValueType(type, leftPaddedAddress);
} else {
decodedKey = decodeValueType(type, encodedKeyPart);
}

return { type, value: decodedKey };
}
Expand Down
Loading
Loading