From e6ab6f38bacdec90c960ff1db4781d161b1f4103 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 12:58:49 -0800 Subject: Split EVM data types and factory into separate files --- .../src/abi_encoder/evm_data_types/address.ts | 51 +++++++++++ .../utils/src/abi_encoder/evm_data_types/array.ts | 55 ++++++++++++ .../utils/src/abi_encoder/evm_data_types/bool.ts | 50 +++++++++++ .../abi_encoder/evm_data_types/dynamic_bytes.ts | 58 ++++++++++++ .../utils/src/abi_encoder/evm_data_types/index.ts | 11 +++ .../utils/src/abi_encoder/evm_data_types/int.ts | 33 +++++++ .../utils/src/abi_encoder/evm_data_types/method.ts | 90 +++++++++++++++++++ .../utils/src/abi_encoder/evm_data_types/number.ts | 100 +++++++++++++++++++++ .../src/abi_encoder/evm_data_types/pointer.ts | 15 ++++ .../src/abi_encoder/evm_data_types/static_bytes.ts | 68 ++++++++++++++ .../utils/src/abi_encoder/evm_data_types/string.ts | 47 ++++++++++ .../utils/src/abi_encoder/evm_data_types/tuple.ts | 23 +++++ .../utils/src/abi_encoder/evm_data_types/uint.ts | 33 +++++++ 13 files changed, 634 insertions(+) create mode 100644 packages/utils/src/abi_encoder/evm_data_types/address.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/array.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/bool.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/index.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/int.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/method.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/number.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/pointer.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/string.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/tuple.ts create mode 100644 packages/utils/src/abi_encoder/evm_data_types/uint.ts (limited to 'packages/utils/src/abi_encoder/evm_data_types') 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..4bd992cab --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -0,0 +1,51 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class Address extends PayloadDataType { + public static ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X = "Address must start with '0x'"; + public static ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES = 'Address must be 20 bytes'; + 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 - + Address._ADDRESS_SIZE_IN_BYTES; + + public static matchType(type: string): boolean { + return type === 'address'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, Address._SIZE_KNOWN_AT_COMPILE_TIME); + if (!Address.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Address with bad input: ${dataItem}`); + } + } + + public getSignature(): string { + return 'address'; + } + + public encodeValue(value: string): Buffer { + if (!value.startsWith('0x')) { + throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X); + } + const valueAsBuffer = ethUtil.toBuffer(value); + if (valueAsBuffer.byteLength !== Address._ADDRESS_SIZE_IN_BYTES) { + throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES); + } + const encodedValueBuf = ethUtil.setLengthLeft(valueAsBuffer, Constants.EVM_WORD_WIDTH_IN_BYTES); + return encodedValueBuf; + } + + public decodeValue(calldata: RawCalldata): string { + const paddedValueBuf = calldata.popWord(); + const valueBuf = paddedValueBuf.slice(Address._DECODED_ADDRESS_OFFSET_IN_BYTES); + const value = ethUtil.bufferToHex(valueBuf); + return value; + } +} 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..707af7c7e --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -0,0 +1,55 @@ +import { DataItem } from 'ethereum-types'; + +import * as Constants from '../constants'; +import { DataTypeFactory, MemberDataType } from '../data_type'; + +export class Array extends MemberDataType { + private static readonly _matcher = RegExp('^(.+)\\[([0-9]*)\\]$'); + private readonly _arraySignature: string; + private readonly _elementType: string; + + public static matchType(type: string): boolean { + return Array._matcher.test(type); + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + // Sanity check + const matches = Array._matcher.exec(dataItem.type); + if (matches === null || matches.length !== 3) { + throw new Error(`Could not parse array: ${dataItem.type}`); + } else if (matches[1] === undefined) { + throw new Error(`Could not parse array type: ${dataItem.type}`); + } else if (matches[2] === undefined) { + throw new Error(`Could not parse array length: ${dataItem.type}`); + } + + const isArray = true; + const arrayElementType = matches[1]; + const arrayLength = matches[2] === '' ? undefined : parseInt(matches[2], Constants.DEC_BASE); + super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); + this._elementType = arrayElementType; + this._arraySignature = this._computeSignature(); + } + + public getSignature(): string { + return this._arraySignature; + } + + private _computeSignature(): string { + const dataItem: DataItem = { + type: this._elementType, + name: 'N/A', + }; + const components = this.getDataItem().components; + if (components !== undefined) { + dataItem.components = components; + } + const elementDataType = this.getFactory().mapDataItemToDataType(dataItem); + const type = elementDataType.getSignature(); + if (this._arrayLength === undefined) { + return `${type}[]`; + } else { + return `${type}[${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..aee2727c7 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -0,0 +1,50 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { BigNumber } from '../../configured_bignumber'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class Bool extends PayloadDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + + public static matchType(type: string): boolean { + return type === 'bool'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, Bool._SIZE_KNOWN_AT_COMPILE_TIME); + if (!Bool.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Bool with bad input: ${dataItem}`); + } + } + + public getSignature(): string { + return 'bool'; + } + + 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) ? false : true; + /* tslint:enable boolean-naming */ + return value; + } +} 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..51165881a --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -0,0 +1,58 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class DynamicBytes extends PayloadDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; + + public static matchType(type: string): boolean { + return type === 'bytes'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, DynamicBytes._SIZE_KNOWN_AT_COMPILE_TIME); + if (!DynamicBytes.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate DynamicBytes with bad input: ${dataItem}`); + } + } + + public encodeValue(value: string | Buffer): Buffer { + if (typeof value === 'string' && !value.startsWith('0x')) { + throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix. Got '${value}'`); + } + const valueBuf = ethUtil.toBuffer(value); + if (value.length % 2 !== 0) { + throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`); + } + + const wordsForValue = Math.ceil(valueBuf.byteLength / Constants.EVM_WORD_WIDTH_IN_BYTES); + const paddedDynamicBytesForValue = wordsForValue * Constants.EVM_WORD_WIDTH_IN_BYTES; + const paddedValueBuf = ethUtil.setLengthRight(valueBuf, paddedDynamicBytesForValue); + const paddedLengthBuf = ethUtil.setLengthLeft( + ethUtil.toBuffer(valueBuf.byteLength), + Constants.EVM_WORD_WIDTH_IN_BYTES, + ); + const encodedValueBuf = Buffer.concat([paddedLengthBuf, paddedValueBuf]); + return encodedValueBuf; + } + + public decodeValue(calldata: RawCalldata): string { + const lengthBuf = calldata.popWord(); + const lengthHex = ethUtil.bufferToHex(lengthBuf); + const length = parseInt(lengthHex, Constants.HEX_BASE); + const wordsForValue = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); + const paddedValueBuf = calldata.popWords(wordsForValue); + const valueBuf = paddedValueBuf.slice(0, length); + const decodedValue = ethUtil.bufferToHex(valueBuf); + return decodedValue; + } + + public getSignature(): string { + return 'bytes'; + } +} diff --git a/packages/utils/src/abi_encoder/evm_data_types/index.ts b/packages/utils/src/abi_encoder/evm_data_types/index.ts new file mode 100644 index 000000000..fc0edabf1 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/index.ts @@ -0,0 +1,11 @@ +export * from './address'; +export * from './bool'; +export * from './int'; +export * from './uint'; +export * from './static_bytes'; +export * from './dynamic_bytes'; +export * from './string'; +export * from './pointer'; +export * from './tuple'; +export * from './array'; +export * from './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..ba5b4cac9 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -0,0 +1,33 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; + +import { BigNumber } from '../../configured_bignumber'; +import { DataTypeFactory } from '../data_type'; + +import { Number } from './number'; + +export class Int extends Number { + 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}$', + ); + + public static matchType(type: string): boolean { + return Int._matcher.test(type); + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, Int._matcher, dataTypeFactory); + } + + public getMaxValue(): BigNumber { + return new BigNumber(2).toPower(this._width - 1).sub(1); + } + + public getMinValue(): BigNumber { + return new BigNumber(2).toPower(this._width - 1).times(-1); + } + + public getSignature(): string { + return `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..e8e717bf1 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -0,0 +1,90 @@ +import { DataItem, MethodAbi } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { DecodingRules, EncodingRules, RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataType, DataTypeFactory, MemberDataType } from '../data_type'; + +import { StaticBytes } from './static_bytes'; +import { Tuple } from './tuple'; + +export class Method extends MemberDataType { + // TMP + public selector: string; + + private readonly _methodSignature: string; + private readonly _methodSelector: string; + private readonly _returnDataTypes: DataType[]; + private readonly _returnDataItem: DataItem; + + public constructor(abi: MethodAbi, dataTypeFactory: DataTypeFactory) { + super({ type: 'method', name: abi.name, components: abi.inputs }, dataTypeFactory); + this._methodSignature = this._computeSignature(); + this.selector = this._methodSelector = this._computeSelector(); + this._returnDataTypes = []; + this._returnDataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; + const dummy = new StaticBytes({ type: 'byte', name: 'DUMMY' }, dataTypeFactory); // @TODO TMP + _.each(abi.outputs, (dataItem: DataItem) => { + this._returnDataTypes.push(this.getFactory().create(dataItem, dummy)); + }); + } + + public encode(value: any, rules?: EncodingRules): string { + const calldata = super.encode(value, rules, this.selector); + return calldata; + } + + public decode(calldata: string, rules?: DecodingRules): any[] | object { + if (!calldata.startsWith(this.selector)) { + throw new Error( + `Tried to decode calldata, but it was missing the function selector. Expected '${this.selector}'.`, + ); + } + const hasSelector = true; + const value = super.decode(calldata, rules, hasSelector); + return value; + } + + public encodeReturnValues(value: any, rules?: EncodingRules): string { + const returnDataType = new Tuple(this._returnDataItem, this.getFactory()); + const returndata = returnDataType.encode(value, rules); + return returndata; + } + + public decodeReturnValues(returndata: string, rules?: DecodingRules): any { + const returnValues: any[] = []; + const rules_: DecodingRules = rules ? rules : { structsAsObjects: false }; + const rawReturnData = new RawCalldata(returndata, false); + _.each(this._returnDataTypes, (dataType: DataType) => { + returnValues.push(dataType.generateValue(rawReturnData, 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/number.ts b/packages/utils/src/abi_encoder/evm_data_types/number.ts new file mode 100644 index 000000000..17201362e --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/number.ts @@ -0,0 +1,100 @@ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { BigNumber } from '../../configured_bignumber'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export abstract class Number extends PayloadDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + private static readonly _MAX_WIDTH: number = 256; + private static readonly _DEFAULT_WIDTH: number = Number._MAX_WIDTH; + protected _width: number; + + constructor(dataItem: DataItem, matcher: RegExp, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, Number._SIZE_KNOWN_AT_COMPILE_TIME); + const matches = matcher.exec(dataItem.type); + if (matches === null) { + throw new Error(`Tried to instantiate Number with bad input: ${dataItem}`); + } + this._width = + matches !== null && matches.length === 2 && matches[1] !== undefined + ? parseInt(matches[1], Constants.DEC_BASE) + : (this._width = Number._DEFAULT_WIDTH); + } + + public encodeValue(value_: BigNumber | string | number): Buffer { + const value = new BigNumber(value_, 10); + if (value.greaterThan(this.getMaxValue())) { + throw new Error(`Tried to assign value of ${value}, which exceeds max value of ${this.getMaxValue()}`); + } else if (value.lessThan(this.getMinValue())) { + throw new Error(`Tried to assign value of ${value}, which exceeds min value of ${this.getMinValue()}`); + } + + let valueBuf: Buffer; + if (value.greaterThanOrEqualTo(0)) { + valueBuf = ethUtil.setLengthLeft( + ethUtil.toBuffer(`0x${value.toString(Constants.HEX_BASE)}`), + Constants.EVM_WORD_WIDTH_IN_BYTES, + ); + } else { + // BigNumber can't write a negative hex value, so we use twos-complement conversion to do it ourselves. + // Step 1/3: Convert value to positive binary string + const binBase = 2; + const valueBin = value.times(-1).toString(binBase); + + // Step 2/3: Invert binary value + let invertedValueBin = '1'.repeat(Constants.EVM_WORD_WIDTH_IN_BITS - valueBin.length); + _.each(valueBin, (bit: string) => { + invertedValueBin += bit === '1' ? '0' : '1'; + }); + const invertedValue = new BigNumber(invertedValueBin, binBase); + + // Step 3/3: Add 1 to inverted value + // The result is the two's-complement represent of the input value. + const negativeValue = invertedValue.plus(1); + + // Convert the negated value to a hex string + valueBuf = ethUtil.setLengthLeft( + ethUtil.toBuffer(`0x${negativeValue.toString(Constants.HEX_BASE)}`), + Constants.EVM_WORD_WIDTH_IN_BYTES, + ); + } + + return valueBuf; + } + + public decodeValue(calldata: RawCalldata): BigNumber { + const paddedValueBuf = calldata.popWord(); + const paddedValueHex = ethUtil.bufferToHex(paddedValueBuf); + let value = new BigNumber(paddedValueHex, 16); + if (this.getMinValue().lessThan(0)) { + // Check if we're negative + const valueBin = value.toString(Constants.BIN_BASE); + if (valueBin.length === Constants.EVM_WORD_WIDTH_IN_BITS && valueBin[0].startsWith('1')) { + // Negative + // Step 1/3: Invert binary value + let invertedValueBin = ''; + _.each(valueBin, (bit: string) => { + invertedValueBin += bit === '1' ? '0' : '1'; + }); + const invertedValue = new BigNumber(invertedValueBin, Constants.BIN_BASE); + + // Step 2/3: Add 1 to inverted value + // The result is the two's-complement represent of the input value. + const positiveValue = invertedValue.plus(1); + + // Step 3/3: Invert positive value + const negativeValue = positiveValue.times(-1); + value = negativeValue; + } + } + + return value; + } + + public abstract getMaxValue(): BigNumber; + public abstract getMinValue(): BigNumber; +} 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..e0bd3509c --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -0,0 +1,15 @@ +import { DataItem } from 'ethereum-types'; + +import { DataType, DataTypeFactory, DependentDataType } from '../data_type'; + +export class Pointer extends DependentDataType { + 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._dependency.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..309dca234 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -0,0 +1,68 @@ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class StaticBytes extends PayloadDataType { + 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 StaticBytes._matcher.test(type); + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, StaticBytes._SIZE_KNOWN_AT_COMPILE_TIME); + const matches = StaticBytes._matcher.exec(dataItem.type); + if (!StaticBytes.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Byte with bad input: ${dataItem}`); + } + this._width = + matches !== null && matches.length === 3 && matches[2] !== undefined + ? parseInt(matches[2], Constants.DEC_BASE) + : StaticBytes._DEFAULT_WIDTH; + } + + public getSignature(): string { + // Note that `byte` reduces to `bytes1` + return `bytes${this._width}`; + } + + public encodeValue(value: string | Buffer): Buffer { + // Sanity check if string + if (typeof value === 'string' && !value.startsWith('0x')) { + throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix.`); + } + // Convert value into a buffer and do bounds checking + 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()}`, + ); + } else if (value.length % 2 !== 0) { + throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`); + } + + // Store value as hex + const evmWordWidth = 32; + const paddedValue = ethUtil.setLengthRight(valueBuf, evmWordWidth); + return paddedValue; + } + + public decodeValue(calldata: RawCalldata): string { + const paddedValueBuf = calldata.popWord(); + const valueBuf = paddedValueBuf.slice(0, this._width); + const value = ethUtil.bufferToHex(valueBuf); + return value; + } +} 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..96b36e735 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -0,0 +1,47 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class String extends PayloadDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; + + public static matchType(type: string): boolean { + return type === 'string'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, String._SIZE_KNOWN_AT_COMPILE_TIME); + if (!String.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate String with bad input: ${dataItem}`); + } + } + + public encodeValue(value: string): Buffer { + const wordsForValue = Math.ceil(value.length / Constants.EVM_WORD_WIDTH_IN_BYTES); + const paddedDynamicBytesForValue = wordsForValue * Constants.EVM_WORD_WIDTH_IN_BYTES; + const valueBuf = ethUtil.setLengthRight(new Buffer(value), paddedDynamicBytesForValue); + const lengthBuf = ethUtil.setLengthLeft(ethUtil.toBuffer(value.length), Constants.EVM_WORD_WIDTH_IN_BYTES); + const encodedValueBuf = Buffer.concat([lengthBuf, valueBuf]); + return encodedValueBuf; + } + + public decodeValue(calldata: RawCalldata): string { + const lengthBuf = calldata.popWord(); + const lengthHex = ethUtil.bufferToHex(lengthBuf); + const length = parseInt(lengthHex, Constants.HEX_BASE); + const wordsForValue = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); + const paddedValueBuf = calldata.popWords(wordsForValue); + const valueBuf = paddedValueBuf.slice(0, length); + const value = valueBuf.toString('ascii'); + return value; + } + + public getSignature(): string { + return 'string'; + } +} 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..0db29c1eb --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -0,0 +1,23 @@ +import { DataItem } from 'ethereum-types'; + +import { DataTypeFactory, MemberDataType } from '../data_type'; + +export class Tuple extends MemberDataType { + private readonly _tupleSignature: string; + + public static matchType(type: string): boolean { + return type === 'tuple'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory); + if (!Tuple.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); + } + this._tupleSignature = this._computeSignatureOfMembers(); + } + + public getSignature(): string { + return this._tupleSignature; + } +} 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..86b31ab4c --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -0,0 +1,33 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; + +import { BigNumber } from '../../configured_bignumber'; +import { DataTypeFactory } from '../data_type'; + +import { Number } from './number'; + +export class UInt extends Number { + 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}$', + ); + + public static matchType(type: string): boolean { + return UInt._matcher.test(type); + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, UInt._matcher, dataTypeFactory); + } + + public getMaxValue(): BigNumber { + return new BigNumber(2).toPower(this._width).sub(1); + } + + public getMinValue(): BigNumber { + return new BigNumber(0); + } + + public getSignature(): string { + return `uint${this._width}`; + } +} -- cgit v1.2.3 From aed8b083b587e7b420ac6129b04004dea95c3f3a Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 13:24:45 -0800 Subject: Split Calldata into multiple files - 1 class per file --- packages/utils/src/abi_encoder/evm_data_types/method.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index e8e717bf1..c4e6ee93a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -2,9 +2,10 @@ import { DataItem, MethodAbi } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { DecodingRules, EncodingRules, RawCalldata } from '../calldata'; +import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; import { DataType, DataTypeFactory, MemberDataType } from '../data_type'; +import { DecodingRules, EncodingRules } from '../utils/rules'; import { StaticBytes } from './static_bytes'; import { Tuple } from './tuple'; -- cgit v1.2.3 From d4d917f350fb6916df50f3aa5cf09ce68b316aa1 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 13:25:49 -0800 Subject: Ran prettier --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 4bd992cab..3e2c9e78c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -13,7 +13,7 @@ export class Address extends PayloadDataType { 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 - - Address._ADDRESS_SIZE_IN_BYTES; + Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { return type === 'address'; -- cgit v1.2.3 From a47901370bc69d6247e941c569bc9fe824516db9 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 13:40:26 -0800 Subject: Ran prettier --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/array.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/int.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/method.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/number.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/pointer.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/string.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/tuple.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 2 +- 12 files changed, 14 insertions(+), 14 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 3e2c9e78c..707e265f8 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -3,9 +3,9 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataTypeFactory, PayloadDataType } from '../data_type'; export class Address extends PayloadDataType { public static ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X = "Address must start with '0x'"; @@ -13,7 +13,7 @@ export class Address extends PayloadDataType { 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 - - Address._ADDRESS_SIZE_IN_BYTES; + Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { return type === 'address'; diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 707af7c7e..f334282b8 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -1,7 +1,7 @@ import { DataItem } from 'ethereum-types'; +import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; import * as Constants from '../constants'; -import { DataTypeFactory, MemberDataType } from '../data_type'; export class Array extends MemberDataType { private static readonly _matcher = RegExp('^(.+)\\[([0-9]*)\\]$'); diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index aee2727c7..4b9dd32b1 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -4,9 +4,9 @@ import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataTypeFactory, PayloadDataType } from '../data_type'; export class Bool extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; 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 index 51165881a..5fca668e4 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -3,9 +3,9 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataTypeFactory, PayloadDataType } from '../data_type'; export class DynamicBytes extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index ba5b4cac9..ec41b9cfc 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -2,7 +2,7 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../data_type'; +import { DataTypeFactory } from '../abstract_data_types'; import { Number } from './number'; diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index c4e6ee93a..ce33755f1 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -2,9 +2,9 @@ import { DataItem, MethodAbi } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; +import { DataType, DataTypeFactory, MemberDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataType, DataTypeFactory, MemberDataType } from '../data_type'; import { DecodingRules, EncodingRules } from '../utils/rules'; import { StaticBytes } from './static_bytes'; diff --git a/packages/utils/src/abi_encoder/evm_data_types/number.ts b/packages/utils/src/abi_encoder/evm_data_types/number.ts index 17201362e..8ff5e9a6a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/number.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/number.ts @@ -3,9 +3,9 @@ import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataTypeFactory, PayloadDataType } from '../data_type'; export abstract class Number extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts index e0bd3509c..d4411df9b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -1,6 +1,6 @@ import { DataItem } from 'ethereum-types'; -import { DataType, DataTypeFactory, DependentDataType } from '../data_type'; +import { DataType, DataTypeFactory, DependentDataType } from '../abstract_data_types'; export class Pointer extends DependentDataType { constructor(destDataType: DataType, parentDataType: DataType, dataTypeFactory: DataTypeFactory) { 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 index 309dca234..90e872c78 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -2,9 +2,9 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataTypeFactory, PayloadDataType } from '../data_type'; export class StaticBytes extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; @@ -46,7 +46,7 @@ export class StaticBytes extends PayloadDataType { if (valueBuf.byteLength > this._width) { throw new Error( `Tried to assign ${value} (${ - valueBuf.byteLength + valueBuf.byteLength } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, ); } else if (value.length % 2 !== 0) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 96b36e735..c0bde8649 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -3,9 +3,9 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../constants'; -import { DataTypeFactory, PayloadDataType } from '../data_type'; export class String extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 0db29c1eb..4a90e375a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -1,6 +1,6 @@ import { DataItem } from 'ethereum-types'; -import { DataTypeFactory, MemberDataType } from '../data_type'; +import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; export class Tuple extends MemberDataType { private readonly _tupleSignature: string; diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 86b31ab4c..ced3ef08b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -2,7 +2,7 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../data_type'; +import { DataTypeFactory } from '../abstract_data_types'; import { Number } from './number'; -- cgit v1.2.3 From 5a748fb4e5ce9603cf100af5d46c323934ab17ad Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 13:56:37 -0800 Subject: Split ABI Encoder/Decoder tests into separate files --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 707e265f8..0107fdc50 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -13,7 +13,7 @@ export class Address extends PayloadDataType { 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 - - Address._ADDRESS_SIZE_IN_BYTES; + Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { return type === 'address'; 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 index 90e872c78..4e49db609 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -46,7 +46,7 @@ export class StaticBytes extends PayloadDataType { if (valueBuf.byteLength > this._width) { throw new Error( `Tried to assign ${value} (${ - valueBuf.byteLength + valueBuf.byteLength } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, ); } else if (value.length % 2 !== 0) { -- cgit v1.2.3 From a895dacd4e20238087245c274564f694c71f7f6e Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 14:18:01 -0800 Subject: moved abi encoder constants into utils dir --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/array.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/method.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/number.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/string.ts | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 0107fdc50..aab5a0605 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export class Address extends PayloadDataType { public static ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X = "Address must start with '0x'"; @@ -13,7 +13,7 @@ export class Address extends PayloadDataType { 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 - - Address._ADDRESS_SIZE_IN_BYTES; + Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { return type === 'address'; diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index f334282b8..9963b6f32 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -1,7 +1,7 @@ import { DataItem } from 'ethereum-types'; import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export class Array extends MemberDataType { private static readonly _matcher = RegExp('^(.+)\\[([0-9]*)\\]$'); diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 4b9dd32b1..6bc299544 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -6,7 +6,7 @@ import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export class Bool extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; 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 index 5fca668e4..626e266c9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export class DynamicBytes extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index ce33755f1..50d676b4a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { DataType, DataTypeFactory, MemberDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; import { StaticBytes } from './static_bytes'; diff --git a/packages/utils/src/abi_encoder/evm_data_types/number.ts b/packages/utils/src/abi_encoder/evm_data_types/number.ts index 8ff5e9a6a..86acdce07 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/number.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/number.ts @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export abstract class Number extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; 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 index 4e49db609..77dde1333 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export class StaticBytes extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; @@ -46,7 +46,7 @@ export class StaticBytes extends PayloadDataType { if (valueBuf.byteLength > this._width) { throw new Error( `Tried to assign ${value} (${ - valueBuf.byteLength + valueBuf.byteLength } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, ); } else if (value.length % 2 !== 0) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index c0bde8649..47ad7cb97 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../constants'; +import * as Constants from '../utils/constants'; export class String extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; -- cgit v1.2.3 From dcc439c2e3a756af75889ddf3b22146322d1d97d Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 20 Nov 2018 16:28:37 -0800 Subject: Ran prettier --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index aab5a0605..9ae22bd9c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -13,7 +13,7 @@ export class Address extends PayloadDataType { 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 - - Address._ADDRESS_SIZE_IN_BYTES; + Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { return type === 'address'; 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 index 77dde1333..9a2a99ec7 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -46,7 +46,7 @@ export class StaticBytes extends PayloadDataType { if (valueBuf.byteLength > this._width) { throw new Error( `Tried to assign ${value} (${ - valueBuf.byteLength + valueBuf.byteLength } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, ); } else if (value.length % 2 !== 0) { -- cgit v1.2.3 From dc7092e1eb11ff9844efe02e367ef37592f38c55 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 15:32:13 -0800 Subject: Removed mapDataItemToDataType from Factory. Now its just ::create() --- .../utils/src/abi_encoder/evm_data_types/array.ts | 2 +- .../utils/src/abi_encoder/evm_data_types/method.ts | 22 ++++++---------------- 2 files changed, 7 insertions(+), 17 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 9963b6f32..dd8184fd0 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -44,7 +44,7 @@ export class Array extends MemberDataType { if (components !== undefined) { dataItem.components = components; } - const elementDataType = this.getFactory().mapDataItemToDataType(dataItem); + const elementDataType = this.getFactory().create(dataItem); const type = elementDataType.getSignature(); if (this._arrayLength === undefined) { return `${type}[]`; diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 50d676b4a..671b80890 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -7,7 +7,6 @@ import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; -import { StaticBytes } from './static_bytes'; import { Tuple } from './tuple'; export class Method extends MemberDataType { @@ -16,19 +15,14 @@ export class Method extends MemberDataType { private readonly _methodSignature: string; private readonly _methodSelector: string; - private readonly _returnDataTypes: DataType[]; - private readonly _returnDataItem: DataItem; + private readonly _returnDataType: DataType; public constructor(abi: MethodAbi, dataTypeFactory: DataTypeFactory) { super({ type: 'method', name: abi.name, components: abi.inputs }, dataTypeFactory); this._methodSignature = this._computeSignature(); this.selector = this._methodSelector = this._computeSelector(); - this._returnDataTypes = []; - this._returnDataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; - const dummy = new StaticBytes({ type: 'byte', name: 'DUMMY' }, dataTypeFactory); // @TODO TMP - _.each(abi.outputs, (dataItem: DataItem) => { - this._returnDataTypes.push(this.getFactory().create(dataItem, dummy)); - }); + const returnDataItem: DataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; + this._returnDataType = new Tuple(returnDataItem, this.getFactory()); } public encode(value: any, rules?: EncodingRules): string { @@ -48,18 +42,14 @@ export class Method extends MemberDataType { } public encodeReturnValues(value: any, rules?: EncodingRules): string { - const returnDataType = new Tuple(this._returnDataItem, this.getFactory()); - const returndata = returnDataType.encode(value, rules); - return returndata; + const returnData = this._returnDataType.encode(value, rules); + return returnData; } public decodeReturnValues(returndata: string, rules?: DecodingRules): any { - const returnValues: any[] = []; const rules_: DecodingRules = rules ? rules : { structsAsObjects: false }; const rawReturnData = new RawCalldata(returndata, false); - _.each(this._returnDataTypes, (dataType: DataType) => { - returnValues.push(dataType.generateValue(rawReturnData, rules_)); - }); + const returnValues = this._returnDataType.generateValue(rawReturnData, rules_); return returnValues; } -- cgit v1.2.3 From dd8bb6d08b6e837304a76e9707b79e070f951e4e Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 15:38:29 -0800 Subject: Made default encoding/decoding rules global to all modules in encoder --- .../utils/src/abi_encoder/evm_data_types/method.ts | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 671b80890..2faffd44e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -10,30 +10,28 @@ import { DecodingRules, EncodingRules } from '../utils/rules'; import { Tuple } from './tuple'; export class Method extends MemberDataType { - // TMP - public selector: string; - private readonly _methodSignature: string; private readonly _methodSelector: string; private readonly _returnDataType: DataType; public constructor(abi: MethodAbi, dataTypeFactory: DataTypeFactory) { - super({ type: 'method', name: abi.name, components: abi.inputs }, dataTypeFactory); + const methodDataItem = { type: 'method', name: abi.name, components: abi.inputs }; + super(methodDataItem, dataTypeFactory); this._methodSignature = this._computeSignature(); - this.selector = this._methodSelector = this._computeSelector(); + this._methodSelector = this._computeSelector(); const returnDataItem: DataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; this._returnDataType = new Tuple(returnDataItem, this.getFactory()); } public encode(value: any, rules?: EncodingRules): string { - const calldata = super.encode(value, rules, this.selector); + const calldata = super.encode(value, rules, this._methodSelector); return calldata; } public decode(calldata: string, rules?: DecodingRules): any[] | object { - if (!calldata.startsWith(this.selector)) { + if (!calldata.startsWith(this._methodSelector)) { throw new Error( - `Tried to decode calldata, but it was missing the function selector. Expected '${this.selector}'.`, + `Tried to decode calldata, but it was missing the function selector. Expected '${this._methodSelector}'.`, ); } const hasSelector = true; @@ -46,10 +44,11 @@ export class Method extends MemberDataType { return returnData; } - public decodeReturnValues(returndata: string, rules?: DecodingRules): any { - const rules_: DecodingRules = rules ? rules : { structsAsObjects: false }; - const rawReturnData = new RawCalldata(returndata, false); - const returnValues = this._returnDataType.generateValue(rawReturnData, rules_); + public decodeReturnValues(returndata: string, rules_?: DecodingRules): any { + const rules: DecodingRules = rules_ ? rules_ : Constants.DEFAULT_DECODING_RULES; + const returnDataHasSelector = false; + const rawReturnData = new RawCalldata(returndata, returnDataHasSelector); + const returnValues = this._returnDataType.generateValue(rawReturnData, rules); return returnValues; } -- cgit v1.2.3 From 173fc1dcefa266704dd80de6335c03b73b7d8702 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 15:50:49 -0800 Subject: Moved encoder selector check into DataType --- packages/utils/src/abi_encoder/evm_data_types/method.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 2faffd44e..f2e417485 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -3,7 +3,6 @@ import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { DataType, DataTypeFactory, MemberDataType } from '../abstract_data_types'; -import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; @@ -29,13 +28,7 @@ export class Method extends MemberDataType { } public decode(calldata: string, rules?: DecodingRules): any[] | object { - if (!calldata.startsWith(this._methodSelector)) { - throw new Error( - `Tried to decode calldata, but it was missing the function selector. Expected '${this._methodSelector}'.`, - ); - } - const hasSelector = true; - const value = super.decode(calldata, rules, hasSelector); + const value = super.decode(calldata, rules, this._methodSelector); return value; } @@ -44,11 +37,8 @@ export class Method extends MemberDataType { return returnData; } - public decodeReturnValues(returndata: string, rules_?: DecodingRules): any { - const rules: DecodingRules = rules_ ? rules_ : Constants.DEFAULT_DECODING_RULES; - const returnDataHasSelector = false; - const rawReturnData = new RawCalldata(returndata, returnDataHasSelector); - const returnValues = this._returnDataType.generateValue(rawReturnData, rules); + public decodeReturnValues(returndata: string, rules?: DecodingRules): any { + const returnValues = this._returnDataType.decode(returndata, rules); return returnValues; } -- cgit v1.2.3 From 58a2dfbc4d191ea21e6a749371e586dcff3b3239 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 17:04:50 -0800 Subject: Final rounds on evm data types --- .../src/abi_encoder/evm_data_types/static_bytes.ts | 58 ++++++++++++---------- .../utils/src/abi_encoder/evm_data_types/string.ts | 33 +++++++----- .../utils/src/abi_encoder/evm_data_types/tuple.ts | 8 +-- 3 files changed, 58 insertions(+), 41 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') 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 index 9a2a99ec7..afa9afdf2 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -11,7 +11,6 @@ export class StaticBytes extends PayloadDataType { 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; @@ -19,16 +18,20 @@ export class StaticBytes extends PayloadDataType { return StaticBytes._matcher.test(type); } + private static _decodeWidthFromType(type: string): number { + const matches = StaticBytes._matcher.exec(type); + const width = (matches !== null && matches.length === 3 && matches[2] !== undefined) + ? parseInt(matches[2], Constants.DEC_BASE) + : StaticBytes._DEFAULT_WIDTH; + return width; + } + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { super(dataItem, dataTypeFactory, StaticBytes._SIZE_KNOWN_AT_COMPILE_TIME); - const matches = StaticBytes._matcher.exec(dataItem.type); if (!StaticBytes.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Byte with bad input: ${dataItem}`); + throw new Error(`Tried to instantiate Static Bytes with bad input: ${dataItem}`); } - this._width = - matches !== null && matches.length === 3 && matches[2] !== undefined - ? parseInt(matches[2], Constants.DEC_BASE) - : StaticBytes._DEFAULT_WIDTH; + this._width = StaticBytes._decodeWidthFromType(dataItem.type); } public getSignature(): string { @@ -37,11 +40,30 @@ export class StaticBytes extends PayloadDataType { } public encodeValue(value: string | Buffer): Buffer { - // Sanity check if string - if (typeof value === 'string' && !value.startsWith('0x')) { - throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix.`); + // 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 (!value.startsWith('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.`); + } } - // Convert value into a buffer and do bounds checking const valueBuf = ethUtil.toBuffer(value); if (valueBuf.byteLength > this._width) { throw new Error( @@ -49,20 +71,6 @@ export class StaticBytes extends PayloadDataType { valueBuf.byteLength } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, ); - } else if (value.length % 2 !== 0) { - throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`); } - - // Store value as hex - const evmWordWidth = 32; - const paddedValue = ethUtil.setLengthRight(valueBuf, evmWordWidth); - return paddedValue; - } - - public decodeValue(calldata: RawCalldata): string { - const paddedValueBuf = calldata.popWord(); - const valueBuf = paddedValueBuf.slice(0, this._width); - const value = ethUtil.bufferToHex(valueBuf); - return value; } } diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 47ad7cb97..15b93e447 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -13,7 +13,7 @@ export class String extends PayloadDataType { public static matchType(type: string): boolean { return type === 'string'; } - + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { super(dataItem, dataTypeFactory, String._SIZE_KNOWN_AT_COMPILE_TIME); if (!String.matchType(dataItem.type)) { @@ -22,21 +22,30 @@ export class String extends PayloadDataType { } public encodeValue(value: string): Buffer { - const wordsForValue = Math.ceil(value.length / Constants.EVM_WORD_WIDTH_IN_BYTES); - const paddedDynamicBytesForValue = wordsForValue * Constants.EVM_WORD_WIDTH_IN_BYTES; - const valueBuf = ethUtil.setLengthRight(new Buffer(value), paddedDynamicBytesForValue); - const lengthBuf = ethUtil.setLengthLeft(ethUtil.toBuffer(value.length), Constants.EVM_WORD_WIDTH_IN_BYTES); - const encodedValueBuf = Buffer.concat([lengthBuf, valueBuf]); + // Encoded value is of the form: , 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 encodedValueBuf = Buffer.concat([lengthBufPadded, valueBufPadded]); return encodedValueBuf; } public decodeValue(calldata: RawCalldata): string { - const lengthBuf = calldata.popWord(); - const lengthHex = ethUtil.bufferToHex(lengthBuf); - const length = parseInt(lengthHex, Constants.HEX_BASE); - const wordsForValue = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); - const paddedValueBuf = calldata.popWords(wordsForValue); - const valueBuf = paddedValueBuf.slice(0, length); + // Encoded value is of the form: , 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; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 4a90e375a..89dd6604d 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -3,8 +3,8 @@ import { DataItem } from 'ethereum-types'; import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; export class Tuple extends MemberDataType { - private readonly _tupleSignature: string; - + private readonly _signature: string; + public static matchType(type: string): boolean { return type === 'tuple'; } @@ -14,10 +14,10 @@ export class Tuple extends MemberDataType { if (!Tuple.matchType(dataItem.type)) { throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); } - this._tupleSignature = this._computeSignatureOfMembers(); + this._signature = this._computeSignatureOfMembers(); } public getSignature(): string { - return this._tupleSignature; + return this._signature; } } -- cgit v1.2.3 From d2d89adbddaec435ddb65545a86fc4dc981de521 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 17:12:21 -0800 Subject: Abstracted out encoding/decoding of numeric values into its own utility. Could be useful elsewhere. --- .../utils/src/abi_encoder/evm_data_types/array.ts | 6 +- .../utils/src/abi_encoder/evm_data_types/int.ts | 6 +- .../utils/src/abi_encoder/evm_data_types/number.ts | 71 ++-------------------- .../src/abi_encoder/evm_data_types/static_bytes.ts | 8 +-- .../utils/src/abi_encoder/evm_data_types/string.ts | 2 +- .../utils/src/abi_encoder/evm_data_types/tuple.ts | 2 +- .../utils/src/abi_encoder/evm_data_types/uint.ts | 6 +- 7 files changed, 21 insertions(+), 80 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index dd8184fd0..527cdadfe 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -4,17 +4,17 @@ import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; import * as Constants from '../utils/constants'; export class Array extends MemberDataType { - private static readonly _matcher = RegExp('^(.+)\\[([0-9]*)\\]$'); + private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); private readonly _arraySignature: string; private readonly _elementType: string; public static matchType(type: string): boolean { - return Array._matcher.test(type); + return Array._MATCHER.test(type); } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { // Sanity check - const matches = Array._matcher.exec(dataItem.type); + const matches = Array._MATCHER.exec(dataItem.type); if (matches === null || matches.length !== 3) { throw new Error(`Could not parse array: ${dataItem.type}`); } else if (matches[1] === undefined) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index ec41b9cfc..457c41b28 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -7,16 +7,16 @@ import { DataTypeFactory } from '../abstract_data_types'; import { Number } from './number'; export class Int extends Number { - private static readonly _matcher = RegExp( + 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}$', ); public static matchType(type: string): boolean { - return Int._matcher.test(type); + return Int._MATCHER.test(type); } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, Int._matcher, dataTypeFactory); + super(dataItem, Int._MATCHER, dataTypeFactory); } public getMaxValue(): BigNumber { diff --git a/packages/utils/src/abi_encoder/evm_data_types/number.ts b/packages/utils/src/abi_encoder/evm_data_types/number.ts index 86acdce07..053a574e3 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/number.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/number.ts @@ -1,11 +1,11 @@ import { DataItem } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; +import * as EncoderMath from '../utils/math'; export abstract class Number extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; @@ -25,73 +25,14 @@ export abstract class Number extends PayloadDataType { : (this._width = Number._DEFAULT_WIDTH); } - public encodeValue(value_: BigNumber | string | number): Buffer { - const value = new BigNumber(value_, 10); - if (value.greaterThan(this.getMaxValue())) { - throw new Error(`Tried to assign value of ${value}, which exceeds max value of ${this.getMaxValue()}`); - } else if (value.lessThan(this.getMinValue())) { - throw new Error(`Tried to assign value of ${value}, which exceeds min value of ${this.getMinValue()}`); - } - - let valueBuf: Buffer; - if (value.greaterThanOrEqualTo(0)) { - valueBuf = ethUtil.setLengthLeft( - ethUtil.toBuffer(`0x${value.toString(Constants.HEX_BASE)}`), - Constants.EVM_WORD_WIDTH_IN_BYTES, - ); - } else { - // BigNumber can't write a negative hex value, so we use twos-complement conversion to do it ourselves. - // Step 1/3: Convert value to positive binary string - const binBase = 2; - const valueBin = value.times(-1).toString(binBase); - - // Step 2/3: Invert binary value - let invertedValueBin = '1'.repeat(Constants.EVM_WORD_WIDTH_IN_BITS - valueBin.length); - _.each(valueBin, (bit: string) => { - invertedValueBin += bit === '1' ? '0' : '1'; - }); - const invertedValue = new BigNumber(invertedValueBin, binBase); - - // Step 3/3: Add 1 to inverted value - // The result is the two's-complement represent of the input value. - const negativeValue = invertedValue.plus(1); - - // Convert the negated value to a hex string - valueBuf = ethUtil.setLengthLeft( - ethUtil.toBuffer(`0x${negativeValue.toString(Constants.HEX_BASE)}`), - Constants.EVM_WORD_WIDTH_IN_BYTES, - ); - } - - return valueBuf; + public encodeValue(value: BigNumber | string | number): Buffer { + const encodedValue = EncoderMath.safeEncodeNumericValue(value, this.getMinValue(), this.getMaxValue()); + return encodedValue; } public decodeValue(calldata: RawCalldata): BigNumber { - const paddedValueBuf = calldata.popWord(); - const paddedValueHex = ethUtil.bufferToHex(paddedValueBuf); - let value = new BigNumber(paddedValueHex, 16); - if (this.getMinValue().lessThan(0)) { - // Check if we're negative - const valueBin = value.toString(Constants.BIN_BASE); - if (valueBin.length === Constants.EVM_WORD_WIDTH_IN_BITS && valueBin[0].startsWith('1')) { - // Negative - // Step 1/3: Invert binary value - let invertedValueBin = ''; - _.each(valueBin, (bit: string) => { - invertedValueBin += bit === '1' ? '0' : '1'; - }); - const invertedValue = new BigNumber(invertedValueBin, Constants.BIN_BASE); - - // Step 2/3: Add 1 to inverted value - // The result is the two's-complement represent of the input value. - const positiveValue = invertedValue.plus(1); - - // Step 3/3: Invert positive value - const negativeValue = positiveValue.times(-1); - value = negativeValue; - } - } - + const valueBuf = calldata.popWord(); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, this.getMinValue(), this.getMaxValue()); return value; } 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 index afa9afdf2..0d01e6105 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -8,18 +8,18 @@ import * as Constants from '../utils/constants'; export class StaticBytes extends PayloadDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _matcher = RegExp( + 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 StaticBytes._matcher.test(type); + return StaticBytes._MATCHER.test(type); } private static _decodeWidthFromType(type: string): number { - const matches = StaticBytes._matcher.exec(type); + const matches = StaticBytes._MATCHER.exec(type); const width = (matches !== null && matches.length === 3 && matches[2] !== undefined) ? parseInt(matches[2], Constants.DEC_BASE) : StaticBytes._DEFAULT_WIDTH; @@ -55,7 +55,7 @@ export class StaticBytes extends PayloadDataType { this._sanityCheckValue(value); return value; } - + private _sanityCheckValue(value: string | Buffer): void { if (typeof value === 'string') { if (!value.startsWith('0x')) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 15b93e447..428ea21db 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -13,7 +13,7 @@ export class String extends PayloadDataType { public static matchType(type: string): boolean { return type === 'string'; } - + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { super(dataItem, dataTypeFactory, String._SIZE_KNOWN_AT_COMPILE_TIME); if (!String.matchType(dataItem.type)) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 89dd6604d..63d9dfa9e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -4,7 +4,7 @@ import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; export class Tuple extends MemberDataType { private readonly _signature: string; - + public static matchType(type: string): boolean { return type === 'tuple'; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index ced3ef08b..c2b6e214a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -7,16 +7,16 @@ import { DataTypeFactory } from '../abstract_data_types'; import { Number } from './number'; export class UInt extends Number { - private static readonly _matcher = RegExp( + 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}$', ); public static matchType(type: string): boolean { - return UInt._matcher.test(type); + return UInt._MATCHER.test(type); } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, UInt._matcher, dataTypeFactory); + super(dataItem, UInt._MATCHER, dataTypeFactory); } public getMaxValue(): BigNumber { -- cgit v1.2.3 From ebaf9dd275403cdecfb3364876737fcbcd0eab82 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 17:25:04 -0800 Subject: Removed abstract Number class. --- .../utils/src/abi_encoder/evm_data_types/int.ts | 42 ++++++++++++++++----- .../utils/src/abi_encoder/evm_data_types/number.ts | 41 --------------------- .../utils/src/abi_encoder/evm_data_types/uint.ts | 43 +++++++++++++++++----- 3 files changed, 67 insertions(+), 59 deletions(-) delete mode 100644 packages/utils/src/abi_encoder/evm_data_types/number.ts (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 457c41b28..83aeacd66 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -2,29 +2,53 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../utils/constants'; +import * as EncoderMath from '../utils/math'; -import { Number } from './number'; - -export class Int extends Number { +export class Int extends PayloadDataType { 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 = Int._MAX_WIDTH; + private _width: number; + private _minValue: BigNumber; + private _maxValue: BigNumber; public static matchType(type: string): boolean { return Int._MATCHER.test(type); } + private static _decodeWidthFromType(type: string): number { + const matches = Int._MATCHER.exec(type); + const width = (matches !== null && matches.length === 2 && matches[1] !== undefined) + ? parseInt(matches[1], Constants.DEC_BASE) + : Int._DEFAULT_WIDTH; + return width; + } + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, Int._MATCHER, dataTypeFactory); + super(dataItem, dataTypeFactory, Int._SIZE_KNOWN_AT_COMPILE_TIME); + if (!Int.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Int with bad input: ${dataItem}`); + } + this._width = Int._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 getMaxValue(): BigNumber { - return 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 getMinValue(): BigNumber { - return new BigNumber(2).toPower(this._width - 1).times(-1); + public decodeValue(calldata: RawCalldata): BigNumber { + const valueBuf = calldata.popWord(); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); + return value; } public getSignature(): string { diff --git a/packages/utils/src/abi_encoder/evm_data_types/number.ts b/packages/utils/src/abi_encoder/evm_data_types/number.ts deleted file mode 100644 index 053a574e3..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/number.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { DataItem } from 'ethereum-types'; -import * as _ from 'lodash'; - -import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; -import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; -import * as EncoderMath from '../utils/math'; - -export abstract class Number extends PayloadDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _MAX_WIDTH: number = 256; - private static readonly _DEFAULT_WIDTH: number = Number._MAX_WIDTH; - protected _width: number; - - constructor(dataItem: DataItem, matcher: RegExp, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, Number._SIZE_KNOWN_AT_COMPILE_TIME); - const matches = matcher.exec(dataItem.type); - if (matches === null) { - throw new Error(`Tried to instantiate Number with bad input: ${dataItem}`); - } - this._width = - matches !== null && matches.length === 2 && matches[1] !== undefined - ? parseInt(matches[1], Constants.DEC_BASE) - : (this._width = Number._DEFAULT_WIDTH); - } - - public encodeValue(value: BigNumber | string | number): Buffer { - const encodedValue = EncoderMath.safeEncodeNumericValue(value, this.getMinValue(), this.getMaxValue()); - return encodedValue; - } - - public decodeValue(calldata: RawCalldata): BigNumber { - const valueBuf = calldata.popWord(); - const value = EncoderMath.safeDecodeNumericValue(valueBuf, this.getMinValue(), this.getMaxValue()); - return value; - } - - public abstract getMaxValue(): BigNumber; - public abstract getMinValue(): BigNumber; -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index c2b6e214a..832ab075c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -2,32 +2,57 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../utils/constants'; +import * as EncoderMath from '../utils/math'; -import { Number } from './number'; - -export class UInt extends Number { +export class UInt extends PayloadDataType { 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 = UInt._MAX_WIDTH; + private _width: number; + private _minValue: BigNumber; + private _maxValue: BigNumber; public static matchType(type: string): boolean { return UInt._MATCHER.test(type); } + private static _decodeWidthFromType(type: string): number { + const matches = UInt._MATCHER.exec(type); + const width = (matches !== null && matches.length === 2 && matches[1] !== undefined) + ? parseInt(matches[1], Constants.DEC_BASE) + : UInt._DEFAULT_WIDTH; + return width; + } + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, UInt._MATCHER, dataTypeFactory); + super(dataItem, dataTypeFactory, UInt._SIZE_KNOWN_AT_COMPILE_TIME); + if (!UInt.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate UInt with bad input: ${dataItem}`); + } + this._width = UInt._decodeWidthFromType(dataItem.type); + this._minValue = new BigNumber(0); + this._maxValue = new BigNumber(2).toPower(this._width).sub(1); } - public getMaxValue(): BigNumber { - return new BigNumber(2).toPower(this._width).sub(1); + public encodeValue(value: BigNumber | string | number): Buffer { + const encodedValue = EncoderMath.safeEncodeNumericValue(value, this._minValue, this._maxValue); + return encodedValue; } - public getMinValue(): BigNumber { - return new BigNumber(0); + public decodeValue(calldata: RawCalldata): BigNumber { + const valueBuf = calldata.popWord(); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); + return value; } public getSignature(): string { return `uint${this._width}`; } } + -- cgit v1.2.3 From acd364b71c8b3ddb6d4d75d8667cc7f50b18694d Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 17:37:14 -0800 Subject: Comments and inline documentation for dynamic bytes --- .../utils/src/abi_encoder/evm_data_types/bool.ts | 8 ++-- .../abi_encoder/evm_data_types/dynamic_bytes.ts | 55 +++++++++++++--------- .../utils/src/abi_encoder/evm_data_types/string.ts | 4 +- 3 files changed, 39 insertions(+), 28 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 6bc299544..82a519aae 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -22,10 +22,6 @@ export class Bool extends PayloadDataType { } } - public getSignature(): string { - return 'bool'; - } - public encodeValue(value: boolean): Buffer { const encodedValue = value ? '0x1' : '0x0'; const encodedValueBuf = ethUtil.setLengthLeft( @@ -47,4 +43,8 @@ export class Bool extends PayloadDataType { /* tslint:enable boolean-naming */ return value; } + + public getSignature(): string { + return 'bool'; + } } 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 index 626e266c9..ce6ace627 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -17,42 +17,53 @@ export class DynamicBytes extends PayloadDataType { public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { super(dataItem, dataTypeFactory, DynamicBytes._SIZE_KNOWN_AT_COMPILE_TIME); if (!DynamicBytes.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate DynamicBytes with bad input: ${dataItem}`); + throw new Error(`Tried to instantiate Dynamic Bytes with bad input: ${dataItem}`); } } public encodeValue(value: string | Buffer): Buffer { - if (typeof value === 'string' && !value.startsWith('0x')) { - throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix. Got '${value}'`); - } + // Encoded value is of the form: , with each field padded to be word-aligned. + // 1/3 Construct the length const valueBuf = ethUtil.toBuffer(value); - if (value.length % 2 !== 0) { - throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`); - } - - const wordsForValue = Math.ceil(valueBuf.byteLength / Constants.EVM_WORD_WIDTH_IN_BYTES); - const paddedDynamicBytesForValue = wordsForValue * Constants.EVM_WORD_WIDTH_IN_BYTES; - const paddedValueBuf = ethUtil.setLengthRight(valueBuf, paddedDynamicBytesForValue); - const paddedLengthBuf = ethUtil.setLengthLeft( - ethUtil.toBuffer(valueBuf.byteLength), - Constants.EVM_WORD_WIDTH_IN_BYTES, - ); - const encodedValueBuf = Buffer.concat([paddedLengthBuf, paddedValueBuf]); - return encodedValueBuf; + 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 + this._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: , 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); - const wordsForValue = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); - const paddedValueBuf = calldata.popWords(wordsForValue); - const valueBuf = paddedValueBuf.slice(0, length); - const decodedValue = ethUtil.bufferToHex(valueBuf); - return decodedValue; + // 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); + this._sanityCheckValue(value); + return value; } public getSignature(): string { return 'bytes'; } + + private _sanityCheckValue(value: string | Buffer): void { + if (typeof value !== 'string') { + return; + } + if (!value.startsWith('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.`); + } + } } diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 428ea21db..2bb6541a3 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -32,8 +32,8 @@ export class String extends PayloadDataType { const valueBuf = new Buffer(value); const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded); // 3/3 Combine length and value - const encodedValueBuf = Buffer.concat([lengthBufPadded, valueBufPadded]); - return encodedValueBuf; + const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]); + return encodedValue; } public decodeValue(calldata: RawCalldata): string { -- cgit v1.2.3 From 22ce3e2e29fb50d9b9244c9ee567c124586be9ae Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 17:46:45 -0800 Subject: Comments for Array --- .../utils/src/abi_encoder/evm_data_types/array.ts | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 527cdadfe..77e38ebd7 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -12,21 +12,26 @@ export class Array extends MemberDataType { return Array._MATCHER.test(type); } - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - // Sanity check - const matches = Array._MATCHER.exec(dataItem.type); + private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined|number] { + const matches = Array._MATCHER.exec(type); if (matches === null || matches.length !== 3) { - throw new Error(`Could not parse array: ${dataItem.type}`); + throw new Error(`Could not parse array: ${type}`); } else if (matches[1] === undefined) { - throw new Error(`Could not parse array type: ${dataItem.type}`); + throw new Error(`Could not parse array type: ${type}`); } else if (matches[2] === undefined) { - throw new Error(`Could not parse array length: ${dataItem.type}`); + throw new Error(`Could not parse array length: ${type}`); } - - const isArray = true; const arrayElementType = matches[1]; const arrayLength = 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] = Array._decodeElementTypeAndLengthFromType(dataItem.type); super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); + // Set array properties this._elementType = arrayElementType; this._arraySignature = this._computeSignature(); } @@ -36,20 +41,22 @@ export class Array extends MemberDataType { } private _computeSignature(): string { - const dataItem: DataItem = { + // Compute signature for a single array element + const elementDataItem: DataItem = { type: this._elementType, name: 'N/A', }; - const components = this.getDataItem().components; - if (components !== undefined) { - dataItem.components = components; + const elementComponents = this.getDataItem().components; + if (elementComponents !== undefined) { + elementDataItem.components = elementComponents; } - const elementDataType = this.getFactory().create(dataItem); - const type = elementDataType.getSignature(); + const elementDataType = this.getFactory().create(elementDataItem); + const elementSignature = elementDataType.getSignature(); + // Construct signature for array of type `element` if (this._arrayLength === undefined) { - return `${type}[]`; + return `${elementSignature}[]`; } else { - return `${type}[${this._arrayLength}]`; + return `${elementSignature}[${this._arrayLength}]`; } } } -- cgit v1.2.3 From bb4d02e413119132f283ee17549cf5e1732d75b5 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 17:47:33 -0800 Subject: Comments for Address --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 9ae22bd9c..84f6665cb 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -34,17 +34,17 @@ export class Address extends PayloadDataType { if (!value.startsWith('0x')) { throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X); } - const valueAsBuffer = ethUtil.toBuffer(value); - if (valueAsBuffer.byteLength !== Address._ADDRESS_SIZE_IN_BYTES) { + const valueBuf = ethUtil.toBuffer(value); + if (valueBuf.byteLength !== Address._ADDRESS_SIZE_IN_BYTES) { throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES); } - const encodedValueBuf = ethUtil.setLengthLeft(valueAsBuffer, Constants.EVM_WORD_WIDTH_IN_BYTES); + const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, Constants.EVM_WORD_WIDTH_IN_BYTES); return encodedValueBuf; } public decodeValue(calldata: RawCalldata): string { - const paddedValueBuf = calldata.popWord(); - const valueBuf = paddedValueBuf.slice(Address._DECODED_ADDRESS_OFFSET_IN_BYTES); + const valueBufPadded = calldata.popWord(); + const valueBuf = valueBufPadded.slice(Address._DECODED_ADDRESS_OFFSET_IN_BYTES); const value = ethUtil.bufferToHex(valueBuf); return value; } -- cgit v1.2.3 From 9a51af46ee4a35b3d1ce2fcdc6f561aa68307cf0 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 18:24:46 -0800 Subject: Payload -> Blob, Dependent -> Pointer, Member -> Set --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/array.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 4 ++-- .../src/abi_encoder/evm_data_types/dynamic_bytes.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/int.ts | 10 +++++----- packages/utils/src/abi_encoder/evm_data_types/method.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/pointer.ts | 4 ++-- .../utils/src/abi_encoder/evm_data_types/static_bytes.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/string.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/tuple.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 16 +++++++--------- 11 files changed, 30 insertions(+), 32 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 84f6665cb..71aa293b5 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -3,11 +3,11 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; -export class Address extends PayloadDataType { +export class Address extends AbstractDataTypes.Blob { public static ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X = "Address must start with '0x'"; public static ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES = 'Address must be 20 bytes'; private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 77e38ebd7..54f7ba9fa 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -1,9 +1,9 @@ import { DataItem } from 'ethereum-types'; -import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import * as Constants from '../utils/constants'; -export class Array extends MemberDataType { +export class Array extends AbstractDataTypes.Set { private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); private readonly _arraySignature: string; private readonly _elementType: string; diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 82a519aae..031acc88a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -4,11 +4,11 @@ import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; -export class Bool extends PayloadDataType { +export class Bool extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; public static matchType(type: string): boolean { 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 index ce6ace627..01d83d11a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -3,11 +3,11 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; -export class DynamicBytes extends PayloadDataType { +export class DynamicBytes extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 83aeacd66..8a82ed4cc 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -2,21 +2,21 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; import * as EncoderMath from '../utils/math'; -export class Int extends PayloadDataType { +export class Int extends AbstractDataTypes.Blob { 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 = Int._MAX_WIDTH; - private _width: number; - private _minValue: BigNumber; - private _maxValue: BigNumber; + private readonly _width: number; + private readonly _minValue: BigNumber; + private readonly _maxValue: BigNumber; public static matchType(type: string): boolean { return Int._MATCHER.test(type); diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index f2e417485..bd4732097 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -2,13 +2,13 @@ import { DataItem, MethodAbi } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { DataType, DataTypeFactory, MemberDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_types'; import * as Constants from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; import { Tuple } from './tuple'; -export class Method extends MemberDataType { +export class Method extends AbstractDataTypes.Set { private readonly _methodSignature: string; private readonly _methodSelector: string; private readonly _returnDataType: DataType; diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts index d4411df9b..00c743d2b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -1,8 +1,8 @@ import { DataItem } from 'ethereum-types'; -import { DataType, DataTypeFactory, DependentDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_types'; -export class Pointer extends DependentDataType { +export class Pointer extends AbstractDataTypes.Pointer { constructor(destDataType: DataType, parentDataType: DataType, dataTypeFactory: DataTypeFactory) { const destDataItem = destDataType.getDataItem(); const dataItem: DataItem = { name: `ptr<${destDataItem.name}>`, type: `ptr<${destDataItem.type}>` }; 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 index 0d01e6105..d0b41194e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -2,11 +2,11 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; -export class StaticBytes extends PayloadDataType { +export class StaticBytes extends AbstractDataTypes.Blob { 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))$', diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 2bb6541a3..6ab3513c9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -3,11 +3,11 @@ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; -export class String extends PayloadDataType { +export class String extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 63d9dfa9e..3802f96c0 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -1,8 +1,8 @@ import { DataItem } from 'ethereum-types'; -import { DataTypeFactory, MemberDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; -export class Tuple extends MemberDataType { +export class Tuple extends AbstractDataTypes.Set { private readonly _signature: string; public static matchType(type: string): boolean { diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 832ab075c..b1bc690d8 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -2,21 +2,21 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; import * as EncoderMath from '../utils/math'; -export class UInt extends PayloadDataType { +export class UInt extends AbstractDataTypes.Blob { 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 = UInt._MAX_WIDTH; - private _width: number; - private _minValue: BigNumber; - private _maxValue: BigNumber; + private static readonly _MIN_VALUE = new BigNumber(0); + private readonly _width: number; + private readonly _maxValue: BigNumber; public static matchType(type: string): boolean { return UInt._MATCHER.test(type); @@ -36,18 +36,17 @@ export class UInt extends PayloadDataType { throw new Error(`Tried to instantiate UInt with bad input: ${dataItem}`); } this._width = UInt._decodeWidthFromType(dataItem.type); - this._minValue = new BigNumber(0); this._maxValue = new BigNumber(2).toPower(this._width).sub(1); } public encodeValue(value: BigNumber | string | number): Buffer { - const encodedValue = EncoderMath.safeEncodeNumericValue(value, this._minValue, this._maxValue); + const encodedValue = EncoderMath.safeEncodeNumericValue(value, UInt._MIN_VALUE, this._maxValue); return encodedValue; } public decodeValue(calldata: RawCalldata): BigNumber { const valueBuf = calldata.popWord(); - const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, UInt._MIN_VALUE, this._maxValue); return value; } @@ -55,4 +54,3 @@ export class UInt extends PayloadDataType { return `uint${this._width}`; } } - -- cgit v1.2.3 From 8f73f53c95d8ba887558863b8b726a2b3f5b7e2b Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 20:05:41 -0800 Subject: Moved calldata iterator logic into its own iterator clas --- packages/utils/src/abi_encoder/evm_data_types/pointer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts index 00c743d2b..e7c172afb 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -10,6 +10,6 @@ export class Pointer extends AbstractDataTypes.Pointer { } public getSignature(): string { - return this._dependency.getSignature(); + return this._destination.getSignature(); } } -- cgit v1.2.3 From ad1b5af4e59ba750c019cab1f5ec9584b8645101 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 25 Nov 2018 21:40:37 -0800 Subject: Ran prettier --- packages/utils/src/abi_encoder/evm_data_types/array.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/int.ts | 7 ++++--- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 7 ++++--- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 7 ++++--- 4 files changed, 13 insertions(+), 10 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 54f7ba9fa..8cf2cf7cf 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -12,7 +12,7 @@ export class Array extends AbstractDataTypes.Set { return Array._MATCHER.test(type); } - private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined|number] { + private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined | number] { const matches = Array._MATCHER.exec(type); if (matches === null || matches.length !== 3) { throw new Error(`Could not parse array: ${type}`); diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 8a82ed4cc..032cd045a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -24,9 +24,10 @@ export class Int extends AbstractDataTypes.Blob { private static _decodeWidthFromType(type: string): number { const matches = Int._MATCHER.exec(type); - const width = (matches !== null && matches.length === 2 && matches[1] !== undefined) - ? parseInt(matches[1], Constants.DEC_BASE) - : Int._DEFAULT_WIDTH; + const width = + matches !== null && matches.length === 2 && matches[1] !== undefined + ? parseInt(matches[1], Constants.DEC_BASE) + : Int._DEFAULT_WIDTH; return width; } 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 index d0b41194e..2c649cb33 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -20,9 +20,10 @@ export class StaticBytes extends AbstractDataTypes.Blob { private static _decodeWidthFromType(type: string): number { const matches = StaticBytes._MATCHER.exec(type); - const width = (matches !== null && matches.length === 3 && matches[2] !== undefined) - ? parseInt(matches[2], Constants.DEC_BASE) - : StaticBytes._DEFAULT_WIDTH; + const width = + matches !== null && matches.length === 3 && matches[2] !== undefined + ? parseInt(matches[2], Constants.DEC_BASE) + : StaticBytes._DEFAULT_WIDTH; return width; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index b1bc690d8..b5b7683a2 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -24,9 +24,10 @@ export class UInt extends AbstractDataTypes.Blob { private static _decodeWidthFromType(type: string): number { const matches = UInt._MATCHER.exec(type); - const width = (matches !== null && matches.length === 2 && matches[1] !== undefined) - ? parseInt(matches[1], Constants.DEC_BASE) - : UInt._DEFAULT_WIDTH; + const width = + matches !== null && matches.length === 2 && matches[1] !== undefined + ? parseInt(matches[1], Constants.DEC_BASE) + : UInt._DEFAULT_WIDTH; return width; } -- cgit v1.2.3 From f31d4ddffd8dd97f2b2dc226f4f132d1c3192c76 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 13:10:34 -0800 Subject: Replaced null/undefined checks with lodash --- packages/utils/src/abi_encoder/evm_data_types/array.ts | 13 +++++++------ packages/utils/src/abi_encoder/evm_data_types/int.ts | 3 ++- .../utils/src/abi_encoder/evm_data_types/static_bytes.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 8cf2cf7cf..a86283c2a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -1,4 +1,5 @@ import { DataItem } from 'ethereum-types'; +import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import * as Constants from '../utils/constants'; @@ -14,15 +15,15 @@ export class Array extends AbstractDataTypes.Set { private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined | number] { const matches = Array._MATCHER.exec(type); - if (matches === null || matches.length !== 3) { + if (_.isNull(matches) || matches.length !== 3) { throw new Error(`Could not parse array: ${type}`); - } else if (matches[1] === undefined) { + } else if (_.isUndefined(matches[1])) { throw new Error(`Could not parse array type: ${type}`); - } else if (matches[2] === undefined) { + } else if (_.isUndefined(matches[2])) { throw new Error(`Could not parse array length: ${type}`); } const arrayElementType = matches[1]; - const arrayLength = matches[2] === '' ? undefined : parseInt(matches[2], Constants.DEC_BASE); + const arrayLength = _.isEmpty(matches[2]) ? undefined : parseInt(matches[2], Constants.DEC_BASE); return [arrayElementType, arrayLength]; } @@ -47,13 +48,13 @@ export class Array extends AbstractDataTypes.Set { name: 'N/A', }; const elementComponents = this.getDataItem().components; - if (elementComponents !== undefined) { + if (!_.isUndefined(elementComponents)) { elementDataItem.components = elementComponents; } const elementDataType = this.getFactory().create(elementDataItem); const elementSignature = elementDataType.getSignature(); // Construct signature for array of type `element` - if (this._arrayLength === undefined) { + if (_.isUndefined(this._arrayLength)) { return `${elementSignature}[]`; } else { return `${elementSignature}[${this._arrayLength}]`; diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 032cd045a..5c5193644 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -1,5 +1,6 @@ /* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; +import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; @@ -25,7 +26,7 @@ export class Int extends AbstractDataTypes.Blob { private static _decodeWidthFromType(type: string): number { const matches = Int._MATCHER.exec(type); const width = - matches !== null && matches.length === 2 && matches[1] !== undefined + !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) ? parseInt(matches[1], Constants.DEC_BASE) : Int._DEFAULT_WIDTH; return width; 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 index 2c649cb33..3a2ad410f 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -21,7 +21,7 @@ export class StaticBytes extends AbstractDataTypes.Blob { private static _decodeWidthFromType(type: string): number { const matches = StaticBytes._MATCHER.exec(type); const width = - matches !== null && matches.length === 3 && matches[2] !== undefined + !_.isNull(matches) && matches.length === 3 && !_.isUndefined(matches[2]) ? parseInt(matches[2], Constants.DEC_BASE) : StaticBytes._DEFAULT_WIDTH; return width; diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index b5b7683a2..76b944610 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -1,5 +1,6 @@ /* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; +import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; @@ -25,7 +26,7 @@ export class UInt extends AbstractDataTypes.Blob { private static _decodeWidthFromType(type: string): number { const matches = UInt._MATCHER.exec(type); const width = - matches !== null && matches.length === 2 && matches[1] !== undefined + !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) ? parseInt(matches[1], Constants.DEC_BASE) : UInt._DEFAULT_WIDTH; return width; -- cgit v1.2.3 From 3f545da9f86856b54cd226c29174ac1ae085e35b Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 13:28:17 -0800 Subject: Switched implicit conversions to explicit lodash calls --- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 031acc88a..fa115bb87 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -39,7 +39,7 @@ export class Bool extends AbstractDataTypes.Blob { throw new Error(`Failed to decode boolean. Expected 0x0 or 0x1, got ${valueHex}`); } /* tslint:disable boolean-naming */ - const value: boolean = valueNumber.equals(0) ? false : true; + const value: boolean = !valueNumber.equals(0); /* tslint:enable boolean-naming */ return value; } -- cgit v1.2.3 From 1f693ea12142bc761f4067871d92e7d2662cf256 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 13:31:44 -0800 Subject: Changed from .startsWith to _.startsWith --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 71aa293b5..52fc8e7b9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -31,7 +31,7 @@ export class Address extends AbstractDataTypes.Blob { } public encodeValue(value: string): Buffer { - if (!value.startsWith('0x')) { + if (!_.startsWith(value, '0x')) { throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X); } const valueBuf = ethUtil.toBuffer(value); 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 index 01d83d11a..98d90b7e4 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -60,7 +60,7 @@ export class DynamicBytes extends AbstractDataTypes.Blob { if (typeof value !== 'string') { return; } - if (!value.startsWith('0x')) { + 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.`); 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 index 3a2ad410f..68f212f79 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -59,7 +59,7 @@ export class StaticBytes extends AbstractDataTypes.Blob { private _sanityCheckValue(value: string | Buffer): void { if (typeof value === 'string') { - if (!value.startsWith('0x')) { + 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.`); -- cgit v1.2.3 From f479212410b238a7673983148f403b3a220083af Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 15:28:26 -0800 Subject: Style cleanup. Improved wording of some error messages. --- .../src/abi_encoder/evm_data_types/address.ts | 12 ++++++---- .../utils/src/abi_encoder/evm_data_types/bool.ts | 4 +++- .../abi_encoder/evm_data_types/dynamic_bytes.ts | 28 ++++++++++++---------- .../utils/src/abi_encoder/evm_data_types/int.ts | 1 - .../utils/src/abi_encoder/evm_data_types/string.ts | 4 +++- .../utils/src/abi_encoder/evm_data_types/uint.ts | 1 - 6 files changed, 28 insertions(+), 22 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 52fc8e7b9..25ff55903 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -1,4 +1,3 @@ -/* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -26,10 +25,8 @@ export class Address extends AbstractDataTypes.Blob { } } - public getSignature(): string { - return 'address'; - } - + // Disable prefer-function-over-method for inherited abstract methods. + /* tslint:disable prefer-function-over-method */ public encodeValue(value: string): Buffer { if (!_.startsWith(value, '0x')) { throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X); @@ -48,4 +45,9 @@ export class Address extends AbstractDataTypes.Blob { const value = ethUtil.bufferToHex(valueBuf); return value; } + + public getSignature(): string { + return 'address'; + } + /* tslint:enable prefer-function-over-method */ } diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index fa115bb87..7e135aba9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -1,4 +1,3 @@ -/* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -22,6 +21,8 @@ export class Bool extends AbstractDataTypes.Blob { } } + // 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( @@ -47,4 +48,5 @@ export class Bool extends AbstractDataTypes.Blob { public getSignature(): string { return '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 index 98d90b7e4..ad22c8c6e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -1,4 +1,3 @@ -/* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -14,6 +13,17 @@ export class DynamicBytes extends AbstractDataTypes.Blob { return type === '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, DynamicBytes._SIZE_KNOWN_AT_COMPILE_TIME); if (!DynamicBytes.matchType(dataItem.type)) { @@ -21,6 +31,8 @@ export class DynamicBytes extends AbstractDataTypes.Blob { } } + // 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: , with each field padded to be word-aligned. // 1/3 Construct the length @@ -48,22 +60,12 @@ export class DynamicBytes extends AbstractDataTypes.Blob { const valueBufPadded = calldata.popWords(wordsToStoreValuePadded); const valueBuf = valueBufPadded.slice(0, length); const value = ethUtil.bufferToHex(valueBuf); - this._sanityCheckValue(value); + DynamicBytes._sanityCheckValue(value); return value; } public getSignature(): string { return 'bytes'; } - - private _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.`); - } - } + /* 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 index 5c5193644..9d328bba9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -1,4 +1,3 @@ -/* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; import * as _ from 'lodash'; diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 6ab3513c9..08f928d8a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -1,4 +1,3 @@ -/* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -21,6 +20,8 @@ export class String extends AbstractDataTypes.Blob { } } + // 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: , with each field padded to be word-aligned. // 1/3 Construct the length @@ -53,4 +54,5 @@ export class String extends AbstractDataTypes.Blob { public getSignature(): string { return 'string'; } + /* tslint:enable prefer-function-over-method */ } diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 76b944610..4357f15d2 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -1,4 +1,3 @@ -/* tslint:disable prefer-function-over-method */ import { DataItem } from 'ethereum-types'; import * as _ from 'lodash'; -- cgit v1.2.3 From e7bdf4717da9d1fd50cda3dba4e9549dfbc337f7 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 15:32:50 -0800 Subject: Fixed build error: was using `this` instead of class name to reference a static function that used to be an instance method. --- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') 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 index ad22c8c6e..fecd1db6b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -42,7 +42,7 @@ export class DynamicBytes extends AbstractDataTypes.Blob { const lengthBuf = ethUtil.toBuffer(valueBuf.byteLength); const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, Constants.EVM_WORD_WIDTH_IN_BYTES); // 2/3 Construct the value - this._sanityCheckValue(value); + DynamicBytes._sanityCheckValue(value); const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded); // 3/3 Combine length and value const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]); -- cgit v1.2.3 From f196dc9e35d12aad281142371af4d2c32db1fd60 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 16:20:56 -0800 Subject: Use ethUti.isValidAddress in encoder --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 25ff55903..950901ea8 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -7,8 +7,6 @@ import { RawCalldata } from '../calldata'; import * as Constants from '../utils/constants'; export class Address extends AbstractDataTypes.Blob { - public static ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X = "Address must start with '0x'"; - public static ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES = 'Address must be 20 bytes'; 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 - @@ -28,13 +26,10 @@ export class Address extends AbstractDataTypes.Blob { // Disable prefer-function-over-method for inherited abstract methods. /* tslint:disable prefer-function-over-method */ public encodeValue(value: string): Buffer { - if (!_.startsWith(value, '0x')) { - throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_START_WITH_0X); + if (!ethUtil.isValidAddress(value)) { + throw new Error(`Invalid address: '${value}'`); } const valueBuf = ethUtil.toBuffer(value); - if (valueBuf.byteLength !== Address._ADDRESS_SIZE_IN_BYTES) { - throw new Error(Address.ERROR_MESSAGE_ADDRESS_MUST_BE_20_BYTES); - } const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, Constants.EVM_WORD_WIDTH_IN_BYTES); return encodedValueBuf; } -- cgit v1.2.3 From 14c094d050e7b2d0a4b31d02dbe58a54153be7bb Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 16:33:51 -0800 Subject: Use SolidityTypes from `ethereum-types` package. --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 6 +++--- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 6 +++--- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 6 +++--- packages/utils/src/abi_encoder/evm_data_types/int.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/string.ts | 6 +++--- packages/utils/src/abi_encoder/evm_data_types/tuple.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 4 ++-- 8 files changed, 20 insertions(+), 20 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 950901ea8..07a0bd10c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -13,7 +13,7 @@ export class Address extends AbstractDataTypes.Blob { Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { - return type === 'address'; + return type === SolidityTypes.Address; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { @@ -42,7 +42,7 @@ export class Address extends AbstractDataTypes.Blob { } public getSignature(): string { - return 'address'; + return SolidityTypes.Address; } /* tslint:enable prefer-function-over-method */ } diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 7e135aba9..7af13506b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -11,7 +11,7 @@ export class Bool extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; public static matchType(type: string): boolean { - return type === 'bool'; + return type === SolidityTypes.Bool; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { @@ -46,7 +46,7 @@ export class Bool extends AbstractDataTypes.Blob { } public getSignature(): string { - return 'bool'; + 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 index fecd1db6b..ac2a1fb6e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -10,7 +10,7 @@ export class DynamicBytes extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { - return type === 'bytes'; + return type === SolidityTypes.Bytes; } private static _sanityCheckValue(value: string | Buffer): void { @@ -65,7 +65,7 @@ export class DynamicBytes extends AbstractDataTypes.Blob { } public getSignature(): string { - return 'bytes'; + 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 index 9d328bba9..3e465fc15 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; @@ -53,6 +53,6 @@ export class Int extends AbstractDataTypes.Blob { } public getSignature(): string { - return `int${this._width}`; + return `${SolidityTypes.Int}${this._width}`; } } 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 index 68f212f79..ed1f51f7e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -37,7 +37,7 @@ export class StaticBytes extends AbstractDataTypes.Blob { public getSignature(): string { // Note that `byte` reduces to `bytes1` - return `bytes${this._width}`; + return `${SolidityTypes.Bytes}${this._width}`; } public encodeValue(value: string | Buffer): Buffer { diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 08f928d8a..e5b2d5f33 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -10,7 +10,7 @@ export class String extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { - return type === 'string'; + return type === SolidityTypes.String; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { @@ -52,7 +52,7 @@ export class String extends AbstractDataTypes.Blob { } public getSignature(): string { - return '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 index 3802f96c0..40859f62e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; @@ -6,7 +6,7 @@ export class Tuple extends AbstractDataTypes.Set { private readonly _signature: string; public static matchType(type: string): boolean { - return type === 'tuple'; + return type === SolidityTypes.Tuple; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 4357f15d2..970400a57 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -1,4 +1,4 @@ -import { DataItem } from 'ethereum-types'; +import { DataItem, SolidityTypes } from 'ethereum-types'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; @@ -52,6 +52,6 @@ export class UInt extends AbstractDataTypes.Blob { } public getSignature(): string { - return `uint${this._width}`; + return `${SolidityTypes.Uint}${this._width}`; } } -- cgit v1.2.3 From 029b8d59507df25aa9c7d1b096c8d873eb6ae4da Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 27 Nov 2018 17:11:15 -0800 Subject: Changed constants to an exported enum; this is 0x convention --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 6 +++--- packages/utils/src/abi_encoder/evm_data_types/array.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 6 +++--- .../utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 12 ++++++------ packages/utils/src/abi_encoder/evm_data_types/int.ts | 4 ++-- packages/utils/src/abi_encoder/evm_data_types/method.ts | 4 ++-- .../utils/src/abi_encoder/evm_data_types/static_bytes.ts | 6 +++--- packages/utils/src/abi_encoder/evm_data_types/string.ts | 12 ++++++------ packages/utils/src/abi_encoder/evm_data_types/uint.ts | 4 ++-- 9 files changed, 29 insertions(+), 29 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 07a0bd10c..c45355639 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -4,12 +4,12 @@ import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; export class Address extends AbstractDataTypes.Blob { 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 - + private static readonly _DECODED_ADDRESS_OFFSET_IN_BYTES = constants.EVM_WORD_WIDTH_IN_BYTES - Address._ADDRESS_SIZE_IN_BYTES; public static matchType(type: string): boolean { @@ -30,7 +30,7 @@ export class Address extends AbstractDataTypes.Blob { throw new Error(`Invalid address: '${value}'`); } const valueBuf = ethUtil.toBuffer(value); - const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, Constants.EVM_WORD_WIDTH_IN_BYTES); + const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES); return encodedValueBuf; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index a86283c2a..272cc4132 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -2,7 +2,7 @@ import { DataItem } from 'ethereum-types'; import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; export class Array extends AbstractDataTypes.Set { private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); @@ -23,7 +23,7 @@ export class Array extends AbstractDataTypes.Set { 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); + const arrayLength = _.isEmpty(matches[2]) ? undefined : parseInt(matches[2], constants.DEC_BASE); return [arrayElementType, 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 index 7af13506b..0c29f690a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; export class Bool extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; @@ -27,7 +27,7 @@ export class Bool extends AbstractDataTypes.Blob { const encodedValue = value ? '0x1' : '0x0'; const encodedValueBuf = ethUtil.setLengthLeft( ethUtil.toBuffer(encodedValue), - Constants.EVM_WORD_WIDTH_IN_BYTES, + constants.EVM_WORD_WIDTH_IN_BYTES, ); return encodedValueBuf; } @@ -35,7 +35,7 @@ export class Bool extends AbstractDataTypes.Blob { public decodeValue(calldata: RawCalldata): boolean { const valueBuf = calldata.popWord(); const valueHex = ethUtil.bufferToHex(valueBuf); - const valueNumber = new BigNumber(valueHex, Constants.HEX_BASE); + 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}`); } 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 index ac2a1fb6e..2c256cfa9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; export class DynamicBytes extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; @@ -37,10 +37,10 @@ export class DynamicBytes extends AbstractDataTypes.Blob { // Encoded value is of the form: , 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 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); + const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, constants.EVM_WORD_WIDTH_IN_BYTES); // 2/3 Construct the value DynamicBytes._sanityCheckValue(value); const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded); @@ -54,9 +54,9 @@ export class DynamicBytes extends AbstractDataTypes.Blob { // 1/2 Decode length const lengthBuf = calldata.popWord(); const lengthHex = ethUtil.bufferToHex(lengthBuf); - const length = parseInt(lengthHex, Constants.HEX_BASE); + const length = parseInt(lengthHex, constants.HEX_BASE); // 2/2 Decode value - const wordsToStoreValuePadded = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); + 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); diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 3e465fc15..244b720e3 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; export class Int extends AbstractDataTypes.Blob { @@ -26,7 +26,7 @@ export class Int extends AbstractDataTypes.Blob { const matches = Int._MATCHER.exec(type); const width = !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) - ? parseInt(matches[1], Constants.DEC_BASE) + ? parseInt(matches[1], constants.DEC_BASE) : Int._DEFAULT_WIDTH; return 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 index bd4732097..7256a93d9 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -3,7 +3,7 @@ import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_types'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; import { Tuple } from './tuple'; @@ -62,7 +62,7 @@ export class Method extends AbstractDataTypes.Set { ethUtil.toBuffer( ethUtil .sha3(signature) - .slice(Constants.HEX_SELECTOR_BYTE_OFFSET_IN_CALLDATA, Constants.HEX_SELECTOR_LENGTH_IN_BYTES), + .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/static_bytes.ts b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts index ed1f51f7e..5453d47a0 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; export class StaticBytes extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; @@ -22,7 +22,7 @@ export class StaticBytes extends AbstractDataTypes.Blob { const matches = StaticBytes._MATCHER.exec(type); const width = !_.isNull(matches) && matches.length === 3 && !_.isUndefined(matches[2]) - ? parseInt(matches[2], Constants.DEC_BASE) + ? parseInt(matches[2], constants.DEC_BASE) : StaticBytes._DEFAULT_WIDTH; return width; } @@ -45,7 +45,7 @@ export class StaticBytes extends AbstractDataTypes.Blob { 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); + const valuePadded = ethUtil.setLengthRight(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES); return valuePadded; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index e5b2d5f33..ac62ea264 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; export class String extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; @@ -25,10 +25,10 @@ export class String extends AbstractDataTypes.Blob { public encodeValue(value: string): Buffer { // Encoded value is of the form: , 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 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); + 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); @@ -42,9 +42,9 @@ export class String extends AbstractDataTypes.Blob { // 1/2 Decode length const lengthBufPadded = calldata.popWord(); const lengthHexPadded = ethUtil.bufferToHex(lengthBufPadded); - const length = parseInt(lengthHexPadded, Constants.HEX_BASE); + const length = parseInt(lengthHexPadded, constants.HEX_BASE); // 2/2 Decode value - const wordsToStoreValuePadded = Math.ceil(length / Constants.EVM_WORD_WIDTH_IN_BYTES); + 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'); diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 970400a57..df7ea38a4 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; +import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; export class UInt extends AbstractDataTypes.Blob { @@ -26,7 +26,7 @@ export class UInt extends AbstractDataTypes.Blob { const matches = UInt._MATCHER.exec(type); const width = !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) - ? parseInt(matches[1], Constants.DEC_BASE) + ? parseInt(matches[1], constants.DEC_BASE) : UInt._DEFAULT_WIDTH; return width; } -- cgit v1.2.3 From a172ab158e2eaca8256ef881c3f2d4098987ec8a Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Wed, 28 Nov 2018 13:22:18 -0800 Subject: Explicit imports for EVM Data Types --- .../utils/src/abi_encoder/evm_data_types/address.ts | 10 +++++----- .../utils/src/abi_encoder/evm_data_types/array.ts | 8 ++++---- .../utils/src/abi_encoder/evm_data_types/bool.ts | 6 +++--- .../src/abi_encoder/evm_data_types/dynamic_bytes.ts | 10 +++++----- .../utils/src/abi_encoder/evm_data_types/index.ts | 11 ----------- packages/utils/src/abi_encoder/evm_data_types/int.ts | 16 ++++++++-------- .../utils/src/abi_encoder/evm_data_types/method.ts | 6 +++--- .../utils/src/abi_encoder/evm_data_types/pointer.ts | 2 +- .../src/abi_encoder/evm_data_types/static_bytes.ts | 14 +++++++------- .../utils/src/abi_encoder/evm_data_types/string.ts | 6 +++--- .../utils/src/abi_encoder/evm_data_types/tuple.ts | 4 ++-- .../utils/src/abi_encoder/evm_data_types/uint.ts | 20 ++++++++++---------- 12 files changed, 51 insertions(+), 62 deletions(-) delete mode 100644 packages/utils/src/abi_encoder/evm_data_types/index.ts (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index c45355639..769c5a81c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -6,19 +6,19 @@ import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class Address extends AbstractDataTypes.Blob { +export class AddressDataType extends AbstractDataTypes.Blob { 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 - - Address._ADDRESS_SIZE_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, Address._SIZE_KNOWN_AT_COMPILE_TIME); - if (!Address.matchType(dataItem.type)) { + 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}`); } } @@ -36,7 +36,7 @@ export class Address extends AbstractDataTypes.Blob { public decodeValue(calldata: RawCalldata): string { const valueBufPadded = calldata.popWord(); - const valueBuf = valueBufPadded.slice(Address._DECODED_ADDRESS_OFFSET_IN_BYTES); + const valueBuf = valueBufPadded.slice(AddressDataType._DECODED_ADDRESS_OFFSET_IN_BYTES); const value = ethUtil.bufferToHex(valueBuf); return value; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 272cc4132..1736bcef0 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -4,17 +4,17 @@ import * as _ from 'lodash'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { constants } from '../utils/constants'; -export class Array extends AbstractDataTypes.Set { +export class ArrayDataType extends AbstractDataTypes.Set { private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); private readonly _arraySignature: string; private readonly _elementType: string; public static matchType(type: string): boolean { - return Array._MATCHER.test(type); + return ArrayDataType._MATCHER.test(type); } private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined | number] { - const matches = Array._MATCHER.exec(type); + 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])) { @@ -30,7 +30,7 @@ export class Array extends AbstractDataTypes.Set { public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { // Construct parent const isArray = true; - const [arrayElementType, arrayLength] = Array._decodeElementTypeAndLengthFromType(dataItem.type); + const [arrayElementType, arrayLength] = ArrayDataType._decodeElementTypeAndLengthFromType(dataItem.type); super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); // Set array properties this._elementType = arrayElementType; diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 0c29f690a..32eda9c39 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -7,7 +7,7 @@ import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class Bool extends AbstractDataTypes.Blob { +export class BoolDataType extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; public static matchType(type: string): boolean { @@ -15,8 +15,8 @@ export class Bool extends AbstractDataTypes.Blob { } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, Bool._SIZE_KNOWN_AT_COMPILE_TIME); - if (!Bool.matchType(dataItem.type)) { + 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}`); } } 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 index 2c256cfa9..c8ff47c3d 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -6,7 +6,7 @@ import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class DynamicBytes extends AbstractDataTypes.Blob { +export class DynamicBytesDataType extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { @@ -25,8 +25,8 @@ export class DynamicBytes extends AbstractDataTypes.Blob { } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, DynamicBytes._SIZE_KNOWN_AT_COMPILE_TIME); - if (!DynamicBytes.matchType(dataItem.type)) { + 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}`); } } @@ -42,7 +42,7 @@ export class DynamicBytes extends AbstractDataTypes.Blob { const lengthBuf = ethUtil.toBuffer(valueBuf.byteLength); const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, constants.EVM_WORD_WIDTH_IN_BYTES); // 2/3 Construct the value - DynamicBytes._sanityCheckValue(value); + DynamicBytesDataType._sanityCheckValue(value); const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded); // 3/3 Combine length and value const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]); @@ -60,7 +60,7 @@ export class DynamicBytes extends AbstractDataTypes.Blob { const valueBufPadded = calldata.popWords(wordsToStoreValuePadded); const valueBuf = valueBufPadded.slice(0, length); const value = ethUtil.bufferToHex(valueBuf); - DynamicBytes._sanityCheckValue(value); + DynamicBytesDataType._sanityCheckValue(value); return value; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/index.ts b/packages/utils/src/abi_encoder/evm_data_types/index.ts deleted file mode 100644 index fc0edabf1..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -export * from './address'; -export * from './bool'; -export * from './int'; -export * from './uint'; -export * from './static_bytes'; -export * from './dynamic_bytes'; -export * from './string'; -export * from './pointer'; -export * from './tuple'; -export * from './array'; -export * from './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 index 244b720e3..aee8320a6 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -7,36 +7,36 @@ import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; -export class Int extends AbstractDataTypes.Blob { +export class IntDataType extends AbstractDataTypes.Blob { 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 = Int._MAX_WIDTH; + 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 Int._MATCHER.test(type); + return IntDataType._MATCHER.test(type); } private static _decodeWidthFromType(type: string): number { - const matches = Int._MATCHER.exec(type); + const matches = IntDataType._MATCHER.exec(type); const width = !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) ? parseInt(matches[1], constants.DEC_BASE) - : Int._DEFAULT_WIDTH; + : IntDataType._DEFAULT_WIDTH; return width; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, Int._SIZE_KNOWN_AT_COMPILE_TIME); - if (!Int.matchType(dataItem.type)) { + 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 = Int._decodeWidthFromType(dataItem.type); + 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); } diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 7256a93d9..de8809edf 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -6,9 +6,9 @@ import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_t import { constants } from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; -import { Tuple } from './tuple'; +import { TupleDataType } from './tuple'; -export class Method extends AbstractDataTypes.Set { +export class MethodDataType extends AbstractDataTypes.Set { private readonly _methodSignature: string; private readonly _methodSelector: string; private readonly _returnDataType: DataType; @@ -19,7 +19,7 @@ export class Method extends AbstractDataTypes.Set { this._methodSignature = this._computeSignature(); this._methodSelector = this._computeSelector(); const returnDataItem: DataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; - this._returnDataType = new Tuple(returnDataItem, this.getFactory()); + this._returnDataType = new TupleDataType(returnDataItem, this.getFactory()); } public encode(value: any, rules?: EncodingRules): string { diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts index e7c172afb..7ca428760 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -2,7 +2,7 @@ import { DataItem } from 'ethereum-types'; import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_types'; -export class Pointer extends AbstractDataTypes.Pointer { +export class PointerDataType extends AbstractDataTypes.Pointer { constructor(destDataType: DataType, parentDataType: DataType, dataTypeFactory: DataTypeFactory) { const destDataItem = destDataType.getDataItem(); const dataItem: DataItem = { name: `ptr<${destDataItem.name}>`, type: `ptr<${destDataItem.type}>` }; 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 index 5453d47a0..3b270630e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -6,7 +6,7 @@ import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class StaticBytes extends AbstractDataTypes.Blob { +export class StaticBytesDataType extends AbstractDataTypes.Blob { 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))$', @@ -15,24 +15,24 @@ export class StaticBytes extends AbstractDataTypes.Blob { private readonly _width: number; public static matchType(type: string): boolean { - return StaticBytes._MATCHER.test(type); + return StaticBytesDataType._MATCHER.test(type); } private static _decodeWidthFromType(type: string): number { - const matches = StaticBytes._MATCHER.exec(type); + const matches = StaticBytesDataType._MATCHER.exec(type); const width = !_.isNull(matches) && matches.length === 3 && !_.isUndefined(matches[2]) ? parseInt(matches[2], constants.DEC_BASE) - : StaticBytes._DEFAULT_WIDTH; + : StaticBytesDataType._DEFAULT_WIDTH; return width; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, StaticBytes._SIZE_KNOWN_AT_COMPILE_TIME); - if (!StaticBytes.matchType(dataItem.type)) { + 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 = StaticBytes._decodeWidthFromType(dataItem.type); + this._width = StaticBytesDataType._decodeWidthFromType(dataItem.type); } public getSignature(): string { diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index ac62ea264..d7e9ec7fe 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -6,7 +6,7 @@ import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class String extends AbstractDataTypes.Blob { +export class StringDataType extends AbstractDataTypes.Blob { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { @@ -14,8 +14,8 @@ export class String extends AbstractDataTypes.Blob { } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, String._SIZE_KNOWN_AT_COMPILE_TIME); - if (!String.matchType(dataItem.type)) { + 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}`); } } diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 40859f62e..5ba875b9e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -2,7 +2,7 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; -export class Tuple extends AbstractDataTypes.Set { +export class TupleDataType extends AbstractDataTypes.Set { private readonly _signature: string; public static matchType(type: string): boolean { @@ -11,7 +11,7 @@ export class Tuple extends AbstractDataTypes.Set { public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { super(dataItem, dataTypeFactory); - if (!Tuple.matchType(dataItem.type)) { + if (!TupleDataType.matchType(dataItem.type)) { throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); } this._signature = this._computeSignatureOfMembers(); diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index df7ea38a4..a5989ea11 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -7,47 +7,47 @@ import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; -export class UInt extends AbstractDataTypes.Blob { +export class UIntDataType extends AbstractDataTypes.Blob { 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 = UInt._MAX_WIDTH; + 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 UInt._MATCHER.test(type); + return UIntDataType._MATCHER.test(type); } private static _decodeWidthFromType(type: string): number { - const matches = UInt._MATCHER.exec(type); + const matches = UIntDataType._MATCHER.exec(type); const width = !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) ? parseInt(matches[1], constants.DEC_BASE) - : UInt._DEFAULT_WIDTH; + : UIntDataType._DEFAULT_WIDTH; return width; } public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, UInt._SIZE_KNOWN_AT_COMPILE_TIME); - if (!UInt.matchType(dataItem.type)) { + 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 = UInt._decodeWidthFromType(dataItem.type); + 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, UInt._MIN_VALUE, this._maxValue); + 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, UInt._MIN_VALUE, this._maxValue); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, UIntDataType._MIN_VALUE, this._maxValue); return value; } -- cgit v1.2.3 From b8ea322541e291b84f261bffcc77baf85dae08c1 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Wed, 28 Nov 2018 13:35:53 -0800 Subject: Explicit imports for abstract data types. --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/array.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/int.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/method.ts | 6 ++++-- packages/utils/src/abi_encoder/evm_data_types/pointer.ts | 6 ++++-- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/string.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/tuple.ts | 5 +++-- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 5 +++-- 11 files changed, 35 insertions(+), 22 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 769c5a81c..17363b5f3 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -2,11 +2,12 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class AddressDataType extends AbstractDataTypes.Blob { +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 - diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 1736bcef0..7595cb667 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -1,10 +1,11 @@ import { DataItem } from 'ethereum-types'; import * as _ from 'lodash'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractSetDataType } from '../abstract_data_types/types/set'; import { constants } from '../utils/constants'; -export class ArrayDataType extends AbstractDataTypes.Set { +export class ArrayDataType extends AbstractSetDataType { private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); private readonly _arraySignature: string; private readonly _elementType: string; diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 32eda9c39..778a01d8a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -3,11 +3,12 @@ import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class BoolDataType extends AbstractDataTypes.Blob { +export class BoolDataType extends AbstractBlobDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; public static matchType(type: string): boolean { 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 index c8ff47c3d..ac8e2b716 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -2,11 +2,12 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class DynamicBytesDataType extends AbstractDataTypes.Blob { +export class DynamicBytesDataType extends AbstractBlobDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index aee8320a6..6b5c3bf78 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -2,12 +2,13 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; -export class IntDataType extends AbstractDataTypes.Blob { +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}$', ); diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index de8809edf..b1cd1377f 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -2,13 +2,15 @@ import { DataItem, MethodAbi } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_types'; +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 AbstractDataTypes.Set { +export class MethodDataType extends AbstractSetDataType { private readonly _methodSignature: string; private readonly _methodSelector: string; private readonly _returnDataType: DataType; diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts index 7ca428760..389e75927 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -1,8 +1,10 @@ import { DataItem } from 'ethereum-types'; -import { AbstractDataTypes, DataType, DataTypeFactory } from '../abstract_data_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 AbstractDataTypes.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}>` }; 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 index 3b270630e..28584d445 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -2,11 +2,12 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class StaticBytesDataType extends AbstractDataTypes.Blob { +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))$', diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index d7e9ec7fe..7b6af747b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -2,11 +2,12 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; -export class StringDataType extends AbstractDataTypes.Blob { +export class StringDataType extends AbstractBlobDataType { private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; public static matchType(type: string): boolean { diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 5ba875b9e..31593c882 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -1,8 +1,9 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractSetDataType } from '../abstract_data_types/types/set'; -export class TupleDataType extends AbstractDataTypes.Set { +export class TupleDataType extends AbstractSetDataType { private readonly _signature: string; public static matchType(type: string): boolean { diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index a5989ea11..45cb366f7 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -2,12 +2,13 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; import * as _ from 'lodash'; import { BigNumber } from '../../configured_bignumber'; -import { AbstractDataTypes, DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; import { RawCalldata } from '../calldata'; import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; -export class UIntDataType extends AbstractDataTypes.Blob { +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}$', ); -- cgit v1.2.3 From 2da7cadefa877ff824da8fbaecd59dbff5028728 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Wed, 28 Nov 2018 13:47:01 -0800 Subject: Explicit imports for calldata --- packages/utils/src/abi_encoder/evm_data_types/address.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/bool.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/int.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/string.ts | 2 +- packages/utils/src/abi_encoder/evm_data_types/uint.ts | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) (limited to 'packages/utils/src/abi_encoder/evm_data_types') diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 17363b5f3..88846b1fa 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { DataTypeFactory } from '../abstract_data_types/interfaces'; import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; export class AddressDataType extends AbstractBlobDataType { diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 778a01d8a..d713d5a94 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -5,7 +5,7 @@ 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'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; export class BoolDataType extends AbstractBlobDataType { 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 index ac8e2b716..5277efd6c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { DataTypeFactory } from '../abstract_data_types/interfaces'; import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; export class DynamicBytesDataType extends AbstractBlobDataType { diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 6b5c3bf78..f1dcf5ea1 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -4,7 +4,7 @@ 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'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; 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 index 28584d445..2e371c505 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { DataTypeFactory } from '../abstract_data_types/interfaces'; import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; export class StaticBytesDataType extends AbstractBlobDataType { diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 7b6af747b..91a72ad3f 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { DataTypeFactory } from '../abstract_data_types/interfaces'; import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; export class StringDataType extends AbstractBlobDataType { diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 45cb366f7..5180f0cf3 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -4,7 +4,7 @@ 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'; +import { RawCalldata } from '../calldata/raw_calldata'; import { constants } from '../utils/constants'; import * as EncoderMath from '../utils/math'; -- cgit v1.2.3