1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.hexToU8a = hexToU8a;
|
7 |
|
8 | var _alphabet = require("./alphabet");
|
9 |
|
10 | var _stripPrefix = require("./stripPrefix");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function hexToU8a(_value) {
|
31 | let bitLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
|
32 |
|
33 | if (!_value) {
|
34 | return new Uint8Array();
|
35 | }
|
36 |
|
37 | const value = (0, _stripPrefix.hexStripPrefix)(_value).toLowerCase();
|
38 | const valLength = value.length / 2;
|
39 | const endLength = Math.ceil(bitLength === -1 ? valLength : bitLength / 8);
|
40 | const result = new Uint8Array(endLength);
|
41 | const offset = endLength > valLength ? endLength - valLength : 0;
|
42 | const dv = new DataView(result.buffer, offset);
|
43 | const mod = (endLength - offset) % 2;
|
44 | const length = endLength - offset - mod;
|
45 |
|
46 | for (let i = 0; i < length; i += 2) {
|
47 | const idx = i * 2;
|
48 | dv.setUint16(i, _alphabet.HEX_TO_U16[value.substring(idx, idx + 4)]);
|
49 | }
|
50 |
|
51 | if (mod) {
|
52 | dv.setUint8(length, _alphabet.HEX_TO_U8[value.substring(value.length - 2)]);
|
53 | }
|
54 |
|
55 | return result;
|
56 | } |
\ | No newline at end of file |