Align BigInt functions with BN (option handling) (#1587)
* Align BigInt functions with BN (option handling) * CHANGELOG * Pass bitLength parameter
This commit is contained in:
@@ -2,11 +2,17 @@
|
||||
|
||||
## master
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Ledger support for Stafi (Thanks to https://github.com/kaelnew)
|
||||
|
||||
Changes:
|
||||
|
||||
- Remove deprecated signatures for `bnToHex`, `bnToU8a`, `formatBalance`, `hexToBn`, `u8aToBn`
|
||||
- Align option handling of `n*` (BigInt) functions with `bn*` (BN)
|
||||
- Allow for usage without `@types/node` installed
|
||||
- Adjust for available ESM-only dependencies
|
||||
- Upgrade dependencies to latest stable versions
|
||||
- Upgrade to `@polkadot/wasm-crypto` 6.2.3
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import type { BN } from '../bn/bn';
|
||||
import type { HexString, NumberOptions, ToBigInt, ToBn } from '../types';
|
||||
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { nToU8a } from './toU8a';
|
||||
|
||||
@@ -15,14 +14,8 @@ const ZERO_STR = '0x00';
|
||||
* @name nToHex
|
||||
* @summary Creates a hex value from a bigint object.
|
||||
*/
|
||||
export function nToHex <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): HexString {
|
||||
export function nToHex <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, { bitLength, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
|
||||
return !value
|
||||
? ZERO_STR
|
||||
: u8aToHex(
|
||||
nToU8a(value, objectSpread(
|
||||
// We spread here, the default for hex values is BE (JSONRPC via substrate)
|
||||
{ isLe: false, isNegative: false },
|
||||
options
|
||||
))
|
||||
);
|
||||
: u8aToHex(nToU8a(value, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
|
||||
@@ -6,14 +6,13 @@ import type { NumberOptions, ToBigInt, ToBn } from '../types';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { _0n, _1n } from './consts';
|
||||
import { nToBigInt } from './toBigInt';
|
||||
|
||||
const DIV = BigInt(256);
|
||||
const NEG_MASK = BigInt(0xff);
|
||||
|
||||
function toU8a (value: bigint, { isLe, isNegative }: NumberOptions): Uint8Array {
|
||||
function toU8a (value: bigint, isLe: boolean, isNegative: boolean): Uint8Array {
|
||||
const arr: number[] = [];
|
||||
|
||||
if (isNegative) {
|
||||
@@ -44,34 +43,29 @@ function toU8a (value: bigint, { isLe, isNegative }: NumberOptions): Uint8Array
|
||||
* @name nToU8a
|
||||
* @summary Creates a Uint8Array object from a bigint.
|
||||
*/
|
||||
export function nToU8a <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): Uint8Array {
|
||||
const opts: NumberOptions = objectSpread(
|
||||
{ bitLength: -1, isLe: true, isNegative: false },
|
||||
options
|
||||
);
|
||||
|
||||
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 opts.bitLength === -1
|
||||
return bitLength === -1
|
||||
? new Uint8Array()
|
||||
: new Uint8Array(Math.ceil((opts.bitLength || 0) / 8));
|
||||
: new Uint8Array(Math.ceil((bitLength || 0) / 8));
|
||||
}
|
||||
|
||||
const u8a = toU8a(valueBi, opts);
|
||||
const u8a = toU8a(valueBi, isLe, isNegative);
|
||||
|
||||
if (opts.bitLength === -1) {
|
||||
if (bitLength === -1) {
|
||||
return u8a;
|
||||
}
|
||||
|
||||
const byteLength = Math.ceil((opts.bitLength || 0) / 8);
|
||||
const byteLength = Math.ceil((bitLength || 0) / 8);
|
||||
const output = new Uint8Array(byteLength);
|
||||
|
||||
if (opts.isNegative) {
|
||||
if (isNegative) {
|
||||
output.fill(0xff);
|
||||
}
|
||||
|
||||
output.set(u8a, opts.isLe ? 0 : byteLength - u8a.length);
|
||||
output.set(u8a, isLe ? 0 : byteLength - u8a.length);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { u8aToBigInt } from '../u8a/toBigInt';
|
||||
import { hexToU8a } from './toU8a';
|
||||
|
||||
@@ -13,11 +12,8 @@ import { hexToU8a } from './toU8a';
|
||||
* @name hexToBigInt
|
||||
* @summary Creates a BigInt instance object from a hex string.
|
||||
*/
|
||||
export function hexToBigInt (value?: string | null, options: ToBnOptions = {}): bigint {
|
||||
export function hexToBigInt (value?: string | null, { isLe = false, isNegative = false }: ToBnOptions = {}): bigint {
|
||||
return !value || value === '0x'
|
||||
? BigInt(0)
|
||||
: u8aToBigInt(
|
||||
hexToU8a(value),
|
||||
objectSpread({ isLe: false, isNegative: false }, options)
|
||||
);
|
||||
: u8aToBigInt(hexToU8a(value), { isLe, isNegative });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user