Skip to content

fix: toHex() refuses a negative value instead of returning 0x-5 - #1767

Merged
PhilippeR26 merged 2 commits into
starknet-io:developfrom
bardrop-oss:fix-tohex-negative
Nov 26, 2026
Merged

PhilippeR26 merged 2 commits into
starknet-io:developfrom
bardrop-oss:fix-tohex-negative

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Motivation and Resolution

toHex() turns a negative value into a string that is not hex:

const { num, addAddressPadding } = require('starknet');

num.toHex(-5);              // '0x-5'
num.toHex64(-5);            // '0x00000000000000000000000000000000000000000000000000000000000000-5'
num.toStorageKey(-1);       // '0x00000000000000000000000000000000000000000000000000000000000000-1'
addAddressPadding(-1);      // '0x00000000000000000000000000000000000000000000000000000000000000-1'
num.isHex(num.toHex(-5));   // false

Expected: an error. A hex string (and a felt) has no sign. toHex64 even has a length guard, but the sign is padded into the 64 characters, so the bad value has length 66 and passes. toStorageKey is used by getStorageAt, so the bad key would only fail later at the node.

Cause: src/utils/num.ts toHex() does addHexPrefix(toBigInt(value).toString(16)), and (-5n).toString(16) is '-5'. toHex64() and toStorageKey() do the same toString(16) on their own.

Fix:

  • toHex() throws Invalid input: -5 is negative, a hex-string cannot hold a sign.
  • toHex64() and toStorageKey() now build on toHex(), so they throw too. addAddressPadding() already used toHex().
  • getHex() in typedData.ts catches the error of toHex() 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 when toBigInt() 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 so getStorageAt keys) throw for a negative value instead of returning a string with a - in it.

Development related changes

  • New cases in __tests__/utils/num.test.ts (toHex, toStorageKey, toHex64) and __tests__/utils/address.test.ts (addAddressPadding). The 4 changed tests fail on develop and pass with this change.

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Linked the issues which this PR resolves (no issue, found while testing num)
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing (unit config: 97 suites / 2402 tests, was 2400; lint and ts:check clean; devnet tests not run locally)

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 PhilippeR26 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utils/num.ts Outdated
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`);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A plain if instead of assert, so the message is only built when the value is negative:

Suggested change
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`);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d4b6dee: a plain if, same error and message, so the tests stay as they are.

@PhilippeR26
PhilippeR26 merged commit e2036f1 into starknet-io:develop Nov 26, 2026
9 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 26, 2026
## [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))
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 11.9.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants