Skip to content

eth: preserve EIP-712 integer literals - #141

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 28, 2026 •

Copy link
Copy Markdown
Contributor

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:

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
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from 5fce867 to 5dcd801 Compare November 29, 2026 08:20
benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 29, 2026
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)
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch 2 times, most recently from 6091e6d to 51d846f Compare November 29, 2026 08:24
benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 29, 2026
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)
benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 29, 2026
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
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from 51d846f to 154370b Compare November 29, 2026 08:27
@benma-agent benma-agent changed the title eth: preserve exact EIP-712 JSON integers eth: reject unsafe EIP-712 numbers Nov 29, 2026
@benma
benma requested a review from Tomasvrba November 29, 2026 08:29
@benma
benma marked this pull request as ready for review November 29, 2026 08:30

@Tomasvrba Tomasvrba 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.

Same design concern as the go api, plus some minor nits

Comment thread src/eth.rs Outdated
}

fn encode_value(typ: &MemberType, value: &Value) -> Result<Vec<u8>, String> {
const MAX_SAFE_INTEGER: f64 = ((1u64 << 53) - 1) as f64;

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.

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.

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 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.

Comment thread src/eth.rs Outdated
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] {

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.

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.

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.

Removed the loop; those values only exercised Null rejection.

Comment thread src/eth.rs Outdated
/// 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)`.

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: 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.

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.

Clarified “integral after decoding” and removed the test expecting 1.0000000000000001 to become 1. The docs describe the remaining float-rounding limitation.

benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 30, 2026
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)
@benma-agent benma-agent changed the title eth: reject unsafe EIP-712 numbers eth: preserve EIP-712 integer literals Nov 30, 2026
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.
@benma-agent
benma-agent force-pushed the benma-agent/eip712-exact-json-integers branch from 78a8e50 to 52c7613 Compare November 30, 2026 07:54

This branch has not been deployed

No deployments
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