1 |
|
2 |
|
3 | import { isNumber } from "../is/number.js";
|
4 | import { objectSpread } from "../object/spread.js";
|
5 | import { bnToBn } from "./toBn.js";
|
6 | const DEFAULT_OPTS = {
|
7 | bitLength: -1,
|
8 | isLe: true,
|
9 | isNegative: false
|
10 | };
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function bnToU8a(value, arg1 = DEFAULT_OPTS, arg2) {
|
28 | const {
|
29 | bitLength,
|
30 | isLe,
|
31 | isNegative
|
32 | } = objectSpread({
|
33 | bitLength: -1,
|
34 | isLe: true,
|
35 | isNegative: false
|
36 | }, isNumber(arg1) ? {
|
37 | bitLength: arg1,
|
38 | isLe: arg2
|
39 | } : arg1);
|
40 | const valueBn = bnToBn(value);
|
41 | const byteLength = bitLength === -1 ? Math.ceil(valueBn.bitLength() / 8) : Math.ceil((bitLength || 0) / 8);
|
42 |
|
43 | if (!value) {
|
44 | return bitLength === -1 ? new Uint8Array() : new Uint8Array(byteLength);
|
45 | }
|
46 |
|
47 | const output = new Uint8Array(byteLength);
|
48 | const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn;
|
49 | output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
|
50 | return output;
|
51 | }
|
52 |
|
53 | export { bnToU8a }; |
\ | No newline at end of file |