Skip to content

Commit

Permalink
Handle Buffer in encodeParameters and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed May 4, 2020
1 parent 3af318e commit 4b08c04
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
18 changes: 9 additions & 9 deletions packages/web3-eth-abi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/web3-eth-abi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"main": "src/index.js",
"dependencies": {
"@ethersproject/abi": "5.0.0-beta.151",
"@ethersproject/abi": "5.0.0-beta.152",
"underscore": "1.9.1",
"web3-utils": "1.2.7"
},
Expand Down
12 changes: 10 additions & 2 deletions packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,19 @@ ABICoder.prototype.encodeParameter = function (type, param) {
*/
ABICoder.prototype.encodeParameters = function (types, params) {
const bytesToEvenLength = (param) => {
if (Buffer.isBuffer(param)) {
param = utils.toHex(param)
}
// bitwise AND operator returns true if odd
if (param.length & 1) {
return '0x0' + param.substring(2)
}
return param
}
const bytes32ToFixedLength = (param) => {
if (Buffer.isBuffer(param)) {
param = utils.toHex(param)
}
if (param.length < 66) { // 66 includes `0x`
return utils.rightPad(param, 64)
}
Expand All @@ -116,7 +122,7 @@ ABICoder.prototype.encodeParameters = function (types, params) {
this.mapTypes(types),
params.map(function (param, index) {
let type = types[index]
if (typeof type === 'object') {
if (typeof type === 'object' && type.type) {
// We may get a named type of shape {name, type}
type = type.type
}
Expand All @@ -126,19 +132,21 @@ ABICoder.prototype.encodeParameters = function (types, params) {
return param.toString(10);
}

// Convert odd-length bytes to even
if (type === 'bytes') {
param = bytesToEvenLength(param)
} else if (type === 'bytes[]') {
param = param.map(p => bytesToEvenLength(p))
}

// Convert bytes32 to fixed length
if (type === 'bytes32') {
param = bytes32ToFixedLength(param)
} else if (type === 'bytes32[]') {
param = param.map(p => bytes32ToFixedLength(p))
}

// Format tuples for above bytes and bytes32 rules
// Format tuples for above rules
if (typeof type === 'string' && type.includes('tuple')) {
const coder = ethersAbiCoder._getCoder(ParamType.from(type));
const modifyParams = (coder, param) => {
Expand Down
5 changes: 5 additions & 0 deletions test/abi.encodeParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,11 @@ describe('lib/solidity/coder', function () {
values: [new BigNumber(42), new BigNumber(42)],
expected: '000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000002a'
});
test({
types: ['bytes', 'bytes32'],
values: [Buffer.from('cool'), Buffer.from('beans')],
expected: '00000000000000000000000000000000000000000000000000000000000000406265616e730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004636f6f6c00000000000000000000000000000000000000000000000000000000'
});
});
});

Expand Down

0 comments on commit 4b08c04

Please sign in to comment.