This commit is contained in:
Jaco
2021-11-15 14:30:31 +02:00
committed by GitHub
parent f9e5c033ab
commit ac06f38118
6 changed files with 25 additions and 24 deletions
+1 -1
View File
@@ -48,7 +48,7 @@
"@polkadot/x-textencoder": "7.8.3-16",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.12.0",
"camelcase": "^6.2.0",
"camelcase": "^6.2.1",
"ip-regex": "^4.3.0"
}
}
+3 -1
View File
@@ -14,6 +14,7 @@ interface Options extends ToBnOptions {
}
const ZERO_STR = '0x00';
const DEFAULT_OPTS: Options = { bitLength: -1, isLe: false, isNegative: false };
/**
* @name bnToHex
@@ -32,13 +33,14 @@ const ZERO_STR = '0x00';
*/
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString;
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString;
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: false, isNegative: false }, arg2?: boolean): HexString {
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = DEFAULT_OPTS, arg2?: boolean): HexString {
if (!value) {
return ZERO_STR;
}
return u8aToHex(
bnToU8a(value, objectSpread(
// We spread here, the default for hex values is BE (JSONRPC via substrate)
{ isLe: false, isNegative: false },
isNumber(arg1)
? { bitLength: arg1, isLe: arg2 }
+6 -2
View File
@@ -12,6 +12,8 @@ interface Options extends ToBnOptions {
bitLength?: number;
}
const DEFAULT_OPTS: Options = { bitLength: -1, isLe: true, isNegative: false };
function createEmpty (byteLength: number, options: Options): Uint8Array {
return options.bitLength === -1
? new Uint8Array()
@@ -20,7 +22,9 @@ function createEmpty (byteLength: number, options: Options): Uint8Array {
function createValue (valueBn: BN, byteLength: number, { isLe, isNegative }: Options): Uint8Array {
const output = new Uint8Array(byteLength);
const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn;
const bn = isNegative
? valueBn.toTwos(byteLength * 8)
: valueBn;
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
@@ -43,7 +47,7 @@ function createValue (valueBn: BN, byteLength: number, { isLe, isNegative }: Opt
*/
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array;
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = DEFAULT_OPTS, arg2?: boolean): Uint8Array {
const options: Options = objectSpread(
{ bitLength: -1, isLe: true, isNegative: false },
isNumber(arg1)
+7 -14
View File
@@ -8,11 +8,7 @@ import { isBoolean } from '../is/boolean';
import { objectSpread } from '../object/spread';
import { hexStripPrefix } from './stripPrefix';
function reverse (value: string): string {
return (value.match(/.{1,2}/g) || [])
.reverse()
.join('');
}
const DEFAULT_OPTS: ToBnOptions = { isLe: false, isNegative: false };
/**
* @name hexToBn
@@ -32,26 +28,23 @@ function reverse (value: string): string {
* hexToBn('0x123480001f'); // => BN(0x123480001f)
* ```
*/
export function hexToBn (value?: string | null, options: ToBnOptions | boolean = { isLe: false, isNegative: false }): BN {
export function hexToBn (value?: string | null, options: ToBnOptions | boolean = DEFAULT_OPTS): BN {
if (!value) {
return new BN(0);
}
const _options: ToBnOptions = objectSpread(
const { isLe, isNegative }: ToBnOptions = objectSpread(
{ isLe: false, isNegative: false },
isBoolean(options)
? { isLe: options }
: options
);
const _value = hexStripPrefix(value);
// FIXME: Use BN's 3rd argument `isLe` once this issue is fixed
// https://github.com/indutny/bn.js/issues/208
const bn = new BN((_options.isLe ? reverse(_value) : _value) || '00', 16);
const stripped = hexStripPrefix(value);
const bn = new BN(stripped || '00', 16, isLe ? 'le' : 'be');
// fromTwos takes as parameter the number of bits, which is the hex length
// multiplied by 4.
return _options.isNegative
? bn.fromTwos(_value.length * 4)
return isNegative
? bn.fromTwos(stripped.length * 4)
: bn;
}
+3 -1
View File
@@ -7,6 +7,8 @@ import type { ToBnOptions } from '../types';
import { hexToBn } from '../hex/toBn';
import { u8aToHex } from './toHex';
const DEFAULT_OPTS: ToBnOptions = { isLe: true, isNegative: false };
/**
* @name u8aToBn
* @summary Creates a BN from a Uint8Array object.
@@ -25,7 +27,7 @@ import { u8aToHex } from './toHex';
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = { isLe: true, isNegative: false }): BN {
export function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = DEFAULT_OPTS): BN {
return hexToBn(
u8aToHex(value),
options
+5 -5
View File
@@ -2119,7 +2119,7 @@ __metadata:
"@polkadot/x-textencoder": 7.8.3-16
"@types/bn.js": ^4.11.6
bn.js: ^4.12.0
camelcase: ^6.2.0
camelcase: ^6.2.1
ip-regex: ^4.3.0
languageName: unknown
linkType: soft
@@ -3665,10 +3665,10 @@ __metadata:
languageName: node
linkType: hard
"camelcase@npm:^6.2.0":
version: 6.2.0
resolution: "camelcase@npm:6.2.0"
checksum: 8335cfd0ecc472eae685896a42afd8c9dacd193a91f569120b931c87deb053a1ba82102031b9b48a4dbc1d18066caeacf2e4ace8c3c7f0d02936d348dc0b5a87
"camelcase@npm:^6.2.0, camelcase@npm:^6.2.1":
version: 6.2.1
resolution: "camelcase@npm:6.2.1"
checksum: d876272ef76391ebf8442fb7ea1d77e80ae179ce1339e021a8731b4895fd190dc19e148e045469cff5825d4c089089f3fff34d804d3f49115d55af97dd6ac0af
languageName: node
linkType: hard