Explicit imports for EVM Data Types

This commit is contained in:
Greg Hysen
2018-11-28 13:22:18 -08:00
parent 5c13353fb2
commit a172ab158e
14 changed files with 87 additions and 76 deletions

View File

@@ -3,69 +3,79 @@ import { DataItem, MethodAbi } from 'ethereum-types';
import * as _ from 'lodash';
import { DataType, DataTypeFactory } from './abstract_data_types';
import * as Impl from './evm_data_types';
import { AddressDataType } from './evm_data_types/address';
import { ArrayDataType } from './evm_data_types/array';
import { BoolDataType } from './evm_data_types/bool';
import { DynamicBytesDataType } from './evm_data_types/dynamic_bytes';
import { IntDataType } from './evm_data_types/int';
import { MethodDataType } from './evm_data_types/method';
import { PointerDataType } from './evm_data_types/pointer';
import { StaticBytesDataType } from './evm_data_types/static_bytes';
import { StringDataType } from './evm_data_types/string';
import { TupleDataType } from './evm_data_types/tuple';
import { UIntDataType } from './evm_data_types/uint';
export class Address extends Impl.Address {
export class Address extends AddressDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class Bool extends Impl.Bool {
export class Bool extends BoolDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class Int extends Impl.Int {
export class Int extends IntDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class UInt extends Impl.UInt {
export class UInt extends UIntDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class StaticBytes extends Impl.StaticBytes {
export class StaticBytes extends StaticBytesDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class DynamicBytes extends Impl.DynamicBytes {
export class DynamicBytes extends DynamicBytesDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class String extends Impl.String {
export class String extends StringDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class Pointer extends Impl.Pointer {
export class Pointer extends PointerDataType {
public constructor(destDataType: DataType, parentDataType: DataType) {
super(destDataType, parentDataType, EvmDataTypeFactory.getInstance());
}
}
export class Tuple extends Impl.Tuple {
export class Tuple extends TupleDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class Array extends Impl.Array {
export class Array extends ArrayDataType {
public constructor(dataItem: DataItem) {
super(dataItem, EvmDataTypeFactory.getInstance());
}
}
export class Method extends Impl.Method {
export class Method extends MethodDataType {
public constructor(abi: MethodAbi) {
super(abi, EvmDataTypeFactory.getInstance());
}
@@ -105,7 +115,7 @@ export class EvmDataTypeFactory implements DataTypeFactory {
} else if (String.matchType(dataItem.type)) {
dataType = new String(dataItem);
}
// @TODO: Implement Fixed/UFixed types
// @TODO: DataTypeement Fixed/UFixed types
if (_.isUndefined(dataType)) {
throw new Error(`Unrecognized data type: '${dataItem.type}'`);
} else if (!_.isUndefined(parentDataType) && !dataType.isStatic()) {

View File

@@ -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;
}

View File

@@ -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;

View File

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

View File

@@ -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;
}

View File

@@ -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';

View File

@@ -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);
}

View File

@@ -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 {

View File

@@ -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}>` };

View File

@@ -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 {

View File

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

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -1,2 +1,14 @@
export { EncodingRules, DecodingRules } from './utils/rules';
export * from './evm_data_type_factory';
export {
Address,
Array,
Bool,
DynamicBytes,
Int,
Method,
Pointer,
StaticBytes,
String,
Tuple,
UInt,
} from './evm_data_type_factory';