Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Unreleased
- Reject unsafe JavaScript numbers in EIP-712 integer fields. Pass values outside
`±(2^53 - 1)` as decimal strings or `bigint`s.
- Preserve the sign of positive EIP-712 signed integers supplied as JavaScript numbers.
- Reset unfinished sessions when connecting to firmware v9.28.0 or newer, allowing host
reconnects while the device remains powered on.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ await bb02.ethSignMessage(1n, keypath, msg);
EIP-712 typed messages are passed as JavaScript values. `use_antiklepto`
defaults to `true` when omitted.

Pass integer fields as safe integer `number`s, decimal strings, or `bigint`s.
Values outside `±(2^53 - 1)` must use strings or `bigint`s built from the
original exact value; JavaScript may already round large numeric literals or
parsed JSON numbers before this call. Converting a rounded `number` to a
string or `bigint` cannot recover the lost digits.

```ts
await bb02.ethSignTypedMessage(1n, keypath, typedData);
```
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,9 @@ export class PairedBitBox {
/**
* Sign an EIP-712 typed message.
*
* Integer fields accept only safe `number` values. Use decimal strings or `bigint`
* for larger values, constructed before any lossy number conversion.
*
* `use_antiklepto` defaults to `true` when omitted.
*/
async ethSignTypedMessage(
Expand Down
8 changes: 4 additions & 4 deletions src/internal/eth/eip712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ function parseUintValue(value: unknown): bigint {
return BigInt(value);
}
if (typeof value === 'number') {
if (!Number.isInteger(value) || value < 0) {
throw new TypedMessageError('Number is not an uint');
if (!Number.isSafeInteger(value) || value < 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same concern as the go and rust apis

Switching from Number.isInteger to Number.isSafeInteger rejects exact large round numbers like 1e18 (1 ETH in wei) that @metamask/eth-sig-util (used by Rabby and MetaMask) and viem accept.

Because the BitBox02 always displays the exact number it signs on the device screen, there isn't a blind-signing security issue here, rejecting > 2^53 - 1 mostly causes dApps sending { amount: 1e18 } to fail on BitBox02 while continuing to work in MetaMask and Rabby's software wallet. Unless we specifically want to break compatibility with those dApps, I'd lean toward keeping Number.isInteger here and closing this PR.

@benma-agent benma-agent Nov 30, 2026 •

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.

Closing this PR and retaining Number.isInteger. Callers needing exact large integers should supply decimal strings or bigint.

throw new TypedMessageError('Number is not a safe uint');
}
return BigInt(value);
}
Expand All @@ -224,8 +224,8 @@ function parseIntValue(value: unknown): bigint {
return BigInt(value);
}
if (typeof value === 'number') {
if (!Number.isInteger(value)) {
throw new TypedMessageError('Number is not an int');
if (!Number.isSafeInteger(value)) {
throw new TypedMessageError('Number is not a safe int');
}
return BigInt(value);
}
Expand Down
65 changes: 65 additions & 0 deletions test/eth-eip712.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,71 @@ describe('encodeValue', () => {
}
});

it.each(['uint256', 'int256'])('accepts safe integer numbers for %s', (type) => {
const typ = parseType(type, empty);
for (const [value, expected] of [
[0, type === 'uint256' ? '' : '00'],
[128.0, type === 'uint256' ? '80' : '0080'],
[1e3, '03e8'],
[Number.MAX_SAFE_INTEGER, '1fffffffffffff'],
] as const) {
expect(Buffer.from(encodeValue(typ, value)).toString('hex')).toBe(expected);
}
});

it('accepts the negative safe integer boundary', () => {
const typ = parseType('int256', empty);
expect(Buffer.from(encodeValue(typ, Number.MIN_SAFE_INTEGER)).toString('hex'))
.toBe('e0000000000001');
});

it.each(['uint256', 'int256'])('rejects unsafe and noninteger numbers for %s', (type) => {
const typ = parseType(type, empty);
for (const value of [
Number.MAX_SAFE_INTEGER + 1,
JSON.parse('9007199254740993'),
JSON.parse('18446744073709551617'),
128.5,
Number.NaN,
Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY,
]) {
expect(() => encodeValue(typ, value)).toThrow(
expect.objectContaining({ code: 'eth-typed-message' }),
);
}
});

it('rejects negative unsafe integer numbers', () => {
const typ = parseType('int256', empty);
for (const value of [Number.MIN_SAFE_INTEGER - 1, JSON.parse('-9007199254740993')]) {
expect(() => encodeValue(typ, value)).toThrow(
expect.objectContaining({ code: 'eth-typed-message' }),
);
}
});

it.each([
['uint256', '9007199254740993', '20000000000001'],
['int256', '9007199254740993', '20000000000001'],
['int256', '-9007199254740993', 'dfffffffffffff'],
['uint256', '18446744073709551617', '010000000000000001'],
])('preserves exact %s value %s as a string or bigint', (type, value, expected) => {
const typ = parseType(type, empty);
for (const input of [value, BigInt(value)]) {
expect(Buffer.from(encodeValue(typ, input)).toString('hex')).toBe(expected);
}
});

it.each(['uint256', 'int256'])('rejects decimal and exponent strings for %s', (type) => {
const typ = parseType(type, empty);
for (const value of ['128.0', '1e3']) {
expect(() => encodeValue(typ, value)).toThrow(
expect.objectContaining({ code: 'eth-typed-message' }),
);
}
});

it('encodes booleans', () => {
expect(Array.from(encodeValue(parseType('bool', empty), false))).toEqual([0]);
expect(Array.from(encodeValue(parseType('bool', empty), true))).toEqual([1]);
Expand Down
Loading