nostr-double-ratchet 0.0.37 → 0.0.48
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/README.md +52 -15
- package/dist/AppKeys.d.ts +52 -0
- package/dist/AppKeys.d.ts.map +1 -0
- package/dist/AppKeysManager.d.ts +136 -0
- package/dist/AppKeysManager.d.ts.map +1 -0
- package/dist/Invite.d.ts +7 -7
- package/dist/Invite.d.ts.map +1 -1
- package/dist/Session.d.ts +29 -0
- package/dist/Session.d.ts.map +1 -1
- package/dist/SessionManager.d.ts +46 -22
- package/dist/SessionManager.d.ts.map +1 -1
- package/dist/StorageAdapter.d.ts.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/inviteUtils.d.ts +122 -0
- package/dist/inviteUtils.d.ts.map +1 -0
- package/dist/nostr-double-ratchet.es.js +2843 -1901
- package/dist/nostr-double-ratchet.umd.js +1 -1
- package/dist/types.d.ts +32 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +20 -2
- package/dist/utils.d.ts.map +1 -1
- package/package.json +5 -19
- package/src/AppKeys.ts +210 -0
- package/src/AppKeysManager.ts +405 -0
- package/src/Invite.ts +69 -89
- package/src/Session.ts +47 -4
- package/src/SessionManager.ts +478 -300
- package/src/StorageAdapter.ts +5 -6
- package/src/index.ts +4 -1
- package/src/inviteUtils.ts +271 -0
- package/src/types.ts +37 -2
- package/src/utils.ts +45 -8
- package/LICENSE +0 -21
- package/dist/UserRecord.d.ts +0 -117
- package/dist/UserRecord.d.ts.map +0 -1
- package/src/UserRecord.ts +0 -338
package/src/UserRecord.ts
DELETED
|
@@ -1,338 +0,0 @@
|
|
|
1
|
-
import { Session } from './Session';
|
|
2
|
-
import { NostrSubscribe } from './types';
|
|
3
|
-
|
|
4
|
-
interface DeviceRecord {
|
|
5
|
-
deviceId: string;
|
|
6
|
-
publicKey: string;
|
|
7
|
-
activeSession?: Session;
|
|
8
|
-
inactiveSessions: Session[];
|
|
9
|
-
isStale: boolean;
|
|
10
|
-
staleTimestamp?: number;
|
|
11
|
-
lastActivity?: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Manages sessions for a single user across multiple devices
|
|
16
|
-
* Structure: UserRecord → DeviceRecord → Sessions
|
|
17
|
-
*/
|
|
18
|
-
export class UserRecord {
|
|
19
|
-
private deviceRecords: Map<string, DeviceRecord> = new Map();
|
|
20
|
-
private isStale: boolean = false;
|
|
21
|
-
private staleTimestamp?: number;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
public readonly userId: string,
|
|
25
|
-
private readonly nostrSubscribe: NostrSubscribe,
|
|
26
|
-
) {
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// ============================================================================
|
|
30
|
-
// Device Management
|
|
31
|
-
// ============================================================================
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Creates or updates a device record for this user
|
|
35
|
-
*/
|
|
36
|
-
public upsertDevice(deviceId: string, publicKey: string): DeviceRecord {
|
|
37
|
-
let record = this.deviceRecords.get(deviceId);
|
|
38
|
-
|
|
39
|
-
if (!record) {
|
|
40
|
-
record = {
|
|
41
|
-
deviceId,
|
|
42
|
-
publicKey,
|
|
43
|
-
inactiveSessions: [],
|
|
44
|
-
isStale: false,
|
|
45
|
-
lastActivity: Date.now()
|
|
46
|
-
};
|
|
47
|
-
this.deviceRecords.set(deviceId, record);
|
|
48
|
-
} else if (record.publicKey !== publicKey) {
|
|
49
|
-
// Public key changed - mark old sessions as inactive and update
|
|
50
|
-
if (record.activeSession) {
|
|
51
|
-
record.inactiveSessions.push(record.activeSession);
|
|
52
|
-
record.activeSession = undefined;
|
|
53
|
-
}
|
|
54
|
-
record.publicKey = publicKey;
|
|
55
|
-
record.lastActivity = Date.now();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return record;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Gets a device record by deviceId
|
|
63
|
-
*/
|
|
64
|
-
public getDevice(deviceId: string): DeviceRecord | undefined {
|
|
65
|
-
return this.deviceRecords.get(deviceId);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Gets all device records for this user
|
|
70
|
-
*/
|
|
71
|
-
public getAllDevices(): DeviceRecord[] {
|
|
72
|
-
return Array.from(this.deviceRecords.values());
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Gets all active (non-stale) device records
|
|
77
|
-
*/
|
|
78
|
-
public getActiveDevices(): DeviceRecord[] {
|
|
79
|
-
if (this.isStale) return [];
|
|
80
|
-
return Array.from(this.deviceRecords.values()).filter(device => !device.isStale);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Removes a device record and closes all its sessions
|
|
85
|
-
*/
|
|
86
|
-
public removeDevice(deviceId: string): boolean {
|
|
87
|
-
const record = this.deviceRecords.get(deviceId);
|
|
88
|
-
if (!record) return false;
|
|
89
|
-
|
|
90
|
-
// Close all sessions for this device
|
|
91
|
-
record.activeSession?.close();
|
|
92
|
-
record.inactiveSessions.forEach(session => session.close());
|
|
93
|
-
|
|
94
|
-
return this.deviceRecords.delete(deviceId);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ============================================================================
|
|
98
|
-
// Session Management
|
|
99
|
-
// ============================================================================
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Adds or updates a session for a specific device
|
|
103
|
-
*/
|
|
104
|
-
public upsertSession(deviceId: string, session: Session, publicKey?: string): void {
|
|
105
|
-
const device = this.upsertDevice(deviceId, publicKey || session.state?.theirNextNostrPublicKey || '');
|
|
106
|
-
|
|
107
|
-
// If there's an active session, move it to inactive
|
|
108
|
-
if (device.activeSession) {
|
|
109
|
-
device.inactiveSessions.unshift(device.activeSession);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Set the new session as active
|
|
113
|
-
session.name = deviceId; // Ensure session name matches deviceId
|
|
114
|
-
device.activeSession = session;
|
|
115
|
-
device.lastActivity = Date.now();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Gets the active session for a specific device
|
|
120
|
-
*/
|
|
121
|
-
public getActiveSession(deviceId: string): Session | undefined {
|
|
122
|
-
const device = this.deviceRecords.get(deviceId);
|
|
123
|
-
return device?.isStale ? undefined : device?.activeSession;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Gets all sessions (active + inactive) for a specific device
|
|
128
|
-
*/
|
|
129
|
-
public getDeviceSessions(deviceId: string): Session[] {
|
|
130
|
-
const device = this.deviceRecords.get(deviceId);
|
|
131
|
-
if (!device) return [];
|
|
132
|
-
|
|
133
|
-
const sessions: Session[] = [];
|
|
134
|
-
if (device.activeSession) {
|
|
135
|
-
sessions.push(device.activeSession);
|
|
136
|
-
}
|
|
137
|
-
sessions.push(...device.inactiveSessions);
|
|
138
|
-
return sessions;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Gets all active sessions across all devices for this user
|
|
143
|
-
*/
|
|
144
|
-
public getActiveSessions(): Session[] {
|
|
145
|
-
const sessions: Session[] = [];
|
|
146
|
-
|
|
147
|
-
for (const device of this.getActiveDevices()) {
|
|
148
|
-
if (device.activeSession) {
|
|
149
|
-
sessions.push(device.activeSession);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Sort by ability to send messages (prioritize ready sessions)
|
|
154
|
-
sessions.sort((a, b) => {
|
|
155
|
-
const aCanSend = !!(a.state?.theirNextNostrPublicKey && a.state?.ourCurrentNostrKey);
|
|
156
|
-
const bCanSend = !!(b.state?.theirNextNostrPublicKey && b.state?.ourCurrentNostrKey);
|
|
157
|
-
|
|
158
|
-
if (aCanSend && !bCanSend) return -1;
|
|
159
|
-
if (!aCanSend && bCanSend) return 1;
|
|
160
|
-
return 0;
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
return sessions;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Gets all sessions (active + inactive) across all devices
|
|
168
|
-
*/
|
|
169
|
-
public getAllSessions(): Session[] {
|
|
170
|
-
const sessions: Session[] = [];
|
|
171
|
-
|
|
172
|
-
for (const device of this.deviceRecords.values()) {
|
|
173
|
-
if (device.activeSession) {
|
|
174
|
-
sessions.push(device.activeSession);
|
|
175
|
-
}
|
|
176
|
-
sessions.push(...device.inactiveSessions);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return sessions;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Gets sessions that are ready to send messages
|
|
184
|
-
*/
|
|
185
|
-
public getSendableSessions(): Session[] {
|
|
186
|
-
return this.getActiveSessions().filter(session =>
|
|
187
|
-
!!(session.state?.theirNextNostrPublicKey && session.state?.ourCurrentNostrKey)
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// ============================================================================
|
|
192
|
-
// Stale Management
|
|
193
|
-
// ============================================================================
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Marks a specific device as stale
|
|
197
|
-
*/
|
|
198
|
-
public markDeviceStale(deviceId: string): void {
|
|
199
|
-
const device = this.deviceRecords.get(deviceId);
|
|
200
|
-
if (device) {
|
|
201
|
-
device.isStale = true;
|
|
202
|
-
device.staleTimestamp = Date.now();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Marks the entire user record as stale
|
|
208
|
-
*/
|
|
209
|
-
public markUserStale(): void {
|
|
210
|
-
this.isStale = true;
|
|
211
|
-
this.staleTimestamp = Date.now();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Removes stale devices and sessions older than maxLatency
|
|
216
|
-
*/
|
|
217
|
-
public pruneStaleRecords(maxLatency: number): void {
|
|
218
|
-
const now = Date.now();
|
|
219
|
-
|
|
220
|
-
// Remove stale devices
|
|
221
|
-
for (const [deviceId, device] of this.deviceRecords.entries()) {
|
|
222
|
-
if (device.isStale && device.staleTimestamp &&
|
|
223
|
-
(now - device.staleTimestamp) > maxLatency) {
|
|
224
|
-
this.removeDevice(deviceId);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Remove entire user record if stale
|
|
229
|
-
if (this.isStale && this.staleTimestamp &&
|
|
230
|
-
(now - this.staleTimestamp) > maxLatency) {
|
|
231
|
-
this.close();
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// ============================================================================
|
|
236
|
-
// Utility Methods
|
|
237
|
-
// ============================================================================
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Gets the most recently active device
|
|
241
|
-
*/
|
|
242
|
-
public getMostActiveDevice(): DeviceRecord | undefined {
|
|
243
|
-
const activeDevices = this.getActiveDevices();
|
|
244
|
-
if (activeDevices.length === 0) return undefined;
|
|
245
|
-
|
|
246
|
-
return activeDevices.reduce((most, current) => {
|
|
247
|
-
const mostActivity = most.lastActivity || 0;
|
|
248
|
-
const currentActivity = current.lastActivity || 0;
|
|
249
|
-
return currentActivity > mostActivity ? current : most;
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Gets the preferred session (from most active device)
|
|
255
|
-
*/
|
|
256
|
-
public getPreferredSession(): Session | undefined {
|
|
257
|
-
const mostActive = this.getMostActiveDevice();
|
|
258
|
-
return mostActive?.activeSession;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Checks if this user has any active sessions
|
|
263
|
-
*/
|
|
264
|
-
public hasActiveSessions(): boolean {
|
|
265
|
-
return this.getActiveSessions().length > 0;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Gets total count of devices
|
|
270
|
-
*/
|
|
271
|
-
public getDeviceCount(): number {
|
|
272
|
-
return this.deviceRecords.size;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Gets total count of active sessions
|
|
277
|
-
*/
|
|
278
|
-
public getActiveSessionCount(): number {
|
|
279
|
-
return this.getActiveSessions().length;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Cleanup when destroying the user record
|
|
284
|
-
*/
|
|
285
|
-
public close(): void {
|
|
286
|
-
for (const device of this.deviceRecords.values()) {
|
|
287
|
-
device.activeSession?.close();
|
|
288
|
-
device.inactiveSessions.forEach(session => session.close());
|
|
289
|
-
}
|
|
290
|
-
this.deviceRecords.clear();
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// ============================================================================
|
|
294
|
-
// Legacy Compatibility Methods
|
|
295
|
-
// ============================================================================
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* @deprecated Use upsertDevice instead
|
|
299
|
-
*/
|
|
300
|
-
public conditionalUpdate(deviceId: string, publicKey: string): void {
|
|
301
|
-
this.upsertDevice(deviceId, publicKey);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* @deprecated Use upsertSession instead
|
|
306
|
-
*/
|
|
307
|
-
public insertSession(deviceId: string, session: Session): void {
|
|
308
|
-
this.upsertSession(deviceId, session);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Creates a new session for a device
|
|
313
|
-
*/
|
|
314
|
-
public createSession(
|
|
315
|
-
deviceId: string,
|
|
316
|
-
sharedSecret: Uint8Array,
|
|
317
|
-
ourCurrentPrivateKey: Uint8Array,
|
|
318
|
-
isInitiator: boolean,
|
|
319
|
-
name?: string
|
|
320
|
-
): Session {
|
|
321
|
-
const device = this.getDevice(deviceId);
|
|
322
|
-
if (!device) {
|
|
323
|
-
throw new Error(`No device record found for ${deviceId}`);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
const session = Session.init(
|
|
327
|
-
this.nostrSubscribe,
|
|
328
|
-
device.publicKey,
|
|
329
|
-
ourCurrentPrivateKey,
|
|
330
|
-
isInitiator,
|
|
331
|
-
sharedSecret,
|
|
332
|
-
name || deviceId
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
this.upsertSession(deviceId, session);
|
|
336
|
-
return session;
|
|
337
|
-
}
|
|
338
|
-
}
|