diff --git a/src/lib/decodeData.ts b/src/lib/decodeData.ts index a12e4c26..64e5f097 100644 --- a/src/lib/decodeData.ts +++ b/src/lib/decodeData.ts @@ -24,7 +24,11 @@ import { isHexStrict } from 'web3-utils'; import { COMPACT_BYTES_ARRAY_STRING } from '../constants/constants'; import { DecodeDataInput, DecodeDataOutput } from '../types/decodeData'; -import { ERC725JSONSchema } from '../types/ERC725JSONSchema'; +import { + ALL_VALUE_TYPES, + ERC725JSONSchema, + isValidValueType, +} from '../types/ERC725JSONSchema'; import { isDynamicKeyName } from './encodeKeyName'; import { valueContentEncodingMap, decodeValueType } from './encoder'; import { getSchemaElement } from './getSchemaElement'; @@ -73,15 +77,6 @@ export const isValidTuple = (valueType: string, valueContent: string) => { .substring(1, valueContent.length - 1) .split(','); - const tuplesValidValueTypes = [ - 'bytes2', - 'bytes4', - 'bytes8', - 'bytes16', - 'bytes32', - 'address', - ]; - if (valueTypeParts.length !== valueContentParts.length) { throw new Error( `Invalid tuple for valueType: ${valueType} / valueContent: ${valueContent}. They should have the same number of elements. Got: ${valueTypeParts.length} and ${valueContentParts.length}`, @@ -89,9 +84,9 @@ export const isValidTuple = (valueType: string, valueContent: string) => { } for (let i = 0; i < valueTypeParts.length; i++) { - if (!tuplesValidValueTypes.includes(valueTypeParts[i])) { + if (!isValidValueType(valueTypeParts[i])) { throw new Error( - `Invalid tuple for valueType: ${valueType} / valueContent: ${valueContent}. Type: ${valueTypeParts[i]} is not valid. Valid types are: ${tuplesValidValueTypes}`, + `Invalid tuple for valueType: ${valueType} / valueContent: ${valueContent}. Type: ${valueTypeParts[i]} is not valid. Valid types are: ${ALL_VALUE_TYPES}`, ); } diff --git a/src/lib/encoder.ts b/src/lib/encoder.ts index 6ff989b3..c941c355 100644 --- a/src/lib/encoder.ts +++ b/src/lib/encoder.ts @@ -52,6 +52,7 @@ import { UNKNOWN_VERIFICATION_METHOD, } from '../constants/constants'; import { getVerificationMethod, hashData, countNumberOfBytes } from './utils'; +import { ERC725JSONSchemaValueType } from '../types/ERC725JSONSchema'; const abiCoder = AbiCoder; @@ -726,13 +727,9 @@ export const valueContentEncodingMap = (valueContent: string) => { }; export function encodeValueType( - type: string, + type: ERC725JSONSchemaValueType | string, // for tuples and CompactBytesArray, value: string | string[] | number | number[] | boolean | boolean[], ): string { - if (!valueTypeEncodingMap[type]) { - throw new Error('Could not encode valueType: "' + type + '".'); - } - if (typeof value === 'undefined' || value === null) { return value; } @@ -740,11 +737,10 @@ export function encodeValueType( return valueTypeEncodingMap[type].encode(value); } -export function decodeValueType(type: string, value: string) { - if (!valueTypeEncodingMap[type]) { - throw new Error('Could not decode valueType: "' + type + '".'); - } - +export function decodeValueType( + type: ERC725JSONSchemaValueType | string, // for tuples and CompactBytesArray + value: string, +) { if (value === '0x') return null; if (typeof value === 'undefined' || value === null) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index a29a9a72..4122666c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -71,7 +71,7 @@ import { isValidTuple } from './decodeData'; */ export function encodeKeyValue( valueContent: string, - valueType: ERC725JSONSchemaValueType, + valueType: ERC725JSONSchemaValueType | string, decodedValue: | string | string[] @@ -360,7 +360,7 @@ export function encodeKey( */ export function decodeKeyValue( valueContent: string, - valueType: ERC725JSONSchemaValueType, + valueType: ERC725JSONSchemaValueType | string, // string for tuples and CompactBytesArray value, name?: string, ) { diff --git a/src/types/ERC725JSONSchema.ts b/src/types/ERC725JSONSchema.ts index 817c2e1e..e1b6b661 100644 --- a/src/types/ERC725JSONSchema.ts +++ b/src/types/ERC725JSONSchema.ts @@ -18,24 +18,131 @@ export type ERC725JSONSchemaValueContent = | 'Boolean' | string; // for tuples -export type ERC725JSONSchemaValueType = - | 'bool' - | 'boolean' - | 'string' - | 'address' - | 'uint256' - | 'bytes32' - | 'bytes' - | 'bytes4' - | 'string[]' - | 'address[]' - | 'uint256[]' - | 'bytes32[]' - | 'bytes4[]' - | 'bytes[]' - | 'bool[]' - | 'boolean[]' - | string; // for tuples; +export const ALL_VALUE_TYPES = [ + // unsigned integers + 'uint8', + 'uint16', + 'uint24', + 'uint32', + 'uint40', + 'uint48', + 'uint56', + 'uint64', + 'uint72', + 'uint80', + 'uint88', + 'uint96', + 'uint104', + 'uint112', + 'uint120', + 'uint128', + 'uint136', + 'uint144', + 'uint152', + 'uint160', + 'uint168', + 'uint176', + 'uint184', + 'uint192', + 'uint200', + 'uint208', + 'uint216', + 'uint224', + 'uint232', + 'uint240', + 'uint248', + 'uint256', + // signed integers + 'int8', + 'int16', + 'int24', + 'int32', + 'int40', + 'int48', + 'int56', + 'int64', + 'int72', + 'int80', + 'int88', + 'int96', + 'int104', + 'int112', + 'int120', + 'int128', + 'int136', + 'int144', + 'int152', + 'int160', + 'int168', + 'int176', + 'int184', + 'int192', + 'int200', + 'int208', + 'int216', + 'int224', + 'int232', + 'int240', + 'int248', + 'int256', + // bytesN + 'bytes1', + 'bytes2', + 'bytes3', + 'bytes4', + 'bytes5', + 'bytes6', + 'bytes7', + 'bytes8', + 'bytes9', + 'bytes10', + 'bytes11', + 'bytes12', + 'bytes13', + 'bytes14', + 'bytes15', + 'bytes16', + 'bytes17', + 'bytes18', + 'bytes19', + 'bytes20', + 'bytes21', + 'bytes22', + 'bytes23', + 'bytes24', + 'bytes25', + 'bytes26', + 'bytes27', + 'bytes28', + 'bytes29', + 'bytes30', + 'bytes31', + 'bytes32', + // others static types + 'bool', + 'boolean', + 'address', + // array and dynamic types + 'string', + 'bytes', + // arrays + 'string[]', + 'address[]', + 'uint256[]', + 'bytes32[]', + 'bytes4[]', + 'bytes[]', + 'bool[]', + 'boolean[]', +] as const; + +export type ERC725JSONSchemaValueType = (typeof ALL_VALUE_TYPES)[number]; + +export function isValidValueType( + value: string, +): value is ERC725JSONSchemaValueType { + return ALL_VALUE_TYPES.includes(value as ERC725JSONSchemaValueType); +} /** * ```javascript title=Example @@ -54,5 +161,5 @@ export interface ERC725JSONSchema { key: string; // The keccak256 hash of the name. This is the actual key that MUST be retrievable via ERC725Y.getData(bytes32 key) keyType: ERC725JSONSchemaKeyType; // Types that determine how the values should be interpreted. valueContent: ERC725JSONSchemaValueContent | string; // string holds '0x1345ABCD...' If the value content are specific bytes, than the returned value is expected to equal those bytes. - valueType: ERC725JSONSchemaValueType; + valueType: ERC725JSONSchemaValueType | string; // The type of the value. This is used to determine how the value should be encoded / decode (`string` for tuples and CompactBytesArray). }