@freesignal/protocol 0.7.7 → 0.7.8
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/dist/node.d.ts +1 -0
- package/dist/node.js +29 -26
- package/package.json +1 -1
package/dist/node.d.ts
CHANGED
|
@@ -86,6 +86,7 @@ export declare class FreeSignalNode {
|
|
|
86
86
|
packBootstrap(): Promise<Datagram>;
|
|
87
87
|
sendBootstrap(receiverId: string | UserId): Promise<void>;
|
|
88
88
|
protected decrypt(datagram: EncryptedDatagram | Datagram | Uint8Array): Promise<MessageEventData>;
|
|
89
|
+
protected openHandshake(datagram: Datagram | EncryptedDatagram | Uint8Array): Promise<KeySession>;
|
|
89
90
|
protected open(datagram: Datagram | EncryptedDatagram | Uint8Array): Promise<void>;
|
|
90
91
|
}
|
|
91
92
|
declare class SessionMap implements LocalStorage<string, KeySession> {
|
package/dist/node.js
CHANGED
|
@@ -221,38 +221,41 @@ class FreeSignalNode {
|
|
|
221
221
|
return { session, payload: decrypted };
|
|
222
222
|
});
|
|
223
223
|
}
|
|
224
|
+
openHandshake(datagram) {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
const encrypted = types_1.EncryptedDatagram.from(datagram);
|
|
227
|
+
if (!encrypted.payload)
|
|
228
|
+
throw new Error("Missing payload");
|
|
229
|
+
if (yield this.sessions.has(encrypted.sessionTag)) {
|
|
230
|
+
//console.debug("Opening Handshake Ack");
|
|
231
|
+
const session = yield this.sessions.get(encrypted.sessionTag);
|
|
232
|
+
const { payload } = yield this.decrypt(encrypted);
|
|
233
|
+
if (!session)
|
|
234
|
+
throw new Error("Session not found for sessionTag: " + encrypted.sessionTag);
|
|
235
|
+
if (!(0, utils_1.compareBytes)(payload, crypto_1.default.ECDH.scalarMult(this.privateIdentityKey.exchangeKey, session.identityKey.exchangeKey)))
|
|
236
|
+
throw new Error("Error validating handshake data");
|
|
237
|
+
return session;
|
|
238
|
+
}
|
|
239
|
+
//console.debug("Opening Handshake Syn");
|
|
240
|
+
const data = (0, utils_1.decodeData)(encrypted.payload);
|
|
241
|
+
if (!encrypted.verify(types_1.IdentityKey.from(data.identityKey).signatureKey))
|
|
242
|
+
throw new Error("Signature not verified");
|
|
243
|
+
const { session, associatedData } = yield this.keyExchange.digestMessage(data);
|
|
244
|
+
yield this.sessions.set(session.sessionTag, session);
|
|
245
|
+
yield this.users.set(session.userId.toString(), session.sessionTag);
|
|
246
|
+
yield this.bundles.set(session.userId.toString(), (0, utils_1.decodeData)(associatedData));
|
|
247
|
+
yield this.sendHandshake(session);
|
|
248
|
+
return session;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
224
251
|
open(datagram) {
|
|
225
252
|
return __awaiter(this, void 0, void 0, function* () {
|
|
226
253
|
if (datagram instanceof Uint8Array)
|
|
227
254
|
datagram = types_1.Datagram.from(datagram);
|
|
228
255
|
switch (datagram.protocol) {
|
|
229
|
-
case types_1.Protocols.HANDSHAKE:
|
|
230
|
-
|
|
231
|
-
if (!encrypted.payload)
|
|
232
|
-
throw new Error("Missing payload");
|
|
233
|
-
if (yield this.sessions.has(encrypted.sessionTag)) {
|
|
234
|
-
//console.debug("Opening Handshake Ack");
|
|
235
|
-
const session = yield this.sessions.get(encrypted.sessionTag);
|
|
236
|
-
const { payload } = yield this.decrypt(encrypted);
|
|
237
|
-
if (!session)
|
|
238
|
-
throw new Error("Session not found for sessionTag: " + datagram.sessionTag);
|
|
239
|
-
if (!(0, utils_1.compareBytes)(payload, crypto_1.default.ECDH.scalarMult(this.privateIdentityKey.exchangeKey, session.identityKey.exchangeKey)))
|
|
240
|
-
throw new Error("Error validating handshake data");
|
|
241
|
-
this.emitter.emit('handshake', { session });
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
//console.debug("Opening Handshake Syn");
|
|
245
|
-
const data = (0, utils_1.decodeData)(encrypted.payload);
|
|
246
|
-
if (!datagram.verify(types_1.IdentityKey.from(data.identityKey).signatureKey))
|
|
247
|
-
throw new Error("Signature not verified");
|
|
248
|
-
const { session, associatedData } = yield this.keyExchange.digestMessage(data);
|
|
249
|
-
yield this.sessions.set(session.sessionTag, session);
|
|
250
|
-
yield this.users.set(session.userId.toString(), session.sessionTag);
|
|
251
|
-
yield this.bundles.set(session.userId.toString(), (0, utils_1.decodeData)(associatedData));
|
|
252
|
-
yield this.sendHandshake(session);
|
|
253
|
-
this.emitter.emit('handshake', { session });
|
|
256
|
+
case types_1.Protocols.HANDSHAKE:
|
|
257
|
+
this.emitter.emit('handshake', { session: yield this.openHandshake(datagram) });
|
|
254
258
|
return;
|
|
255
|
-
}
|
|
256
259
|
case types_1.Protocols.MESSAGE:
|
|
257
260
|
//console.debug("Opening Message");
|
|
258
261
|
this.emitter.emit('message', yield this.decrypt(datagram));
|