aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_encoder/evm_data_types
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-12-04 06:48:15 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-12-04 06:48:15 +0800
commit7086bd32ee23f0931be26194eb28af8178f858eb (patch)
tree53764373a8228b00d88939fdecc6f6a1ae633c9e /packages/utils/src/abi_encoder/evm_data_types
parent50bfbda79a312651581f03614c1b4f4cbbe49cf1 (diff)
parent50df67e7511460f051f91785bb4384485077ef60 (diff)
downloaddexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.gz
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.bz2
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.lz
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.xz
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.zst
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.zip
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/website/instant-configurator
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_types')
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/address.ts49
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/array.ts64
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/bool.ts53
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts72
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/int.ts59
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/method.ts72
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/pointer.ts17
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts78
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/string.ts59
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/tuple.ts24
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/uint.ts58
11 files changed, 605 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts
new file mode 100644
index 000000000..88846b1fa
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts
@@ -0,0 +1,49 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+
+export class AddressDataType extends AbstractBlobDataType {
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true;
+ private static readonly _ADDRESS_SIZE_IN_BYTES = 20;
+ private static readonly _DECODED_ADDRESS_OFFSET_IN_BYTES = constants.EVM_WORD_WIDTH_IN_BYTES -
+ AddressDataType._ADDRESS_SIZE_IN_BYTES;
+
+ public static matchType(type: string): boolean {
+ return type === SolidityTypes.Address;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, AddressDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!AddressDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate Address with bad input: ${dataItem}`);
+ }
+ }
+
+ // Disable prefer-function-over-method for inherited abstract methods.
+ /* tslint:disable prefer-function-over-method */
+ public encodeValue(value: string): Buffer {
+ if (!ethUtil.isValidAddress(value)) {
+ throw new Error(`Invalid address: '${value}'`);
+ }
+ const valueBuf = ethUtil.toBuffer(value);
+ const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES);
+ return encodedValueBuf;
+ }
+
+ public decodeValue(calldata: RawCalldata): string {
+ const valueBufPadded = calldata.popWord();
+ const valueBuf = valueBufPadded.slice(AddressDataType._DECODED_ADDRESS_OFFSET_IN_BYTES);
+ const value = ethUtil.bufferToHex(valueBuf);
+ return value;
+ }
+
+ public getSignature(): string {
+ return SolidityTypes.Address;
+ }
+ /* tslint:enable prefer-function-over-method */
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts
new file mode 100644
index 000000000..7595cb667
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts
@@ -0,0 +1,64 @@
+import { DataItem } from 'ethereum-types';
+import * as _ from 'lodash';
+
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractSetDataType } from '../abstract_data_types/types/set';
+import { constants } from '../utils/constants';
+
+export class ArrayDataType extends AbstractSetDataType {
+ private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$');
+ private readonly _arraySignature: string;
+ private readonly _elementType: string;
+
+ public static matchType(type: string): boolean {
+ return ArrayDataType._MATCHER.test(type);
+ }
+
+ private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined | number] {
+ const matches = ArrayDataType._MATCHER.exec(type);
+ if (_.isNull(matches) || matches.length !== 3) {
+ throw new Error(`Could not parse array: ${type}`);
+ } else if (_.isUndefined(matches[1])) {
+ throw new Error(`Could not parse array type: ${type}`);
+ } else if (_.isUndefined(matches[2])) {
+ throw new Error(`Could not parse array length: ${type}`);
+ }
+ const arrayElementType = matches[1];
+ const arrayLength = _.isEmpty(matches[2]) ? undefined : parseInt(matches[2], constants.DEC_BASE);
+ return [arrayElementType, arrayLength];
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ // Construct parent
+ const isArray = true;
+ const [arrayElementType, arrayLength] = ArrayDataType._decodeElementTypeAndLengthFromType(dataItem.type);
+ super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType);
+ // Set array properties
+ this._elementType = arrayElementType;
+ this._arraySignature = this._computeSignature();
+ }
+
+ public getSignature(): string {
+ return this._arraySignature;
+ }
+
+ private _computeSignature(): string {
+ // Compute signature for a single array element
+ const elementDataItem: DataItem = {
+ type: this._elementType,
+ name: 'N/A',
+ };
+ const elementComponents = this.getDataItem().components;
+ if (!_.isUndefined(elementComponents)) {
+ elementDataItem.components = elementComponents;
+ }
+ const elementDataType = this.getFactory().create(elementDataItem);
+ const elementSignature = elementDataType.getSignature();
+ // Construct signature for array of type `element`
+ if (_.isUndefined(this._arrayLength)) {
+ return `${elementSignature}[]`;
+ } else {
+ return `${elementSignature}[${this._arrayLength}]`;
+ }
+ }
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts
new file mode 100644
index 000000000..d713d5a94
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts
@@ -0,0 +1,53 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+import { BigNumber } from '../../configured_bignumber';
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+
+export class BoolDataType extends AbstractBlobDataType {
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true;
+
+ public static matchType(type: string): boolean {
+ return type === SolidityTypes.Bool;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, BoolDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!BoolDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate Bool with bad input: ${dataItem}`);
+ }
+ }
+
+ // Disable prefer-function-over-method for inherited abstract methods.
+ /* tslint:disable prefer-function-over-method */
+ public encodeValue(value: boolean): Buffer {
+ const encodedValue = value ? '0x1' : '0x0';
+ const encodedValueBuf = ethUtil.setLengthLeft(
+ ethUtil.toBuffer(encodedValue),
+ constants.EVM_WORD_WIDTH_IN_BYTES,
+ );
+ return encodedValueBuf;
+ }
+
+ public decodeValue(calldata: RawCalldata): boolean {
+ const valueBuf = calldata.popWord();
+ const valueHex = ethUtil.bufferToHex(valueBuf);
+ const valueNumber = new BigNumber(valueHex, constants.HEX_BASE);
+ if (!(valueNumber.equals(0) || valueNumber.equals(1))) {
+ throw new Error(`Failed to decode boolean. Expected 0x0 or 0x1, got ${valueHex}`);
+ }
+ /* tslint:disable boolean-naming */
+ const value: boolean = !valueNumber.equals(0);
+ /* tslint:enable boolean-naming */
+ return value;
+ }
+
+ public getSignature(): string {
+ return SolidityTypes.Bool;
+ }
+ /* tslint:enable prefer-function-over-method */
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts
new file mode 100644
index 000000000..5277efd6c
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts
@@ -0,0 +1,72 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+
+export class DynamicBytesDataType extends AbstractBlobDataType {
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false;
+
+ public static matchType(type: string): boolean {
+ return type === SolidityTypes.Bytes;
+ }
+
+ private static _sanityCheckValue(value: string | Buffer): void {
+ if (typeof value !== 'string') {
+ return;
+ }
+ if (!_.startsWith(value, '0x')) {
+ throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix.`);
+ } else if (value.length % 2 !== 0) {
+ throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`);
+ }
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, DynamicBytesDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!DynamicBytesDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate Dynamic Bytes with bad input: ${dataItem}`);
+ }
+ }
+
+ // Disable prefer-function-over-method for inherited abstract methods.
+ /* tslint:disable prefer-function-over-method */
+ public encodeValue(value: string | Buffer): Buffer {
+ // Encoded value is of the form: <length><value>, with each field padded to be word-aligned.
+ // 1/3 Construct the length
+ const valueBuf = ethUtil.toBuffer(value);
+ const wordsToStoreValuePadded = Math.ceil(valueBuf.byteLength / constants.EVM_WORD_WIDTH_IN_BYTES);
+ const bytesToStoreValuePadded = wordsToStoreValuePadded * constants.EVM_WORD_WIDTH_IN_BYTES;
+ const lengthBuf = ethUtil.toBuffer(valueBuf.byteLength);
+ const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, constants.EVM_WORD_WIDTH_IN_BYTES);
+ // 2/3 Construct the value
+ DynamicBytesDataType._sanityCheckValue(value);
+ const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded);
+ // 3/3 Combine length and value
+ const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]);
+ return encodedValue;
+ }
+
+ public decodeValue(calldata: RawCalldata): string {
+ // Encoded value is of the form: <length><value>, with each field padded to be word-aligned.
+ // 1/2 Decode length
+ const lengthBuf = calldata.popWord();
+ const lengthHex = ethUtil.bufferToHex(lengthBuf);
+ const length = parseInt(lengthHex, constants.HEX_BASE);
+ // 2/2 Decode value
+ const wordsToStoreValuePadded = Math.ceil(length / constants.EVM_WORD_WIDTH_IN_BYTES);
+ const valueBufPadded = calldata.popWords(wordsToStoreValuePadded);
+ const valueBuf = valueBufPadded.slice(0, length);
+ const value = ethUtil.bufferToHex(valueBuf);
+ DynamicBytesDataType._sanityCheckValue(value);
+ return value;
+ }
+
+ public getSignature(): string {
+ return SolidityTypes.Bytes;
+ }
+ /* tslint:enable prefer-function-over-method */
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts
new file mode 100644
index 000000000..f1dcf5ea1
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts
@@ -0,0 +1,59 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as _ from 'lodash';
+
+import { BigNumber } from '../../configured_bignumber';
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+import * as EncoderMath from '../utils/math';
+
+export class IntDataType extends AbstractBlobDataType {
+ private static readonly _MATCHER = RegExp(
+ '^int(8|16|24|32|40|48|56|64|72|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256){0,1}$',
+ );
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true;
+ private static readonly _MAX_WIDTH: number = 256;
+ private static readonly _DEFAULT_WIDTH: number = IntDataType._MAX_WIDTH;
+ private readonly _width: number;
+ private readonly _minValue: BigNumber;
+ private readonly _maxValue: BigNumber;
+
+ public static matchType(type: string): boolean {
+ return IntDataType._MATCHER.test(type);
+ }
+
+ private static _decodeWidthFromType(type: string): number {
+ const matches = IntDataType._MATCHER.exec(type);
+ const width =
+ !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1])
+ ? parseInt(matches[1], constants.DEC_BASE)
+ : IntDataType._DEFAULT_WIDTH;
+ return width;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, IntDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!IntDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate Int with bad input: ${dataItem}`);
+ }
+ this._width = IntDataType._decodeWidthFromType(dataItem.type);
+ this._minValue = new BigNumber(2).toPower(this._width - 1).times(-1);
+ this._maxValue = new BigNumber(2).toPower(this._width - 1).sub(1);
+ }
+
+ public encodeValue(value: BigNumber | string | number): Buffer {
+ const encodedValue = EncoderMath.safeEncodeNumericValue(value, this._minValue, this._maxValue);
+ return encodedValue;
+ }
+
+ public decodeValue(calldata: RawCalldata): BigNumber {
+ const valueBuf = calldata.popWord();
+ const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue);
+ return value;
+ }
+
+ public getSignature(): string {
+ return `${SolidityTypes.Int}${this._width}`;
+ }
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts
new file mode 100644
index 000000000..b1cd1377f
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts
@@ -0,0 +1,72 @@
+import { DataItem, MethodAbi } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+import { DataType } from '../abstract_data_types/data_type';
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractSetDataType } from '../abstract_data_types/types/set';
+import { constants } from '../utils/constants';
+import { DecodingRules, EncodingRules } from '../utils/rules';
+
+import { TupleDataType } from './tuple';
+
+export class MethodDataType extends AbstractSetDataType {
+ private readonly _methodSignature: string;
+ private readonly _methodSelector: string;
+ private readonly _returnDataType: DataType;
+
+ public constructor(abi: MethodAbi, dataTypeFactory: DataTypeFactory) {
+ const methodDataItem = { type: 'method', name: abi.name, components: abi.inputs };
+ super(methodDataItem, dataTypeFactory);
+ this._methodSignature = this._computeSignature();
+ this._methodSelector = this._computeSelector();
+ const returnDataItem: DataItem = { type: 'tuple', name: abi.name, components: abi.outputs };
+ this._returnDataType = new TupleDataType(returnDataItem, this.getFactory());
+ }
+
+ public encode(value: any, rules?: EncodingRules): string {
+ const calldata = super.encode(value, rules, this._methodSelector);
+ return calldata;
+ }
+
+ public decode(calldata: string, rules?: DecodingRules): any[] | object {
+ const value = super.decode(calldata, rules, this._methodSelector);
+ return value;
+ }
+
+ public encodeReturnValues(value: any, rules?: EncodingRules): string {
+ const returnData = this._returnDataType.encode(value, rules);
+ return returnData;
+ }
+
+ public decodeReturnValues(returndata: string, rules?: DecodingRules): any {
+ const returnValues = this._returnDataType.decode(returndata, rules);
+ return returnValues;
+ }
+
+ public getSignature(): string {
+ return this._methodSignature;
+ }
+
+ public getSelector(): string {
+ return this._methodSelector;
+ }
+
+ private _computeSignature(): string {
+ const memberSignature = this._computeSignatureOfMembers();
+ const methodSignature = `${this.getDataItem().name}${memberSignature}`;
+ return methodSignature;
+ }
+
+ private _computeSelector(): string {
+ const signature = this._computeSignature();
+ const selector = ethUtil.bufferToHex(
+ ethUtil.toBuffer(
+ ethUtil
+ .sha3(signature)
+ .slice(constants.HEX_SELECTOR_BYTE_OFFSET_IN_CALLDATA, constants.HEX_SELECTOR_LENGTH_IN_BYTES),
+ ),
+ );
+ return selector;
+ }
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts
new file mode 100644
index 000000000..389e75927
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts
@@ -0,0 +1,17 @@
+import { DataItem } from 'ethereum-types';
+
+import { DataType } from '../abstract_data_types/data_type';
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractPointerDataType } from '../abstract_data_types/types/pointer';
+
+export class PointerDataType extends AbstractPointerDataType {
+ constructor(destDataType: DataType, parentDataType: DataType, dataTypeFactory: DataTypeFactory) {
+ const destDataItem = destDataType.getDataItem();
+ const dataItem: DataItem = { name: `ptr<${destDataItem.name}>`, type: `ptr<${destDataItem.type}>` };
+ super(dataItem, dataTypeFactory, destDataType, parentDataType);
+ }
+
+ public getSignature(): string {
+ return this._destination.getSignature();
+ }
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts
new file mode 100644
index 000000000..2e371c505
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts
@@ -0,0 +1,78 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+
+export class StaticBytesDataType extends AbstractBlobDataType {
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true;
+ private static readonly _MATCHER = RegExp(
+ '^(byte|bytes(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32))$',
+ );
+ private static readonly _DEFAULT_WIDTH = 1;
+ private readonly _width: number;
+
+ public static matchType(type: string): boolean {
+ return StaticBytesDataType._MATCHER.test(type);
+ }
+
+ private static _decodeWidthFromType(type: string): number {
+ const matches = StaticBytesDataType._MATCHER.exec(type);
+ const width =
+ !_.isNull(matches) && matches.length === 3 && !_.isUndefined(matches[2])
+ ? parseInt(matches[2], constants.DEC_BASE)
+ : StaticBytesDataType._DEFAULT_WIDTH;
+ return width;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, StaticBytesDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!StaticBytesDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate Static Bytes with bad input: ${dataItem}`);
+ }
+ this._width = StaticBytesDataType._decodeWidthFromType(dataItem.type);
+ }
+
+ public getSignature(): string {
+ // Note that `byte` reduces to `bytes1`
+ return `${SolidityTypes.Bytes}${this._width}`;
+ }
+
+ public encodeValue(value: string | Buffer): Buffer {
+ // 1/2 Convert value into a buffer and do bounds checking
+ this._sanityCheckValue(value);
+ const valueBuf = ethUtil.toBuffer(value);
+ // 2/2 Store value as hex
+ const valuePadded = ethUtil.setLengthRight(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES);
+ return valuePadded;
+ }
+
+ public decodeValue(calldata: RawCalldata): string {
+ const valueBufPadded = calldata.popWord();
+ const valueBuf = valueBufPadded.slice(0, this._width);
+ const value = ethUtil.bufferToHex(valueBuf);
+ this._sanityCheckValue(value);
+ return value;
+ }
+
+ private _sanityCheckValue(value: string | Buffer): void {
+ if (typeof value === 'string') {
+ if (!_.startsWith(value, '0x')) {
+ throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix.`);
+ } else if (value.length % 2 !== 0) {
+ throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`);
+ }
+ }
+ const valueBuf = ethUtil.toBuffer(value);
+ if (valueBuf.byteLength > this._width) {
+ throw new Error(
+ `Tried to assign ${value} (${
+ valueBuf.byteLength
+ } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`,
+ );
+ }
+ }
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts
new file mode 100644
index 000000000..91a72ad3f
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts
@@ -0,0 +1,59 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+
+export class StringDataType extends AbstractBlobDataType {
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false;
+
+ public static matchType(type: string): boolean {
+ return type === SolidityTypes.String;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, StringDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!StringDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate String with bad input: ${dataItem}`);
+ }
+ }
+
+ // Disable prefer-function-over-method for inherited abstract methods.
+ /* tslint:disable prefer-function-over-method */
+ public encodeValue(value: string): Buffer {
+ // Encoded value is of the form: <length><value>, with each field padded to be word-aligned.
+ // 1/3 Construct the length
+ const wordsToStoreValuePadded = Math.ceil(value.length / constants.EVM_WORD_WIDTH_IN_BYTES);
+ const bytesToStoreValuePadded = wordsToStoreValuePadded * constants.EVM_WORD_WIDTH_IN_BYTES;
+ const lengthBuf = ethUtil.toBuffer(value.length);
+ const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, constants.EVM_WORD_WIDTH_IN_BYTES);
+ // 2/3 Construct the value
+ const valueBuf = new Buffer(value);
+ const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded);
+ // 3/3 Combine length and value
+ const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]);
+ return encodedValue;
+ }
+
+ public decodeValue(calldata: RawCalldata): string {
+ // Encoded value is of the form: <length><value>, with each field padded to be word-aligned.
+ // 1/2 Decode length
+ const lengthBufPadded = calldata.popWord();
+ const lengthHexPadded = ethUtil.bufferToHex(lengthBufPadded);
+ const length = parseInt(lengthHexPadded, constants.HEX_BASE);
+ // 2/2 Decode value
+ const wordsToStoreValuePadded = Math.ceil(length / constants.EVM_WORD_WIDTH_IN_BYTES);
+ const valueBufPadded = calldata.popWords(wordsToStoreValuePadded);
+ const valueBuf = valueBufPadded.slice(0, length);
+ const value = valueBuf.toString('ascii');
+ return value;
+ }
+
+ public getSignature(): string {
+ return SolidityTypes.String;
+ }
+ /* tslint:enable prefer-function-over-method */
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts
new file mode 100644
index 000000000..31593c882
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts
@@ -0,0 +1,24 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractSetDataType } from '../abstract_data_types/types/set';
+
+export class TupleDataType extends AbstractSetDataType {
+ private readonly _signature: string;
+
+ public static matchType(type: string): boolean {
+ return type === SolidityTypes.Tuple;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory);
+ if (!TupleDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`);
+ }
+ this._signature = this._computeSignatureOfMembers();
+ }
+
+ public getSignature(): string {
+ return this._signature;
+ }
+}
diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts
new file mode 100644
index 000000000..5180f0cf3
--- /dev/null
+++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts
@@ -0,0 +1,58 @@
+import { DataItem, SolidityTypes } from 'ethereum-types';
+import * as _ from 'lodash';
+
+import { BigNumber } from '../../configured_bignumber';
+import { DataTypeFactory } from '../abstract_data_types/interfaces';
+import { AbstractBlobDataType } from '../abstract_data_types/types/blob';
+import { RawCalldata } from '../calldata/raw_calldata';
+import { constants } from '../utils/constants';
+import * as EncoderMath from '../utils/math';
+
+export class UIntDataType extends AbstractBlobDataType {
+ private static readonly _MATCHER = RegExp(
+ '^uint(8|16|24|32|40|48|56|64|72|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256){0,1}$',
+ );
+ private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true;
+ private static readonly _MAX_WIDTH: number = 256;
+ private static readonly _DEFAULT_WIDTH: number = UIntDataType._MAX_WIDTH;
+ private static readonly _MIN_VALUE = new BigNumber(0);
+ private readonly _width: number;
+ private readonly _maxValue: BigNumber;
+
+ public static matchType(type: string): boolean {
+ return UIntDataType._MATCHER.test(type);
+ }
+
+ private static _decodeWidthFromType(type: string): number {
+ const matches = UIntDataType._MATCHER.exec(type);
+ const width =
+ !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1])
+ ? parseInt(matches[1], constants.DEC_BASE)
+ : UIntDataType._DEFAULT_WIDTH;
+ return width;
+ }
+
+ public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) {
+ super(dataItem, dataTypeFactory, UIntDataType._SIZE_KNOWN_AT_COMPILE_TIME);
+ if (!UIntDataType.matchType(dataItem.type)) {
+ throw new Error(`Tried to instantiate UInt with bad input: ${dataItem}`);
+ }
+ this._width = UIntDataType._decodeWidthFromType(dataItem.type);
+ this._maxValue = new BigNumber(2).toPower(this._width).sub(1);
+ }
+
+ public encodeValue(value: BigNumber | string | number): Buffer {
+ const encodedValue = EncoderMath.safeEncodeNumericValue(value, UIntDataType._MIN_VALUE, this._maxValue);
+ return encodedValue;
+ }
+
+ public decodeValue(calldata: RawCalldata): BigNumber {
+ const valueBuf = calldata.popWord();
+ const value = EncoderMath.safeDecodeNumericValue(valueBuf, UIntDataType._MIN_VALUE, this._maxValue);
+ return value;
+ }
+
+ public getSignature(): string {
+ return `${SolidityTypes.Uint}${this._width}`;
+ }
+}