From db900d180cf7a5b9affbe7ff486b6e3e1e038e84 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Mon, 4 Feb 2019 16:25:00 -0800 Subject: Decode NULL as false --- packages/utils/CHANGELOG.json | 9 +++++++++ packages/utils/src/abi_encoder/evm_data_types/bool.ts | 3 ++- packages/utils/test/abi_encoder/evm_data_types_test.ts | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'packages') diff --git a/packages/utils/CHANGELOG.json b/packages/utils/CHANGELOG.json index 0fb199e58..d8c1bb9ce 100644 --- a/packages/utils/CHANGELOG.json +++ b/packages/utils/CHANGELOG.json @@ -1,4 +1,13 @@ [ + { + "version": "4.0.1", + "changes": [ + { + "note": "ABI Decode NULL as False" + "pr": 1582 + } + ] + }, { "version": "4.0.0", "changes": [ diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 23298bc88..ffccd6e53 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -36,7 +36,8 @@ export class BoolDataType extends AbstractBlobDataType { public decodeValue(calldata: RawCalldata): boolean { const valueBuf = calldata.popWord(); const valueHex = ethUtil.bufferToHex(valueBuf); - const valueNumber = new BigNumber(valueHex, constants.HEX_BASE); + // Hack @hysz: there are some cases where `false` is encoded as 0x instead of 0x0. + const valueNumber = valueHex === '0x' ? new BigNumber(0) : new BigNumber(valueHex, constants.HEX_BASE); if (!(valueNumber.isEqualTo(0) || valueNumber.isEqualTo(1))) { throw new Error(`Failed to decode boolean. Expected 0x0 or 0x1, got ${valueHex}`); } diff --git a/packages/utils/test/abi_encoder/evm_data_types_test.ts b/packages/utils/test/abi_encoder/evm_data_types_test.ts index 4814ce28b..c146890e0 100644 --- a/packages/utils/test/abi_encoder/evm_data_types_test.ts +++ b/packages/utils/test/abi_encoder/evm_data_types_test.ts @@ -489,6 +489,24 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const argsEncodedFromSignature = dataTypeFromSignature.encode(args); expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); + it('Null should decode as False', async () => { + // Hack @hysz: there are some cases where `false` is encoded as 0x instead of 0x0. + // Create DataType object + const testDataItem = { name: 'Boolean', type: 'bool' }; + const dataType = new AbiEncoder.Bool(testDataItem); + // Construct args to be encoded + const args = false; + // Encode Args and validate result + const encodedArgs = '0x'; + const expectedEncodedArgs = '0x0000000000000000000000000000000000000000000000000000000000000000'; + // Decode Encoded Args and validate result + const decodedArgs = dataType.decode(encodedArgs); + expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); + }); }); describe('Integer', () => { -- cgit