1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.hexToU8a = hexToU8a;
|
7 |
|
8 | var _stripPrefix = require("./stripPrefix");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function hexToU8a(_value) {
|
29 | let bitLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
|
30 |
|
31 | if (!_value) {
|
32 | return new Uint8Array();
|
33 | }
|
34 |
|
35 | const value = (0, _stripPrefix.hexStripPrefix)(_value);
|
36 | const buf = Buffer.from(value, 'hex');
|
37 | const valLength = value.length / 2;
|
38 | const resultLength = Math.ceil(bitLength === -1 ? valLength : bitLength / 8);
|
39 |
|
40 | if (resultLength === valLength) {
|
41 | return Uint8Array.from(buf);
|
42 | }
|
43 |
|
44 | const offset = resultLength > valLength ? resultLength - valLength : 0;
|
45 |
|
46 | if (offset) {
|
47 | const u8a = new Uint8Array(resultLength);
|
48 | u8a.set(buf, offset);
|
49 | return u8a;
|
50 | }
|
51 |
|
52 | return Uint8Array.from(buf.slice(0, resultLength));
|
53 | } |
\ | No newline at end of file |