1 |
|
2 |
|
3 | import { hexStripPrefix } from "./stripPrefix.js";
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export function hexToU8a(_value, bitLength = -1) {
|
21 | if (!_value) {
|
22 | return new Uint8Array();
|
23 | }
|
24 |
|
25 | const value = hexStripPrefix(_value);
|
26 | const buf = Buffer.from(value, 'hex');
|
27 | const valLength = value.length / 2;
|
28 | const resultLength = Math.ceil(bitLength === -1 ? valLength : bitLength / 8);
|
29 |
|
30 | if (resultLength === valLength) {
|
31 | return Uint8Array.from(buf);
|
32 | }
|
33 |
|
34 | const offset = resultLength > valLength ? resultLength - valLength : 0;
|
35 |
|
36 | if (offset) {
|
37 | const u8a = new Uint8Array(resultLength);
|
38 | u8a.set(buf, offset);
|
39 | return u8a;
|
40 | }
|
41 |
|
42 | return Uint8Array.from(buf.slice(0, resultLength));
|
43 | } |
\ | No newline at end of file |