diff --git a/CHANGELOG.md b/CHANGELOG.md index 04ec6c8..d1f5fd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 6985338..c68d8f5 100644 --- a/README.md +++ b/README.md @@ -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); ``` diff --git a/src/index.ts b/src/index.ts index bf24aa4..e612cfe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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( diff --git a/src/internal/eth/eip712.ts b/src/internal/eth/eip712.ts index 2357f9a..afa6a85 100644 --- a/src/internal/eth/eip712.ts +++ b/src/internal/eth/eip712.ts @@ -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) { + throw new TypedMessageError('Number is not a safe uint'); } return BigInt(value); } @@ -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); } diff --git a/test/eth-eip712.test.ts b/test/eth-eip712.test.ts index 7eca418..7a0948c 100644 --- a/test/eth-eip712.test.ts +++ b/test/eth-eip712.test.ts @@ -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]);