diff options
author | Fabio B <kandinsky454@protonmail.ch> | 2019-02-05 18:32:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-05 18:32:00 +0800 |
commit | b2f35057a56faa82f31d8c5ef09249171dc130d2 (patch) | |
tree | 402260286abfc969ed62ba4bf83b237dfa2d377d /packages | |
parent | 74293d5bc844035781d65aa85051ef3bbb7b8b1a (diff) | |
parent | a3198b7df8a563768d68414b4f01485f0694ac0a (diff) | |
download | dexon-0x-contracts-b2f35057a56faa82f31d8c5ef09249171dc130d2.tar.gz dexon-0x-contracts-b2f35057a56faa82f31d8c5ef09249171dc130d2.tar.zst dexon-0x-contracts-b2f35057a56faa82f31d8c5ef09249171dc130d2.zip |
Merge pull request #1582 from 0xProject/fix/utils/abiDecodeNullAsFalse
Decode NULL as False in Abi Encoder
Diffstat (limited to 'packages')
-rw-r--r-- | packages/utils/CHANGELOG.json | 9 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/bool.ts | 3 | ||||
-rw-r--r-- | packages/utils/test/abi_encoder/evm_data_types_test.ts | 18 |
3 files changed, 29 insertions, 1 deletions
diff --git a/packages/utils/CHANGELOG.json b/packages/utils/CHANGELOG.json index 0fb199e58..df415040b 100644 --- a/packages/utils/CHANGELOG.json +++ b/packages/utils/CHANGELOG.json @@ -1,5 +1,14 @@ [ { + "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', () => { |