@freesignal/protocol 0.7.0 → 0.7.1

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
@@ -36,6 +36,7 @@ type NodeEventData = {
36
36
  header: DatagramHeader;
37
37
  payload?: Uint8Array;
38
38
  datagram?: Datagram;
39
+ request?: BootstrapRequest;
39
40
  };
40
41
  type MessageEventData = {
41
42
  header: DatagramHeader;
@@ -48,23 +49,25 @@ export declare class FreeSignalNode {
48
49
  protected readonly keyExchange: KeyExchange;
49
50
  protected readonly discovers: Set<string>;
50
51
  protected readonly bootstraps: LocalStorage<string, BootstrapRequest>;
51
- protected readonly emitter: EventEmitter<"message" | "send" | "handshaked" | "ping", NodeEventData>;
52
+ protected readonly emitter: EventEmitter<"message" | "send" | "handshake" | "ping" | "bootstrap", NodeEventData>;
52
53
  constructor(storage: Database<{
53
54
  sessions: LocalStorage<string, ExportedKeySession>;
54
55
  keyExchange: LocalStorage<string, Crypto.KeyPair>;
55
56
  bundles: LocalStorage<string, KeyExchangeDataBundle>;
56
57
  bootstraps: LocalStorage<string, BootstrapRequest>;
57
58
  }>, privateIdentityKey?: PrivateIdentityKey);
59
+ protected messageHandler: EventCall<"message", NodeEventData>;
60
+ protected sendHandler: EventCall<"send", NodeEventData>;
61
+ protected handshakeHandler: EventCall<"handshake", NodeEventData>;
62
+ protected bootstrapHandler: EventCall<"bootstrap", NodeEventData>;
58
63
  onMessage: (data: MessageEventData) => void;
59
64
  onSend: (data: Uint8Array) => void;
60
- onHandshaked: (userId: UserId) => void;
65
+ onHandshake: (userId: UserId) => void;
66
+ onRequest: (request: BootstrapRequest) => void;
67
+ getRequest(userId: string): Promise<BootstrapRequest | undefined>;
61
68
  waitHandshaked(userId: UserId | string, timeout?: number): Promise<void>;
62
69
  get identityKey(): IdentityKey;
63
70
  get userId(): UserId;
64
- readonly requests: {
65
- onRequest: (request: BootstrapRequest) => void;
66
- getRequest: (userId: string) => Promise<BootstrapRequest | undefined>;
67
- };
68
71
  protected encrypt(receiverId: string | UserId, protocol: Protocols, data: Uint8Array): Promise<Datagram>;
69
72
  sendHandshake(data: KeyExchangeData): Promise<void>;
70
73
  sendHandshake(receiverId: string | UserId): Promise<void>;
package/dist/node.js CHANGED
@@ -82,29 +82,33 @@ class FreeSignalNode {
82
82
  constructor(storage, privateIdentityKey) {
83
83
  this.discovers = new Set();
84
84
  this.emitter = new easyemitter_ts_1.default();
85
+ this.messageHandler = (data) => { var _a, _b; return this.onMessage({ header: (_a = data.data) === null || _a === void 0 ? void 0 : _a.header, payload: (_b = data.data) === null || _b === void 0 ? void 0 : _b.payload }); };
86
+ this.sendHandler = (data) => this.onSend(data.data.datagram.toBytes());
87
+ this.handshakeHandler = (data) => this.onHandshake(types_1.UserId.from(data.data.header.sender));
88
+ this.bootstrapHandler = (data) => { var _a; return this.onRequest((_a = data.data) === null || _a === void 0 ? void 0 : _a.request); };
85
89
  this.onMessage = () => { };
86
90
  this.onSend = () => { };
87
- this.onHandshaked = () => { };
88
- this.requests = {
89
- onRequest: () => { },
90
- getRequest: (userId) => {
91
- return this.bootstraps.get(userId);
92
- }
93
- };
91
+ this.onHandshake = () => { };
92
+ this.onRequest = () => { };
94
93
  this.privateIdentityKey = privateIdentityKey !== null && privateIdentityKey !== void 0 ? privateIdentityKey : (0, _1.createIdentity)();
95
94
  this.sessions = new SessionMap(storage.sessions);
96
95
  this.keyExchange = new x3dh_1.KeyExchange(storage.keyExchange, this.privateIdentityKey);
97
96
  this.bundles = storage.bundles;
98
97
  this.bootstraps = storage.bootstraps;
99
- this.emitter.on('send', (data) => this.onSend(data.data.datagram.toBytes()));
100
- this.emitter.on('handshaked', (data) => this.onHandshaked(types_1.UserId.from(data.data.header.sender)));
98
+ this.emitter.on('message', this.messageHandler);
99
+ this.emitter.on('send', this.sendHandler);
100
+ this.emitter.on('handshake', this.handshakeHandler);
101
+ this.emitter.on('bootstrap', this.bootstrapHandler);
102
+ }
103
+ getRequest(userId) {
104
+ return this.bootstraps.get(userId);
101
105
  }
102
106
  waitHandshaked(userId, timeout) {
103
107
  return __awaiter(this, void 0, void 0, function* () {
104
108
  var _a;
105
109
  if (timeout)
106
110
  setTimeout(() => { throw new Error(); }, timeout);
107
- while (((_a = (yield this.emitter.wait('handshaked', timeout))) === null || _a === void 0 ? void 0 : _a.header.sender) !== userId.toString())
111
+ while (((_a = (yield this.emitter.wait('handshake', timeout))) === null || _a === void 0 ? void 0 : _a.header.sender) !== userId.toString())
108
112
  ;
109
113
  });
110
114
  }
@@ -186,6 +190,8 @@ class FreeSignalNode {
186
190
  sendBootstrap(receiverId) {
187
191
  return __awaiter(this, void 0, void 0, function* () {
188
192
  //console.debug("Sending Bootstrap");
193
+ if (yield this.sessions.has(receiverId.toString()))
194
+ throw new Error("Session exists");
189
195
  const datagram = new types_1.Datagram(this.userId.toString(), receiverId.toString(), types_1.Protocols.BOOTSTRAP, (0, utils_1.encodeData)(yield this.keyExchange.generateData()));
190
196
  this.emitter.emit('send', { header: datagram.header, datagram });
191
197
  });
@@ -225,7 +231,7 @@ class FreeSignalNode {
225
231
  throw new Error("Missing user");
226
232
  if (!(0, utils_1.compareBytes)(payload, crypto_1.default.ECDH.scalarMult(this.privateIdentityKey.exchangeKey, identityKey.exchangeKey)))
227
233
  throw new Error("Error validating handshake data");
228
- this.emitter.emit('handshaked', { header: datagram.header });
234
+ this.emitter.emit('handshake', { header: datagram.header });
229
235
  return;
230
236
  }
231
237
  //console.debug("Opening Handshake Syn");
@@ -236,7 +242,7 @@ class FreeSignalNode {
236
242
  yield this.sessions.set(session.userId.toString(), session);
237
243
  yield this.bundles.set(session.userId.toString(), (0, utils_1.decodeData)(associatedData));
238
244
  yield this.sendHandshake(session.userId);
239
- this.emitter.emit('handshaked', { header: datagram.header });
245
+ this.emitter.emit('handshake', { header: datagram.header });
240
246
  return;
241
247
  case types_1.Protocols.MESSAGE:
242
248
  //console.debug("Opening Message");
@@ -295,7 +301,7 @@ class FreeSignalNode {
295
301
  this.sendHandshake(request.data);
296
302
  };
297
303
  yield this.bootstraps.set(datagram.sender, request);
298
- this.requests.onRequest(request);
304
+ this.emitter.emit('bootstrap', { header: datagram.header, request });
299
305
  return;
300
306
  case types_1.Protocols.PING:
301
307
  this.emitter.emit('ping', { header: datagram.header });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freesignal/protocol",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Signal Protocol implementation in javascript",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "author": "Christian Braghette",