Skip to content

Commit

Permalink
implement bigintPower to avoid ** with bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Altabba committed Oct 12, 2023
1 parent 59b9731 commit d3a1a18
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/web3-utils/src/string_manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { Numbers } from 'web3-types';
import { NibbleWidthError } from 'web3-errors';
import { isHexStrict, validator, utils as validatorUtils } from 'web3-validator';
import { isHexStrict, validator, utils as validatorUtils, bigintPower } from 'web3-validator';
import { numberToHex, toHex, toNumber } from './converters.js';

/**
Expand Down Expand Up @@ -115,7 +115,7 @@ export const toTwosComplement = (value: Numbers, nibbleWidth = 64): string => {

if (val >= 0) return padLeft(toHex(val), nibbleWidth);

const largestBit = BigInt(2) ** BigInt(nibbleWidth * 4);
const largestBit = bigintPower(BigInt(2), BigInt(nibbleWidth * 4));
if (-val >= largestBit) {
throw new NibbleWidthError(`value: ${value}, nibbleWidth: ${nibbleWidth}`);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export const fromTwosComplement = (value: Numbers, nibbleWidth = 64): number | b
// check the largest bit to see if negative
if (nibbleWidth * 4 !== largestBit) return val;

const complement = BigInt(2) ** (BigInt(nibbleWidth) * BigInt(4));
const complement = bigintPower(BigInt(2), BigInt(nibbleWidth) * BigInt(4));

return toNumber(BigInt(val) - complement);
};
17 changes: 14 additions & 3 deletions packages/web3-validator/src/validation/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ import { isHexStrict } from './string.js';
*/
export const isBigInt = (value: ValidInputTypes): boolean => typeof value === 'bigint';

// Note: this could be simplified using ** operator, but babel does not handle it well
// you can find more at: https://github.com/babel/babel/issues/13109 and https://github.com/web3/web3.js/issues/6187
/** @internal */
export const bigintPower = (base: bigint, expo: bigint) => {
let res = base;
for (let index = 1; index < expo; index += 1) {
res *= base;
}
return res;
};

export const isUInt = (
value: ValidInputTypes,
options: { abiType: string; bitSize?: never } | { bitSize: number; abiType?: never } = {
Expand All @@ -49,7 +60,7 @@ export const isUInt = (
size = options.bitSize;
}

const maxSize = BigInt(2) ** BigInt(size ?? 256) - BigInt(1);
const maxSize = bigintPower(BigInt(2), BigInt(size ?? 256)) - BigInt(1);

try {
const valueToCheck =
Expand Down Expand Up @@ -94,8 +105,8 @@ export const isInt = (
size = options.bitSize;
}

const maxSize = BigInt(2) ** BigInt((size ?? 256) - 1);
const minSize = BigInt(-1) * BigInt(2) ** BigInt((size ?? 256) - 1);
const maxSize = bigintPower(BigInt(2), BigInt((size ?? 256) - 1));
const minSize = BigInt(-1) * bigintPower(BigInt(2), BigInt((size ?? 256) - 1));

try {
const valueToCheck =
Expand Down

0 comments on commit d3a1a18

Please sign in to comment.