1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.hexToBn = void 0;
|
4 | const bn_js_1 = require("../bn/bn.js");
|
5 | const stripPrefix_js_1 = require("./stripPrefix.js");
|
6 | /**
|
7 | * @name hexToBn
|
8 | * @summary Creates a BN.js object from a hex string.
|
9 | * @description
|
10 | * `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
11 | * @param _value The value to convert
|
12 | * @param _options Options to pass while converting
|
13 | * @param _options.isLe Convert using Little Endian
|
14 | * @param _options.isNegative Convert using two's complement
|
15 | * @example
|
16 | * <BR>
|
17 | *
|
18 | * ```javascript
|
19 | * import { hexToBn } from '@polkadot/util';
|
20 | *
|
21 | * hexToBn('0x123480001f'); // => BN(0x123480001f)
|
22 | * ```
|
23 | */
|
24 | function hexToBn(value, { isLe = false, isNegative = false } = {}) {
|
25 | if (!value || value === '0x') {
|
26 | return new bn_js_1.BN(0);
|
27 | }
|
28 | const stripped = (0, stripPrefix_js_1.hexStripPrefix)(value);
|
29 | const bn = new bn_js_1.BN(stripped, 16, isLe ? 'le' : 'be');
|
30 | // fromTwos takes as parameter the number of bits, which is the hex length
|
31 | // multiplied by 4 (2 bytes being 8 bits)
|
32 | return isNegative
|
33 | ? bn.fromTwos(stripped.length * 4)
|
34 | : bn;
|
35 | }
|
36 | exports.hexToBn = hexToBn;
|