Skip to content

Commit

Permalink
Merge pull request #3593 from ethereum/issue/3592
Browse files Browse the repository at this point in the history
Fix param encoding for BN in arrays
  • Loading branch information
ryanio authored Jun 19, 2020
2 parents db82053 + c114e31 commit aebe49f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,4 @@ Released with 1.0.0-beta.37 code base.

- Extend `_txInputFormatter` with hex prefix check (#3317)
- Extract revert reason string for geth >= 1.9.15 (#3520)
- Incorrect param encoding of BN object in arrayed inputs (#3592)
14 changes: 7 additions & 7 deletions packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ ABICoder.prototype.encodeParameters = function (types, params) {
type = type.type
}

// Format BN to string
if (utils.isBN(param) || utils.isBigNumber(param)) {
return param.toString(10);
}

param = self.formatParam(type, param)

// Format params for tuples
Expand Down Expand Up @@ -252,6 +247,11 @@ ABICoder.prototype.formatParam = function (type, param) {
const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
const paramTypeNumberArray = new RegExp(/^(u?int)([0-9]*)\[\]$/);

// Format BN to string
if (utils.isBN(param) || utils.isBigNumber(param)) {
return param.toString(10);
}

if (type.match(paramTypeBytesArray) || type.match(paramTypeNumberArray)) {
return param.map(p => this.formatParam(type.replace('[]', ''), p))
}
Expand Down Expand Up @@ -285,9 +285,9 @@ ABICoder.prototype.formatParam = function (type, param) {
param = utils.rightPad(param, size * 2)
}
}

// format odd-length bytes to even-length
if (param.length % 2 === 1) {
if (param.length % 2 === 1) {
param = '0x0' + param.substring(2)
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/abi.encodeParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,35 @@ describe('lib/solidity/coder', function () {
value: new BigNumber(42),
expected: '000000000000000000000000000000000000000000000000000000000000002a'
});
test({
type: 'uint256[]',
value: ['42'],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'000000000000000000000000000000000000000000000000000000000000002a'
});
test({
type: 'uint256[]',
value: [new BN(42)],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'000000000000000000000000000000000000000000000000000000000000002a'
});
test({
type: 'uint256[]',
value: [new BigNumber(42)],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'000000000000000000000000000000000000000000000000000000000000002a'
});
test({
type: 'uint256[]',
value: [new BN(42), new BN(42)],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'000000000000000000000000000000000000000000000000000000000000002a' +
'000000000000000000000000000000000000000000000000000000000000002a'
});
});
});

Expand Down

0 comments on commit aebe49f

Please sign in to comment.