From ee00769be171531db725288f8d485964941af529 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 5 Sep 2017 15:29:29 +0200 Subject: Use schema validation to distinguish txData argument --- src/contract.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/contract.ts') diff --git a/src/contract.ts b/src/contract.ts index 26da67b62..bd5afc16c 100644 --- a/src/contract.ts +++ b/src/contract.ts @@ -1,12 +1,14 @@ import * as Web3 from 'web3'; import * as _ from 'lodash'; import promisify = require('es6-promisify'); +import {SchemaValidator, schemas} from '0x-json-schemas'; export class Contract implements Web3.ContractInstance { public address: string; public abi: Web3.ContractAbi; private contract: Web3.ContractInstance; private defaults: Partial; + private validator: SchemaValidator; // This class instance is going to be populated with functions and events depending on the ABI // and we don't know their types in advance [name: string]: any; @@ -17,6 +19,7 @@ export class Contract implements Web3.ContractInstance { this.defaults = defaults; this.populateEvents(); this.populateFunctions(); + this.validator = new SchemaValidator(); } private populateFunctions(): void { const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === 'function'); @@ -47,7 +50,7 @@ export class Contract implements Web3.ContractInstance { const promise = new Promise((resolve, reject) => { const lastArg = args[args.length - 1]; let txData: Partial = {}; - if (_.isObject(lastArg) && !_.isArray(lastArg) && !lastArg.isBigNumber) { + if (this.isTxData(lastArg)) { txData = args.pop(); } txData = { @@ -69,4 +72,8 @@ export class Contract implements Web3.ContractInstance { }; return promisifiedWithDefaultParams; } + private isTxData(lastArg: any): boolean { + const isValid = this.validator.isValid(lastArg, schemas.txDataSchema); + return isValid; + } } -- cgit v1.2.3