Repository navigation
fix: toHex() refuses a negative value instead of returning 0x-5 - #1767
Conversation
toHex(-5) returned '0x-5', which is not a hex string. toHex64(-5) and toStorageKey(-1) padded the sign into a 66-character string, so the length guard of toHex64 did not catch it, and addAddressPadding(-1) gave the same kind of value. They now throw. getHex() in typedData keeps hashing the same values: a negative number still throws, now with a clear message, and is not read as text.
PhilippeR26
left a comment
There was a problem hiding this comment.
Thanks, the fix is correct, and keeping '-1' from being hashed as text in getHex() was the right call.
One change before merging, see the suggestion in src/utils/num.ts. toHex() is called very often (calldata, hashes, RPC parameters), and the template literal passed to assert is built on every call, even for a valid value. So the decimal conversion of bn runs even when nothing is thrown. In a quick local measurement on a felt-sized value, toHex() went from about 115 ns to 420 ns per call. With a plain if, it stays at about 120 ns. The error and its message stay the same, so the tests do not change.
| export function toHex(value: BigNumberish): string { | ||
| return addHexPrefix(toBigInt(value).toString(16)); | ||
| const bn = toBigInt(value); | ||
| assert(bn >= 0n, `Invalid input: ${bn} is negative, a hex-string cannot hold a sign`); |
There was a problem hiding this comment.
A plain if instead of assert, so the message is only built when the value is negative:
| assert(bn >= 0n, `Invalid input: ${bn} is negative, a hex-string cannot hold a sign`); | |
| if (bn < 0n) { | |
| throw new Error(`Invalid input: ${bn} is negative, a hex-string cannot hold a sign`); | |
| } |
There was a problem hiding this comment.
Done in d4b6dee: a plain if, same error and message, so the tests stay as they are.
## [11.9.3](v11.9.2...v11.9.3) (2026-11-26) ### Bug Fixes * toHex() refuses a negative value instead of returning 0x-5 ([#1767](#1767)) ([e2036f1](e2036f1))
|
🎉 This PR is included in version 11.9.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Motivation and Resolution
toHex()turns a negative value into a string that is not hex:Expected: an error. A hex string (and a felt) has no sign.
toHex64even has a length guard, but the sign is padded into the 64 characters, so the bad value has length 66 and passes.toStorageKeyis used bygetStorageAt, so the bad key would only fail later at the node.Cause:
src/utils/num.tstoHex()doesaddHexPrefix(toBigInt(value).toString(16)), and(-5n).toString(16)is'-5'.toHex64()andtoStorageKey()do the sametoString(16)on their own.Fix:
toHex()throwsInvalid input: -5 is negative, a hex-string cannot hold a sign.toHex64()andtoStorageKey()now build ontoHex(), so they throw too.addAddressPadding()already usedtoHex().getHex()intypedData.tscatches the error oftoHex()and then reads a string as text. Without care,'-1'would now be hashed as the text "-1". So it now only falls back to text whentoBigInt()fails, and a negative number still throws (before:Cannot convert 0x-1 to a BigInt). I checked revision 0 and 1 messages with felt, u128, i128, bool and ContractAddress fields and values-1,'-1',-1n,'-0x1','-abc': every hash is the same as before, only the error text changed.RPC version (if applicable)
N/A
Usage related changes
num.toHex,num.toHex64,num.toStorageKey,num.cleanHex,addAddressPadding(and sogetStorageAtkeys) throw for a negative value instead of returning a string with a-in it.Development related changes
__tests__/utils/num.test.ts(toHex,toStorageKey,toHex64) and__tests__/utils/address.test.ts(addAddressPadding). The 4 changed tests fail ondevelopand pass with this change.Checklist:
num)