UNPKG

1.14 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.hexToBn = hexToBn;
7
8var _bn = require("../bn/bn");
9
10var _boolean = require("../is/boolean");
11
12var _spread = require("../object/spread");
13
14var _stripPrefix = require("./stripPrefix");
15
16// Copyright 2017-2022 @polkadot/util authors & contributors
17// SPDX-License-Identifier: Apache-2.0
18
19/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
20function hexToBn(value) {
21 let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
22
23 if (!value || value === '0x') {
24 return new _bn.BN(0);
25 } // For hex, default to BE
26
27
28 const {
29 isLe,
30 isNegative
31 } = (0, _spread.objectSpread)({
32 isLe: false,
33 isNegative: false
34 }, (0, _boolean.isBoolean)(options) ? {
35 isLe: options
36 } : options);
37 const stripped = (0, _stripPrefix.hexStripPrefix)(value);
38 const bn = new _bn.BN(stripped, 16, isLe ? 'le' : 'be'); // fromTwos takes as parameter the number of bits, which is the hex length
39 // multiplied by 4 (2 bytes being 8 bits)
40
41 return isNegative ? bn.fromTwos(stripped.length * 4) : bn;
42}
\No newline at end of file