ethers.js/lib.esm/utils/maths.js

187 lines
5.9 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
import { hexlify, isBytesLike } from "./data.js";
2022-09-16 05:58:45 +03:00
import { throwArgumentError } from "./errors.js";
2022-09-05 23:57:11 +03:00
const BN_0 = BigInt(0);
const BN_1 = BigInt(1);
2022-09-16 05:58:45 +03:00
// IEEE 754 support 53-bits of mantissa
const maxValue = 0x1fffffffffffff;
2022-09-05 23:57:11 +03:00
/**
* Convert %%value%% from a twos-compliment value of %%width%% bits.
*/
export function fromTwos(_value, _width) {
2022-09-16 05:58:45 +03:00
const value = getBigInt(_value, "value");
const width = BigInt(getNumber(_width, "width"));
2022-09-05 23:57:11 +03:00
// Top bit set; treat as a negative value
if (value >> (width - BN_1)) {
const mask = (BN_1 << width) - BN_1;
return -(((~value) & mask) + BN_1);
}
return value;
}
/**
* Convert %%value%% to a twos-compliment value of %%width%% bits.
*/
export function toTwos(_value, _width) {
2022-09-16 05:58:45 +03:00
const value = getBigInt(_value, "value");
const width = BigInt(getNumber(_width, "width"));
2022-09-05 23:57:11 +03:00
if (value < BN_0) {
const mask = (BN_1 << width) - BN_1;
return ((~(-value)) & mask) + BN_1;
}
return value;
}
/**
* Mask %%value%% with a bitmask of %%bits%% ones.
*/
export function mask(_value, _bits) {
2022-09-16 05:58:45 +03:00
const value = getBigInt(_value, "value");
const bits = BigInt(getNumber(_bits, "bits"));
2022-09-05 23:57:11 +03:00
return value & ((BN_1 << bits) - BN_1);
}
2022-09-16 05:58:45 +03:00
/**
* Gets a [[BigInt]] from %%value%%. If it is an invalid value for
* a BigInt, then an ArgumentError will be thrown for %%name%%.
*/
export function getBigInt(value, name) {
switch (typeof (value)) {
case "bigint": return value;
case "number":
if (!Number.isInteger(value)) {
throwArgumentError("underflow", name || "value", value);
}
else if (value < -maxValue || value > maxValue) {
throwArgumentError("overflow", name || "value", value);
}
return BigInt(value);
case "string":
try {
2022-10-01 08:34:06 +03:00
if (value[0] === "-" && value[1] !== "-") {
return -BigInt(value.substring(1));
}
2022-09-16 05:58:45 +03:00
return BigInt(value);
}
catch (e) {
throwArgumentError(`invalid BigNumberish string: ${e.message}`, name || "value", value);
}
}
return throwArgumentError("invalid BigNumberish value", name || "value", value);
}
const Nibbles = "0123456789abcdef";
2022-09-05 23:57:11 +03:00
/*
* Converts %%value%% to a BigInt. If %%value%% is a Uint8Array, it
* is treated as Big Endian data.
*/
export function toBigInt(value) {
if (value instanceof Uint8Array) {
let result = "0x0";
for (const v of value) {
result += Nibbles[v >> 4];
result += Nibbles[v & 0x0f];
}
return BigInt(result);
}
2022-09-16 05:58:45 +03:00
return getBigInt(value);
}
/**
* Gets a //number// from %%value%%. If it is an invalid value for
* a //number//, then an ArgumentError will be thrown for %%name%%.
*/
export function getNumber(value, name) {
switch (typeof (value)) {
case "bigint":
if (value < -maxValue || value > maxValue) {
throwArgumentError("overflow", name || "value", value);
}
return Number(value);
case "number":
if (!Number.isInteger(value)) {
throwArgumentError("underflow", name || "value", value);
}
else if (value < -maxValue || value > maxValue) {
throwArgumentError("overflow", name || "value", value);
}
return value;
case "string":
try {
return getNumber(BigInt(value), name);
}
catch (e) {
throwArgumentError(`invalid numeric string: ${e.message}`, name || "value", value);
}
}
return throwArgumentError("invalid numeric value", name || "value", value);
2022-09-05 23:57:11 +03:00
}
/*
* Converts %%value%% to a number. If %%value%% is a Uint8Array, it
* is treated as Big Endian data. Throws if the value is not safe.
*/
export function toNumber(value) {
2022-09-16 05:58:45 +03:00
return getNumber(toBigInt(value));
2022-09-05 23:57:11 +03:00
}
/**
* Converts %%value%% to a Big Endian hexstring, optionally padded to
* %%width%% bytes.
*/
export function toHex(_value, _width) {
2022-09-16 05:58:45 +03:00
const value = getBigInt(_value, "value");
2022-09-05 23:57:11 +03:00
if (value < 0) {
throw new Error("cannot convert negative value to hex");
}
let result = value.toString(16);
if (_width == null) {
// Ensure the value is of even length
if (result.length % 2) {
result = "0" + result;
}
}
else {
2022-09-16 05:58:45 +03:00
const width = getNumber(_width, "width");
2022-09-05 23:57:11 +03:00
if (width * 2 < result.length) {
throw new Error(`value ${value} exceeds width ${width}`);
}
// Pad the value to the required width
while (result.length < (width * 2)) {
result = "0" + result;
}
}
return "0x" + result;
}
/**
* Converts %%value%% to a Big Endian Uint8Array.
*/
export function toArray(_value) {
2022-09-16 05:58:45 +03:00
const value = getBigInt(_value, "value");
2022-09-05 23:57:11 +03:00
if (value < 0) {
throw new Error("cannot convert negative value to hex");
}
if (value === BN_0) {
return new Uint8Array([]);
}
let hex = value.toString(16);
if (hex.length % 2) {
hex = "0" + hex;
}
const result = new Uint8Array(hex.length / 2);
for (let i = 0; i < result.length; i++) {
const offset = i * 2;
result[i] = parseInt(hex.substring(offset, offset + 2), 16);
}
return result;
}
2022-09-16 05:58:45 +03:00
/**
* Returns a [[HexString]] for %%value%% safe to use as a //Quantity//.
*
* A //Quantity// does not have and leading 0 values unless the value is
* the literal value `0x0`. This is most commonly used for JSSON-RPC
* numeric values.
*/
2022-09-05 23:57:11 +03:00
export function toQuantity(value) {
let result = hexlify(isBytesLike(value) ? value : toArray(value)).substring(2);
while (result.substring(0, 1) === "0") {
result = result.substring(1);
}
if (result === "") {
result = "0";
}
return "0x" + result;
}
//# sourceMappingURL=maths.js.map