nostr-double-ratchet 0.0.31 → 0.0.32

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.
@@ -1,6 +1,7 @@
1
1
  import { NostrPublish, NostrSubscribe, Rumor } from "./types";
2
2
  import { Invite } from "./Invite";
3
3
  import { StorageAdapter } from "./StorageAdapter";
4
+ export type OnEventCallback = (event: Rumor, from: string) => void;
4
5
  export default class SessionManager {
5
6
  private userRecords;
6
7
  private nostrSubscribe;
@@ -28,7 +29,7 @@ export default class SessionManager {
28
29
  listenToUser(userPubkey: string): void;
29
30
  stopListeningToUser(userPubkey: string): void;
30
31
  private internalSubscriptions;
31
- onEvent(callback: (event: Rumor) => void): () => void;
32
+ onEvent(callback: OnEventCallback): () => void;
32
33
  close(): void;
33
34
  /**
34
35
  * Accept an invite as our own device, persist the session, and publish the acceptance event.
@@ -1 +1 @@
1
- {"version":3,"file":"SessionManager.d.ts","sourceRoot":"","sources":["../src/SessionManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,YAAY,EAAE,cAAc,EAAE,KAAK,EAAe,MAAM,SAAS,CAAA;AAE7F,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEjC,OAAO,EAAE,cAAc,EAA0B,MAAM,kBAAkB,CAAA;AAIzE,MAAM,CAAC,OAAO,OAAO,cAAc;IAC/B,OAAO,CAAC,WAAW,CAAqC;IACxD,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,YAAY,CAAc;IAClC,OAAO,CAAC,cAAc,CAAY;IAClC,OAAO,CAAC,kBAAkB,CAAsC;IAChE,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,YAAY,CAA4F;gBAG5G,cAAc,EAAE,UAAU,EAC1B,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,cAAc,EAC9B,YAAY,E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,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,YAAY,CAA4F;gBAG5G,cAAc,EAAE,UAAU,EAC1B,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,cAAc,EAC9B,YAAY,EAAE,YAAY,EAC1B,OAAO,GAAE,cAA6C;IAc1D,OAAO,CAAC,YAAY,CAAQ;IAE5B;;;;OAIG;IACU,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YAwGpB,YAAY;YAiCZ,WAAW;IAOzB,WAAW,IAAI,MAAM;IAIrB,SAAS,IAAI,MAAM;IAOb,QAAQ,CAAC,oBAAoB,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAQnD,SAAS,CAAC,oBAAoB,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC;IA4DnE,YAAY,CAAC,UAAU,EAAE,MAAM;IA8D/B,mBAAmB,CAAC,UAAU,EAAE,MAAM;IAStC,OAAO,CAAC,qBAAqB,CAAkC;IAE/D,OAAO,CAAC,QAAQ,EAAE,eAAe;IAkBjC,KAAK;IAiBL;;;OAGG;IACU,eAAe,CAAC,MAAM,EAAE,MAAM;CAgB9C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nostr-double-ratchet",
3
- "version": "0.0.31",
3
+ "version": "0.0.32",
4
4
  "type": "module",
5
5
  "packageManager": "yarn@1.22.22",
6
6
  "description": "Nostr double ratchet library",
@@ -6,6 +6,8 @@ import { StorageAdapter, InMemoryStorageAdapter } from "./StorageAdapter"
6
6
  import { serializeSessionState, deserializeSessionState } from "./utils"
7
7
  import { Session } from "./Session"
8
8
 
9
+ export type OnEventCallback = (event: Rumor, from: string) => void
10
+
9
11
  export default class SessionManager {
10
12
  private userRecords: Map<string, UserRecord> = new Map()
11
13
  private nostrSubscribe: NostrSubscribe
@@ -44,6 +46,7 @@ export default class SessionManager {
44
46
  * Can be awaited by callers that need deterministic readiness.
45
47
  */
46
48
  public async init(): Promise<void> {
49
+ console.log("Initialising SessionManager")
47
50
  if (this._initialised) return
48
51
 
49
52
  const ourPublicKey = getPublicKey(this.ourIdentityKey)
@@ -67,7 +70,11 @@ export default class SessionManager {
67
70
  this.invite = invite
68
71
 
69
72
  // Publish our own invite
70
- this.nostrPublish(invite.getEvent())?.catch(() => {})
73
+ console.log("Publishing our own invite", invite)
74
+ const event = invite.getEvent()
75
+ this.nostrPublish(event).then((verifiedEvent) => {
76
+ console.log("Invite published", verifiedEvent)
77
+ }).catch((e) => console.error("Failed to publish our own invite", e))
71
78
 
72
79
  // 2b. Listen for acceptances of *our* invite and create sessions
73
80
  this.invite.listen(
@@ -90,7 +97,7 @@ export default class SessionManager {
90
97
  this.saveSession(targetUserKey, deviceKey, session)
91
98
 
92
99
  session.onEvent((_event: Rumor) => {
93
- this.internalSubscriptions.forEach(cb => cb(_event))
100
+ this.internalSubscriptions.forEach(cb => cb(_event, targetUserKey))
94
101
  })
95
102
  } catch {/* ignore errors */}
96
103
  }
@@ -128,7 +135,7 @@ export default class SessionManager {
128
135
  this.saveSession(ourPublicKey, deviceId, session)
129
136
 
130
137
  session.onEvent((_event: Rumor) => {
131
- this.internalSubscriptions.forEach(cb => cb(_event))
138
+ this.internalSubscriptions.forEach(cb => cb(_event, ourPublicKey))
132
139
  })
133
140
  } catch (err) {
134
141
  // eslint-disable-next-line no-console
@@ -167,7 +174,7 @@ export default class SessionManager {
167
174
  this.saveSession(ownerPubKey, deviceId, session)
168
175
 
169
176
  session.onEvent((_event: Rumor) => {
170
- this.internalSubscriptions.forEach(cb => cb(_event))
177
+ this.internalSubscriptions.forEach(cb => cb(_event, ownerPubKey))
171
178
  })
172
179
  } catch {
173
180
  // corrupted entry — ignore
@@ -204,7 +211,7 @@ export default class SessionManager {
204
211
  async sendEvent(recipientIdentityKey: string, event: Partial<Rumor>) {
205
212
  console.log("Sending event to", recipientIdentityKey, event)
206
213
  // Immediately notify local subscribers so that UI can render sent message optimistically
207
- this.internalSubscriptions.forEach(cb => cb(event as Rumor))
214
+ this.internalSubscriptions.forEach(cb => cb(event as Rumor, recipientIdentityKey))
208
215
 
209
216
  const results = []
210
217
  const publishPromises: Promise<any>[] = []
@@ -295,7 +302,7 @@ export default class SessionManager {
295
302
 
296
303
  // Register all existing callbacks on the new session
297
304
  session.onEvent((_event: Rumor) => {
298
- this.internalSubscriptions.forEach(callback => callback(_event))
305
+ this.internalSubscriptions.forEach(callback => callback(_event, userPubkey))
299
306
  })
300
307
 
301
308
  const queuedMessages = this.messageQueue.get(userPubkey)
@@ -332,16 +339,16 @@ export default class SessionManager {
332
339
  }
333
340
 
334
341
  // Update onEvent to include internalSubscriptions management
335
- private internalSubscriptions: Set<(event: Rumor) => void> = new Set()
342
+ private internalSubscriptions: Set<OnEventCallback> = new Set()
336
343
 
337
- onEvent(callback: (event: Rumor) => void) {
344
+ onEvent(callback: OnEventCallback) {
338
345
  this.internalSubscriptions.add(callback)
339
346
 
340
347
  // Subscribe to existing sessions
341
- for (const userRecord of this.userRecords.values()) {
348
+ for (const [pubkey, userRecord] of this.userRecords.entries()) {
342
349
  for (const session of userRecord.getActiveSessions()) {
343
- session.onEvent((_event: Rumor) => {
344
- callback(_event)
350
+ session.onEvent((event: Rumor) => {
351
+ callback(event, pubkey)
345
352
  })
346
353
  }
347
354
  }