From aa471e7d4f749baa63f1d16df6f0c9da725780f4 Mon Sep 17 00:00:00 2001 From: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:07:09 +0200 Subject: [PATCH] fix padRight validation failure on large uint (#7265) * fix padRight validation failure on large uint * update CHANGELOG.md --- packages/web3-utils/CHANGELOG.md | 4 ++++ packages/web3-utils/src/string_manipulation.ts | 6 +++--- packages/web3-utils/test/fixtures/string_manipulation.ts | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/web3-utils/CHANGELOG.md b/packages/web3-utils/CHANGELOG.md index 8f25c4c6b20..45439db666e 100644 --- a/packages/web3-utils/CHANGELOG.md +++ b/packages/web3-utils/CHANGELOG.md @@ -235,3 +235,7 @@ Documentation: - `_sendPendingRequests` will catch unhandled errors from `_sendToSocket` (#6968) ## [Unreleased] + +### Fixed + +- fix `padRight` validation failure on large `uint` (#7265) diff --git a/packages/web3-utils/src/string_manipulation.ts b/packages/web3-utils/src/string_manipulation.ts index 88a0a9a5534..37e82b04b3a 100644 --- a/packages/web3-utils/src/string_manipulation.ts +++ b/packages/web3-utils/src/string_manipulation.ts @@ -75,11 +75,11 @@ export const padRight = (value: Numbers, characterAmount: number, sign = '0'): s return value.padEnd(characterAmount, sign); } - validator.validate(['int'], [value]); - const hexString = typeof value === 'string' && isHexStrict(value) ? value : numberToHex(value); - const prefixLength = hexString.startsWith('-') ? 3 : 2; + + validator.validate([hexString.startsWith('-') ? 'int' : 'uint'], [value]); + return hexString.padEnd(characterAmount + prefixLength, sign); }; diff --git a/packages/web3-utils/test/fixtures/string_manipulation.ts b/packages/web3-utils/test/fixtures/string_manipulation.ts index dde49247677..77cbdfac8e9 100644 --- a/packages/web3-utils/test/fixtures/string_manipulation.ts +++ b/packages/web3-utils/test/fixtures/string_manipulation.ts @@ -56,6 +56,14 @@ export const padRightData: [[Numbers, number, string], HexString][] = [ [[BigInt(10000), 8, '0'], '0x27100000'], [[BigInt(-14), 8, '0'], '-0xe0000000'], [['15.5', 8, '0'], '15.50000'], + [ + ['0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64, '0'], + '0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', + ], + [ + ['0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64, '0'], + '0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', + ], ]; export const toTwosComplementData: [[Numbers, number | undefined], HexString][] = [