Skip to content

Commit

Permalink
feat(wip): add support for more value types
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Nov 9, 2023
1 parent 380513a commit 63cce57
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 43 deletions.
19 changes: 7 additions & 12 deletions src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -73,25 +77,16 @@ 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}`,
);
}

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}`,
);
}

Expand Down
16 changes: 6 additions & 10 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -726,25 +727,20 @@ 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;
}

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) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import { isValidTuple } from './decodeData';
*/
export function encodeKeyValue(
valueContent: string,
valueType: ERC725JSONSchemaValueType,
valueType: ERC725JSONSchemaValueType | string,
decodedValue:
| string
| string[]
Expand Down Expand Up @@ -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,
) {
Expand Down
145 changes: 126 additions & 19 deletions src/types/ERC725JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
}

0 comments on commit 63cce57

Please sign in to comment.