From ac06f38118ad8185c085aa51f064d0118ac040f4 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 15 Nov 2021 13:30:31 +0100 Subject: [PATCH] Bump deps (#1187) --- packages/util/package.json | 2 +- packages/util/src/bn/toHex.ts | 4 +++- packages/util/src/bn/toU8a.ts | 8 ++++++-- packages/util/src/hex/toBn.ts | 21 +++++++-------------- packages/util/src/u8a/toBn.ts | 4 +++- yarn.lock | 10 +++++----- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/util/package.json b/packages/util/package.json index 1e70af8a1..ceeffc313 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -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" } } diff --git a/packages/util/src/bn/toHex.ts b/packages/util/src/bn/toHex.ts index 3143e9f88..145c9cc31 100644 --- a/packages/util/src/bn/toHex.ts +++ b/packages/util/src/bn/toHex.ts @@ -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 (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString; function bnToHex (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString; -function bnToHex (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: false, isNegative: false }, arg2?: boolean): HexString { +function bnToHex (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 } diff --git a/packages/util/src/bn/toU8a.ts b/packages/util/src/bn/toU8a.ts index e97113bf7..1250b0ce1 100644 --- a/packages/util/src/bn/toU8a.ts +++ b/packages/util/src/bn/toU8a.ts @@ -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 (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array; function bnToU8a (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array; -function bnToU8a (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array { +function bnToU8a (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) diff --git a/packages/util/src/hex/toBn.ts b/packages/util/src/hex/toBn.ts index fd410cd7c..a1a592bfa 100644 --- a/packages/util/src/hex/toBn.ts +++ b/packages/util/src/hex/toBn.ts @@ -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; } diff --git a/packages/util/src/u8a/toBn.ts b/packages/util/src/u8a/toBn.ts index 2959ce216..293898dd7 100644 --- a/packages/util/src/u8a/toBn.ts +++ b/packages/util/src/u8a/toBn.ts @@ -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 diff --git a/yarn.lock b/yarn.lock index 420ced2ee..a919f54b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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