diff --git a/__tests__/utils/address.test.ts b/__tests__/utils/address.test.ts index 1b42bf565..03007e189 100644 --- a/__tests__/utils/address.test.ts +++ b/__tests__/utils/address.test.ts @@ -36,6 +36,11 @@ describe('addAddressPadding', () => { expect(addAddressPadding(addr)).toBe(padded); }); + + test('should throw for a negative value', () => { + expect(() => addAddressPadding(-1)).toThrow('is negative'); + expect(() => addAddressPadding(-1n)).toThrow('is negative'); + }); }); describe('validateAndParseAddress', () => { diff --git a/__tests__/utils/num.test.ts b/__tests__/utils/num.test.ts index 796542652..b8c30c4e3 100644 --- a/__tests__/utils/num.test.ts +++ b/__tests__/utils/num.test.ts @@ -73,6 +73,14 @@ describe('toHex', () => { test("should throw for '0x', which holds no digit", () => { expect(() => toHex('0x')).toThrow("Invalid input: '0x' holds no hexadecimal digit"); }); + + test('should throw for a negative value, rather than return 0x-5', () => { + const message = 'Invalid input: -5 is negative, a hex-string cannot hold a sign'; + expect(() => toHex(-5)).toThrow(message); + expect(() => toHex(-5n)).toThrow(message); + expect(() => toHex('-5')).toThrow(message); + expect(toHex(-0)).toBe('0x0'); + }); }); describe('cleanHex', () => { @@ -251,6 +259,8 @@ describe('toStorageKey, toHex64', () => { const key3 = num.toStorageKey(123n); expect(key3).toEqual('0x000000000000000000000000000000000000000000000000000000000000007b'); expect(key3.length).toEqual(66); + + expect(() => num.toStorageKey(-1)).toThrow('is negative'); }); test('should convert to 0x + 64 hex restricted', () => { @@ -283,5 +293,8 @@ describe('toStorageKey, toHex64', () => { const key4 = num.toHex64('0x82bdafb0c4a2b03cd0f16ddcc3339da37f2cbb1aecb2a419764e35b7c3a8ec29'); expect(key4).toEqual('0x82bdafb0c4a2b03cd0f16ddcc3339da37f2cbb1aecb2a419764e35b7c3a8ec29'); expect(key4.length).toEqual(66); + + // '-5' padded to 64 characters has the expected length, but is not hex + expect(() => num.toHex64(-5)).toThrow('is negative'); }); }); diff --git a/src/utils/num.ts b/src/utils/num.ts index ed0436fb6..c2bbc27f7 100644 --- a/src/utils/num.ts +++ b/src/utils/num.ts @@ -62,17 +62,24 @@ export function tryToBigInt(value: BigNumberish | undefined) { /** * Convert BigNumberish to hex-string * + * A negative value is refused: a hex-string has no sign, and `'0x-5'` is not one. * @param {BigNumberish} value value to convert * @returns {string} converted number in hex-string format + * @throws {Error} when the value is negative, or cannot be read by {@link toBigInt} * @example * ```typescript * toHex(100); // '0x64' * toHex('200'); // '0xc8' * toHex('0x00023AB'); // '0x23ab' + * toHex(-5); // throws Error('Invalid input: -5 is negative, a hex-string cannot hold a sign') * ``` */ export function toHex(value: BigNumberish): string { - return addHexPrefix(toBigInt(value).toString(16)); + const bn = toBigInt(value); + if (bn < 0n) { + throw new Error(`Invalid input: ${bn} is negative, a hex-string cannot hold a sign`); + } + return addHexPrefix(bn.toString(16)); } /** @@ -109,7 +116,7 @@ export function cleanHex(hex: string): string { */ export function toStorageKey(number: BigNumberish): string { // TODO: This is not completely correct as it will not enforce first 0 and second [0-7], 0x82bda... will pass as valid and should be false - return addHexPrefix(toBigInt(number).toString(16).padStart(64, '0')); + return addHexPrefix(removeHexPrefix(toHex(number)).padStart(64, '0')); } /** @@ -126,7 +133,7 @@ export function toStorageKey(number: BigNumberish): string { * ``` */ export function toHex64(number: BigNumberish): string { - const res = addHexPrefix(toBigInt(number).toString(16).padStart(64, '0')); + const res = addHexPrefix(removeHexPrefix(toHex(number)).padStart(64, '0')); if (res.length !== 66) throw TypeError('number is too big for hex 0x(64) representation'); return res; } diff --git a/src/utils/typedData.ts b/src/utils/typedData.ts index 086a00b20..e4a4a7fc1 100644 --- a/src/utils/typedData.ts +++ b/src/utils/typedData.ts @@ -30,7 +30,7 @@ import { getSelectorFromName, } from './hash'; import { MerkleTree } from './merkle'; -import { isBigNumberish, isHex, toHex } from './num'; +import { isBigNumberish, isHex, toBigInt, toHex } from './num'; import { isBoolean, isString } from './typed'; interface Context { @@ -154,14 +154,17 @@ function identifyRevision({ types, domain }: TypedData) { } function getHex(value: BigNumberish): string { + let bn: bigint; try { - return toHex(value); + bn = toBigInt(value); } catch (e) { if (isString(value)) { return toHex(CairoBytes31.fromText(value).toBigInt()); } throw new Error(`Invalid BigNumberish: ${value}`); } + // a number is never read as text, even when it is negative and so cannot be hashed + return toHex(bn); } /**