mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-11-04 23:11:06 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
commit
ec06da0ebc
687 files changed
+48096
No files matched your search
@@ -0,0 +1,112 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
|
||||
/**
|
||||
* @name _0n
|
||||
* @summary BigInt constant for 0.
|
||||
*/
|
||||
export const _0n = /*#__PURE__*/ BigInt(0);
|
||||
|
||||
/**
|
||||
* @name _1n
|
||||
* @summary BigInt constant for 1.
|
||||
*/
|
||||
export const _1n = /*#__PURE__*/ BigInt(1);
|
||||
|
||||
/**
|
||||
* @name _2n
|
||||
* @summary BigInt constant for 2.
|
||||
*/
|
||||
export const _2n = /*#__PURE__*/ BigInt(2);
|
||||
|
||||
/**
|
||||
* @name _3n
|
||||
* @summary BigInt constant for 3.
|
||||
*/
|
||||
export const _3n = /*#__PURE__*/ BigInt(3);
|
||||
|
||||
/**
|
||||
* @name _4n
|
||||
* @summary BigInt constant for 4.
|
||||
*/
|
||||
export const _4n = /*#__PURE__*/ BigInt(4);
|
||||
|
||||
/**
|
||||
* @name _5n
|
||||
* @summary BigInt constant for 5.
|
||||
*/
|
||||
export const _5n = /*#__PURE__*/ BigInt(5);
|
||||
|
||||
/**
|
||||
* @name _6n
|
||||
* @summary BigInt constant for 6.
|
||||
*/
|
||||
export const _6n = /*#__PURE__*/ BigInt(6);
|
||||
|
||||
/**
|
||||
* @name _7n
|
||||
* @summary BigInt constant for 7.
|
||||
*/
|
||||
export const _7n = /*#__PURE__*/ BigInt(7);
|
||||
|
||||
/**
|
||||
* @name _8n
|
||||
* @summary BigInt constant for 8.
|
||||
*/
|
||||
export const _8n = /*#__PURE__*/ BigInt(8);
|
||||
|
||||
/**
|
||||
* @name _9n
|
||||
* @summary BigInt constant for 9.
|
||||
*/
|
||||
export const _9n = /*#__PURE__*/ BigInt(9);
|
||||
|
||||
/**
|
||||
* @name _10n
|
||||
* @summary BigInt constant for 10.
|
||||
*/
|
||||
export const _10n = /*#__PURE__*/ BigInt(10);
|
||||
|
||||
/**
|
||||
* @name _100n
|
||||
* @summary BigInt constant for 100.
|
||||
*/
|
||||
export const _100n = /*#__PURE__*/ BigInt(100);
|
||||
|
||||
/**
|
||||
* @name _1000n
|
||||
* @summary BigInt constant for 1000.
|
||||
*/
|
||||
export const _1000n = /*#__PURE__*/ BigInt(1_000);
|
||||
|
||||
/**
|
||||
* @name _1Mn
|
||||
* @summary BigInt constant for 1,000,000 (million).
|
||||
*/
|
||||
export const _1Mn = /*#__PURE__*/ BigInt(1_000_000);
|
||||
|
||||
/**
|
||||
* @name _1Bn
|
||||
* @summary BigInt constant for 1,000,000,000 (billion).
|
||||
*/
|
||||
export const _1Bn = /*#__PURE__*/ BigInt(1_000_000_000);
|
||||
|
||||
/**
|
||||
* @name _1Qn
|
||||
* @summary BigInt constant for 1,000,000,000,000,000,000 (quitillion).
|
||||
*/
|
||||
export const _1Qn = _1Bn * _1Bn;
|
||||
|
||||
/**
|
||||
* @name _2pow53n
|
||||
* @summary BigInt constant for MAX_SAFE_INTEGER
|
||||
*/
|
||||
export const _2pow53n = /*#__PURE__*/ BigInt(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
/**
|
||||
* @name _sqrt2pow53n
|
||||
* @summary BigInt constant for Math.sqrt(MAX_SAFE_INTEGER)
|
||||
*/
|
||||
export const _sqrt2pow53n = /*#__PURE__*/ BigInt(94906265);
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/** @internal */
|
||||
export function createCmp <T> (cmp: (a: T, b: T) => boolean): (...items: T[]) => T {
|
||||
return (...items: T[]): T => {
|
||||
const count = items.length;
|
||||
|
||||
if (count === 0) {
|
||||
throw new Error('Must provide one or more arguments');
|
||||
}
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < count; i++) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Utility methods to convert to and from `bigint` objects
|
||||
*/
|
||||
|
||||
// all named
|
||||
export { nMax, nMin } from './min.js';
|
||||
export { nSqrt } from './sqrt.js';
|
||||
export { nToBigInt } from './toBigInt.js';
|
||||
export { nToHex } from './toHex.js';
|
||||
export { nToU8a } from './toU8a.js';
|
||||
|
||||
// all starred
|
||||
export * from './consts.js';
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { nMax } from './index.js';
|
||||
|
||||
describe('nMax', (): void => {
|
||||
it('finds maximum (sorted)', (): void => {
|
||||
expect(
|
||||
nMax(1n, 2n, 3n)
|
||||
).toEqual(3n);
|
||||
});
|
||||
|
||||
it('finds maximum (unsorted)', (): void => {
|
||||
expect(
|
||||
nMax(2n, 3n, 1n)
|
||||
).toEqual(3n);
|
||||
});
|
||||
|
||||
it('returns a single item', (): void => {
|
||||
expect(
|
||||
nMax(1n)
|
||||
).toEqual(1n);
|
||||
});
|
||||
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => nMax()
|
||||
).toThrow(/Must provide one or more arguments/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { nMin } from './index.js';
|
||||
|
||||
describe('nMin', (): void => {
|
||||
it('finds BN minimum', (): void => {
|
||||
expect(
|
||||
nMin(2n, 1n, 3n)
|
||||
).toEqual(1n);
|
||||
});
|
||||
|
||||
it('returns a single item', (): void => {
|
||||
expect(
|
||||
nMin(1n)
|
||||
).toEqual(1n);
|
||||
});
|
||||
|
||||
it('fails when no items are available', (): void => {
|
||||
expect(
|
||||
() => nMin()
|
||||
).toThrow(/Must provide one or more arguments/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createCmp } from './helpers.js';
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export const nMax = /*#__PURE__*/ createCmp<bigint>((a, b) => a > b);
|
||||
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export const nMin = /*#__PURE__*/ createCmp<bigint>((a, b) => a < b);
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { _sqrt2pow53n, nSqrt } from './index.js';
|
||||
|
||||
// eslint-disable-next-line jest/no-export
|
||||
export const TESTS: [value: string | number | BN | bigint, expected: string | number][] = [
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[4, 2],
|
||||
[256 * 256, 256],
|
||||
[Number.MAX_SAFE_INTEGER, 94906265],
|
||||
[new BN(Number.MAX_SAFE_INTEGER).iaddn(256), 94906265],
|
||||
[12345678n * 12345679n, 12345678],
|
||||
// test cases below from https://github.com/Aisse-258/bigint-isqrt/blob/f5254b9750841959022461c1353437a07a08f501/test/sqrt-test.js
|
||||
[
|
||||
'54866395443885995655625',
|
||||
'234235768925'
|
||||
],
|
||||
[
|
||||
'82120471531550314555681345949499512621827274120673745141541602816614526075010755373654280259022317599142038423759320355177481886719814621305828811322920076213800348341464996337890625',
|
||||
'9062034624274524065844376014975805577107171799890766992670739972241112960081909332275390625'
|
||||
],
|
||||
[
|
||||
'2068290204957779940494571454815902632050207752627007664813877111611972735314294907791993008455381265730624848057015476004143868408297250386512042711005577373327434315310235583583151773358353534031397385969275475421659036494173765903706963544628565027679444680637920371210258368358706248446698622704138943704561842220533563976838506992543814273403403362954987589637283890281226038798503294069403370486523697009383229578343444337394508531937972775639593763364402389410142290440075517598007930025487176173228232153685095699851764630884360483051570513804913157899314303269924424908606245851172031453725690942288677622973423417902662711606364188470325930406288190754356367655606430407552275285889968860367977288564364154999338284288438279464648663835830140826171534470717958048887869721791862539529444820484648908879364324577951008050851801647916673254009490596379549192296404841454780884358889449367135382779230574781333008561195244620152128505091883408484094800737142399855337799331037607433934520934135669364954550499058123797911762228642423567268540444910163468496805547450495819213412636594319148417857753712526066272727725420886898680432958409892237191284583904066400736417812258327303739879128916456683685232170600928808798462776174857486589598776587550843543720784829419478239563366491666155716208452087857080883906200829769222868347286071365156305396955420561718593130305703074908891135467193627721682044757187980688062367562451981939375400543212890625',
|
||||
'45478458691536369397924851088318871329614555665525810477032568598360481742967756704524034986984134271202405190906906195123751965012732122548540879928668891416468847696038707003466943811913626146981446753434347552196868717612248915137033581428310504778371173112125038060138252719589122657221314190626986951627417768579641354255445988772645883238873672997347648890137913979299598785628581961589739331483895853398160785485300758458294999124984266041998145956341386549681176624661615465976970855592467853824812945820721933491609237311130118125761849911376495970746282571994312288380577879004620824004760109618074670810797544446334767103935754106103208789391060008291027361311679876439695844637933763721093782805837690830230712890625'
|
||||
]
|
||||
];
|
||||
|
||||
describe('nSqrt', (): void => {
|
||||
it('fails on < 0 roots', (): void => {
|
||||
expect(
|
||||
() => nSqrt(-1n)
|
||||
).toThrow(/negative numbers is not supported/);
|
||||
});
|
||||
|
||||
it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => {
|
||||
expect(
|
||||
BigInt(
|
||||
~~Math.sqrt(
|
||||
Number.MAX_SAFE_INTEGER
|
||||
)
|
||||
) === _sqrt2pow53n
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([value, expected], i): void => {
|
||||
it(`#${i}: calcs ${expected}`, (): void => {
|
||||
expect(
|
||||
nSqrt(value) === BigInt(expected)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/index.js';
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
|
||||
import { _0n, _1n, _2pow53n, _sqrt2pow53n } from './consts.js';
|
||||
import { nToBigInt } from './toBigInt.js';
|
||||
|
||||
/**
|
||||
* @name nSqrt
|
||||
* @summary Calculates the integer square root of a bigint
|
||||
*/
|
||||
export function nSqrt <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
const n = nToBigInt(value);
|
||||
|
||||
if (n < _0n) {
|
||||
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 <= _2pow53n) {
|
||||
// ~~ is more performant that Math.floor
|
||||
return BigInt(~~Math.sqrt(Number(n)));
|
||||
}
|
||||
|
||||
// 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 = _sqrt2pow53n;
|
||||
|
||||
while (true) {
|
||||
const x1 = ((n / x0) + x0) >> _1n;
|
||||
|
||||
if (x0 === x1 || (x0 === (x1 - _1n))) {
|
||||
return x0;
|
||||
}
|
||||
|
||||
x0 = x1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { nToBigInt } from './index.js';
|
||||
|
||||
describe('nToBigInt', (): void => {
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
nToBigInt(null)
|
||||
).toEqual(0n);
|
||||
});
|
||||
|
||||
it('converts 0x values to 0x00', (): void => {
|
||||
expect(
|
||||
nToBigInt('0x')
|
||||
).toEqual(0n);
|
||||
});
|
||||
|
||||
it('converts BN values to bigint', (): void => {
|
||||
expect(
|
||||
nToBigInt(new BN(128))
|
||||
).toEqual(128n);
|
||||
});
|
||||
|
||||
it('converts BigInt values to bigint', (): void => {
|
||||
expect(
|
||||
nToBigInt(128821n)
|
||||
).toEqual(128821n);
|
||||
});
|
||||
|
||||
it('converts number values to bigint', (): void => {
|
||||
expect(
|
||||
nToBigInt(128)
|
||||
).toEqual(128n);
|
||||
});
|
||||
|
||||
it('converts string to bigint', (): void => {
|
||||
expect(
|
||||
nToBigInt('123')
|
||||
).toEqual(123n);
|
||||
});
|
||||
|
||||
it('converts hex to bigint', (): void => {
|
||||
expect(
|
||||
nToBigInt('0x0123')
|
||||
).toEqual(0x123n);
|
||||
});
|
||||
|
||||
it('converts Compact to bigint (via toBn)', (): void => {
|
||||
expect(
|
||||
nToBigInt({
|
||||
something: 'test',
|
||||
toBn: () => new BN(1234)
|
||||
})
|
||||
).toEqual(1234n);
|
||||
});
|
||||
|
||||
it('converts Compact to bigint (via toBigInt)', (): void => {
|
||||
expect(
|
||||
nToBigInt({
|
||||
something: 'test',
|
||||
toBigInt: () => 1234n
|
||||
})
|
||||
).toEqual(1234n);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
|
||||
import { hexToBigInt } from '../hex/toBigInt.js';
|
||||
import { isBn } from '../is/bn.js';
|
||||
import { isHex } from '../is/hex.js';
|
||||
import { isToBigInt } from '../is/toBigInt.js';
|
||||
import { isToBn } from '../is/toBn.js';
|
||||
|
||||
/**
|
||||
* @name nToBigInt
|
||||
* @summary Creates a bigInt value from a BN, bigint, string (base 10 or hex) or number input.
|
||||
*/
|
||||
export function nToBigInt <ExtToBn extends ToBigInt | ToBn> (value?: ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
return typeof value === 'bigint'
|
||||
? value
|
||||
: !value
|
||||
? BigInt(0)
|
||||
: isHex(value)
|
||||
? hexToBigInt(value.toString())
|
||||
: isBn(value)
|
||||
? BigInt(value.toString())
|
||||
: isToBigInt(value)
|
||||
? value.toBigInt()
|
||||
: isToBn(value)
|
||||
? BigInt(value.toBn().toString())
|
||||
: BigInt(value);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { nToHex } from './index.js';
|
||||
|
||||
describe('nToHex', (): void => {
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
nToHex(null)
|
||||
).toBe('0x00');
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (with bitLength)', (): void => {
|
||||
expect(
|
||||
nToHex(null, { bitLength: 32 })
|
||||
).toBe('0x00000000');
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation', (): void => {
|
||||
expect(
|
||||
nToHex(128)
|
||||
).toBe('0x80');
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation (bitLength)', (): void => {
|
||||
expect(
|
||||
nToHex(128n, { bitLength: 16 })
|
||||
).toBe('0x0080');
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation (bitLength + le)', (): void => {
|
||||
expect(
|
||||
nToHex(128, { bitLength: 16, isLe: true })
|
||||
).toBe('0x8000');
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation (LE)', (): void => {
|
||||
expect(
|
||||
nToHex(128, { bitLength: 16, isLe: true })
|
||||
).toBe('0x8000');
|
||||
});
|
||||
|
||||
it('handles negative numbers', (): void => {
|
||||
expect(
|
||||
nToHex(-1234, { isNegative: true })
|
||||
).toBe('0xfb2e');
|
||||
});
|
||||
|
||||
it('handles negative numbers (with bitLength)', (): void => {
|
||||
expect(
|
||||
nToHex(-1234, { bitLength: 32, isNegative: true })
|
||||
).toBe('0xfffffb2e');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { HexString, NumberOptions, ToBigInt, ToBn } from '../types.js';
|
||||
|
||||
import { u8aToHex } from '../u8a/index.js';
|
||||
import { nToU8a } from './toU8a.js';
|
||||
|
||||
/**
|
||||
* @name nToHex
|
||||
* @summary Creates a hex value from a bigint object.
|
||||
*/
|
||||
export function nToHex <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
|
||||
return u8aToHex(nToU8a(value || 0, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayRange } from '../array/index.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { nToU8a } from './index.js';
|
||||
|
||||
// eslint-disable-next-line jest/no-export
|
||||
export const TESTS: [isLe: boolean, isNegative: boolean, numarr: number[], strval: string][] = [
|
||||
// LE, positive numbers
|
||||
[true, false, [0x12], '18'],
|
||||
[true, false, [0x12, 0x34], '13330'],
|
||||
[true, false, [0x12, 0x34, 0x56], '5649426'],
|
||||
[true, false, [0x12, 0x34, 0x56, 0x78], '2018915346'],
|
||||
[true, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '663443878930'],
|
||||
[true, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '207371629900818'],
|
||||
[true, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '159954953172672629770948536149615195154'],
|
||||
// LE, positive numbers (w/ signed flag)
|
||||
[true, true, [12], '12'],
|
||||
[true, true, [210, 4], '1234'],
|
||||
[true, true, [64, 226, 1], '123456'],
|
||||
[true, true, [21, 205, 91, 7], '123456789'],
|
||||
[true, true, [203, 36, 104, 12, 8], '34567890123'],
|
||||
[true, true, [255, 159, 114, 78, 24, 9], '9999999999999'],
|
||||
// LE, negative numbers
|
||||
[true, true, [244], '-12'],
|
||||
[true, true, [46, 251], '-1234'],
|
||||
[true, true, [192, 29, 254], '-123456'],
|
||||
[true, true, [255, 255, 255, 255], '-1'],
|
||||
[true, true, [254, 255, 255, 255], '-2'],
|
||||
[true, true, [235, 50, 164, 248], '-123456789'],
|
||||
[true, true, [0, 0, 0, 128], '-2147483648'],
|
||||
[true, true, [0, 0, 0, 240], '-268435456'],
|
||||
[true, true, [65, 86, 129, 173, 254], '-5678999999'],
|
||||
[true, true, [1, 96, 141, 177, 231, 246], '-9999999999999'],
|
||||
[true, true, [1, 0, 156, 88, 76, 73, 31, 242], '-999999999999999999'],
|
||||
// BE
|
||||
[false, false, [0x12], '18'],
|
||||
[false, false, [0x12, 0x34], '4660'],
|
||||
[false, false, [0x12, 0x34, 0x56], '1193046'],
|
||||
[false, false, [0x12, 0x34, 0x56, 0x78], '305419896'],
|
||||
[false, true, [0xf2, 0x34, 0x56, 0x78], '-231451016'],
|
||||
[false, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '78187493530'],
|
||||
[false, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '20015998343868'],
|
||||
[false, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '24197857161011715162171839636988778104']
|
||||
];
|
||||
|
||||
const ptest = arrayRange(65536).map((v) => [v]);
|
||||
|
||||
describe('nToU8a', (): void => {
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
|
||||
const bitLength = numarr.length * 8;
|
||||
|
||||
it(`#${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
|
||||
expect(
|
||||
nToU8a(
|
||||
BigInt(strval),
|
||||
{ bitLength, isLe, isNegative }
|
||||
)
|
||||
).toEqual(new Uint8Array(numarr));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
nToU8a(null)
|
||||
).toEqual(new Uint8Array(1));
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (bitLength)', (): void => {
|
||||
expect(
|
||||
nToU8a(null, { bitLength: 32 })
|
||||
).toEqual(new Uint8Array([0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation', (): void => {
|
||||
expect(
|
||||
nToU8a(0x123456n, { isLe: false })
|
||||
).toEqual(new Uint8Array([0x12, 0x34, 0x56]));
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation (bitLength)', (): void => {
|
||||
expect(
|
||||
nToU8a(0x123456n, { bitLength: 32, isLe: false })
|
||||
).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56]));
|
||||
});
|
||||
|
||||
it('converts using little endian (as set)', (): void => {
|
||||
expect(
|
||||
nToU8a(0x123456n, { bitLength: 32, isLe: true })
|
||||
).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
|
||||
});
|
||||
|
||||
perf('nToU8a', 250000, ptest, nToU8a);
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { NumberOptions, ToBigInt, ToBn } from '../types.js';
|
||||
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
|
||||
import { _0n, _1n } from './consts.js';
|
||||
import { nToBigInt } from './toBigInt.js';
|
||||
|
||||
const DIV = BigInt(256);
|
||||
const NEG_MASK = BigInt(0xff);
|
||||
|
||||
function toU8a (value: bigint, isLe: boolean, isNegative: boolean): Uint8Array {
|
||||
const arr: number[] = [];
|
||||
const withSigned = isNegative && (value < _0n);
|
||||
|
||||
if (withSigned) {
|
||||
value = (value + _1n) * -_1n;
|
||||
}
|
||||
|
||||
while (value !== _0n) {
|
||||
const mod = value % DIV;
|
||||
const val = Number(
|
||||
withSigned
|
||||
? mod ^ NEG_MASK
|
||||
: mod
|
||||
);
|
||||
|
||||
if (isLe) {
|
||||
arr.push(val);
|
||||
} else {
|
||||
arr.unshift(val);
|
||||
}
|
||||
|
||||
value = (value - mod) / DIV;
|
||||
}
|
||||
|
||||
return Uint8Array.from(arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nToU8a
|
||||
* @summary Creates a Uint8Array object from a bigint.
|
||||
*/
|
||||
export function nToU8a <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = true, isNegative = false }: NumberOptions = {}): Uint8Array {
|
||||
const valueBi = nToBigInt(value);
|
||||
|
||||
if (valueBi === _0n) {
|
||||
return bitLength === -1
|
||||
? new Uint8Array(1)
|
||||
: new Uint8Array(Math.ceil((bitLength || 0) / 8));
|
||||
}
|
||||
|
||||
const u8a = toU8a(valueBi, isLe, isNegative);
|
||||
|
||||
if (bitLength === -1) {
|
||||
return u8a;
|
||||
}
|
||||
|
||||
const byteLength = Math.ceil((bitLength || 0) / 8);
|
||||
const output = new Uint8Array(byteLength);
|
||||
|
||||
if (isNegative) {
|
||||
output.fill(0xff);
|
||||
}
|
||||
|
||||
output.set(u8a, isLe ? 0 : byteLength - u8a.length);
|
||||
|
||||
return output;
|
||||
}
|
||||
Reference in new issue
Block a user