@freesignal/protocol 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/crypto.js DELETED
@@ -1,156 +0,0 @@
1
- "use strict";
2
- /**
3
- * FreeSignal Protocol
4
- *
5
- * Copyright (C) 2025 Christian Braghette
6
- *
7
- * This program is free software: you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation, either version 3 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program. If not, see <https://www.gnu.org/licenses/>
19
- */
20
- var __importDefault = (this && this.__importDefault) || function (mod) {
21
- return (mod && mod.__esModule) ? mod : { "default": mod };
22
- };
23
- Object.defineProperty(exports, "__esModule", { value: true });
24
- const js_sha3_1 = require("js-sha3");
25
- const tweetnacl_1 = __importDefault(require("tweetnacl"));
26
- const uuid_1 = require("uuid");
27
- const utils_1 = require("./utils");
28
- class CryptoConstructor {
29
- constructor() {
30
- this.KeyPair = {
31
- isKeyPair(obj) {
32
- if (typeof obj === 'object' && obj.publicKey && obj.secretKey)
33
- return true;
34
- return false;
35
- }
36
- };
37
- this.box = new CryptoConstructor.box();
38
- this.ECDH = new CryptoConstructor.ECDH();
39
- this.EdDSA = new CryptoConstructor.EdDSA();
40
- this.UUID = new CryptoConstructor.UUID();
41
- this.randomBytes = tweetnacl_1.default.randomBytes;
42
- }
43
- hash(message, algorithm = 'sha256') {
44
- switch (algorithm) {
45
- case 'sha224':
46
- return new Uint8Array(js_sha3_1.sha3_224.digest(message));
47
- case 'sha256':
48
- return new Uint8Array(js_sha3_1.sha3_256.digest(message));
49
- case 'sha384':
50
- return new Uint8Array(js_sha3_1.sha3_384.digest(message));
51
- case 'sha512':
52
- return new Uint8Array(js_sha3_1.sha3_512.digest(message));
53
- default:
54
- throw new Error("Error hashing");
55
- }
56
- }
57
- hmac(key, message, length = 32, algorithm = 'kmac256') {
58
- length *= 8;
59
- switch (algorithm) {
60
- case 'kmac128':
61
- return new Uint8Array(js_sha3_1.kmac128.digest(key, message, length, new Uint8Array()));
62
- case 'kmac256':
63
- return new Uint8Array(js_sha3_1.kmac256.digest(key, message, length, new Uint8Array()));
64
- default:
65
- throw new Error("Error hashing");
66
- }
67
- }
68
- hkdf(key, salt, info, length = 32) {
69
- return new Uint8Array(js_sha3_1.kmac256.digest(key, salt, length * 8, info !== null && info !== void 0 ? info : new Uint8Array()));
70
- }
71
- scalarMult(n, p) {
72
- return tweetnacl_1.default.scalarMult(n, p);
73
- }
74
- }
75
- (function (CryptoConstructor) {
76
- class box {
77
- constructor() {
78
- this.keyLength = tweetnacl_1.default.secretbox.keyLength;
79
- this.nonceLength = tweetnacl_1.default.secretbox.nonceLength;
80
- }
81
- encrypt(msg, nonce, key) {
82
- return tweetnacl_1.default.secretbox(msg, nonce, key);
83
- }
84
- decrypt(msg, nonce, key) {
85
- var _a;
86
- return (_a = tweetnacl_1.default.secretbox.open(msg, nonce, key)) !== null && _a !== void 0 ? _a : undefined;
87
- }
88
- }
89
- CryptoConstructor.box = box;
90
- class ECDH {
91
- constructor() {
92
- this.publicKeyLength = tweetnacl_1.default.box.publicKeyLength;
93
- this.secretKeyLength = tweetnacl_1.default.box.secretKeyLength;
94
- }
95
- keyPair(secretKey) {
96
- if (secretKey)
97
- return tweetnacl_1.default.box.keyPair.fromSecretKey(secretKey);
98
- return tweetnacl_1.default.box.keyPair();
99
- }
100
- sharedKey(publicKey, secretKey) {
101
- return tweetnacl_1.default.scalarMult(publicKey, secretKey);
102
- }
103
- }
104
- CryptoConstructor.ECDH = ECDH;
105
- class EdDSA {
106
- constructor() {
107
- this.publicKeyLength = tweetnacl_1.default.sign.publicKeyLength;
108
- this.secretKeyLength = tweetnacl_1.default.sign.secretKeyLength;
109
- this.signatureLength = tweetnacl_1.default.sign.signatureLength;
110
- this.seedLength = tweetnacl_1.default.sign.seedLength;
111
- }
112
- keyPair(secretKey) {
113
- if (secretKey)
114
- return tweetnacl_1.default.sign.keyPair.fromSecretKey(secretKey);
115
- return tweetnacl_1.default.sign.keyPair();
116
- }
117
- keyPairFromSeed(seed) {
118
- return tweetnacl_1.default.sign.keyPair.fromSeed(seed);
119
- }
120
- sign(msg, secretKey) {
121
- return tweetnacl_1.default.sign.detached(msg, secretKey);
122
- }
123
- verify(msg, sig, publicKey) {
124
- return tweetnacl_1.default.sign.detached.verify(msg, sig, publicKey);
125
- }
126
- }
127
- CryptoConstructor.EdDSA = EdDSA;
128
- class UUID {
129
- generate() {
130
- return new UUIDv4();
131
- }
132
- stringify(arr, offset) {
133
- return (0, uuid_1.stringify)(arr, offset);
134
- }
135
- parse(uuid) {
136
- return (0, uuid_1.parse)(uuid);
137
- }
138
- }
139
- CryptoConstructor.UUID = UUID;
140
- class UUIDv4 {
141
- constructor() {
142
- this.value = (0, uuid_1.v4)();
143
- }
144
- toString() {
145
- return this.value;
146
- }
147
- toJSON() {
148
- return this.value;
149
- }
150
- toBuffer() {
151
- return (0, utils_1.decodeUTF8)(this.value);
152
- }
153
- }
154
- })(CryptoConstructor || (CryptoConstructor = {}));
155
- const crypto = new CryptoConstructor();
156
- exports.default = crypto;
package/data.d.ts DELETED
@@ -1,67 +0,0 @@
1
- /**
2
- * FreeSignal Protocol
3
- *
4
- * Copyright (C) 2025 Christian Braghette
5
- *
6
- * This program is free software: you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation, either version 3 of the License, or
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program. If not, see <https://www.gnu.org/licenses/>
18
- */
19
- import { Encodable } from "./types";
20
- export declare enum Protocols {
21
- NULL = "",
22
- MESSAGE = "/freesignal/message/1.0.0",
23
- RELAY = "/freesignal/relay/1.0.0"
24
- }
25
- export declare namespace Protocols {
26
- function isProtocol(protocol: any): boolean;
27
- function fromCode(code: number): Protocols;
28
- function toCode(protocol: Protocols): number;
29
- function encode(protocol: Protocols, length?: number): Uint8Array;
30
- function decode(array: Uint8Array): Protocols;
31
- }
32
- export interface Datagram {
33
- readonly id: string;
34
- readonly version: number;
35
- readonly senderKey: string;
36
- readonly senderRelay?: string;
37
- readonly receiverKey: string;
38
- readonly receiverRelay?: string;
39
- readonly protocol: Protocols;
40
- readonly createdAt: number;
41
- payload?: Uint8Array;
42
- }
43
- export declare namespace Datagram {
44
- const version = 1;
45
- function create(sender: Uint8Array | string, receiver: Uint8Array | string, protocol: Protocols, payload?: Uint8Array | Encodable): DatagramConstructor;
46
- function isDatagram(obj: any): boolean;
47
- function from(data: Uint8Array | Datagram | string): DatagramConstructor;
48
- }
49
- declare class DatagramConstructor implements Encodable, Datagram {
50
- readonly id: string;
51
- readonly version: number;
52
- readonly senderKey: string;
53
- readonly senderRelay?: string;
54
- readonly receiverKey: string;
55
- readonly receiverRelay?: string;
56
- readonly protocol: Protocols;
57
- readonly createdAt: number;
58
- payload?: Uint8Array;
59
- private static headerOffset;
60
- constructor(sender: Uint8Array | string, receiver: Uint8Array | string, protocol: Protocols, payload?: Uint8Array | Encodable);
61
- constructor(data: Uint8Array | Datagram);
62
- encode(compression?: boolean): Uint8Array;
63
- encodeSigned(secretKey: Uint8Array, compression?: boolean): Uint8Array;
64
- toString(): string;
65
- toJSON(): string;
66
- }
67
- export {};
package/data.js DELETED
@@ -1,181 +0,0 @@
1
- "use strict";
2
- /**
3
- * FreeSignal Protocol
4
- *
5
- * Copyright (C) 2025 Christian Braghette
6
- *
7
- * This program is free software: you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation, either version 3 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program. If not, see <https://www.gnu.org/licenses/>
19
- */
20
- var __importDefault = (this && this.__importDefault) || function (mod) {
21
- return (mod && mod.__esModule) ? mod : { "default": mod };
22
- };
23
- Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.Datagram = exports.Protocols = void 0;
25
- const utils_1 = require("./utils");
26
- const crypto_1 = __importDefault(require("./crypto"));
27
- const fflate_1 = __importDefault(require("fflate"));
28
- var Protocols;
29
- (function (Protocols) {
30
- Protocols["NULL"] = "";
31
- Protocols["MESSAGE"] = "/freesignal/message/1.0.0";
32
- Protocols["RELAY"] = "/freesignal/relay/1.0.0";
33
- })(Protocols || (exports.Protocols = Protocols = {}));
34
- (function (Protocols) {
35
- function isProtocol(protocol) {
36
- return Object.values(Protocols).includes(protocol);
37
- }
38
- Protocols.isProtocol = isProtocol;
39
- function fromCode(code) {
40
- return Object.values(Protocols)[code];
41
- }
42
- Protocols.fromCode = fromCode;
43
- function toCode(protocol) {
44
- return Object.values(Protocols).indexOf(protocol);
45
- }
46
- Protocols.toCode = toCode;
47
- function encode(protocol, length) {
48
- /*const raw = numberToUint8Array(Protocols.toCode(protocol), length).reverse();
49
- raw[0] |= (raw.length - 1) << 6;
50
- return raw;*/
51
- return (0, utils_1.numberToUint8Array)(Protocols.toCode(protocol), length !== null && length !== void 0 ? length : 4, 'big');
52
- }
53
- Protocols.encode = encode;
54
- function decode(array) {
55
- array[0] &= 63;
56
- array = array.reverse();
57
- return Protocols.fromCode((0, utils_1.numberFromUint8Array)(array));
58
- }
59
- Protocols.decode = decode;
60
- })(Protocols || (exports.Protocols = Protocols = {}));
61
- var Datagram;
62
- (function (Datagram) {
63
- Datagram.version = 1;
64
- function create(sender, receiver, protocol, payload) {
65
- return new DatagramConstructor(sender, receiver, protocol, payload);
66
- }
67
- Datagram.create = create;
68
- function isDatagram(obj) {
69
- return obj instanceof DatagramConstructor || (obj && typeof obj === 'object' && 'id' in obj && 'version' in obj && 'sender' in obj && 'receiver' in obj && 'protocol' in obj && 'createdAt' in obj);
70
- }
71
- Datagram.isDatagram = isDatagram;
72
- function from(data) {
73
- if (typeof data === 'string') {
74
- const decoded = (0, utils_1.decodeBase64)(data);
75
- return new DatagramConstructor(decoded);
76
- }
77
- else
78
- return new DatagramConstructor(data);
79
- }
80
- Datagram.from = from;
81
- })(Datagram || (exports.Datagram = Datagram = {}));
82
- class DatagramConstructor {
83
- constructor(data, receiver, protocol, payload) {
84
- if (!receiver && !protocol && !payload) {
85
- if (data instanceof Uint8Array) {
86
- this.version = data[0] & 63;
87
- this.protocol = Protocols.decode(data.subarray(1, 4));
88
- this.id = crypto_1.default.UUID.stringify(data.subarray(4, 20));
89
- this.createdAt = (0, utils_1.numberFromUint8Array)(data.subarray(20, 28));
90
- this.senderKey = (0, utils_1.encodeBase64)(data.subarray(28, 28 + crypto_1.default.EdDSA.publicKeyLength));
91
- this.receiverKey = (0, utils_1.encodeBase64)(data.subarray(28 + crypto_1.default.EdDSA.publicKeyLength, DatagramConstructor.headerOffset));
92
- const senderRelayOffset = data.indexOf(255, DatagramConstructor.headerOffset);
93
- const receiverRelayOffset = data.indexOf(255, senderRelayOffset + 1);
94
- this.senderRelay = (0, utils_1.encodeUTF8)(data.subarray(DatagramConstructor.headerOffset, senderRelayOffset)) ? "" : undefined;
95
- this.receiverRelay = (0, utils_1.encodeUTF8)(data.subarray(senderRelayOffset + 1, receiverRelayOffset)) ? "" : undefined;
96
- if (data[0] & 128) {
97
- const signature = data.subarray(data.length - crypto_1.default.EdDSA.signatureLength);
98
- if (!crypto_1.default.EdDSA.verify(data.subarray(0, data.length - crypto_1.default.EdDSA.signatureLength), signature, data.subarray(28, 28 + crypto_1.default.EdDSA.publicKeyLength)))
99
- throw new Error('Invalid signature for Datagram');
100
- }
101
- if (data[0] & 64)
102
- this.payload = fflate_1.default.inflateSync(data.subarray(receiverRelayOffset + 1, data.length));
103
- else
104
- this.payload = data.subarray(receiverRelayOffset + 1, data.length);
105
- }
106
- else if (Datagram.isDatagram(data)) {
107
- const datagram = data;
108
- this.id = datagram.id;
109
- this.version = datagram.version;
110
- this.senderKey = datagram.senderKey;
111
- this.receiverKey = datagram.receiverKey;
112
- this.protocol = datagram.protocol;
113
- this.createdAt = datagram.createdAt;
114
- this.senderRelay = datagram.senderRelay;
115
- this.receiverRelay = datagram.receiverRelay;
116
- this.payload = datagram.payload;
117
- }
118
- else
119
- throw new Error('Invalid constructor arguments for Datagram');
120
- }
121
- else if (typeof data === 'string' || data instanceof Uint8Array) {
122
- this.id = crypto_1.default.UUID.generate().toString();
123
- this.version = Datagram.version;
124
- if (typeof data === 'string') {
125
- const address = data.split('@');
126
- this.senderKey = address[0];
127
- this.senderRelay = address[1];
128
- }
129
- else
130
- this.senderKey = (0, utils_1.encodeBase64)(data);
131
- if (typeof receiver === 'string') {
132
- const address = receiver.split('@');
133
- this.receiverKey = address[0];
134
- this.receiverRelay = address[1];
135
- }
136
- else
137
- this.receiverKey = (0, utils_1.encodeBase64)(receiver);
138
- this.protocol = protocol;
139
- this.createdAt = Date.now();
140
- this.payload = payload instanceof Uint8Array ? payload : payload === null || payload === void 0 ? void 0 : payload.encode();
141
- }
142
- else
143
- throw new Error('Invalid constructor arguments for Datagram');
144
- }
145
- encode(compression = true) {
146
- var _a;
147
- compression = compression && this.payload != undefined && this.payload.length > 1024;
148
- return (0, utils_1.concatUint8Array)(new Uint8Array(1).fill(this.version | (compression ? 64 : 0)), //1
149
- Protocols.encode(this.protocol, 3), //3
150
- (_a = crypto_1.default.UUID.parse(this.id)) !== null && _a !== void 0 ? _a : [], //16
151
- (0, utils_1.numberToUint8Array)(this.createdAt, 8), //8
152
- (0, utils_1.decodeBase64)(this.senderKey), //32
153
- (0, utils_1.decodeBase64)(this.receiverKey), //32
154
- ...(this.senderRelay ? [(0, utils_1.decodeUTF8)(this.senderRelay)] : []), new Uint8Array(1).fill(255), ...(this.receiverRelay ? [(0, utils_1.decodeUTF8)(this.receiverRelay)] : []), new Uint8Array(1).fill(255), ...(this.payload ? [compression ? fflate_1.default.deflateSync(this.payload) : this.payload] : []));
155
- }
156
- encodeSigned(secretKey, compression) {
157
- //if (!this.payload) throw new Error('Cannot sign a datagram without payload');
158
- const header = this.encode(compression);
159
- header[0] |= 128; // Set the sign bit
160
- const signature = crypto_1.default.EdDSA.sign(header, secretKey);
161
- return (0, utils_1.concatUint8Array)(header, signature);
162
- }
163
- toString() {
164
- return (0, utils_1.encodeBase64)(this.encode());
165
- }
166
- toJSON() {
167
- /*return JSON.stringify({
168
- id: this.id,
169
- version: this.version,
170
- senderKey: this.senderKey,
171
- senderRelay: this.senderRelay,
172
- receiverKey: this.receiverKey,
173
- receiverRelay: this.receiverRelay,
174
- protocol: this.protocol,
175
- createdAt: this.createdAt,
176
- payload: this.payload ? encodeBase64(this.payload) : undefined
177
- });*/
178
- return this.toString();
179
- }
180
- }
181
- DatagramConstructor.headerOffset = 28 + crypto_1.default.EdDSA.publicKeyLength * 2;
package/utils.d.ts DELETED
@@ -1,78 +0,0 @@
1
- /**
2
- * FreeSignal Protocol
3
- *
4
- * Copyright (C) 2025 Christian Braghette
5
- *
6
- * This program is free software: you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation, either version 3 of the License, or
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program. If not, see <https://www.gnu.org/licenses/>
18
- */
19
- /**
20
- * Decodes a Uint8Array into a UTF-8 string.
21
- *
22
- * @param array - The input byte array.
23
- * @returns The UTF-8 encoded string.
24
- */
25
- export declare function encodeUTF8(array?: Uint8Array): string;
26
- /**
27
- * Encodes a UTF-8 string into a Uint8Array.
28
- *
29
- * @param string - The input string.
30
- * @returns The resulting Uint8Array.
31
- */
32
- export declare function decodeUTF8(string?: string): Uint8Array;
33
- /**
34
- * Encodes a Uint8Array into a Base64 string.
35
- *
36
- * @param array - The input byte array.
37
- * @returns The Base64 encoded string.
38
- */
39
- export declare function encodeBase64(array?: Uint8Array): string;
40
- /**
41
- * Decodes a Base64 string into a Uint8Array.
42
- *
43
- * @param string - The Base64 string.
44
- * @returns The decoded Uint8Array.
45
- */
46
- export declare function decodeBase64(string?: string): Uint8Array;
47
- export declare function encodeHex(array?: Uint8Array): string;
48
- export declare function decodeHex(string?: string): Uint8Array;
49
- /**
50
- * Converts a Uint8Array into a number.
51
- *
52
- * @param array - The input byte array.
53
- * @returns The resulting number.
54
- */
55
- export declare function numberFromUint8Array(array?: Uint8Array, endian?: 'big' | 'little'): number;
56
- /**
57
- * Converts a number into a Uint8Array of specified length.
58
- *
59
- * @param number - The number to convert.
60
- * @param length - The desired output length.
61
- * @returns A Uint8Array representing the number.
62
- */
63
- export declare function numberToUint8Array(number?: number, length?: number, endian?: 'big' | 'little'): Uint8Array;
64
- /**
65
- * Compare Uint8Arrays.
66
- *
67
- * @param a - First Uint8Array to compare to.
68
- * @param b - Arrays to compare to the first one.
69
- * @returns A boolean value.
70
- */
71
- export declare function verifyUint8Array(a?: Uint8Array, ...b: (Uint8Array | undefined)[]): boolean;
72
- /**
73
- * Concat Uint8Arrays.
74
- *
75
- * @param arrays - Uint8Array to concat.
76
- * @returns A Uint8Array
77
- */
78
- export declare function concatUint8Array(...arrays: Uint8Array[]): Uint8Array;
package/utils.js DELETED
@@ -1,146 +0,0 @@
1
- "use strict";
2
- /**
3
- * FreeSignal Protocol
4
- *
5
- * Copyright (C) 2025 Christian Braghette
6
- *
7
- * This program is free software: you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation, either version 3 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program. If not, see <https://www.gnu.org/licenses/>
19
- */
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.encodeUTF8 = encodeUTF8;
22
- exports.decodeUTF8 = decodeUTF8;
23
- exports.encodeBase64 = encodeBase64;
24
- exports.decodeBase64 = decodeBase64;
25
- exports.encodeHex = encodeHex;
26
- exports.decodeHex = decodeHex;
27
- exports.numberFromUint8Array = numberFromUint8Array;
28
- exports.numberToUint8Array = numberToUint8Array;
29
- exports.verifyUint8Array = verifyUint8Array;
30
- exports.concatUint8Array = concatUint8Array;
31
- const base64_js_1 = require("base64-js");
32
- const tweetnacl_1 = require("tweetnacl");
33
- /**
34
- * Decodes a Uint8Array into a UTF-8 string.
35
- *
36
- * @param array - The input byte array.
37
- * @returns The UTF-8 encoded string.
38
- */
39
- function encodeUTF8(array) {
40
- return new TextDecoder().decode(array);
41
- }
42
- /**
43
- * Encodes a UTF-8 string into a Uint8Array.
44
- *
45
- * @param string - The input string.
46
- * @returns The resulting Uint8Array.
47
- */
48
- function decodeUTF8(string) {
49
- return new TextEncoder().encode(string);
50
- }
51
- /**
52
- * Encodes a Uint8Array into a Base64 string.
53
- *
54
- * @param array - The input byte array.
55
- * @returns The Base64 encoded string.
56
- */
57
- function encodeBase64(array) {
58
- return (0, base64_js_1.fromByteArray)(array !== null && array !== void 0 ? array : new Uint8Array());
59
- }
60
- /**
61
- * Decodes a Base64 string into a Uint8Array.
62
- *
63
- * @param string - The Base64 string.
64
- * @returns The decoded Uint8Array.
65
- */
66
- function decodeBase64(string) {
67
- return (0, base64_js_1.toByteArray)(string !== null && string !== void 0 ? string : "");
68
- }
69
- function encodeHex(array) {
70
- var _a;
71
- return Array.from((_a = array === null || array === void 0 ? void 0 : array.values()) !== null && _a !== void 0 ? _a : []).map(value => value.toString(16).padStart(2, '0')).join('');
72
- }
73
- function decodeHex(string) {
74
- return new Uint8Array(!string ? [] :
75
- Array.from(string).reduce((prev, curr, index) => {
76
- if (index % 2 === 0)
77
- prev.push(curr);
78
- else
79
- prev[prev.length - 1] += curr;
80
- return prev;
81
- }, []).map(value => Number.parseInt(value, 16)));
82
- }
83
- /**
84
- * Converts a Uint8Array into a number.
85
- *
86
- * @param array - The input byte array.
87
- * @returns The resulting number.
88
- */
89
- function numberFromUint8Array(array, endian = 'little') {
90
- let total = 0;
91
- if (array) {
92
- if (endian === 'big')
93
- array = array.reverse();
94
- for (let c = 0; c < array.length; c++)
95
- total += array[c] << (c * 8);
96
- }
97
- return total;
98
- }
99
- /**
100
- * Converts a number into a Uint8Array of specified length.
101
- *
102
- * @param number - The number to convert.
103
- * @param length - The desired output length.
104
- * @returns A Uint8Array representing the number.
105
- */
106
- function numberToUint8Array(number, length, endian = 'little') {
107
- if (!number)
108
- return new Uint8Array(length !== null && length !== void 0 ? length : 0).fill(0);
109
- const arr = [];
110
- while (number > 0) {
111
- arr.push(number & 255);
112
- number = number >>> 8;
113
- }
114
- const out = new Uint8Array(length !== null && length !== void 0 ? length : arr.length);
115
- out.set(arr);
116
- return endian === 'little' ? out : out.reverse();
117
- }
118
- /**
119
- * Compare Uint8Arrays.
120
- *
121
- * @param a - First Uint8Array to compare to.
122
- * @param b - Arrays to compare to the first one.
123
- * @returns A boolean value.
124
- */
125
- function verifyUint8Array(a, ...b) {
126
- b = b.filter(value => value !== undefined);
127
- if (!a || b.length === 0)
128
- return false;
129
- return b.every(b => (0, tweetnacl_1.verify)(a, b));
130
- }
131
- /**
132
- * Concat Uint8Arrays.
133
- *
134
- * @param arrays - Uint8Array to concat.
135
- * @returns A Uint8Array
136
- */
137
- function concatUint8Array(...arrays) {
138
- //arrays = arrays.filter(array => array != undefined);
139
- const out = new Uint8Array(arrays.map(value => value.length).reduce((prev, curr) => prev + curr));
140
- let offset = 0;
141
- arrays.forEach(array => {
142
- out.set(array, offset);
143
- offset += array.length;
144
- });
145
- return out;
146
- }