hive-p2p 1.0.111 → 1.0.113
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/crypto-codex.mjs
CHANGED
|
@@ -13,19 +13,19 @@ export class CryptoCodex {
|
|
|
13
13
|
AVOID_CRYPTO = true; // AVOID CRYPTO OPERATIONS (default) => auto-enable when generate() is called, but can be set to true to disable crypto in any case (e.g. for testing with string ids)
|
|
14
14
|
verbose = NODE.DEFAULT_VERBOSE;
|
|
15
15
|
id;
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
publicKey;
|
|
17
|
+
privateKey;
|
|
18
18
|
get idLength() { return IDENTITY.ARE_IDS_HEX ? IDENTITY.ID_LENGTH / 2 : IDENTITY.ID_LENGTH; }
|
|
19
19
|
|
|
20
20
|
/** @param {string} [nodeId] If provided: used to generate a fake keypair > disable crypto operations */
|
|
21
21
|
constructor(nodeId, verbose = NODE.DEFAULT_VERBOSE) {
|
|
22
|
-
this.privateKey = new Uint8Array(32).fill(0);
|
|
23
|
-
this.publicKey = new Uint8Array(32).fill(0);
|
|
24
22
|
this.verbose = verbose;
|
|
25
23
|
//this.AVOID_CRYPTO = IDENTITY.ARE_IDS_HEX ? false : true; // disable crypto if string ids are used
|
|
26
24
|
if (!nodeId) return; // IF NOT PROVIDED: generate() should be called.
|
|
27
|
-
|
|
25
|
+
|
|
28
26
|
this.id = nodeId.padEnd(IDENTITY.ID_LENGTH, ' ').slice(0, IDENTITY.ID_LENGTH);
|
|
27
|
+
this.privateKey = new Uint8Array(32).fill(0);
|
|
28
|
+
this.publicKey = new Uint8Array(32).fill(0);
|
|
29
29
|
const idBytes = new TextEncoder().encode(this.id); // use nodeId to create a fake public key
|
|
30
30
|
for (let i = 0; i < IDENTITY.ID_LENGTH; i++) this.publicKey[i] = idBytes[i];
|
|
31
31
|
}
|
|
@@ -76,8 +76,8 @@ export class CryptoCodex {
|
|
|
76
76
|
if (!asPublicNode && this.isPublicNode(id)) throw new Error('Seed does not produce a private node identity.');
|
|
77
77
|
if (!await this.pubkeyDifficultyCheck(publicKey)) throw new Error('Seed does not meet difficulty requirements.');
|
|
78
78
|
this.id = id;
|
|
79
|
-
this.privateKey = secretKey;
|
|
80
|
-
this.publicKey = publicKey;
|
|
79
|
+
this.privateKey = new Uint8Array(secretKey);
|
|
80
|
+
this.publicKey = new Uint8Array(publicKey);
|
|
81
81
|
}
|
|
82
82
|
/** @param {boolean} asPublicNode */
|
|
83
83
|
static async generateNewSybilIdentity(asPublicNode, log = true) {
|
|
@@ -135,6 +135,7 @@ export class CryptoCodex {
|
|
|
135
135
|
const MARKER = GOSSIP.MARKERS_BYTES[topic];
|
|
136
136
|
if (typeof MARKER !== 'number') throw new Error(`Failed to create gossip message: wrong topic '${topic}'.`);
|
|
137
137
|
if (typeof timestamp !== 'number') throw new Error('Wrong timestamp type!');
|
|
138
|
+
if (!this.publicKey ||!this.privateKey) throw new Error('KeyPair not initialized!');
|
|
138
139
|
|
|
139
140
|
const neighborsBytes = this.#idsToBytes(neighbors);
|
|
140
141
|
const { dataCode, dataBytes } = this.#dataToBytes(data);
|
|
@@ -161,6 +162,7 @@ export class CryptoCodex {
|
|
|
161
162
|
const MARKER = UNICAST.MARKERS_BYTES[type];
|
|
162
163
|
if (typeof MARKER !== 'number') throw new Error(`Failed to create gossip message: wrong type '${type}'.`);
|
|
163
164
|
if (typeof timestamp !== 'number') throw new Error('Wrong timestamp type!');
|
|
165
|
+
if (!this.publicKey ||!this.privateKey) throw new Error('KeyPair not initialized!');
|
|
164
166
|
if (route.length < 2) throw new Error('Failed to create unicast message: route must have at least 2 nodes (next hop and target).');
|
|
165
167
|
if (route.length > UNICAST.MAX_HOPS + 1) throw new Error(`Failed to create unicast message: route exceeds max hops (${UNICAST.MAX_HOPS}).`);
|
|
166
168
|
|
|
@@ -184,6 +186,7 @@ export class CryptoCodex {
|
|
|
184
186
|
}
|
|
185
187
|
/** @param {Uint8Array} serialized @param {string[]} newRoute */
|
|
186
188
|
createReroutedUnicastMessage(serialized, newRoute) {
|
|
189
|
+
if (!this.publicKey ||!this.privateKey) throw new Error('KeyPair not initialized!');
|
|
187
190
|
if (newRoute.length < 2) throw new Error('Failed to create rerouted unicast message: route must have at least 2 nodes (next hop and target).');
|
|
188
191
|
if (newRoute.length > UNICAST.MAX_HOPS + 1) throw new Error(`Failed to create rerouted unicast message: route exceeds max hops (${UNICAST.MAX_HOPS}).`);
|
|
189
192
|
|
|
@@ -283,8 +286,8 @@ export class CryptoCodex {
|
|
|
283
286
|
|
|
284
287
|
const destId = route[route.length - 1];
|
|
285
288
|
const d = type === 'private_message' && this.id === destId
|
|
286
|
-
|
|
287
|
-
|
|
289
|
+
? this.decryptData(serialized.slice(47 + neighLength, 47 + NDBL), peerStore.privacy[this.#idFromPublicKey(pubkey)]?.sharedSecret)
|
|
290
|
+
: serialized.slice(47 + neighLength, 47 + NDBL);
|
|
288
291
|
|
|
289
292
|
const deserializedData = this.id === destId ? this.#bytesToData(dataCode, d) : d;
|
|
290
293
|
const initialMessageEnd = signatureStart + IDENTITY.SIGNATURE_LENGTH;
|