diff options
author | Hsuan Lee <hsuan@cobinhood.com> | 2019-01-19 18:42:04 +0800 |
---|---|---|
committer | Hsuan Lee <hsuan@cobinhood.com> | 2019-01-19 18:42:04 +0800 |
commit | 7ae38906926dc09bc10670c361af0d2bf0050426 (patch) | |
tree | 5fb10ae366b987db09e4ddb4bc3ba0f75404ad08 /packages/utils/src/abi_encoder/evm_data_types | |
parent | b5fd3c72a08aaa6957917d74c333387a16edf66b (diff) | |
download | dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.gz dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.bz2 dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.lz dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.xz dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.zst dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.zip |
Update dependency packages
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_types')
11 files changed, 0 insertions, 651 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts deleted file mode 100644 index 2278830eb..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; - -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; - -export class AddressDataType extends AbstractBlobDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _ADDRESS_SIZE_IN_BYTES = 20; - private static readonly _DECODED_ADDRESS_OFFSET_IN_BYTES = - constants.EVM_WORD_WIDTH_IN_BYTES - AddressDataType._ADDRESS_SIZE_IN_BYTES; - - public static matchType(type: string): boolean { - return type === SolidityTypes.Address; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, AddressDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!AddressDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Address with bad input: ${dataItem}`); - } - } - - // Disable prefer-function-over-method for inherited abstract methods. - /* tslint:disable prefer-function-over-method */ - public encodeValue(value: string): Buffer { - if (!ethUtil.isValidAddress(value)) { - throw new Error(`Invalid address: '${value}'`); - } - const valueBuf = ethUtil.toBuffer(value); - const encodedValueBuf = ethUtil.setLengthLeft(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES); - return encodedValueBuf; - } - - public decodeValue(calldata: RawCalldata): string { - const valueBufPadded = calldata.popWord(); - const valueBuf = valueBufPadded.slice(AddressDataType._DECODED_ADDRESS_OFFSET_IN_BYTES); - const value = ethUtil.bufferToHex(valueBuf); - const valueLowercase = _.toLower(value); - return valueLowercase; - } - - public getSignatureType(): string { - return SolidityTypes.Address; - } - /* tslint:enable prefer-function-over-method */ -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts deleted file mode 100644 index d9607f47e..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { DataItem } from 'ethereum-types'; -import * as _ from 'lodash'; - -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractSetDataType } from '../abstract_data_types/types/set'; -import { constants } from '../utils/constants'; - -export class ArrayDataType extends AbstractSetDataType { - private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); - private readonly _elementType: string; - - public static matchType(type: string): boolean { - return ArrayDataType._MATCHER.test(type); - } - - private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined | number] { - const matches = ArrayDataType._MATCHER.exec(type); - if (_.isNull(matches) || matches.length !== 3) { - throw new Error(`Could not parse array: ${type}`); - } else if (_.isUndefined(matches[1])) { - throw new Error(`Could not parse array type: ${type}`); - } else if (_.isUndefined(matches[2])) { - throw new Error(`Could not parse array length: ${type}`); - } - const arrayElementType = matches[1]; - const arrayLength = _.isEmpty(matches[2]) ? undefined : parseInt(matches[2], constants.DEC_BASE); - return [arrayElementType, arrayLength]; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - // Construct parent - const isArray = true; - const [arrayElementType, arrayLength] = ArrayDataType._decodeElementTypeAndLengthFromType(dataItem.type); - super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); - // Set array properties - this._elementType = arrayElementType; - } - - public getSignatureType(): string { - return this._computeSignature(false); - } - - public getSignature(isDetailed?: boolean): string { - if (_.isEmpty(this.getDataItem().name) || !isDetailed) { - return this.getSignatureType(); - } - const name = this.getDataItem().name; - const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); - const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; - const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; - const detailedSignature = `${shortName} ${this._computeSignature(isDetailed)}`; - return detailedSignature; - } - - private _computeSignature(isDetailed?: boolean): string { - // Compute signature for a single array element - const elementDataItem: DataItem = { - type: this._elementType, - name: '', - }; - const elementComponents = this.getDataItem().components; - if (!_.isUndefined(elementComponents)) { - elementDataItem.components = elementComponents; - } - const elementDataType = this.getFactory().create(elementDataItem); - const elementSignature = elementDataType.getSignature(isDetailed); - // Construct signature for array of type `element` - if (_.isUndefined(this._arrayLength)) { - return `${elementSignature}[]`; - } else { - return `${elementSignature}[${this._arrayLength}]`; - } - } -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts deleted file mode 100644 index 23298bc88..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; - -import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; - -export class BoolDataType extends AbstractBlobDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - - public static matchType(type: string): boolean { - return type === SolidityTypes.Bool; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, BoolDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!BoolDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Bool with bad input: ${dataItem}`); - } - } - - // Disable prefer-function-over-method for inherited abstract methods. - /* tslint:disable prefer-function-over-method */ - public encodeValue(value: boolean): Buffer { - const encodedValue = value ? '0x1' : '0x0'; - const encodedValueBuf = ethUtil.setLengthLeft( - ethUtil.toBuffer(encodedValue), - constants.EVM_WORD_WIDTH_IN_BYTES, - ); - return encodedValueBuf; - } - - public decodeValue(calldata: RawCalldata): boolean { - const valueBuf = calldata.popWord(); - const valueHex = ethUtil.bufferToHex(valueBuf); - const valueNumber = new BigNumber(valueHex, constants.HEX_BASE); - if (!(valueNumber.isEqualTo(0) || valueNumber.isEqualTo(1))) { - throw new Error(`Failed to decode boolean. Expected 0x0 or 0x1, got ${valueHex}`); - } - /* tslint:disable boolean-naming */ - const value: boolean = !valueNumber.isEqualTo(0); - /* tslint:enable boolean-naming */ - return value; - } - - public getSignatureType(): string { - return SolidityTypes.Bool; - } - /* tslint:enable prefer-function-over-method */ -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts deleted file mode 100644 index fa38b63c0..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; - -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; - -export class DynamicBytesDataType extends AbstractBlobDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; - - public static matchType(type: string): boolean { - return type === SolidityTypes.Bytes; - } - - private static _sanityCheckValue(value: string | Buffer): void { - if (typeof value !== 'string') { - return; - } - if (!_.startsWith(value, '0x')) { - throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix.`); - } else if (value.length % 2 !== 0) { - throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`); - } - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, DynamicBytesDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!DynamicBytesDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Dynamic Bytes with bad input: ${dataItem}`); - } - } - - // Disable prefer-function-over-method for inherited abstract methods. - /* tslint:disable prefer-function-over-method */ - public encodeValue(value: string | Buffer): Buffer { - // Encoded value is of the form: <length><value>, with each field padded to be word-aligned. - // 1/3 Construct the length - const valueBuf = ethUtil.toBuffer(value); - const wordsToStoreValuePadded = Math.ceil(valueBuf.byteLength / constants.EVM_WORD_WIDTH_IN_BYTES); - const bytesToStoreValuePadded = wordsToStoreValuePadded * constants.EVM_WORD_WIDTH_IN_BYTES; - const lengthBuf = ethUtil.toBuffer(valueBuf.byteLength); - const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, constants.EVM_WORD_WIDTH_IN_BYTES); - // 2/3 Construct the value - DynamicBytesDataType._sanityCheckValue(value); - const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded); - // 3/3 Combine length and value - const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]); - return encodedValue; - } - - public decodeValue(calldata: RawCalldata): string { - // Encoded value is of the form: <length><value>, with each field padded to be word-aligned. - // 1/2 Decode length - const lengthBuf = calldata.popWord(); - const lengthHex = ethUtil.bufferToHex(lengthBuf); - const length = parseInt(lengthHex, constants.HEX_BASE); - // 2/2 Decode value - const wordsToStoreValuePadded = Math.ceil(length / constants.EVM_WORD_WIDTH_IN_BYTES); - const valueBufPadded = calldata.popWords(wordsToStoreValuePadded); - const valueBuf = valueBufPadded.slice(0, length); - const value = ethUtil.bufferToHex(valueBuf); - DynamicBytesDataType._sanityCheckValue(value); - return value; - } - - public getSignatureType(): string { - return SolidityTypes.Bytes; - } - /* tslint:enable prefer-function-over-method */ -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts deleted file mode 100644 index f8be1f778..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as _ from 'lodash'; - -import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; -import * as EncoderMath from '../utils/math'; - -export class IntDataType extends AbstractBlobDataType { - private static readonly _MATCHER = RegExp( - '^int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256){0,1}$', - ); - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _MAX_WIDTH: number = 256; - private static readonly _DEFAULT_WIDTH: number = IntDataType._MAX_WIDTH; - private readonly _width: number; - private readonly _minValue: BigNumber; - private readonly _maxValue: BigNumber; - - public static matchType(type: string): boolean { - return IntDataType._MATCHER.test(type); - } - - private static _decodeWidthFromType(type: string): number { - const matches = IntDataType._MATCHER.exec(type); - const width = - !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) - ? parseInt(matches[1], constants.DEC_BASE) - : IntDataType._DEFAULT_WIDTH; - return width; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, IntDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!IntDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Int with bad input: ${dataItem}`); - } - this._width = IntDataType._decodeWidthFromType(dataItem.type); - this._minValue = new BigNumber(2).exponentiatedBy(this._width - 1).times(-1); - this._maxValue = new BigNumber(2).exponentiatedBy(this._width - 1).minus(1); - } - - public encodeValue(value: BigNumber | string | number): Buffer { - const encodedValue = EncoderMath.safeEncodeNumericValue(value, this._minValue, this._maxValue); - return encodedValue; - } - - public decodeValue(calldata: RawCalldata): BigNumber | number { - const valueBuf = calldata.popWord(); - const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); - const numberOfBytesInUint8 = 8; - if (this._width === numberOfBytesInUint8) { - return value.toNumber(); - } - return value; - } - - public getSignatureType(): string { - return `${SolidityTypes.Int}${this._width}`; - } -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts deleted file mode 100644 index c852a0fdf..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { DataItem, MethodAbi } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; - -import { DataType } from '../abstract_data_types/data_type'; -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractSetDataType } from '../abstract_data_types/types/set'; -import { constants } from '../utils/constants'; -import { DecodingRules, EncodingRules } from '../utils/rules'; - -import { TupleDataType } from './tuple'; - -export class MethodDataType extends AbstractSetDataType { - private readonly _methodSignature: string; - private readonly _methodSelector: string; - private readonly _returnDataType: DataType; - - public constructor(abi: MethodAbi, dataTypeFactory: DataTypeFactory) { - const methodDataItem = { type: 'method', name: abi.name, components: abi.inputs }; - super(methodDataItem, dataTypeFactory); - this._methodSignature = this._computeSignature(); - this._methodSelector = this._computeSelector(); - const returnDataItem: DataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; - this._returnDataType = new TupleDataType(returnDataItem, this.getFactory()); - } - - public encode(value: any, rules?: EncodingRules): string { - const calldata = super.encode(value, rules, this._methodSelector); - return calldata; - } - - public decode(calldata: string, rules?: DecodingRules): any[] | object { - const value = super.decode(calldata, rules, this._methodSelector); - return value; - } - - public encodeReturnValues(value: any, rules?: EncodingRules): string { - const returnData = this._returnDataType.encode(value, rules); - return returnData; - } - - public decodeReturnValues(returndata: string, rules?: DecodingRules): any { - const returnValues = this._returnDataType.decode(returndata, rules); - return returnValues; - } - - public strictDecodeReturnValue<T>(returndata: string, rules?: DecodingRules): T { - const returnValues = this._returnDataType.decode(returndata, rules); - const returnValuesAsArray: any = _.isObject(returnValues) ? _.values(returnValues) : [returnValues]; - switch (returnValuesAsArray.length) { - case 0: - return undefined as any; - case 1: - return returnValuesAsArray[0]; - default: - return returnValuesAsArray; - } - } - - public getSignatureType(): string { - return this._methodSignature; - } - - public getSelector(): string { - return this._methodSelector; - } - - private _computeSignature(): string { - const memberSignature = this._computeSignatureOfMembers(); - const methodSignature = `${this.getDataItem().name}${memberSignature}`; - return methodSignature; - } - - private _computeSelector(): string { - const signature = this._computeSignature(); - const selector = ethUtil.bufferToHex( - ethUtil.toBuffer( - ethUtil - .sha3(signature) - .slice(constants.HEX_SELECTOR_BYTE_OFFSET_IN_CALLDATA, constants.HEX_SELECTOR_LENGTH_IN_BYTES), - ), - ); - return selector; - } -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts deleted file mode 100644 index 250db7c64..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { DataItem } from 'ethereum-types'; - -import { DataType } from '../abstract_data_types/data_type'; -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractPointerDataType } from '../abstract_data_types/types/pointer'; - -export class PointerDataType extends AbstractPointerDataType { - constructor(destDataType: DataType, parentDataType: DataType, dataTypeFactory: DataTypeFactory) { - const destDataItem = destDataType.getDataItem(); - const dataItem: DataItem = { name: `ptr<${destDataItem.name}>`, type: `ptr<${destDataItem.type}>` }; - super(dataItem, dataTypeFactory, destDataType, parentDataType); - } - - public getSignatureType(): string { - return this._destination.getSignature(false); - } - - public getSignature(isDetailed?: boolean): string { - return this._destination.getSignature(isDetailed); - } -} 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 deleted file mode 100644 index cbf1957d7..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; - -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; - -export class StaticBytesDataType extends AbstractBlobDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _MATCHER = RegExp( - '^(byte|bytes(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32))$', - ); - private static readonly _DEFAULT_WIDTH = 1; - private readonly _width: number; - - public static matchType(type: string): boolean { - return StaticBytesDataType._MATCHER.test(type); - } - - private static _decodeWidthFromType(type: string): number { - const matches = StaticBytesDataType._MATCHER.exec(type); - const width = - !_.isNull(matches) && matches.length === 3 && !_.isUndefined(matches[2]) - ? parseInt(matches[2], constants.DEC_BASE) - : StaticBytesDataType._DEFAULT_WIDTH; - return width; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, StaticBytesDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!StaticBytesDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Static Bytes with bad input: ${dataItem}`); - } - this._width = StaticBytesDataType._decodeWidthFromType(dataItem.type); - } - - public getSignatureType(): string { - // Note that `byte` reduces to `bytes1` - return `${SolidityTypes.Bytes}${this._width}`; - } - - public encodeValue(value: string | Buffer): Buffer { - // 1/2 Convert value into a buffer and do bounds checking - this._sanityCheckValue(value); - const valueBuf = ethUtil.toBuffer(value); - // 2/2 Store value as hex - const valuePadded = ethUtil.setLengthRight(valueBuf, constants.EVM_WORD_WIDTH_IN_BYTES); - return valuePadded; - } - - public decodeValue(calldata: RawCalldata): string { - const valueBufPadded = calldata.popWord(); - const valueBuf = valueBufPadded.slice(0, this._width); - const value = ethUtil.bufferToHex(valueBuf); - this._sanityCheckValue(value); - return value; - } - - private _sanityCheckValue(value: string | Buffer): void { - if (typeof value === 'string') { - if (!_.startsWith(value, '0x')) { - throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix.`); - } else if (value.length % 2 !== 0) { - throw new Error(`Tried to assign ${value}, which is contains a half-byte. Use full bytes only.`); - } - } - const valueBuf = ethUtil.toBuffer(value); - if (valueBuf.byteLength > this._width) { - throw new Error( - `Tried to assign ${value} (${ - valueBuf.byteLength - } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, - ); - } - } -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts deleted file mode 100644 index 97ac46442..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; - -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; - -export class StringDataType extends AbstractBlobDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; - - public static matchType(type: string): boolean { - return type === SolidityTypes.String; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, StringDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!StringDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate String with bad input: ${dataItem}`); - } - } - - // Disable prefer-function-over-method for inherited abstract methods. - /* tslint:disable prefer-function-over-method */ - public encodeValue(value: string): Buffer { - // Encoded value is of the form: <length><value>, with each field padded to be word-aligned. - // 1/3 Construct the length - const wordsToStoreValuePadded = Math.ceil(value.length / constants.EVM_WORD_WIDTH_IN_BYTES); - const bytesToStoreValuePadded = wordsToStoreValuePadded * constants.EVM_WORD_WIDTH_IN_BYTES; - const lengthBuf = ethUtil.toBuffer(value.length); - const lengthBufPadded = ethUtil.setLengthLeft(lengthBuf, constants.EVM_WORD_WIDTH_IN_BYTES); - // 2/3 Construct the value - const valueBuf = new Buffer(value); - const valueBufPadded = ethUtil.setLengthRight(valueBuf, bytesToStoreValuePadded); - // 3/3 Combine length and value - const encodedValue = Buffer.concat([lengthBufPadded, valueBufPadded]); - return encodedValue; - } - - public decodeValue(calldata: RawCalldata): string { - // Encoded value is of the form: <length><value>, with each field padded to be word-aligned. - // 1/2 Decode length - const lengthBufPadded = calldata.popWord(); - const lengthHexPadded = ethUtil.bufferToHex(lengthBufPadded); - const length = parseInt(lengthHexPadded, constants.HEX_BASE); - // 2/2 Decode value - const wordsToStoreValuePadded = Math.ceil(length / constants.EVM_WORD_WIDTH_IN_BYTES); - const valueBufPadded = calldata.popWords(wordsToStoreValuePadded); - const valueBuf = valueBufPadded.slice(0, length); - const value = valueBuf.toString('ascii'); - return value; - } - - public getSignatureType(): 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 deleted file mode 100644 index 5000c85e8..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as _ from 'lodash'; - -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractSetDataType } from '../abstract_data_types/types/set'; - -export class TupleDataType extends AbstractSetDataType { - public static matchType(type: string): boolean { - return type === SolidityTypes.Tuple; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory); - if (!TupleDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); - } - } - - public getSignatureType(): string { - return this._computeSignatureOfMembers(false); - } - - public getSignature(isDetailed?: boolean): string { - if (_.isEmpty(this.getDataItem().name) || !isDetailed) { - return this.getSignatureType(); - } - const name = this.getDataItem().name; - const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); - const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; - const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; - const detailedSignature = `${shortName} ${this._computeSignatureOfMembers(isDetailed)}`; - return detailedSignature; - } -} diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts deleted file mode 100644 index a82aa789e..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { DataItem, SolidityTypes } from 'ethereum-types'; -import * as _ from 'lodash'; - -import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types/interfaces'; -import { AbstractBlobDataType } from '../abstract_data_types/types/blob'; -import { RawCalldata } from '../calldata/raw_calldata'; -import { constants } from '../utils/constants'; -import * as EncoderMath from '../utils/math'; - -export class UIntDataType extends AbstractBlobDataType { - private static readonly _MATCHER = RegExp( - '^uint(8|16|24|32|40|48|56|64|72|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256){0,1}$', - ); - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _MAX_WIDTH: number = 256; - private static readonly _DEFAULT_WIDTH: number = UIntDataType._MAX_WIDTH; - private static readonly _MIN_VALUE = new BigNumber(0); - private readonly _width: number; - private readonly _maxValue: BigNumber; - - public static matchType(type: string): boolean { - return UIntDataType._MATCHER.test(type); - } - - private static _decodeWidthFromType(type: string): number { - const matches = UIntDataType._MATCHER.exec(type); - const width = - !_.isNull(matches) && matches.length === 2 && !_.isUndefined(matches[1]) - ? parseInt(matches[1], constants.DEC_BASE) - : UIntDataType._DEFAULT_WIDTH; - return width; - } - - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, UIntDataType._SIZE_KNOWN_AT_COMPILE_TIME); - if (!UIntDataType.matchType(dataItem.type)) { - throw new Error(`Tried to instantiate UInt with bad input: ${dataItem}`); - } - this._width = UIntDataType._decodeWidthFromType(dataItem.type); - this._maxValue = new BigNumber(2).exponentiatedBy(this._width).minus(1); - } - - public encodeValue(value: BigNumber | string | number): Buffer { - const encodedValue = EncoderMath.safeEncodeNumericValue(value, UIntDataType._MIN_VALUE, this._maxValue); - return encodedValue; - } - - public decodeValue(calldata: RawCalldata): BigNumber | number { - const valueBuf = calldata.popWord(); - const value = EncoderMath.safeDecodeNumericValue(valueBuf, UIntDataType._MIN_VALUE, this._maxValue); - const numberOfBytesInUint8 = 8; - if (this._width === numberOfBytesInUint8) { - return value.toNumber(); - } - return value; - } - - public getSignatureType(): string { - return `${SolidityTypes.Uint}${this._width}`; - } -} |