mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 04:27:59 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import BN from 'bn.js';
|
||||
export { BN };
|
||||
@@ -0,0 +1,2 @@
|
||||
import BN from 'bn.js';
|
||||
export { BN };
|
||||
Vendored
+91
@@ -0,0 +1,91 @@
|
||||
import { BN } from './bn.js';
|
||||
/**
|
||||
* @name BN_ZERO
|
||||
* @summary BN constant for 0.
|
||||
*/
|
||||
export declare const BN_ZERO: BN;
|
||||
/**
|
||||
* @name BN_ONE
|
||||
* @summary BN constant for 1.
|
||||
*/
|
||||
export declare const BN_ONE: BN;
|
||||
/**
|
||||
* @name BN_TWO
|
||||
* @summary BN constant for 2.
|
||||
*/
|
||||
export declare const BN_TWO: BN;
|
||||
/**
|
||||
* @name BN_THREE
|
||||
* @summary BN constant for 3.
|
||||
*/
|
||||
export declare const BN_THREE: BN;
|
||||
/**
|
||||
* @name BN_FOUR
|
||||
* @summary BN constant for 4.
|
||||
*/
|
||||
export declare const BN_FOUR: BN;
|
||||
/**
|
||||
* @name BN_FIVE
|
||||
* @summary BN constant for 5.
|
||||
*/
|
||||
export declare const BN_FIVE: BN;
|
||||
/**
|
||||
* @name BN_SIX
|
||||
* @summary BN constant for 6.
|
||||
*/
|
||||
export declare const BN_SIX: BN;
|
||||
/**
|
||||
* @name BN_SEVEN
|
||||
* @summary BN constant for 7.
|
||||
*/
|
||||
export declare const BN_SEVEN: BN;
|
||||
/**
|
||||
* @name BN_EIGHT
|
||||
* @summary BN constant for 8.
|
||||
*/
|
||||
export declare const BN_EIGHT: BN;
|
||||
/**
|
||||
* @name BN_NINE
|
||||
* @summary BN constant for 9.
|
||||
*/
|
||||
export declare const BN_NINE: BN;
|
||||
/**
|
||||
* @name BN_TEN
|
||||
* @summary BN constant for 10.
|
||||
*/
|
||||
export declare const BN_TEN: BN;
|
||||
/**
|
||||
* @name BN_HUNDRED
|
||||
* @summary BN constant for 100.
|
||||
*/
|
||||
export declare const BN_HUNDRED: BN;
|
||||
/**
|
||||
* @name BN_THOUSAND
|
||||
* @summary BN constant for 1,000.
|
||||
*/
|
||||
export declare const BN_THOUSAND: BN;
|
||||
/**
|
||||
* @name BN_MILLION
|
||||
* @summary BN constant for 1,000,000.
|
||||
*/
|
||||
export declare const BN_MILLION: BN;
|
||||
/**
|
||||
* @name BN_BILLION
|
||||
* @summary BN constant for 1,000,000,000.
|
||||
*/
|
||||
export declare const BN_BILLION: BN;
|
||||
/**
|
||||
* @name BN_QUINTILL
|
||||
* @summary BN constant for 1,000,000,000,000,000,000.
|
||||
*/
|
||||
export declare const BN_QUINTILL: BN;
|
||||
/**
|
||||
* @name BN_MAX_INTEGER
|
||||
* @summary BN constant for MAX_SAFE_INTEGER
|
||||
*/
|
||||
export declare const BN_MAX_INTEGER: BN;
|
||||
/**
|
||||
* @name BN_SQRT_MAX_INTEGER
|
||||
* @summary BN constant for Math.sqrt(MAX_SAFE_INTEGER)
|
||||
*/
|
||||
export declare const BN_SQRT_MAX_INTEGER: BN;
|
||||
@@ -0,0 +1,91 @@
|
||||
import { BN } from './bn.js';
|
||||
/**
|
||||
* @name BN_ZERO
|
||||
* @summary BN constant for 0.
|
||||
*/
|
||||
export const BN_ZERO = /*#__PURE__*/ new BN(0);
|
||||
/**
|
||||
* @name BN_ONE
|
||||
* @summary BN constant for 1.
|
||||
*/
|
||||
export const BN_ONE = /*#__PURE__*/ new BN(1);
|
||||
/**
|
||||
* @name BN_TWO
|
||||
* @summary BN constant for 2.
|
||||
*/
|
||||
export const BN_TWO = /*#__PURE__*/ new BN(2);
|
||||
/**
|
||||
* @name BN_THREE
|
||||
* @summary BN constant for 3.
|
||||
*/
|
||||
export const BN_THREE = /*#__PURE__*/ new BN(3);
|
||||
/**
|
||||
* @name BN_FOUR
|
||||
* @summary BN constant for 4.
|
||||
*/
|
||||
export const BN_FOUR = /*#__PURE__*/ new BN(4);
|
||||
/**
|
||||
* @name BN_FIVE
|
||||
* @summary BN constant for 5.
|
||||
*/
|
||||
export const BN_FIVE = /*#__PURE__*/ new BN(5);
|
||||
/**
|
||||
* @name BN_SIX
|
||||
* @summary BN constant for 6.
|
||||
*/
|
||||
export const BN_SIX = /*#__PURE__*/ new BN(6);
|
||||
/**
|
||||
* @name BN_SEVEN
|
||||
* @summary BN constant for 7.
|
||||
*/
|
||||
export const BN_SEVEN = /*#__PURE__*/ new BN(7);
|
||||
/**
|
||||
* @name BN_EIGHT
|
||||
* @summary BN constant for 8.
|
||||
*/
|
||||
export const BN_EIGHT = /*#__PURE__*/ new BN(8);
|
||||
/**
|
||||
* @name BN_NINE
|
||||
* @summary BN constant for 9.
|
||||
*/
|
||||
export const BN_NINE = /*#__PURE__*/ new BN(9);
|
||||
/**
|
||||
* @name BN_TEN
|
||||
* @summary BN constant for 10.
|
||||
*/
|
||||
export const BN_TEN = /*#__PURE__*/ new BN(10);
|
||||
/**
|
||||
* @name BN_HUNDRED
|
||||
* @summary BN constant for 100.
|
||||
*/
|
||||
export const BN_HUNDRED = /*#__PURE__*/ new BN(100);
|
||||
/**
|
||||
* @name BN_THOUSAND
|
||||
* @summary BN constant for 1,000.
|
||||
*/
|
||||
export const BN_THOUSAND = /*#__PURE__*/ new BN(1_000);
|
||||
/**
|
||||
* @name BN_MILLION
|
||||
* @summary BN constant for 1,000,000.
|
||||
*/
|
||||
export const BN_MILLION = /*#__PURE__*/ new BN(1_000_000);
|
||||
/**
|
||||
* @name BN_BILLION
|
||||
* @summary BN constant for 1,000,000,000.
|
||||
*/
|
||||
export const BN_BILLION = /*#__PURE__*/ new BN(1_000_000_000);
|
||||
/**
|
||||
* @name BN_QUINTILL
|
||||
* @summary BN constant for 1,000,000,000,000,000,000.
|
||||
*/
|
||||
export const BN_QUINTILL = BN_BILLION.mul(BN_BILLION);
|
||||
/**
|
||||
* @name BN_MAX_INTEGER
|
||||
* @summary BN constant for MAX_SAFE_INTEGER
|
||||
*/
|
||||
export const BN_MAX_INTEGER = /*#__PURE__*/ new BN(Number.MAX_SAFE_INTEGER);
|
||||
/**
|
||||
* @name BN_SQRT_MAX_INTEGER
|
||||
* @summary BN constant for Math.sqrt(MAX_SAFE_INTEGER)
|
||||
*/
|
||||
export const BN_SQRT_MAX_INTEGER = /*#__PURE__*/ new BN(94906265);
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export { hexToBn as bnFromHex } from '../hex/toBn.js';
|
||||
@@ -0,0 +1 @@
|
||||
export { hexToBn as bnFromHex } from '../hex/toBn.js';
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @summary Utility methods to convert to and from `BN` objects
|
||||
*/
|
||||
export { BN } from './bn.js';
|
||||
export { bnFromHex } from './fromHex.js';
|
||||
export { bnMax, bnMin } from './min.js';
|
||||
export { bnSqrt } from './sqrt.js';
|
||||
export { bnToBn } from './toBn.js';
|
||||
export { bnToHex } from './toHex.js';
|
||||
export { bnToU8a } from './toU8a.js';
|
||||
export * from './consts.js';
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @summary Utility methods to convert to and from `BN` objects
|
||||
*/
|
||||
export { BN } from './bn.js';
|
||||
export { bnFromHex } from './fromHex.js';
|
||||
export { bnMax, bnMin } from './min.js';
|
||||
export { bnSqrt } from './sqrt.js';
|
||||
export { bnToBn } from './toBn.js';
|
||||
export { bnToHex } from './toHex.js';
|
||||
export { bnToU8a } from './toU8a.js';
|
||||
export * from './consts.js';
|
||||
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
import type { BN } from './bn.js';
|
||||
/**
|
||||
* @name bnMax
|
||||
* @summary Finds and returns the highest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMax } from '@pezkuwi/util';
|
||||
*
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export declare const bnMax: (...items: BN[]) => BN;
|
||||
/**
|
||||
* @name bnMin
|
||||
* @summary Finds and returns the smallest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMin } from '@pezkuwi/util';
|
||||
*
|
||||
* bnMin([new BN(1), new BN(3), new BN(2)]).toString(); // => '1'
|
||||
* ```
|
||||
*/
|
||||
export declare const bnMin: (...items: BN[]) => BN;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { createCmp } from '../bi/helpers.js';
|
||||
/**
|
||||
* @name bnMax
|
||||
* @summary Finds and returns the highest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMax } from '@pezkuwi/util';
|
||||
*
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export const bnMax = /*#__PURE__*/ createCmp((a, b) => a.gt(b));
|
||||
/**
|
||||
* @name bnMin
|
||||
* @summary Finds and returns the smallest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMin } from '@pezkuwi/util';
|
||||
*
|
||||
* bnMin([new BN(1), new BN(3), new BN(2)]).toString(); // => '1'
|
||||
* ```
|
||||
*/
|
||||
export const bnMin = /*#__PURE__*/ createCmp((a, b) => a.lt(b));
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { ToBn } from '../types.js';
|
||||
import { BN } from './bn.js';
|
||||
/**
|
||||
* @name bnSqrt
|
||||
* @summary Calculates the integer square root of a BN
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnSqrt } from '@pezkuwi/util';
|
||||
*
|
||||
* bnSqrt(new BN(16)).toString(); // => '4'
|
||||
* ```
|
||||
*/
|
||||
export declare function bnSqrt<ExtToBn extends ToBn>(value: ExtToBn | BN | bigint | string | number | null): BN;
|
||||
@@ -0,0 +1,38 @@
|
||||
import { BN } from './bn.js';
|
||||
import { BN_MAX_INTEGER, BN_ONE, BN_SQRT_MAX_INTEGER } from './consts.js';
|
||||
import { bnToBn } from './toBn.js';
|
||||
/**
|
||||
* @name bnSqrt
|
||||
* @summary Calculates the integer square root of a BN
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnSqrt } from '@pezkuwi/util';
|
||||
*
|
||||
* bnSqrt(new BN(16)).toString(); // => '4'
|
||||
* ```
|
||||
*/
|
||||
export function bnSqrt(value) {
|
||||
const n = bnToBn(value);
|
||||
if (n.isNeg()) {
|
||||
throw new Error('square root of negative numbers is not supported');
|
||||
}
|
||||
// https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
|
||||
// shortcut <= 2^53 - 1 to use the JS utils
|
||||
if (n.lte(BN_MAX_INTEGER)) {
|
||||
// ~~ More performant version of Math.floor
|
||||
return new BN(~~Math.sqrt(n.toNumber()));
|
||||
}
|
||||
// Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
|
||||
// output will be larger than this, we expect this to be a safe start
|
||||
let x0 = BN_SQRT_MAX_INTEGER.clone();
|
||||
while (true) {
|
||||
const x1 = n.div(x0).iadd(x0).ishrn(1);
|
||||
if (x0.eq(x1) || x0.eq(x1.sub(BN_ONE))) {
|
||||
return x0;
|
||||
}
|
||||
x0 = x1;
|
||||
}
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
import { BN } from './bn.js';
|
||||
/**
|
||||
* @name bnToBn
|
||||
* @summary Creates a BN value from a BN, bigint, string (base 10 or hex) or number input.
|
||||
* @description
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numbers returns a BN representation.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnToBn } from '@pezkuwi/util';
|
||||
*
|
||||
* bnToBn(0x1234); // => BN(0x1234)
|
||||
* bnToBn(new BN(0x1234)); // => BN(0x1234)
|
||||
* ```
|
||||
*/
|
||||
export declare function bnToBn<ExtToBn extends ToBigInt | ToBn>(value?: ExtToBn | BN | bigint | string | number | null): BN;
|
||||
@@ -0,0 +1,37 @@
|
||||
import { hexToBn } from '../hex/toBn.js';
|
||||
import { isBigInt } from '../is/bigInt.js';
|
||||
import { isHex } from '../is/hex.js';
|
||||
import { isToBigInt } from '../is/toBigInt.js';
|
||||
import { isToBn } from '../is/toBn.js';
|
||||
import { BN } from './bn.js';
|
||||
/**
|
||||
* @name bnToBn
|
||||
* @summary Creates a BN value from a BN, bigint, string (base 10 or hex) or number input.
|
||||
* @description
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numbers returns a BN representation.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnToBn } from '@pezkuwi/util';
|
||||
*
|
||||
* bnToBn(0x1234); // => BN(0x1234)
|
||||
* bnToBn(new BN(0x1234)); // => BN(0x1234)
|
||||
* ```
|
||||
*/
|
||||
export function bnToBn(value) {
|
||||
return value
|
||||
? BN.isBN(value)
|
||||
? value
|
||||
: isHex(value)
|
||||
? hexToBn(value.toString())
|
||||
: isBigInt(value)
|
||||
? new BN(value.toString())
|
||||
: isToBn(value)
|
||||
? value.toBn()
|
||||
: isToBigInt(value)
|
||||
? new BN(value.toBigInt().toString())
|
||||
: new BN(value)
|
||||
: new BN(0);
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import type { HexString, NumberOptions, ToBn } from '../types.js';
|
||||
import type { BN } from './bn.js';
|
||||
/**
|
||||
* @name bnToHex
|
||||
* @summary Creates a hex value from a BN.js bignumber object.
|
||||
* @description
|
||||
* `null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. With `bitLength` set, it fixes the number to the specified length.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnToHex } from '@pezkuwi/util';
|
||||
*
|
||||
* bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
* ```
|
||||
*/
|
||||
export declare function bnToHex<ExtToBn extends ToBn>(value?: ExtToBn | BN | bigint | number | null, { bitLength, isLe, isNegative }?: NumberOptions): HexString;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { u8aToHex } from '../u8a/index.js';
|
||||
import { bnToU8a } from './toU8a.js';
|
||||
/**
|
||||
* @name bnToHex
|
||||
* @summary Creates a hex value from a BN.js bignumber object.
|
||||
* @description
|
||||
* `null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. With `bitLength` set, it fixes the number to the specified length.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnToHex } from '@pezkuwi/util';
|
||||
*
|
||||
* bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
* ```
|
||||
*/
|
||||
export function bnToHex(value, { bitLength = -1, isLe = false, isNegative = false } = {}) {
|
||||
return u8aToHex(bnToU8a(value, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
import type { NumberOptions, ToBn } from '../types.js';
|
||||
import type { BN } from './bn.js';
|
||||
/**
|
||||
* @name bnToU8a
|
||||
* @summary Creates a Uint8Array object from a BN.
|
||||
* @description
|
||||
* `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`. Optionally convert using little-endian format if `isLE` is set.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { bnToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
|
||||
* ```
|
||||
*/
|
||||
export declare function bnToU8a<ExtToBn extends ToBn>(value?: ExtToBn | BN | bigint | number | null, { bitLength, isLe, isNegative }?: NumberOptions): Uint8Array;
|
||||
@@ -0,0 +1,33 @@
|
||||
import { bnToBn } from './toBn.js';
|
||||
const DEFAULT_OPTS = { bitLength: -1, isLe: true, isNegative: false };
|
||||
/**
|
||||
* @name bnToU8a
|
||||
* @summary Creates a Uint8Array object from a BN.
|
||||
* @description
|
||||
* `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`. Optionally convert using little-endian format if `isLE` is set.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { bnToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
|
||||
* ```
|
||||
*/
|
||||
export function bnToU8a(value, { bitLength = -1, isLe = true, isNegative = false } = DEFAULT_OPTS) {
|
||||
const valueBn = bnToBn(value);
|
||||
const byteLength = bitLength === -1
|
||||
? Math.ceil(valueBn.bitLength() / 8)
|
||||
: Math.ceil((bitLength || 0) / 8);
|
||||
if (!value) {
|
||||
return bitLength === -1
|
||||
? new Uint8Array(1)
|
||||
: new Uint8Array(byteLength);
|
||||
}
|
||||
const output = new Uint8Array(byteLength);
|
||||
const bn = isNegative
|
||||
? valueBn.toTwos(byteLength * 8)
|
||||
: valueBn;
|
||||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
|
||||
return output;
|
||||
}
|
||||
Reference in New Issue
Block a user