Repository navigation
eth: preserve EIP-712 integer literals - #141
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)
5fce867 to
5dcd801
Compare
Decode JSON numbers as exact text so values such as 9007199254740993 are not rounded before encoding. Use math/big's rational parser to accept integral decimal and exponent forms, rejecting fractions and values outside the 256-bit range. Related: - [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141) - [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
6091e6d to
51d846f
Compare
Decode JSON numbers as exact text so values such as 9007199254740993 are not rounded before encoding. Parse integer notation directly with math/big, rejecting decimal points, exponents, and values outside 256 bits. Related: - [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141) - [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
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)
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
51d846f to
154370b
Compare
Tomasvrba
left a comment
There was a problem hiding this comment.
Same design concern as the go api, plus some minor nits
| } | ||
|
|
||
| fn encode_value(typ: &MemberType, value: &Value) -> Result<Vec<u8>, String> { | ||
| const MAX_SAFE_INTEGER: f64 = ((1u64 << 53) - 1) as f64; |
There was a problem hiding this comment.
same concern as BitBoxSwiss/bitbox02-api-go#199 (comment)
rejecting JSON numbers above 2^53 - 1 breaks valid payloads with round numbers like 1e18 that MetaMask, Rabby, viem, and go-ethereum accept.
Because eth_sign_typed_message receives the raw JSON string (json_msg: &str), serde_json::Number already stores exact 64-bit integers (n.as_u64() / n.as_i64()). Checking as_u64() / as_i64() first (before falling back to as_f64() for forms like 1e18 or 128.0) fixes both the u64::MAX rounding bug and precision loss on 64-bit integers without breaking 1e18 compatibility.
There was a problem hiding this comment.
Updated in 78a8e50: integer accessors run first, preserving 64-bit integer literals exactly. 1e18 and 128.0 retain the float fallback with explicit conversion bounds; its remaining rounding limitation is documented.
| let value: Value = serde_json::from_str(number).unwrap(); | ||
| assert!(encode_value(&typ, &value).is_err(), "{number}"); | ||
| } | ||
| for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { |
There was a problem hiding this comment.
In serde_json, Value::from(f64::NAN) and Value::from(f64::INFINITY) produce Value::Null (not Value::Number), because JSON has no NaN or Infinity. So this loop only hits the "Wrong type for uint/int" fallback branch 6 times and never enters the number validation code. Since a serde_json::Number can't hold NaN or Infinity anyway, this loop can be removed.
There was a problem hiding this comment.
Removed the loop; those values only exercised Null rejection.
| /// Signs an Ethereum EIP-712 typed message. It returns a 65 byte signature (R, S, and 1 byte | ||
| /// recID). 27 is added to the recID to denote an uncompressed pubkey. | ||
| /// If `use_antiklepto` is false, signing is deterministic and requires firmware >=9.26.0. | ||
| /// Integer fields accept JSON numbers only when finite, integral and within `±(2^53 - 1)`. |
There was a problem hiding this comment.
nit: This comment says JSON numbers must be "integral", but the new test on line 1267 explicitly asserts that "1.0000000000000001" is accepted and rounded to 1. Either clarify "after decoding" (as in the Go PR) or don't lock in the rounding quirk in the test.
There was a problem hiding this comment.
Clarified “integral after decoding” and removed the test expecting 1.0000000000000001 to become 1. The docs describe the remaining float-rounding limitation.
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: - [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141) - [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
Preserve JSON integer literals through the exact i64/u64 accessors and keep the float fallback for integral forms such as 128.0 and 1e18. Reject float conversions outside the destination integer range instead of allowing saturating casts at the upper bound. Document that decimal/exponent forms may round during JSON decoding and remove assertions that obscure the distinction between integer tokens and decoded floats.
78a8e50 to
52c7613
Compare
EIP-712 integer literals could lose precision when converted through f64,
even when serde_json already stored their exact i64/u64 value.
Use the integer accessors first and retain the float fallback for forms
such as 128.0 and 1e18. Check its conversion bounds to prevent saturating
casts. Decimal/exponent forms and integer literals outside serde_json's
64-bit storage can still round during JSON decoding; decimal strings
remain the way to supply larger integers exactly.
Related fixes: