Skip to content

Commit

Permalink
fix: jsdom failing with init bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Nov 30, 2021
1 parent 140f62d commit ba4d351
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,46 +344,50 @@ export function intToBN(value: IntegerType, signed: boolean): BN {
}

export function intToBigInt(value: IntegerType, signed: boolean): bigint {
if (typeof value === 'number') {
if (!Number.isInteger(value)) {
let parsedValue = value;

if (typeof parsedValue === 'number') {
if (!Number.isInteger(parsedValue)) {
throw new RangeError(`Invalid value. Values of type 'number' must be an integer.`);
}
return BigInt(value);
return BigInt(parsedValue);
}
if (typeof value === 'string') {
if (typeof parsedValue === 'string') {
// If hex string then convert to buffer then fall through to the buffer condition
if (value.toLowerCase().startsWith('0x')) {
if (parsedValue.toLowerCase().startsWith('0x')) {
// Trim '0x' hex-prefix
let hex = value.slice(2);
let hex = parsedValue.slice(2);

// Allow odd-length strings like `0xf` -- some libs output these, or even just `0x${num.toString(16)}`
hex = hex.padStart(hex.length + (hex.length % 2), '0');
value = AvailableBufferModule.from(hex, 'hex');

parsedValue = AvailableBufferModule.from(hex, 'hex');
} else {
try {
return BigInt(value);
return BigInt(parsedValue);
} catch (error) {
if (error instanceof SyntaxError) {
throw new RangeError(`Invalid value. String integer '${value}' is not finite.`);
throw new RangeError(`Invalid value. String integer '${parsedValue}' is not finite.`);
}
}
}
}
if (typeof value === 'bigint') {
return value;
if (typeof parsedValue === 'bigint') {
return parsedValue;
}
if (value instanceof Uint8Array || BufferPolyfill.isBuffer(value)) {
if (parsedValue instanceof Uint8Array || AvailableBufferModule.isBuffer(parsedValue)) {
if (signed) {
// Allow byte arrays smaller than 128-bits to be passed.
// This allows positive signed ints like `0x08` (8) or negative signed
// ints like `0xf8` (-8) to be passed without having to pad to 16 bytes.
const bn = new BN(value, 'be').fromTwos(value.byteLength * 8);
const bn = new BN(parsedValue, 'be').fromTwos(parsedValue.byteLength * 8);
return BigInt(bn.toString());
} else {
return BigInt(new BN(value, 'be').toString());
return BigInt(new BN(parsedValue, 'be').toString());
}
}
if (value instanceof BN || BN.isBN(value)) {
return BigInt(value.toString());
if (parsedValue instanceof BN || BN.isBN(parsedValue)) {
return BigInt(parsedValue.toString());
}
throw new TypeError(
`Invalid value type. Must be a number, bigint, integer-string, hex-string, BN.js instance, or Buffer.`
Expand Down

1 comment on commit ba4d351

@vercel
Copy link

@vercel vercel bot commented on ba4d351 Nov 30, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.