1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.hexToU8a = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function hexToU8a(_value, bitLength = -1) {
|
20 | if (!_value) {
|
21 | return new Uint8Array();
|
22 | }
|
23 | const value = _value.startsWith('0x')
|
24 | ? _value.substring(2)
|
25 | : _value;
|
26 | const buf = Buffer.from(value, 'hex');
|
27 | const valLength = value.length / 2;
|
28 | const resultLength = Math.ceil(bitLength === -1
|
29 | ? valLength
|
30 | : bitLength / 8);
|
31 | if (resultLength === valLength) {
|
32 | return Uint8Array.from(buf);
|
33 | }
|
34 | const offset = resultLength > valLength
|
35 | ? resultLength - valLength
|
36 | : 0;
|
37 | if (offset) {
|
38 | const u8a = new Uint8Array(resultLength);
|
39 | u8a.set(buf, offset);
|
40 | return u8a;
|
41 | }
|
42 | return Uint8Array.from(buf.subarray(0, resultLength));
|
43 | }
|
44 | exports.hexToU8a = hexToU8a;
|