@hive-p2p/server 1.0.18 → 1.0.20
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/core/arbiter.mjs +125 -0
- package/core/config.mjs +226 -0
- package/core/crypto-codex.mjs +257 -0
- package/core/gossip.mjs +159 -0
- package/core/ice-offer-manager.mjs +181 -0
- package/core/node-services.mjs +129 -0
- package/core/node.mjs +177 -0
- package/core/peer-store.mjs +252 -0
- package/core/route-builder.mjs +176 -0
- package/core/topologist.mjs +254 -0
- package/core/unicast.mjs +155 -0
- package/libs/xxhash32.mjs +65 -0
- package/package.json +5 -5
- package/rendering/NetworkRenderer.mjs +734 -0
- package/rendering/renderer-options.mjs +85 -0
- package/rendering/renderer-stores.mjs +234 -0
- package/rendering/visualizer.css +138 -0
- package/rendering/visualizer.html +60 -0
- package/rendering/visualizer.mjs +254 -0
- package/services/clock-v2.mjs +196 -0
- package/services/clock.mjs +144 -0
- package/services/converter.mjs +64 -0
- package/services/cryptos.mjs +83 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Converter } from './converter.mjs';
|
|
2
|
+
const IS_BROWSER = typeof window !== 'undefined';
|
|
3
|
+
|
|
4
|
+
// ED25519 EXPOSURE NODEJS/BROWSER COMPATIBLE ---------------------------------
|
|
5
|
+
const [ed_, {sha512}] = await Promise.all([
|
|
6
|
+
import(IS_BROWSER ? 'https://unpkg.com/@noble/ed25519@3.0.0/index.js' : '@noble/ed25519'),
|
|
7
|
+
import(IS_BROWSER ? 'https://unpkg.com/@noble/hashes@2.0.0/sha2.js' : '@noble/hashes/sha2.js')
|
|
8
|
+
]);
|
|
9
|
+
|
|
10
|
+
/** @type {import('@noble/ed25519')} */
|
|
11
|
+
const ed25519 = ed_;
|
|
12
|
+
ed25519.hashes.sha512 = sha512;
|
|
13
|
+
export { ed25519 };
|
|
14
|
+
//-----------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
// ARGON2 EXPOSURE NODEJS/BROWSER COMPATIBLE ----------------------------------
|
|
17
|
+
export class Argon2Unified {
|
|
18
|
+
converter = new Converter();
|
|
19
|
+
/** @type {import('argon2')} */ argon2;
|
|
20
|
+
|
|
21
|
+
/** This function hashes a password using Argon2 - Browser/NodeJS unified
|
|
22
|
+
* @param {string} pass - Password to hash
|
|
23
|
+
* @param {string} salt - Salt to use for the hash
|
|
24
|
+
* @param {number} [mem] - Memory usage in KiB, default: 2**16 = 65_536 (64 MiB) | RECOMMENDED: 2**16
|
|
25
|
+
* @param {number} [time] - Time cost in iterations, default: 1
|
|
26
|
+
* @param {number} [parallelism] - Number of threads to use, default: 1
|
|
27
|
+
* @param {number} [type] - 0: Argon2d, 1: Argon2i, 2: Argon2id, default: 2 (Argon2id)
|
|
28
|
+
* @param {number} [hashLen] - Length of the hash in bytes, default: 32 */
|
|
29
|
+
hash = async (pass, salt, mem = 2**16, time = 1, parallelism = 1, type = 2, hashLen = 32) => {
|
|
30
|
+
const params = this.#createArgon2Params(pass, salt, time, mem, parallelism, type, hashLen);
|
|
31
|
+
const argon2Lib = await this.getArgon2Lib();
|
|
32
|
+
const hashResult = IS_BROWSER ? await argon2Lib.hash(params) : await argon2Lib.hash(pass, params);
|
|
33
|
+
if (!hashResult) return false;
|
|
34
|
+
|
|
35
|
+
const encoded = hashResult.encoded || hashResult;
|
|
36
|
+
const result = this.#standardizeArgon2FromEncoded(encoded);
|
|
37
|
+
if (!result) return false;
|
|
38
|
+
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
async getArgon2Lib() {
|
|
42
|
+
if (this.argon2) return this.argon2;
|
|
43
|
+
|
|
44
|
+
if (!IS_BROWSER) { try {
|
|
45
|
+
const a = await import('argon2');
|
|
46
|
+
this.argon2 = a;
|
|
47
|
+
} catch (error) { throw new Error('Please install argon2 package: npm install argon2'); } }
|
|
48
|
+
if (this.argon2) return this.argon2;
|
|
49
|
+
|
|
50
|
+
try { if (argon2) {
|
|
51
|
+
console.log('Argon2 loaded as a global variable');
|
|
52
|
+
this.argon2 = argon2;
|
|
53
|
+
return this.argon2;
|
|
54
|
+
}} catch (error) { }
|
|
55
|
+
if (this.argon2) return this.argon2;
|
|
56
|
+
|
|
57
|
+
console.log('trying import argon2 ES6 and inject in window');
|
|
58
|
+
const argon2ES6 = await import('../libs/argon2-ES6.min.mjs');
|
|
59
|
+
window.argon2 = argon2ES6.default; // EXPOSE TO GLOBAL SCOPE
|
|
60
|
+
this.argon2 = argon2ES6.default;
|
|
61
|
+
return this.argon2;
|
|
62
|
+
};
|
|
63
|
+
#createArgon2Params(pass = "averylongpassword123456", salt = "saltsaltsaltsaltsalt", time = 1, mem = 2**10, parallelism = 1, type = 2, hashLen = 32) {
|
|
64
|
+
const fixedSalt = salt.padEnd(20, '0').substring(0, 16); // 16 bytes minimum
|
|
65
|
+
return {
|
|
66
|
+
type, pass, parallelism,
|
|
67
|
+
time, timeCost: time, // we preserve both for compatibility
|
|
68
|
+
mem, memoryCost: mem, // we preserve both for compatibility
|
|
69
|
+
hashLen, hashLength: hashLen, // we preserve both for compatibility
|
|
70
|
+
salt: IS_BROWSER ? fixedSalt : Buffer.from(fixedSalt),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
#standardizeArgon2FromEncoded(encoded = '$argon2id$v=19$m=1048576,t=1,p=1$c2FsdHNhbHRzYWx0c2FsdHNhbHQ$UamPN/XTTX4quPewQNw4/s3y1JJeS22cRroh5l7OTMM') {
|
|
74
|
+
const base64 = encoded.split('$').pop();
|
|
75
|
+
const hash = this.converter.base64toBytes(base64);
|
|
76
|
+
const hex = this.converter.bytesToHex(hash);
|
|
77
|
+
/** @type {string} */
|
|
78
|
+
const bitsString = Converter.hexToBits(hex, 'string');
|
|
79
|
+
if (!bitsString) return false;
|
|
80
|
+
return { encoded, hash, hex, bitsString };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//-----------------------------------------------------------------------------
|