diff options
Diffstat (limited to 'packages/utils/src')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_type_factory.ts | 23 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/index.ts | 3 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/utils/signatureParser.ts | 63 |
3 files changed, 34 insertions, 55 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_type_factory.ts b/packages/utils/src/abi_encoder/evm_data_type_factory.ts index 4cc124e0a..5ff7366d6 100644 --- a/packages/utils/src/abi_encoder/evm_data_type_factory.ts +++ b/packages/utils/src/abi_encoder/evm_data_type_factory.ts @@ -2,6 +2,8 @@ import { DataItem, MethodAbi } from 'ethereum-types'; import * as _ from 'lodash'; +import { generateDataItemsFromSignature } from './utils/signatureParser'; + import { DataType } from './abstract_data_types/data_type'; import { DataTypeFactory } from './abstract_data_types/interfaces'; import { AddressDataType } from './evm_data_types/address'; @@ -129,4 +131,25 @@ export class EvmDataTypeFactory implements DataTypeFactory { private constructor() {} } + +// Convenience function +export function create(input: DataItem | DataItem[] | string): DataType { + // Handle different types of input + let dataItems: DataItem[] = []; + if (typeof(input) === 'string') { + dataItems = generateDataItemsFromSignature(input); + } else if(input instanceof Array) { + dataItems = input as DataItem[]; + } else { + dataItems = [input as DataItem]; + } + // Create single data item from input + let dataItem: DataItem = dataItems.length === 1 ? dataItems[0] : { + name: '', + type: 'tuple', + components: dataItems + }; + // Create data type + return EvmDataTypeFactory.getInstance().create(dataItem); +} /* tslint:enable no-construct */ diff --git a/packages/utils/src/abi_encoder/index.ts b/packages/utils/src/abi_encoder/index.ts index 23422dd8b..f7f1ceedc 100644 --- a/packages/utils/src/abi_encoder/index.ts +++ b/packages/utils/src/abi_encoder/index.ts @@ -11,5 +11,6 @@ export { String, Tuple, UInt, + create, } from './evm_data_type_factory'; -export { fromSignature } from './utils/signatureParser'; +//export { fromSignature } from './utils/signatureParser'; diff --git a/packages/utils/src/abi_encoder/utils/signatureParser.ts b/packages/utils/src/abi_encoder/utils/signatureParser.ts index fe5127032..9c1a59c82 100644 --- a/packages/utils/src/abi_encoder/utils/signatureParser.ts +++ b/packages/utils/src/abi_encoder/utils/signatureParser.ts @@ -1,67 +1,22 @@ import * as _ from 'lodash'; -import { DataType } from '../abstract_data_types/data_type'; import { DataItem } from 'ethereum-protocol'; -import { MethodAbi } from 'ethereum-types'; -import * as EvmDataTypes from '../evm_data_type_factory'; -// Valid signatures: -// functionName(param1, param2, ...): (output1, output2, ...) -// functionName(param1, param2, ...) -// (param1, param2, ...) /* -export function fromSignature(signature: string): DataType { - const maxSignatureIndex = signature.length - 1; - // Function name - const isFunction = signature.startsWith('function '); - // Output components - const outputComponentsBeginIndex = signature.indexOf(':'); - const outputComponentsEndIndex = outputComponentsBeginIndex >= 0 ? maxSignatureIndex : 0; - const hasOutputComponents = outputComponentsBeginIndex >= 0; - const outputComponentsSignature = hasOutputComponents ? signature.substring(outputComponentsBeginIndex, outputComponentsEndIndex + 1) : ""; - // Input components - const inputComponentsBeginIndex = signature.indexOf('('); - const inputComponentsEndIndex = hasOutputComponents ? outputComponentsBeginIndex : maxSignatureIndex; - const inputComponentsSignature = signature.substring(inputComponentsBeginIndex, inputComponentsEndIndex + 1); - // Function anme - const functionName = signature.substr(0, inputComponentsBeginIndex); - const isFunction = !_.isEmpty(functionName); - - console.log(`sig - ` + inputComponentsSignature); - // Create data type - let dataType: DataType; - if (isFunction) { - const methodAbi = { - type: 'function', - name: functionName, - inputs: generateDataItems(inputComponentsSignature), - outputs: !_.isEmpty(outputComponentsSignature) ? generateDataItems(outputComponentsSignature) : [], - } as MethodAbi; - dataType = new EvmDataTypes.Method(methodAbi); - } else if(hasOutputComponents) { - throw new Error(`Invalid signature: Contains outputs but no function name.`); - } else { - const inputDataItem = generateDataItem(inputComponentsSignature); - console.log(JSON.stringify(inputDataItem)); - dataType = EvmDataTypes.EvmDataTypeFactory.getInstance().create(inputDataItem); - } - return dataType; -}*/ - -export function fromSignature(signature: string): DataType { - const dataItems = generateDataItems(signature); +export function generateDataItemFromSignature(signature: string): DataItem { + const dataItems = generateDataItemsFromSignature(signature); if (dataItems.length === 1) { - return EvmDataTypes.EvmDataTypeFactory.getInstance().create(dataItems[0]); + return dataItems[0]; } - // this is a tuple - return EvmDataTypes.EvmDataTypeFactory.getInstance().create({ + // signature represents a tuple + return { name: '', type: 'tuple', components: dataItems - }); -} + }; +}*/ -function generateDataItems(signature: string): DataItem[] { +export function generateDataItemsFromSignature(signature: string): DataItem[] { let trimmedSignature = signature; if (signature.startsWith('(')) { if(!signature.endsWith(')')) { @@ -116,7 +71,7 @@ function generateDataItems(signature: string): DataItem[] { case ',': if (parenCount === 0) { //throw new Error(`Generating Data Items`); - const components = currToken.startsWith('(') ? generateDataItems(currToken) : []; + const components = currToken.startsWith('(') ? generateDataItemsFromSignature(currToken) : []; const isTuple = !_.isEmpty(components); const isArray = currTokenIsArray; let dataItem: DataItem = {name: currTokenName, type: ''}; |