1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.bnToU8a = void 0;
|
4 | const toBn_js_1 = require("./toBn.js");
|
5 | const DEFAULT_OPTS = { bitLength: -1, isLe: true, isNegative: false };
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function bnToU8a(value, { bitLength = -1, isLe = true, isNegative = false } = DEFAULT_OPTS) {
|
21 | const valueBn = (0, toBn_js_1.bnToBn)(value);
|
22 | const byteLength = bitLength === -1
|
23 | ? Math.ceil(valueBn.bitLength() / 8)
|
24 | : Math.ceil((bitLength || 0) / 8);
|
25 | if (!value) {
|
26 | return bitLength === -1
|
27 | ? new Uint8Array(1)
|
28 | : new Uint8Array(byteLength);
|
29 | }
|
30 | const output = new Uint8Array(byteLength);
|
31 | const bn = isNegative
|
32 | ? valueBn.toTwos(byteLength * 8)
|
33 | : valueBn;
|
34 | output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
|
35 | return output;
|
36 | }
|
37 | exports.bnToU8a = bnToU8a;
|