glow.ts 1.0.2 → 1.1.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/README.md +6 -21
- package/dist/glow.cjs.js +4 -0
- package/dist/glow.cjs.js.map +1 -0
- package/dist/glow.es.js +993 -0
- package/dist/glow.es.js.map +1 -0
- package/dist/index.d.ts +544 -0
- package/package.json +34 -26
- package/dist/cjs2/config.js +0 -18
- package/dist/cjs2/config.js.map +0 -1
- package/dist/cjs2/crypto-storage/crypto-storage.js +0 -118
- package/dist/cjs2/crypto-storage/crypto-storage.js.map +0 -1
- package/dist/cjs2/crypto-storage/in-memory-storage.js +0 -36
- package/dist/cjs2/crypto-storage/in-memory-storage.js.map +0 -1
- package/dist/cjs2/crypto-storage/local-storage.driver.js +0 -36
- package/dist/cjs2/crypto-storage/local-storage.driver.js.map +0 -1
- package/dist/cjs2/crypto-storage/storage-driver.interface.js +0 -3
- package/dist/cjs2/crypto-storage/storage-driver.interface.js.map +0 -1
- package/dist/cjs2/index.js +0 -28
- package/dist/cjs2/index.js.map +0 -1
- package/dist/cjs2/keyring/keyring.js +0 -151
- package/dist/cjs2/keyring/keyring.js.map +0 -1
- package/dist/cjs2/keys/keys.js +0 -40
- package/dist/cjs2/keys/keys.js.map +0 -1
- package/dist/cjs2/mailbox/mailbox.js +0 -352
- package/dist/cjs2/mailbox/mailbox.js.map +0 -1
- package/dist/cjs2/nacl/encryption.helper.js +0 -101
- package/dist/cjs2/nacl/encryption.helper.js.map +0 -1
- package/dist/cjs2/nacl/js-nacl-driver.js +0 -110
- package/dist/cjs2/nacl/js-nacl-driver.js.map +0 -1
- package/dist/cjs2/nacl/keypair.interface.js +0 -3
- package/dist/cjs2/nacl/keypair.interface.js.map +0 -1
- package/dist/cjs2/nacl/nacl-driver.interface.js +0 -3
- package/dist/cjs2/nacl/nacl-driver.interface.js.map +0 -1
- package/dist/cjs2/nacl/nacl.js +0 -29
- package/dist/cjs2/nacl/nacl.js.map +0 -1
- package/dist/cjs2/relay/network-error.js +0 -12
- package/dist/cjs2/relay/network-error.js.map +0 -1
- package/dist/cjs2/relay/relay-factory.js +0 -22
- package/dist/cjs2/relay/relay-factory.js.map +0 -1
- package/dist/cjs2/relay/relay.js +0 -300
- package/dist/cjs2/relay/relay.js.map +0 -1
- package/dist/cjs2/utils/utils.js +0 -39
- package/dist/cjs2/utils/utils.js.map +0 -1
- package/dist/cjs2/zax.interface.js +0 -46
- package/dist/cjs2/zax.interface.js.map +0 -1
- package/dist/esm/config.js +0 -15
- package/dist/esm/config.js.map +0 -1
- package/dist/esm/crypto-storage/crypto-storage.js +0 -114
- package/dist/esm/crypto-storage/crypto-storage.js.map +0 -1
- package/dist/esm/crypto-storage/in-memory-storage.js +0 -32
- package/dist/esm/crypto-storage/in-memory-storage.js.map +0 -1
- package/dist/esm/crypto-storage/local-storage.driver.js +0 -32
- package/dist/esm/crypto-storage/local-storage.driver.js.map +0 -1
- package/dist/esm/crypto-storage/storage-driver.interface.js +0 -2
- package/dist/esm/crypto-storage/storage-driver.interface.js.map +0 -1
- package/dist/esm/index.js +0 -14
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/keyring/keyring.js +0 -147
- package/dist/esm/keyring/keyring.js.map +0 -1
- package/dist/esm/keys/keys.js +0 -36
- package/dist/esm/keys/keys.js.map +0 -1
- package/dist/esm/mailbox/mailbox.js +0 -348
- package/dist/esm/mailbox/mailbox.js.map +0 -1
- package/dist/esm/nacl/encryption.helper.js +0 -97
- package/dist/esm/nacl/encryption.helper.js.map +0 -1
- package/dist/esm/nacl/js-nacl-driver.js +0 -106
- package/dist/esm/nacl/js-nacl-driver.js.map +0 -1
- package/dist/esm/nacl/keypair.interface.js +0 -2
- package/dist/esm/nacl/keypair.interface.js.map +0 -1
- package/dist/esm/nacl/nacl-driver.interface.js +0 -2
- package/dist/esm/nacl/nacl-driver.interface.js.map +0 -1
- package/dist/esm/nacl/nacl.js +0 -25
- package/dist/esm/nacl/nacl.js.map +0 -1
- package/dist/esm/relay/network-error.js +0 -8
- package/dist/esm/relay/network-error.js.map +0 -1
- package/dist/esm/relay/relay-factory.js +0 -18
- package/dist/esm/relay/relay-factory.js.map +0 -1
- package/dist/esm/relay/relay.js +0 -293
- package/dist/esm/relay/relay.js.map +0 -1
- package/dist/esm/utils/utils.js +0 -35
- package/dist/esm/utils/utils.js.map +0 -1
- package/dist/esm/zax.interface.js +0 -43
- package/dist/esm/zax.interface.js.map +0 -1
- package/dist/types/config.d.ts +0 -10
- package/dist/types/config.d.ts.map +0 -1
- package/dist/types/crypto-storage/crypto-storage.d.ts +0 -20
- package/dist/types/crypto-storage/crypto-storage.d.ts.map +0 -1
- package/dist/types/crypto-storage/in-memory-storage.d.ts +0 -17
- package/dist/types/crypto-storage/in-memory-storage.d.ts.map +0 -1
- package/dist/types/crypto-storage/local-storage.driver.d.ts +0 -15
- package/dist/types/crypto-storage/local-storage.driver.d.ts.map +0 -1
- package/dist/types/crypto-storage/storage-driver.interface.d.ts +0 -11
- package/dist/types/crypto-storage/storage-driver.interface.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -16
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/keyring/keyring.d.ts +0 -43
- package/dist/types/keyring/keyring.d.ts.map +0 -1
- package/dist/types/keys/keys.d.ts +0 -16
- package/dist/types/keys/keys.d.ts.map +0 -1
- package/dist/types/mailbox/mailbox.d.ts +0 -148
- package/dist/types/mailbox/mailbox.d.ts.map +0 -1
- package/dist/types/nacl/encryption.helper.d.ts +0 -37
- package/dist/types/nacl/encryption.helper.d.ts.map +0 -1
- package/dist/types/nacl/js-nacl-driver.d.ts +0 -39
- package/dist/types/nacl/js-nacl-driver.d.ts.map +0 -1
- package/dist/types/nacl/keypair.interface.d.ts +0 -5
- package/dist/types/nacl/keypair.interface.d.ts.map +0 -1
- package/dist/types/nacl/nacl-driver.interface.d.ts +0 -28
- package/dist/types/nacl/nacl-driver.interface.d.ts.map +0 -1
- package/dist/types/nacl/nacl.d.ts +0 -13
- package/dist/types/nacl/nacl.d.ts.map +0 -1
- package/dist/types/relay/network-error.d.ts +0 -6
- package/dist/types/relay/network-error.d.ts.map +0 -1
- package/dist/types/relay/relay-factory.d.ts +0 -10
- package/dist/types/relay/relay-factory.d.ts.map +0 -1
- package/dist/types/relay/relay.d.ts +0 -88
- package/dist/types/relay/relay.d.ts.map +0 -1
- package/dist/types/utils/utils.d.ts +0 -9
- package/dist/types/utils/utils.d.ts.map +0 -1
- package/dist/types/zax.interface.d.ts +0 -101
- package/dist/types/zax.interface.d.ts.map +0 -1
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { NaCl } from './nacl';
|
|
2
|
-
import { Utils } from '../utils/utils';
|
|
3
|
-
/**
|
|
4
|
-
* Includes several static helpers that are build on top of basic NaCl driver methods,
|
|
5
|
-
* to avoid duplicating code in driver implementations.
|
|
6
|
-
*/
|
|
7
|
-
export class EncryptionHelper {
|
|
8
|
-
constructor() { }
|
|
9
|
-
/**
|
|
10
|
-
* Encodes a binary message with `cryptobox`
|
|
11
|
-
*/
|
|
12
|
-
static async encodeMessage(message, pkTo, skFrom, nonceData) {
|
|
13
|
-
const nacl = NaCl.getInstance();
|
|
14
|
-
const nonce = await this.makeNonce(nonceData);
|
|
15
|
-
const ctext = await nacl.crypto_box(message, nonce, pkTo, skFrom);
|
|
16
|
-
return {
|
|
17
|
-
nonce: Utils.toBase64(nonce),
|
|
18
|
-
ctext: Utils.toBase64(ctext)
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Decodes a binary message with `cryptobox_open`
|
|
23
|
-
* @returns null if failed to decode
|
|
24
|
-
*/
|
|
25
|
-
static async decodeMessage(nonce, ctext, pkFrom, skTo) {
|
|
26
|
-
const nacl = NaCl.getInstance();
|
|
27
|
-
const data = await nacl.crypto_box_open(ctext, nonce, pkFrom, skTo);
|
|
28
|
-
if (data) {
|
|
29
|
-
return await nacl.decode_utf8(data);
|
|
30
|
-
}
|
|
31
|
-
return data;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Encodes a binary message with `secretbox` (used for file chunk encryption)
|
|
35
|
-
*/
|
|
36
|
-
static async encodeMessageSymmetric(message, secretKey) {
|
|
37
|
-
const nacl = NaCl.getInstance();
|
|
38
|
-
const nonce = await EncryptionHelper.makeNonce();
|
|
39
|
-
const ctext = await nacl.crypto_secretbox(message, nonce, secretKey);
|
|
40
|
-
return {
|
|
41
|
-
nonce: Utils.toBase64(nonce),
|
|
42
|
-
ctext: Utils.toBase64(ctext)
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Decodes a binary message with `secretbox_open` (used for file chunk decryption)
|
|
47
|
-
*/
|
|
48
|
-
static async decodeMessageSymmetric(nonce, ctext, secretKey) {
|
|
49
|
-
const nacl = NaCl.getInstance();
|
|
50
|
-
return await nacl.crypto_secretbox_open(Utils.fromBase64(ctext), Utils.fromBase64(nonce), secretKey);
|
|
51
|
-
}
|
|
52
|
-
// ---------- Nonce helper ----------
|
|
53
|
-
/**
|
|
54
|
-
* Makes a timestamp nonce that a relay expects for any crypto operations.
|
|
55
|
-
* Timestamp is the first 8 bytes, the rest is random, unless custom `data`
|
|
56
|
-
* is specified. `data` will be packed as next 4 bytes after timestamp.
|
|
57
|
-
*/
|
|
58
|
-
static async makeNonce(data) {
|
|
59
|
-
const nacl = NaCl.getInstance();
|
|
60
|
-
const nonce = await nacl.crypto_box_random_nonce();
|
|
61
|
-
let headerLen;
|
|
62
|
-
if (nonce.length !== nacl.crypto_box_NONCEBYTES) {
|
|
63
|
-
throw new Error('[Mailbox] Wrong crypto_box nonce length');
|
|
64
|
-
}
|
|
65
|
-
// split timestamp integer as an array of bytes
|
|
66
|
-
headerLen = 8; // max timestamp size
|
|
67
|
-
const aTime = this.itoa(Math.floor(Date.now() / 1000));
|
|
68
|
-
if (data) {
|
|
69
|
-
headerLen += 4; // extra 4 bytes for custom data
|
|
70
|
-
}
|
|
71
|
-
// zero out nonce header area
|
|
72
|
-
nonce.fill(0, 0, headerLen);
|
|
73
|
-
// copy the timestamp into the first 8 bytes of nonce
|
|
74
|
-
nonce.set(aTime, 8 - aTime.length);
|
|
75
|
-
// copy data if present
|
|
76
|
-
if (data) {
|
|
77
|
-
const aData = this.itoa(data);
|
|
78
|
-
nonce.set(aData, 12 - aData.length);
|
|
79
|
-
}
|
|
80
|
-
return nonce;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Splits an integer into an array of bytes
|
|
84
|
-
*/
|
|
85
|
-
static itoa(num) {
|
|
86
|
-
// calculate length first
|
|
87
|
-
let hex = num.toString(16);
|
|
88
|
-
hex = hex.length & 1 ? `0${hex}` : hex;
|
|
89
|
-
const len = hex.length / 2;
|
|
90
|
-
const byteArray = new Uint8Array(len);
|
|
91
|
-
for (let j = 0, i = 0; i < hex.length; i += 2, j++) {
|
|
92
|
-
byteArray[j] = parseInt(hex[i] + hex[i + 1], 16);
|
|
93
|
-
}
|
|
94
|
-
return byteArray;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
//# sourceMappingURL=encryption.helper.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"encryption.helper.js","sourceRoot":"","sources":["../../../src/nacl/encryption.helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE9B,OAAO,EAAE,KAAK,EAAU,MAAM,gBAAgB,CAAC;AAE/C;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAE3B,gBAAwB,CAAC;IAEzB;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,OAAmB,EAAE,IAAgB,EAAE,MAAkB,EAAE,SAAkB;QACtG,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QAEhC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QAC9C,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AG,GAAG,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC3B,GAAG,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;QACvC,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;QAC3B,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;QAEtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;YAClD,SAAS,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;SAClD;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF"}
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { box, randomBytes, secretbox, hash } from 'tweetnacl';
|
|
2
|
-
import { sha256 } from 'js-sha256';
|
|
3
|
-
import { Utils } from '../utils/utils';
|
|
4
|
-
/**
|
|
5
|
-
* Implementation based on TweetNaCl.js: {@link https://github.com/dchest/tweetnacl-js}.
|
|
6
|
-
* SHA256 is taken separately from `js-sha256` library, because TweetNacl only offers
|
|
7
|
-
* SHA512 as hashing function, which is incompatible with the current Zax version.
|
|
8
|
-
* {@link https://github.com/emn178/js-sha256}
|
|
9
|
-
*
|
|
10
|
-
* All the methods of this class are actually synchronous, but they are defined
|
|
11
|
-
* as async to conform to the NaClDriver interface, which may have other implementations.
|
|
12
|
-
*/
|
|
13
|
-
export class JsNaClDriver {
|
|
14
|
-
constructor() {
|
|
15
|
-
this.crypto_secretbox_KEYBYTES = secretbox.keyLength;
|
|
16
|
-
this.crypto_box_NONCEBYTES = box.nonceLength;
|
|
17
|
-
}
|
|
18
|
-
async crypto_secretbox_random_nonce() {
|
|
19
|
-
return randomBytes(secretbox.nonceLength);
|
|
20
|
-
}
|
|
21
|
-
async crypto_secretbox(message, nonce, key) {
|
|
22
|
-
return secretbox(message, nonce, key);
|
|
23
|
-
}
|
|
24
|
-
async crypto_secretbox_open(box, nonce, key) {
|
|
25
|
-
return secretbox.open(box, nonce, key);
|
|
26
|
-
}
|
|
27
|
-
async crypto_box(message, nonce, pk, sk) {
|
|
28
|
-
return box(message, nonce, pk, sk);
|
|
29
|
-
}
|
|
30
|
-
async crypto_box_open(cipher, nonce, pk, sk) {
|
|
31
|
-
return box.open(cipher, nonce, pk, sk);
|
|
32
|
-
}
|
|
33
|
-
async crypto_box_random_nonce() {
|
|
34
|
-
return randomBytes(box.nonceLength);
|
|
35
|
-
}
|
|
36
|
-
async crypto_box_keypair() {
|
|
37
|
-
const pair = box.keyPair();
|
|
38
|
-
return {
|
|
39
|
-
boxPk: pair.publicKey,
|
|
40
|
-
boxSk: pair.secretKey
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
async crypto_box_keypair_from_raw_sk(key) {
|
|
44
|
-
const pair = box.keyPair.fromSecretKey(key);
|
|
45
|
-
return {
|
|
46
|
-
boxPk: pair.publicKey,
|
|
47
|
-
boxSk: pair.secretKey
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
async crypto_box_keypair_from_seed(seed) {
|
|
51
|
-
// using SHA512 to hash, per original NaCl docs
|
|
52
|
-
return this.crypto_box_keypair_from_raw_sk(hash(seed).subarray(0, box.secretKeyLength));
|
|
53
|
-
}
|
|
54
|
-
async crypto_hash_sha256(data) {
|
|
55
|
-
return this.from_hex(sha256(data));
|
|
56
|
-
}
|
|
57
|
-
async random_bytes(size) {
|
|
58
|
-
return randomBytes(size);
|
|
59
|
-
}
|
|
60
|
-
// Helper methods are based on `js-nacl` implementations
|
|
61
|
-
// https://github.com/tonyg/js-nacl/blob/cc70775cfc9d68a04905ca65c7f179b33a18066e/nacl_cooked.js
|
|
62
|
-
async encode_latin1(data) {
|
|
63
|
-
return Utils.encode_latin1(data);
|
|
64
|
-
}
|
|
65
|
-
async decode_latin1(data) {
|
|
66
|
-
return Utils.decode_latin1(data);
|
|
67
|
-
}
|
|
68
|
-
async encode_utf8(data) {
|
|
69
|
-
return this.encode_latin1(unescape(encodeURIComponent(data)));
|
|
70
|
-
}
|
|
71
|
-
async decode_utf8(data) {
|
|
72
|
-
return decodeURIComponent(escape(await this.decode_latin1(data)));
|
|
73
|
-
}
|
|
74
|
-
async to_hex(data) {
|
|
75
|
-
const encoded = [];
|
|
76
|
-
for (let i = 0; i < data.length; i++) {
|
|
77
|
-
const hexAlphabet = '0123456789abcdef';
|
|
78
|
-
encoded.push(hexAlphabet[(data[i] >> 4) & 15]);
|
|
79
|
-
encoded.push(hexAlphabet[data[i] & 15]);
|
|
80
|
-
}
|
|
81
|
-
return encoded.join('');
|
|
82
|
-
}
|
|
83
|
-
async from_hex(data) {
|
|
84
|
-
const result = new Uint8Array(data.length / 2);
|
|
85
|
-
for (let i = 0; i < data.length / 2; i++) {
|
|
86
|
-
result[i] = parseInt(data.substr(2 * i, 2), 16);
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* h2(m) = sha256(sha256(64x0 + m))
|
|
92
|
-
* Zero out initial sha256 block, and double hash 0-padded message
|
|
93
|
-
* http://cs.nyu.edu/~dodis/ps/h-of-h.pdf
|
|
94
|
-
*/
|
|
95
|
-
async h2(data) {
|
|
96
|
-
if (!(data instanceof Uint8Array)) {
|
|
97
|
-
data = await this.encode_latin1(data);
|
|
98
|
-
}
|
|
99
|
-
const zeroPaddingLength = 64;
|
|
100
|
-
const extendedSource = new Uint8Array(zeroPaddingLength + data.length);
|
|
101
|
-
extendedSource.fill(0);
|
|
102
|
-
extendedSource.set(data, zeroPaddingLength);
|
|
103
|
-
return this.crypto_hash_sha256(await this.crypto_hash_sha256(extendedSource));
|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AE;YACjC,IAAI,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;SACvC;QAED,MAAM,iBAAiB,GAAG,EAAE,CAAC;QAC7B,MAAM,cAAc,GAAG,IAAI,UAAU,CAAC,iBAAiB,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;QACvE,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACvB,cAAc,CAAC,GAAG,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC,kBAAkB,CAAC,MAAM,IAAI,CAAC,kBAAkB,CAAC,cAAc,CAAC,CAAC,CAAC;IAChF,CAAC;CACF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keypair.interface.js","sourceRoot":"","sources":["../../../src/nacl/keypair.interface.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nacl-driver.interface.js","sourceRoot":"","sources":["../../../src/nacl/nacl-driver.interface.ts"],"names":[],"mappings":""}
|
package/dist/esm/nacl/nacl.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { JsNaClDriver } from './js-nacl-driver';
|
|
2
|
-
/**
|
|
3
|
-
* Facade singleton to access the NaCl driver.
|
|
4
|
-
* Usage: call instance() with a chosen driver, and store the received object to perform further actions.
|
|
5
|
-
*/
|
|
6
|
-
export class NaCl {
|
|
7
|
-
constructor() { }
|
|
8
|
-
static setDefaultInstance() {
|
|
9
|
-
return this.setInstance(new JsNaClDriver());
|
|
10
|
-
}
|
|
11
|
-
static setInstance(driver) {
|
|
12
|
-
if (this.driverInstance) {
|
|
13
|
-
throw new Error('[NaCl] NaCl driver has been already set, it is supposed to be set only once');
|
|
14
|
-
}
|
|
15
|
-
this.driverInstance = driver;
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
static getInstance() {
|
|
19
|
-
if (!this.driverInstance) {
|
|
20
|
-
throw new Error('[NaCl] NaCl instance is not yet set');
|
|
21
|
-
}
|
|
22
|
-
return this.driverInstance;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=nacl.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nacl.js","sourceRoot":"","sources":["../../../src/nacl/nacl.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD;;;GAGG;AACH,MAAM,OAAO,IAAI;IAGf,gBAAwB,CAAC;IAElB,MAAM,CAAC,kBAAkB;QAC9B,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,YAAY,EAAE,CAAC,CAAC;IAC9C,CAAC;IAEM,MAAM,CAAC,WAAW,CAAC,MAAkB;QAC1C,IAAI,IAAI,CAAC,cAAc,EAAE;YACvB,MAAM,IAAI,KAAK,CAAC,6EAA6E,CAAC,CAAC;SAChG;QACD,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC;QAE7B,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,MAAM,CAAC,WAAW;QACvB,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE;YACxB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;SACxD;QAED,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"network-error.js","sourceRoot":"","sources":["../../../src/relay/network-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IAGrC,YAAmB,MAAwB;QACzC,KAAK,CAAC,wBAAwB,MAAM,EAAE,CAAC,CAAC;QADvB,WAAM,GAAN,MAAM,CAAkB;QAFlC,SAAI,GAAG,cAAc,CAAC;IAI/B,CAAC;CACF"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Relay } from './relay';
|
|
2
|
-
export class RelayFactory {
|
|
3
|
-
constructor() {
|
|
4
|
-
this.relays = {};
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Relay factory, that returns a Relay instance for a given URL,
|
|
8
|
-
* or creates a new one if it hasn't yet been initialized.
|
|
9
|
-
*/
|
|
10
|
-
getInstance(url) {
|
|
11
|
-
let relay = this.relays[url];
|
|
12
|
-
if (!relay) {
|
|
13
|
-
relay = this.relays[url] = new Relay(url);
|
|
14
|
-
}
|
|
15
|
-
return relay;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=relay-factory.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"relay-factory.js","sourceRoot":"","sources":["../../../src/relay/relay-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,OAAO,YAAY;IAAzB;QAEU,WAAM,GAA6B,EAAE,CAAC;IAahD,CAAC;IAXC;;;OAGG;IACH,WAAW,CAAC,GAAW;QACrB,IAAI,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,KAAK,EAAE;YACV,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;SAC3C;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
package/dist/esm/relay/relay.js
DELETED
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import { NaCl } from '../nacl/nacl';
|
|
3
|
-
import { EncryptionHelper } from '../nacl/encryption.helper';
|
|
4
|
-
import { config } from '../config';
|
|
5
|
-
import { Utils } from '../utils/utils';
|
|
6
|
-
import { Keys } from '../keys/keys';
|
|
7
|
-
import { RelayCommand } from '../zax.interface';
|
|
8
|
-
import { NetworkError } from './network-error';
|
|
9
|
-
/**
|
|
10
|
-
* Low-level operations with Zax relay
|
|
11
|
-
*/
|
|
12
|
-
export class Relay {
|
|
13
|
-
constructor(url) {
|
|
14
|
-
this.url = url;
|
|
15
|
-
this.difficulty = 0;
|
|
16
|
-
this.nacl = NaCl.getInstance();
|
|
17
|
-
}
|
|
18
|
-
get isConnected() {
|
|
19
|
-
// relay is assumed to be connected when it got session keys and server public
|
|
20
|
-
// sessionKeys are not permanent so relay won't be connected when sessionKeys will be removed within timeout
|
|
21
|
-
return !!(this.sessionKeys && this.publicKey);
|
|
22
|
-
}
|
|
23
|
-
// ---------- Connection initialization ----------
|
|
24
|
-
/**
|
|
25
|
-
* Exchanges tokens with a relay and gets a temp session key for this relay.
|
|
26
|
-
* Returns h₂(signature) and a relay public key
|
|
27
|
-
*/
|
|
28
|
-
async openConnection() {
|
|
29
|
-
this.sessionKeys = new Keys(await this.nacl.crypto_box_keypair());
|
|
30
|
-
this.clientToken = await this.nacl.random_bytes(config.RELAY_TOKEN_LEN);
|
|
31
|
-
const relayToken = await this.fetchRelayToken();
|
|
32
|
-
this.publicKey = await this.fetchRelayPublicKey(relayToken);
|
|
33
|
-
return {
|
|
34
|
-
h2Signature: await this.getSignature(relayToken, this.sessionKeys),
|
|
35
|
-
relayPublicKey: this.publicKey
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Sends a client token to a relay and saves a relay token
|
|
40
|
-
*/
|
|
41
|
-
async fetchRelayToken() {
|
|
42
|
-
if (!this.clientToken) {
|
|
43
|
-
throw new Error('[Relay] clientToken is required please openConnection first');
|
|
44
|
-
}
|
|
45
|
-
const data = await this.httpCall('start_session', Utils.toBase64(this.clientToken));
|
|
46
|
-
// Set a timer to mark a relay instance as having an expired token after a certain time
|
|
47
|
-
this.scheduleTokenExpiration();
|
|
48
|
-
// Relay responds with its own counter token. Until session is established these 2 tokens are handshake id.
|
|
49
|
-
const [token, difficulty] = this.parseResponse('start_session', data);
|
|
50
|
-
this.difficulty = parseInt(difficulty, 10);
|
|
51
|
-
if (this.difficulty > 10) {
|
|
52
|
-
console.log(`[Relay] ${this.url} requested difficulty ${this.difficulty}. Session handshake may take longer.`);
|
|
53
|
-
}
|
|
54
|
-
return Utils.fromBase64(token);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Completes the handshake and saves a relay pubic key
|
|
58
|
-
*/
|
|
59
|
-
async fetchRelayPublicKey(relayToken) {
|
|
60
|
-
if (!this.clientToken) {
|
|
61
|
-
throw new Error('[Relay] clientToken is required please openConnection first');
|
|
62
|
-
}
|
|
63
|
-
// After clientToken is sent to the relay, we use only h2() of it
|
|
64
|
-
const h2ClientToken = Utils.toBase64(await this.nacl.h2(this.clientToken));
|
|
65
|
-
const handshake = new Uint8Array([...this.clientToken, ...relayToken]);
|
|
66
|
-
let sessionHandshake;
|
|
67
|
-
// Compute session handshake based on difficulty level set by the server
|
|
68
|
-
if (this.difficulty === 0) {
|
|
69
|
-
sessionHandshake = await this.nacl.h2(handshake);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
sessionHandshake = await this.ensureNonceDifficulty(handshake);
|
|
73
|
-
}
|
|
74
|
-
// We confirm handshake by sending back h2(clientToken, relay_token)
|
|
75
|
-
const relayPk = await this.httpCall('verify_session', h2ClientToken, Utils.toBase64(sessionHandshake));
|
|
76
|
-
// Relay gives us back temp session key masked by clientToken we started with
|
|
77
|
-
return Utils.fromBase64(relayPk);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Attaches a mailbox and fetches number of messages
|
|
81
|
-
*/
|
|
82
|
-
async prove(payload) {
|
|
83
|
-
if (!this.clientToken) {
|
|
84
|
-
throw new Error('[Relay] clientToken is required please openConnection first');
|
|
85
|
-
}
|
|
86
|
-
if (!this.sessionKeys) {
|
|
87
|
-
throw new Error('[Relay] No session key found, open the connection first');
|
|
88
|
-
}
|
|
89
|
-
const h2ClientToken = Utils.toBase64(await this.nacl.h2(this.clientToken));
|
|
90
|
-
const result = await this.httpCall('prove', h2ClientToken, this.sessionKeys.publicKey, payload.nonce, payload.ctext);
|
|
91
|
-
this.scheduleSessionExpiration();
|
|
92
|
-
return result;
|
|
93
|
-
}
|
|
94
|
-
async encodeMessage(message) {
|
|
95
|
-
var _a;
|
|
96
|
-
if (!this.publicKey) {
|
|
97
|
-
throw new Error('[Relay] No relay public key found, open the connection first');
|
|
98
|
-
}
|
|
99
|
-
if (!this.sessionKeys) {
|
|
100
|
-
throw new Error('[Relay] No session key found, open the connection first');
|
|
101
|
-
}
|
|
102
|
-
return await EncryptionHelper.encodeMessage(await this.nacl.encode_utf8(message), this.publicKey, Utils.fromBase64((_a = this.sessionKeys) === null || _a === void 0 ? void 0 : _a.privateKey));
|
|
103
|
-
}
|
|
104
|
-
async decodeMessage(nonce, ctext) {
|
|
105
|
-
const relayPk = this.publicKey;
|
|
106
|
-
if (!relayPk) {
|
|
107
|
-
throw new Error('[Relay] No relay public key found, open the connection first');
|
|
108
|
-
}
|
|
109
|
-
if (!this.sessionKeys) {
|
|
110
|
-
throw new Error('[Relay] No session key found, open the connection first');
|
|
111
|
-
}
|
|
112
|
-
const decodedData = await EncryptionHelper.decodeMessage(Utils.fromBase64(nonce), Utils.fromBase64(ctext), relayPk, Utils.fromBase64(this.sessionKeys.privateKey));
|
|
113
|
-
if (decodedData === null) {
|
|
114
|
-
throw new Error('[Relay] failed to decode message');
|
|
115
|
-
}
|
|
116
|
-
return JSON.parse(decodedData);
|
|
117
|
-
}
|
|
118
|
-
// ---------- Low-level server request handling ----------
|
|
119
|
-
/**
|
|
120
|
-
* Executes a message/file command on a relay, parses and validates the response
|
|
121
|
-
*/
|
|
122
|
-
async runCmd(command, hpk, message, ctext) {
|
|
123
|
-
if (!Object.keys(RelayCommand).includes(command)) {
|
|
124
|
-
throw new Error(`[Relay] ${this.url} doesn't support command ${command}`);
|
|
125
|
-
}
|
|
126
|
-
const payload = [hpk, message.nonce, message.ctext];
|
|
127
|
-
// if we are sending symmetrically encrypted ctext, it goes on the next line in the payload
|
|
128
|
-
if (ctext) {
|
|
129
|
-
payload.push(ctext);
|
|
130
|
-
}
|
|
131
|
-
const response = await this.httpCall('command', ...payload);
|
|
132
|
-
return this.parseResponse(command, response);
|
|
133
|
-
}
|
|
134
|
-
async getSignature(relayToken, sessionKeys) {
|
|
135
|
-
if (!this.clientToken) {
|
|
136
|
-
throw new Error('[Relay] clientToken is required please openConnection first');
|
|
137
|
-
}
|
|
138
|
-
const clientTempPk = Utils.fromBase64(sessionKeys.publicKey);
|
|
139
|
-
// Alice creates a 32 byte session signature as h₂(a_temp_pk, relayToken, clientToken)
|
|
140
|
-
const signature = new Uint8Array([...clientTempPk, ...relayToken, ...this.clientToken]);
|
|
141
|
-
const h2Signature = await this.nacl.h2(signature);
|
|
142
|
-
return h2Signature;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Executes a call to a relay and return raw string response
|
|
146
|
-
*/
|
|
147
|
-
async httpCall(command, ...params) {
|
|
148
|
-
var _a, _b;
|
|
149
|
-
const requestPayload = {
|
|
150
|
-
url: `${this.url}/${command}`,
|
|
151
|
-
method: 'post',
|
|
152
|
-
headers: {
|
|
153
|
-
'Accept': 'text/plain',
|
|
154
|
-
'Content-Type': 'text/plain'
|
|
155
|
-
},
|
|
156
|
-
data: params.join('\r\n'),
|
|
157
|
-
responseType: 'text',
|
|
158
|
-
timeout: config.RELAY_AJAX_TIMEOUT
|
|
159
|
-
};
|
|
160
|
-
// NOTE: Network and server errors are not handled ny Glow itself.
|
|
161
|
-
// They should instead be handled where the library is used
|
|
162
|
-
let response;
|
|
163
|
-
try {
|
|
164
|
-
response = await axios(requestPayload);
|
|
165
|
-
}
|
|
166
|
-
catch (err) {
|
|
167
|
-
if (err.isAxiosError) {
|
|
168
|
-
const error = err;
|
|
169
|
-
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
|
|
170
|
-
// clear session if unauthorized
|
|
171
|
-
this.clearSession();
|
|
172
|
-
this.clearToken();
|
|
173
|
-
}
|
|
174
|
-
// when no network connection axios response is undefined
|
|
175
|
-
throw new NetworkError(((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) || 0);
|
|
176
|
-
}
|
|
177
|
-
throw err;
|
|
178
|
-
}
|
|
179
|
-
return String(response.data);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Parses relay response and throws an error if its format is unexpected
|
|
183
|
-
*/
|
|
184
|
-
parseResponse(command, rawResponse) {
|
|
185
|
-
const response = rawResponse.split('\r\n');
|
|
186
|
-
if (!rawResponse || !this.validateResponse(command, response.length)) {
|
|
187
|
-
console.log(response);
|
|
188
|
-
throw new Error(`[Relay] ${this.url} - ${command}: Bad response`);
|
|
189
|
-
}
|
|
190
|
-
return response;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Compares the expected number of lines in response with what was actually received from a relay
|
|
194
|
-
*/
|
|
195
|
-
validateResponse(command, lines) {
|
|
196
|
-
switch (command) {
|
|
197
|
-
case RelayCommand.upload:
|
|
198
|
-
case RelayCommand.messageStatus:
|
|
199
|
-
case RelayCommand.delete:
|
|
200
|
-
return lines === 1;
|
|
201
|
-
case RelayCommand.downloadFileChunk:
|
|
202
|
-
return lines === 3;
|
|
203
|
-
default:
|
|
204
|
-
return lines === 2;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* delete token locally before it was removed on server so will reconnect without getting unauthorized error
|
|
209
|
-
*/
|
|
210
|
-
scheduleTokenExpiration() {
|
|
211
|
-
if (this.tokenExpirationTimeoutHandle) {
|
|
212
|
-
clearTimeout(this.tokenExpirationTimeoutHandle);
|
|
213
|
-
}
|
|
214
|
-
this.tokenExpirationTimeoutHandle = setTimeout(() => this.clearToken(), config.RELAY_TOKEN_TIMEOUT);
|
|
215
|
-
}
|
|
216
|
-
clearToken() {
|
|
217
|
-
if (this.tokenExpirationTimeoutHandle) {
|
|
218
|
-
clearTimeout(this.tokenExpirationTimeoutHandle);
|
|
219
|
-
}
|
|
220
|
-
if (this.clientToken) {
|
|
221
|
-
delete this.clientToken;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* established session is valid only for some period need to remove it after it's ttl
|
|
226
|
-
* so relay will reconnect without receiving error from server
|
|
227
|
-
*/
|
|
228
|
-
scheduleSessionExpiration() {
|
|
229
|
-
if (this.sessionExpirationTimeoutHandle) {
|
|
230
|
-
clearTimeout(this.sessionExpirationTimeoutHandle);
|
|
231
|
-
}
|
|
232
|
-
this.sessionExpirationTimeoutHandle = setTimeout(() => {
|
|
233
|
-
this.clearSession();
|
|
234
|
-
}, config.RELAY_SESSION_TIMEOUT);
|
|
235
|
-
}
|
|
236
|
-
clearSession() {
|
|
237
|
-
if (this.sessionExpirationTimeoutHandle) {
|
|
238
|
-
clearTimeout(this.sessionExpirationTimeoutHandle);
|
|
239
|
-
}
|
|
240
|
-
if (this.sessionKeys) {
|
|
241
|
-
delete this.sessionKeys;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
// ---------- Difficulty adjustment ----------
|
|
245
|
-
/**
|
|
246
|
-
* Continuously calculates the nonce until one requested by a relay is found
|
|
247
|
-
*/
|
|
248
|
-
async ensureNonceDifficulty(handshake) {
|
|
249
|
-
let nonce;
|
|
250
|
-
let h2;
|
|
251
|
-
do {
|
|
252
|
-
nonce = await this.nacl.random_bytes(32);
|
|
253
|
-
h2 = await this.nacl.h2(new Uint8Array([...handshake, ...nonce]));
|
|
254
|
-
} while (!this.arrayZeroBits(h2));
|
|
255
|
-
return nonce;
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Returns `true` if the rightmost n bits of a byte are 0.
|
|
259
|
-
* Check whether the rightmost difficulty bits of an Uint8Array are 0, where
|
|
260
|
-
* the lowest indexes of the array represent those rightmost bits. Thus if
|
|
261
|
-
* the difficulty is 17, then array[0] and array[1] should be 0, as should the
|
|
262
|
-
* rightmost bit of array[2]. This is used for our difficulty settings in Zax to
|
|
263
|
-
* reduce burden on a busy server by ensuring clients have to do some
|
|
264
|
-
* additional work during the session handshake
|
|
265
|
-
*/
|
|
266
|
-
arrayZeroBits(array) {
|
|
267
|
-
let byte;
|
|
268
|
-
let n = this.difficulty;
|
|
269
|
-
for (let i = 0; i <= (1 + this.difficulty / 8); i++) {
|
|
270
|
-
byte = array[i];
|
|
271
|
-
if (n <= 0) {
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
if (n > 8) {
|
|
275
|
-
n -= 8;
|
|
276
|
-
if (byte > 0) {
|
|
277
|
-
return false;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
return this.firstZeroBits(byte, n);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
return false;
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Returns `true` if the rightmost n bits of a byte are 0
|
|
288
|
-
*/
|
|
289
|
-
firstZeroBits(byte, n) {
|
|
290
|
-
return byte === ((byte >> n) << n);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
//# sourceMappingURL=relay.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"relay.js","sourceRoot":"","sources":["../../../src/relay/relay.ts"],"names":[],"mappings":"AAAA,OAAO,KAAyC,MAAM,OAAO,CAAC;AAE9D,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAU,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAO/C;;GAEG;AACH,MAAM,OAAO,KAAK;IAShB,YAAmB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;QAPt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AAE;YACnD,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAChB,IAAI,CAAC,IAAI,CAAC,EAAE;gBACV,OAAO,IAAI,CAAC;aACb;YACD,IAAI,CAAC,GAAG,CAAC,EAAE;gBACT,CAAC,IAAI,CAAC,CAAC;gBACP,IAAI,IAAI,GAAG,CAAC,EAAE;oBACZ,OAAO,KAAK,CAAC;iBACd;aACF;iBAAM;gBACL,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;aACpC;SACF;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACK,aAAa,CAAC,IAAY,EAAE,CAAS;QAC3C,OAAO,IAAI,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IACrC,CAAC;CACF"}
|
package/dist/esm/utils/utils.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export class Utils {
|
|
2
|
-
static toBase64(input) {
|
|
3
|
-
const isBrowser = typeof btoa !== 'undefined';
|
|
4
|
-
return isBrowser ?
|
|
5
|
-
btoa(this.decode_latin1(input)) :
|
|
6
|
-
Buffer.from(this.decode_latin1(input), 'latin1').toString('base64');
|
|
7
|
-
}
|
|
8
|
-
static fromBase64(input) {
|
|
9
|
-
const isBrowser = typeof btoa !== 'undefined';
|
|
10
|
-
return isBrowser ?
|
|
11
|
-
this.encode_latin1(atob(input)) :
|
|
12
|
-
this.encode_latin1(Buffer.from(input, 'base64').toString('latin1'));
|
|
13
|
-
}
|
|
14
|
-
static encode_latin1(data) {
|
|
15
|
-
const result = new Uint8Array(data.length);
|
|
16
|
-
for (let i = 0; i < data.length; i++) {
|
|
17
|
-
const c = data.charCodeAt(i);
|
|
18
|
-
if ((c & 0xff) !== c)
|
|
19
|
-
throw { message: 'Cannot encode string in Latin1', str: data };
|
|
20
|
-
result[i] = (c & 0xff);
|
|
21
|
-
}
|
|
22
|
-
return result;
|
|
23
|
-
}
|
|
24
|
-
static decode_latin1(data) {
|
|
25
|
-
const encoded = [];
|
|
26
|
-
for (let i = 0; i < data.length; i++) {
|
|
27
|
-
encoded.push(String.fromCharCode(data[i]));
|
|
28
|
-
}
|
|
29
|
-
return encoded.join('');
|
|
30
|
-
}
|
|
31
|
-
static toObject(pairs) {
|
|
32
|
-
return Array.from(pairs).reduce((acc, [key, value]) => Object.assign(acc, { [key]: value }), {});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/utils/utils.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,KAAK;IAChB,MAAM,CAAC,QAAQ,CAAC,KAAiB;QAC/B,MAAM,SAAS,GAAG,OAAO,IAAI,KAAK,WAAW,CAAC;QAC9C,OAAO,SAAS,CAAC,CAAC;YAChB,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACjC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,KAAa;QAC7B,MAAM,SAAS,GAAG,OAAO,IAAI,KAAK,WAAW,CAAC;QAC9C,OAAO,SAAS,CAAC,CAAC;YAChB,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACjC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,IAAY;QAC/B,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC3C,KA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