From 47b9769d99c70ea985a5e639c9ca37ebb5c7cde9 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Thu, 29 Jun 2023 09:21:36 -1000 Subject: [PATCH] Fix for ABI encoding large negative ints (#6239) * Bump typescript version to 4.9.5 * Update CHANGELOG * Bump ts-node to 10.9.1 * Fix issue with encoding large negative numbers * Correct test cases for check-address-checksum-test * dtslint debug * Convert if/else to ternary * Update CHANGELOG --- CHANGELOG.md | 1 + packages/web3-eth-abi/src/index.js | 7 +++++-- test/eth.abi.encodeParameter.js | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce57b90601a..b229ca1fe97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -685,3 +685,4 @@ Released with 1.0.0-beta.37 code base. ### Fixed - Builds fixed by updating all typescript versions to 4.9.5 (#6238) +- ABI encoding for large negative `int`s (#6239) diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index 600b993c289..3bb6e797c6f 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -268,8 +268,11 @@ ABICoder.prototype.formatParam = function (type, param) { if (match) { let size = parseInt(match[2] || "256"); if (size / 8 < param.length) { - // pad to correct bit width - param = utils.leftPad(param, size); + param = param.startsWith("-") + // pad to correct bit width, with - at the beginning + ? `-${utils.leftPad(param.substring(1), size)}` + // pad to correct bit width + : utils.leftPad(param, size); } } diff --git a/test/eth.abi.encodeParameter.js b/test/eth.abi.encodeParameter.js index 4ba9548fd05..eec17f11fc1 100644 --- a/test/eth.abi.encodeParameter.js +++ b/test/eth.abi.encodeParameter.js @@ -11,6 +11,9 @@ var tests = [{ },{ params: ['bytes32[]', ['0xdf3234', '0xfdfd']], result: '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000' +},{ + params: ['int128', '-170141183460469231731687303715884105727'], + result: '0xffffffffffffffffffffffffffffffff80000000000000000000000000000001' }]; describe('encodeParameter', function () {