Merge pull request #1582 from 0xProject/fix/utils/abiDecodeNullAsFalse
Decode NULL as False in Abi Encoder
This commit is contained in:
commit
b2f35057a5
@ -1,4 +1,13 @@
|
||||
[
|
||||
{
|
||||
"version": "4.0.1",
|
||||
"changes": [
|
||||
{
|
||||
"note": "ABI Decode NULL as False",
|
||||
"pr": 1582
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"version": "4.0.0",
|
||||
"changes": [
|
||||
|
@ -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}`);
|
||||
}
|
||||
|
@ -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', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user