@freesignal/protocol 0.7.6 → 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 CHANGED
@@ -83,8 +83,10 @@ export declare class FreeSignalNode {
83
83
  sendRelay(relayId: string | UserId, receiverId: string | UserId, data: Datagram): Promise<void>;
84
84
  sendPing(receiverId: string | UserId): Promise<void>;
85
85
  sendDiscover(receiverId: string | UserId, discoverId: string | UserId): Promise<void>;
86
+ packBootstrap(): Promise<Datagram>;
86
87
  sendBootstrap(receiverId: string | UserId): Promise<void>;
87
88
  protected decrypt(datagram: EncryptedDatagram | Datagram | Uint8Array): Promise<MessageEventData>;
89
+ protected openHandshake(datagram: Datagram | EncryptedDatagram | Uint8Array): Promise<KeySession>;
88
90
  protected open(datagram: Datagram | EncryptedDatagram | Uint8Array): Promise<void>;
89
91
  }
90
92
  declare class SessionMap implements LocalStorage<string, KeySession> {
package/dist/node.js CHANGED
@@ -190,12 +190,17 @@ class FreeSignalNode {
190
190
  this.emitter.emit('send', yield this.encrypt(receiverId, types_1.Protocols.DISCOVER, (0, utils_1.encodeData)(message)));
191
191
  });
192
192
  }
193
+ packBootstrap() {
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ return new types_1.Datagram(types_1.Protocols.BOOTSTRAP, (0, utils_1.encodeData)(yield this.keyExchange.generateData()));
196
+ });
197
+ }
193
198
  sendBootstrap(receiverId) {
194
199
  return __awaiter(this, void 0, void 0, function* () {
195
200
  //console.debug("Sending Bootstrap");
196
- if (yield this.sessions.has(receiverId.toString()))
201
+ if (receiverId && (yield this.sessions.has(receiverId.toString())))
197
202
  throw new Error("Session exists");
198
- const datagram = new types_1.Datagram(types_1.Protocols.BOOTSTRAP, (0, utils_1.encodeData)(yield this.keyExchange.generateData()));
203
+ const datagram = yield this.packBootstrap();
199
204
  this.emitter.emit('send', { datagram, userId: types_1.UserId.from(receiverId) });
200
205
  });
201
206
  }
@@ -216,38 +221,41 @@ class FreeSignalNode {
216
221
  return { session, payload: decrypted };
217
222
  });
218
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
+ }
219
251
  open(datagram) {
220
252
  return __awaiter(this, void 0, void 0, function* () {
221
253
  if (datagram instanceof Uint8Array)
222
254
  datagram = types_1.Datagram.from(datagram);
223
255
  switch (datagram.protocol) {
224
- case types_1.Protocols.HANDSHAKE: {
225
- const encrypted = types_1.EncryptedDatagram.from(datagram);
226
- if (!encrypted.payload)
227
- throw new Error("Missing payload");
228
- if (yield this.sessions.has(encrypted.sessionTag)) {
229
- //console.debug("Opening Handshake Ack");
230
- const session = yield this.sessions.get(encrypted.sessionTag);
231
- const { payload } = yield this.decrypt(encrypted);
232
- if (!session)
233
- throw new Error("Session not found for sessionTag: " + datagram.sessionTag);
234
- if (!(0, utils_1.compareBytes)(payload, crypto_1.default.ECDH.scalarMult(this.privateIdentityKey.exchangeKey, session.identityKey.exchangeKey)))
235
- throw new Error("Error validating handshake data");
236
- this.emitter.emit('handshake', { session });
237
- return;
238
- }
239
- //console.debug("Opening Handshake Syn");
240
- const data = (0, utils_1.decodeData)(encrypted.payload);
241
- if (!datagram.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
- this.emitter.emit('handshake', { session });
256
+ case types_1.Protocols.HANDSHAKE:
257
+ this.emitter.emit('handshake', { session: yield this.openHandshake(datagram) });
249
258
  return;
250
- }
251
259
  case types_1.Protocols.MESSAGE:
252
260
  //console.debug("Opening Message");
253
261
  this.emitter.emit('message', yield this.decrypt(datagram));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freesignal/protocol",
3
- "version": "0.7.6",
3
+ "version": "0.7.8",
4
4
  "description": "Signal Protocol implementation in javascript",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "author": "Christian Braghette",