UNPKG

1.33 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { BN } from "../bn/bn.js";
4import { isBoolean } from "../is/boolean.js";
5import { objectSpread } from "../object/spread.js";
6/**
7 * @name u8aToBn
8 * @summary Creates a BN from a Uint8Array object.
9 * @description
10 * `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
11 * @param value The value to convert
12 * @param options Options to pass while converting
13 * @param options.isLe Convert using Little Endian (default)
14 * @param options.isNegative Convert using two's complement
15 * @example
16 * <BR>
17 *
18 * ```javascript
19 * import { u8aToBn } from '@polkadot/util';
20 *
21 * u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
22 * ```
23 */
24
25/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
26function u8aToBn(value, options = {}) {
27 // NOTE: This is the same process as followed in the hexToBn conversion
28 // For Uint8Array, default to LE
29 const {
30 isLe,
31 isNegative
32 } = objectSpread({
33 isLe: true,
34 isNegative: false
35 }, isBoolean(options) ? {
36 isLe: options
37 } : options);
38 const bn = new BN(value, isLe ? 'le' : 'be');
39 return isNegative && value.length ? bn.fromTwos(value.length * 8) : bn;
40}
41
42export { u8aToBn };
\No newline at end of file