Skip to content

eth: parse EIP-712 JSON integers exactly - #199

Open
benma-agent wants to merge 2 commits into
BitBoxSwiss:masterfrom
benma-agent:benma-agent/eip712-exact-json-integers
Open

benma-agent wants to merge 2 commits into
BitBoxSwiss:masterfrom
benma-agent:benma-agent/eip712-exact-json-integers

Conversation

@benma-agent

@benma-agent benma-agent commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Decode typed-message numbers without converting them to float64 so large
integers retain every digit. Accept integral decimal and exponent forms,
including 128.0 and 1e18, and reject fractional values instead of rounding
them. Integer strings keep their existing parsing behavior.

Related:

benma-agent added a commit to benma-agent/bitbox-api-ts that referenced this pull request Nov 29, 2026
JavaScript can round integer values before they reach the EIP-712 encoder. Checking only
integrality accepts those rounded values without warning.

Require safe integers for number inputs. Keep decimal strings and bigint inputs for exact
values outside ±(2^53 - 1), and document that converting an already-rounded number cannot
restore the original digits.

Related:

- [bitbox02-api-go #199](BitBoxSwiss/bitbox02-api-go#199)
- [bitbox-api-rs #141](BitBoxSwiss/bitbox-api-rs#141)
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
Converting JSON numbers through f64 can round large integers or turn
fractional values into integers before EIP-712 signing.

Preserve the JSON digits and convert integral decimal and exponent forms
exactly, including values beyond u64. Reject fractions without rounding,
and bound exponent expansion to the 256-bit integer range.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from d83e642 to 7226bae Compare November 29, 2026 08:20
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
Converting JSON numbers through f64 can round large integers or turn
fractional values into integers before EIP-712 signing.

Parse JSON numbers with BigDecimal and convert integral values exactly,
including decimal and exponent notation and integers beyond u64. Reject
fractions without rounding and bound values before integer conversion.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
Converting JSON numbers through f64 can round large integers or turn
fractional values into integers before EIP-712 signing.

Preserve exact JSON integer tokens and parse them directly into big integers
up to 256 bits. Numeric inputs now require integer notation; decimal and
exponent forms such as 128.0 and 1e3 are rejected.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from 7226bae to a7df865 Compare November 29, 2026 08:24
Require decoded numeric values to be integers within the JavaScript
safe-integer range, matching the TypeScript client. Decimal and exponent
notation remains accepted when it decodes to a safe integer. Use decimal
strings for larger values.

Related:

- [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141)
- [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from a7df865 to d6ad3c0 Compare November 29, 2026 08:26
@benma-agent benma-agent changed the title eth: preserve exact EIP-712 JSON integers eth: reject unsafe EIP-712 numbers Nov 29, 2026
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
EIP-712 numeric values outside the safe integer range can lose precision
when decoded as floating point.

Reject numeric values outside ±(2^53 - 1), requiring decimal strings for
larger integers. Keep integral numeric forms such as 128.0 and 1e3, and
parse JSON floats with round-trip precision at the safe-range boundaries.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
@benma
benma requested a review from Tomasvrba November 29, 2026 08:30
@benma
benma marked this pull request as ready for review November 29, 2026 08:30
Comment thread api/firmware/eth.go Outdated
v64 := uint64(v)
if float64(v64) != v {
return nil, errp.Newf("float64 is not an uint: %v", v)
if v < 0 || v > maxSafeInteger || float64(v64) != v {

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.

Rejecting JSON numbers above 2^53 - 1 will break valid signing requests that use large round numbers like 1e18 (1000000000000000000, i.e. 1 ETH in wei). MetaMask (eth-sig-util), Rabby, viem, and go-ethereum all accept 1e18, and previous versions of this library did too.

The strict parsing exists in ethersjs for developers building dApps, so throwing on 1e18 catches mistakes during development.
Every major wallet verifier accepts 1e18 as a number. If we reject it, the user's experience is simply: "this dApp works in MetaMask and Rabby's software wallet, and used to work on my BitBox02, and now fails with an error."

Because ETHSignTypedMessage receives raw JSON bytes (jsonMsg []byte), Go doesn't actually have JavaScript's 2^53 - 1 limit — the precision loss only happens because json.Unmarshal converts numbers into float64. If we decode with json.Decoder.UseNumber() and parse exact integer tokens directly into big.Int, we can preserve every digit accurately (even for 9007199254740993), reject true decimals like 1.0000000000000001, and stay compatible with dApps that send 1e18.

Both Go (ETHSignTypedMessage(..., jsonMsg []byte, ...)) and Rust (eth_sign_typed_message(..., json_msg: &str, ...)) receive raw JSON text — they have every exact digit in front of them.

Instead of fixing their own lossy float64 decoding, go#199 and rs#141 keep decoding through float64 (which still silently turns 1.0000000000000001 into 1) and then reject valid JSON integers above 2^53 - 1 because float64 can't hold them accurately. That's backwards for a Go or Rust library.

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.

Updated in 5ae2b89: UseNumber() plus big.Rat parsing preserves exact large integers and accepts 1e18 and 128.0 without floating-point conversion. Fractional values are rejected.

Comment thread api/firmware/eth_test.go Outdated
{"int64", "-1.28e2", "80"},
{"uint64", "1e3", "03e8"},
// Match JavaScript number semantics after JSON decoding.
{"uint64", "1.0000000000000001", "01"},

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.

This new test locks in a floating-point rounding quirk ("1.0000000000000001" silently turning into 1) as expected behavior. If we switch to exact JSON number parsing later, we'd want to reject fractional inputs like this rather than round them down

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.

That case now asserts rejection. Exact parsing no longer rounds 1.0000000000000001 to 1.

Comment thread api/firmware/eth_test.go Outdated
for _, value := range []float64{
128.5, -128.5, math.NaN(), math.Inf(1), math.Inf(-1),
9007199254740992, -9007199254740992,
9007199254740993, -9007199254740993,

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.

Nit: In a Go []float64 slice, 9007199254740993 is rounded by the compiler to 9007199254740992 before the test runs, so line 316 tests the exact same float values as line 315. To test raw JSON text like "9007199254740993", it would need to go through json.Unmarshal like the test above; otherwise line 316 can be removed.

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.

Replaced the float literals with raw-JSON regression cases. 9007199254740992 and 9007199254740993 now exercise distinct, exactly preserved values.

@Tomasvrba

Copy link
Copy Markdown
Collaborator

One nit and minor review finding, but I would re-visit the strict parsing rejection (dug into it with Neon)

@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from d6ad3c0 to 8f52a1a Compare November 30, 2026 07:46
@benma-agent benma-agent changed the title eth: reject unsafe EIP-712 numbers eth: parse EIP-712 JSON integers exactly Nov 30, 2026
Preserve numeric tokens with UseNumber and parse integral decimal and
exponent values exactly. Reject fractions and keep existing string syntax.
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from 8f52a1a to 5ae2b89 Compare November 30, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants