Skip to content
Merged
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
5 changes: 5 additions & 0 deletions __tests__/utils/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
13 changes: 13 additions & 0 deletions __tests__/utils/num.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
13 changes: 10 additions & 3 deletions src/utils/num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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'));
}

/**
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

/**
Expand Down