Repository navigation
eth: parse EIP-712 JSON integers exactly - #199
benma-agent wants to merge 2 commits into
Conversation
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)
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
d83e642 to
7226bae
Compare
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
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
7226bae to
a7df865
Compare
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)
a7df865 to
d6ad3c0
Compare
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
| 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| {"int64", "-1.28e2", "80"}, | ||
| {"uint64", "1e3", "03e8"}, | ||
| // Match JavaScript number semantics after JSON decoding. | ||
| {"uint64", "1.0000000000000001", "01"}, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
That case now asserts rejection. Exact parsing no longer rounds 1.0000000000000001 to 1.
| for _, value := range []float64{ | ||
| 128.5, -128.5, math.NaN(), math.Inf(1), math.Inf(-1), | ||
| 9007199254740992, -9007199254740992, | ||
| 9007199254740993, -9007199254740993, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Replaced the float literals with raw-JSON regression cases. 9007199254740992 and 9007199254740993 now exercise distinct, exactly preserved values.
|
One nit and minor review finding, but I would re-visit the strict parsing rejection (dug into it with Neon) |
d6ad3c0 to
8f52a1a
Compare
Preserve numeric tokens with UseNumber and parse integral decimal and exponent values exactly. Reject fractions and keep existing string syntax.
8f52a1a to
5ae2b89
Compare
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: