diff --git a/CHANGELOG.md b/CHANGELOG.md index bf85a36c97a..cebe5e44a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index a3b4c02c4d1..7587566172e 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -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 @@ -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)) } @@ -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) } } diff --git a/test/abi.encodeParameter.js b/test/abi.encodeParameter.js index 5cdb9b7e5b2..a27f3018ec0 100644 --- a/test/abi.encodeParameter.js +++ b/test/abi.encodeParameter.js @@ -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' + }); }); });