Style cleanup. Improved wording of some error messages.
This commit is contained in:
@@ -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';
|
||||
@@ -44,7 +43,10 @@ export abstract class Pointer extends DataType {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Disable prefer-function-over-method for inherited abstract method.
|
||||
/* tslint:disable prefer-function-over-method */
|
||||
public isStatic(): boolean {
|
||||
return true;
|
||||
}
|
||||
/* tslint:enable prefer-function-over-method */
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ export abstract class Set extends DataType {
|
||||
public generateValue(calldata: RawCalldata, rules: DecodingRules): any[] | object {
|
||||
let members = this._members;
|
||||
// Case 1: This is an array of undefined length, which means that `this._members` was not
|
||||
// populated in the constructor. So, construct the set of members it now.
|
||||
// populated in the constructor. So we must construct the set of members now.
|
||||
if (this._isArray && _.isUndefined(this._arrayLength)) {
|
||||
const arrayLengthBuf = calldata.popWord();
|
||||
const arrayLengthHex = ethUtil.bufferToHex(arrayLengthBuf);
|
||||
@@ -176,7 +176,7 @@ export abstract class Set extends DataType {
|
||||
private _createMembersWithKeys(dataItem: DataItem): [DataType[], MemberIndexByName] {
|
||||
// Sanity check
|
||||
if (_.isUndefined(dataItem.components)) {
|
||||
throw new Error(`Expected components`);
|
||||
throw new Error(`Tried to create a set using key/value pairs, but no components were defined by the input DataItem '${dataItem.name}'.`);
|
||||
}
|
||||
// Create one member for each component of `dataItem`
|
||||
const members: DataType[] = [];
|
||||
|
||||
@@ -59,7 +59,7 @@ export class Calldata {
|
||||
offset += block.getSizeInBytes();
|
||||
}
|
||||
// Generate hex string
|
||||
const hexString = this._rules.annotate ? this._toAnnotatedString() : this._toCondensedString();
|
||||
const hexString = this._rules.annotate ? this._toHumanReadableCallData() : this._toEvmCompatibeCallDataHex();
|
||||
return hexString;
|
||||
}
|
||||
/**
|
||||
@@ -131,10 +131,7 @@ export class Calldata {
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns EVM-compatible calldata as a Hex string.
|
||||
*/
|
||||
private _toCondensedString(): string {
|
||||
private _toEvmCompatibeCallDataHex(): string {
|
||||
// Sanity check: must have a root block.
|
||||
if (_.isUndefined(this._root)) {
|
||||
throw new Error('expected root');
|
||||
@@ -152,7 +149,7 @@ export class Calldata {
|
||||
return hexValue;
|
||||
}
|
||||
/**
|
||||
* Returns human-redable calldata.
|
||||
* Returns human-readable calldata.
|
||||
*
|
||||
* Example:
|
||||
* simpleFunction(string[], string[])
|
||||
@@ -173,7 +170,7 @@ export class Calldata {
|
||||
* 0xe0 0000000000000000000000000000000000000000000000000000000000000005 array2[1]
|
||||
* 0x100 576f726c64000000000000000000000000000000000000000000000000000000
|
||||
*/
|
||||
private _toAnnotatedString(): string {
|
||||
private _toHumanReadableCallData(): string {
|
||||
// Sanity check: must have a root block.
|
||||
if (_.isUndefined(this._root)) {
|
||||
throw new Error('expected root');
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
/* tslint:disable prefer-function-over-method */
|
||||
/* tslint:disable max-classes-per-file */
|
||||
/* tslint:disable no-construct */
|
||||
import { DataItem, MethodAbi } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
@@ -73,6 +71,7 @@ export class Method extends Impl.Method {
|
||||
}
|
||||
}
|
||||
|
||||
/* tslint:disable no-construct */
|
||||
export class EvmDataTypeFactory implements DataTypeFactory {
|
||||
private static _instance: DataTypeFactory;
|
||||
|
||||
@@ -83,6 +82,7 @@ export class EvmDataTypeFactory implements DataTypeFactory {
|
||||
return EvmDataTypeFactory._instance;
|
||||
}
|
||||
|
||||
/* tslint:disable prefer-function-over-method */
|
||||
public create(dataItem: DataItem, parentDataType?: DataType): DataType {
|
||||
// Create data type
|
||||
let dataType: undefined | DataType;
|
||||
@@ -114,6 +114,8 @@ export class EvmDataTypeFactory implements DataTypeFactory {
|
||||
}
|
||||
return dataType;
|
||||
}
|
||||
/* tslint:enable prefer-function-over-method */
|
||||
|
||||
private constructor() {}
|
||||
}
|
||||
/* tslint:enable no-construct */
|
||||
|
||||
@@ -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 */
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
}
|
||||
|
||||
@@ -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: <length><value>, 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 */
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
/* tslint:disable prefer-function-over-method */
|
||||
import { DataItem } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
|
||||
@@ -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: <length><value>, 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 */
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
/* tslint:disable prefer-function-over-method */
|
||||
import { DataItem } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
|
||||
@@ -79,8 +79,8 @@ export function decodeNumericValue(encodedValue: Buffer, minValue: BigNumber): B
|
||||
}
|
||||
// Case 2/3: value is non-negative because there is no leading 1 (encoded as two's-complement)
|
||||
const valueBin = value.toString(Constants.BIN_BASE);
|
||||
const valueIsNegative = valueBin.length === Constants.EVM_WORD_WIDTH_IN_BITS && _.startsWith(valueBin[0], '1');
|
||||
if (!valueIsNegative) {
|
||||
const isValueNegative = valueBin.length === Constants.EVM_WORD_WIDTH_IN_BITS && _.startsWith(valueBin[0], '1');
|
||||
if (!isValueNegative) {
|
||||
return value;
|
||||
}
|
||||
// Case 3/3: value is negative
|
||||
|
||||
Reference in New Issue
Block a user