1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.nToU8a = void 0;
|
4 | const x_bigint_1 = require("@polkadot/x-bigint");
|
5 | const consts_js_1 = require("./consts.js");
|
6 | const toBigInt_js_1 = require("./toBigInt.js");
|
7 | const DIV = (0, x_bigint_1.BigInt)(256);
|
8 | const NEG_MASK = (0, x_bigint_1.BigInt)(0xff);
|
9 | function toU8a(value, isLe, isNegative) {
|
10 | const arr = [];
|
11 | const withSigned = isNegative && (value < consts_js_1._0n);
|
12 | if (withSigned) {
|
13 | value = (value + consts_js_1._1n) * -consts_js_1._1n;
|
14 | }
|
15 | while (value !== consts_js_1._0n) {
|
16 | const mod = value % DIV;
|
17 | const val = Number(withSigned
|
18 | ? mod ^ NEG_MASK
|
19 | : mod);
|
20 | if (isLe) {
|
21 | arr.push(val);
|
22 | }
|
23 | else {
|
24 | arr.unshift(val);
|
25 | }
|
26 | value = (value - mod) / DIV;
|
27 | }
|
28 | return Uint8Array.from(arr);
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function nToU8a(value, { bitLength = -1, isLe = true, isNegative = false } = {}) {
|
35 | const valueBi = (0, toBigInt_js_1.nToBigInt)(value);
|
36 | if (valueBi === consts_js_1._0n) {
|
37 | return bitLength === -1
|
38 | ? new Uint8Array(1)
|
39 | : new Uint8Array(Math.ceil((bitLength || 0) / 8));
|
40 | }
|
41 | const u8a = toU8a(valueBi, isLe, isNegative);
|
42 | if (bitLength === -1) {
|
43 | return u8a;
|
44 | }
|
45 | const byteLength = Math.ceil((bitLength || 0) / 8);
|
46 | const output = new Uint8Array(byteLength);
|
47 | if (isNegative) {
|
48 | output.fill(0xff);
|
49 | }
|
50 | output.set(u8a, isLe ? 0 : byteLength - u8a.length);
|
51 | return output;
|
52 | }
|
53 | exports.nToU8a = nToU8a;
|