@polkadot/util
Version:
A collection of useful utilities for @polkadot
42 lines (31 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hexToBn = hexToBn;
var _bn = require("../bn/bn");
var _boolean = require("../is/boolean");
var _spread = require("../object/spread");
var _stripPrefix = require("./stripPrefix");
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
function hexToBn(value) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!value || value === '0x') {
return new _bn.BN(0);
} // For hex, default to BE
const {
isLe,
isNegative
} = (0, _spread.objectSpread)({
isLe: false,
isNegative: false
}, (0, _boolean.isBoolean)(options) ? {
isLe: options
} : options);
const stripped = (0, _stripPrefix.hexStripPrefix)(value);
const bn = new _bn.BN(stripped, 16, isLe ? 'le' : 'be'); // fromTwos takes as parameter the number of bits, which is the hex length
// multiplied by 4 (2 bytes being 8 bits)
return isNegative ? bn.fromTwos(stripped.length * 4) : bn;
}