diff --git a/packages/web3-eth-abi/package-lock.json b/packages/web3-eth-abi/package-lock.json index 355210bc6a6..ce6c31fdac6 100644 --- a/packages/web3-eth-abi/package-lock.json +++ b/packages/web3-eth-abi/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@ethersproject/abi": { - "version": "5.0.0-beta.151", - "resolved": "https://registry.npmjs.org/@ethersproject/abi/-/abi-5.0.0-beta.151.tgz", - "integrity": "sha512-LHAr1KagDSdx2UUmnxyKRbMIUJofJbIQdnG6qB++AGS2sXngLUo2oEpjPCuSlZS616lmzbvwKQuoN9cqXO74ZQ==", + "version": "5.0.0-beta.152", + "resolved": "https://registry.npmjs.org/@ethersproject/abi/-/abi-5.0.0-beta.152.tgz", + "integrity": "sha512-+1+ukofyBYNJo8IdIK8lBENkEycgXW1ny1zWT7oYD8GKvczYYrgucRQnJPgt3u9MJYZdje4yxDQFoGd6d4BpRA==", "requires": { "@ethersproject/address": ">=5.0.0-beta.128", "@ethersproject/bignumber": ">=5.0.0-beta.130", @@ -81,14 +81,14 @@ } }, "@ethersproject/logger": { - "version": "5.0.0-beta.136", - "resolved": "https://registry.npmjs.org/@ethersproject/logger/-/logger-5.0.0-beta.136.tgz", - "integrity": "sha512-baWK/4ccsVcyUU20nhp7k+hoRYsiaOfURYlyvQCoUUFKD3mpSRQCH42wxCosZZSCWz4rTHgASLQDdKkBtNVz1w==" + "version": "5.0.0-beta.137", + "resolved": "https://registry.npmjs.org/@ethersproject/logger/-/logger-5.0.0-beta.137.tgz", + "integrity": "sha512-H36iMhWOY+tco1+o2NZUdQT8Gc6Y9795RSPgvluatvjvyt3X6mHtWXes4F8Rc5N/95px++a/ODYVSkSmlr68+A==" }, "@ethersproject/properties": { - "version": "5.0.0-beta.138", - "resolved": "https://registry.npmjs.org/@ethersproject/properties/-/properties-5.0.0-beta.138.tgz", - "integrity": "sha512-vLVftNTxonJ0SkkcMcpXHN9pABD84clh+Cz3TV79qvh+lc0MFX3dnYL1JinBdFnqLPXU9vw2LbTrBhwQY8bzCQ==", + "version": "5.0.0-beta.139", + "resolved": "https://registry.npmjs.org/@ethersproject/properties/-/properties-5.0.0-beta.139.tgz", + "integrity": "sha512-IFMEWxWkx8ATeGtY56pnHXK40eJgfO9UeEsK/24Qzwyl5jA5Lhz6OoElaW3UG5LLupHmLoRuLyuG1WNg2olC3A==", "requires": { "@ethersproject/logger": ">=5.0.0-beta.129" } diff --git a/packages/web3-eth-abi/package.json b/packages/web3-eth-abi/package.json index fe48b1d0b6f..f71f747a065 100644 --- a/packages/web3-eth-abi/package.json +++ b/packages/web3-eth-abi/package.json @@ -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" }, diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index 9aeca508b55..739c7b0a137 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -99,6 +99,9 @@ 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) @@ -106,6 +109,9 @@ ABICoder.prototype.encodeParameters = function (types, params) { 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) } @@ -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 } @@ -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) => { diff --git a/test/abi.encodeParameter.js b/test/abi.encodeParameter.js index 8eeeeb9b001..dae077e5567 100644 --- a/test/abi.encodeParameter.js +++ b/test/abi.encodeParameter.js @@ -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' + }); }); });