1 | // Copyright 2017-2022 @polkadot/util authors & contributors
|
2 | // SPDX-License-Identifier: Apache-2.0
|
3 | import { isNumber } from "../is/number.js";
|
4 | import { objectSpread } from "../object/spread.js";
|
5 | import { u8aToHex } from "../u8a/index.js";
|
6 | import { bnToU8a } from "./toU8a.js";
|
7 | const ZERO_STR = '0x00';
|
8 | const DEFAULT_OPTS = {
|
9 | bitLength: -1,
|
10 | isLe: false,
|
11 | isNegative: false
|
12 | };
|
13 | /**
|
14 | * @name bnToHex
|
15 | * @summary Creates a hex value from a BN.js bignumber object.
|
16 | * @description
|
17 | * `null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. With `bitLength` set, it fixes the number to the specified length.
|
18 | * @example
|
19 | * <BR>
|
20 | *
|
21 | * ```javascript
|
22 | * import BN from 'bn.js';
|
23 | * import { bnToHex } from '@polkadot/util';
|
24 | *
|
25 | * bnToHex(new BN(0x123456)); // => '0x123456'
|
26 | * ```
|
27 | */
|
28 |
|
29 | /** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
30 | function bnToHex(value, arg1 = DEFAULT_OPTS, arg2) {
|
31 | return !value ? ZERO_STR : u8aToHex(bnToU8a(value, objectSpread( // We spread here, the default for hex values is BE (JSONRPC via substrate)
|
32 | {
|
33 | isLe: false,
|
34 | isNegative: false
|
35 | }, isNumber(arg1) ? {
|
36 | bitLength: arg1,
|
37 | isLe: arg2
|
38 | } : arg1)));
|
39 | }
|
40 |
|
41 | export { bnToHex }; |
\ | No newline at end of file |