diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-07-27 05:11:03 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-08-09 05:27:30 +0800 |
commit | 6a5965d73bb542634631d7af76c150795d899744 (patch) | |
tree | aba6597779094b9738c0ad94fb2694a18d22cdc2 /packages/base-contract/src | |
parent | 19cda0eb036b6876964d8074aea1295142d25027 (diff) | |
download | dexon-sol-tools-6a5965d73bb542634631d7af76c150795d899744.tar.gz dexon-sol-tools-6a5965d73bb542634631d7af76c150795d899744.tar.zst dexon-sol-tools-6a5965d73bb542634631d7af76c150795d899744.zip |
Add strictArgumentEncodingCheck to BaseContract and use it in contract templates
Diffstat (limited to 'packages/base-contract/src')
-rw-r--r-- | packages/base-contract/src/index.ts | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts index a240fb8b6..e354f109e 100644 --- a/packages/base-contract/src/index.ts +++ b/packages/base-contract/src/index.ts @@ -82,6 +82,23 @@ export class BaseContract { } return txDataWithDefaults; } + // Throws if the given arguments cannot be safely/correctly encoded based on + // the given inputAbi. An argument may not be considered safely encodeable + // if it overflows the corresponding Solidity type, there is a bug in the + // encoder, or the encoder performs unsafe type coercion. + public static strictArgumentEncodingCheck(inputAbi: DataItem[], args: any[]): void { + const coder = (ethers as any).utils.AbiCoder.defaultCoder; + const params = abiUtils.parseEthersParams(inputAbi); + const rawEncoded = coder.encode(params.names, params.types, args); + const rawDecoded = coder.decode(params.names, params.types, rawEncoded); + for (let i = 0; i < rawDecoded.length; i++) { + const original = args[i]; + const decoded = rawDecoded[i]; + if (!abiUtils.isAbiDataEqual(params.names[i], params.types[i], original, decoded)) { + throw new Error(`Cannot safely encode argument: ${params.names[i]} (${original}) of type ${params.types[i]}. (Possible type overflow or other encoding error)`); + } + } + } protected _lookupEthersInterface(functionSignature: string): ethers.Interface { const ethersInterface = this._ethersInterfacesByFunctionSignature[functionSignature]; if (_.isUndefined(ethersInterface)) { |