1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/**
* MLS relay layer using marmot-ts: subscribes to kinds 443/1059/445,
* manages key packages, and bridges MLS messages into the existing chat UI.
*
* Produces "rumour-like" objects so the UI can consume them alongside NIP-17.
*/
import type { NostrEvent } from "nostr-tools/pure";
import { bytesToHex } from "@noble/hashes/utils.js";
import { SimplePool } from "nostr-tools/pool";
import { Relay } from "nostr-tools/relay";
import { getMarmotClient, getInviteReader, getNetwork, DM_RELAYS } from "$lib/marmot";
const pool = new SimplePool();
// In-memory caches
const peerGroupMap = new Map<string, string>(); // pubkey -> mlsGroupIdHex
const groupPeerMap = new Map<string, string>(); // mlsGroupIdHex -> pubkey
const nostrIdToMlsId = new Map<string, string>(); // nostrGroupIdHex -> mlsGroupIdHex
let mlsRumourCache: any[] = [];
const processedEventIds = new Set<string>();
/** Load cached rumours and processed event IDs from localStorage */
function loadLocalCache(): void {
try {
const cached = localStorage.getItem("mls:rumours");
if (cached) {
const arr = JSON.parse(cached);
if (Array.isArray(arr)) mlsRumourCache = arr;
}
const ids = localStorage.getItem("mls:processedEvents");
if (ids) {
for (const id of JSON.parse(ids)) processedEventIds.add(id);
}
} catch {}
}
/** Save rumour cache and processed event IDs to localStorage */
function saveLocalCache(): void {
try {
localStorage.setItem("mls:rumours", JSON.stringify(mlsRumourCache));
localStorage.setItem("mls:processedEvents", JSON.stringify([...processedEventIds]));
} catch {}
}
/** Get the nostr group ID (used in h tags) from a MarmotGroup */
function getNostrGroupId(group: any): string {
const data = group.groupData;
if (data?.nostrGroupId) return bytesToHex(data.nostrGroupId);
// Fallback to MLS group ID if no marmot data
return bytesToHex(group.id);
}
/** Register the mapping between nostr group ID and MLS group ID */
function registerGroupIds(group: any): void {
const nostrId = getNostrGroupId(group);
const mlsId = bytesToHex(group.id);
nostrIdToMlsId.set(nostrId, mlsId);
}
// Active subscription state — allows adding group subscriptions dynamically
let _activeSub: {
user: any;
relays: string[];
seen: Set<string>;
onNewRumour: (rumour: any) => void;
subscribedGroups: Set<string>;
closers: (() => void)[];
connectedRelays: any[];
} | null = null;
// ---------------------------------------------------------------------------
// Key package management (kind 443)
// ---------------------------------------------------------------------------
/** Publish our MLS key package to relays so others can invite us to groups. */
export async function publishKeyPackage(user: any, relays: string[] = DM_RELAYS): Promise<void> {
const client = getMarmotClient(user.pubkey);
await client.keyPackages.create({ relays, client: "coinos/marmot-ts" });
}
/** Fetch a recipient's key package event from relays (kind 443). */
export async function fetchKeyPackage(pubkey: string, relays: string[] = DM_RELAYS): Promise<NostrEvent | null> {
const events = await pool.querySync(relays, {
kinds: [443],
authors: [pubkey],
limit: 1,
});
if (events.length === 0) return null;
return events.sort((a, b) => b.created_at - a.created_at)[0] as NostrEvent;
}
/** Check if a recipient supports MLS (has a kind 443 key package). */
export async function recipientSupportsMls(pubkey: string, relays: string[] = DM_RELAYS): Promise<boolean> {
const kp = await fetchKeyPackage(pubkey, relays);
return kp !== null;
}
// ---------------------------------------------------------------------------
// Starting an MLS conversation (1:1 = 2-person group)
// ---------------------------------------------------------------------------
/** Start an MLS DM with a recipient. Creates group and sends welcome via NIP-59. */
export async function startMlsConversation(
user: any,
recipientPubkey: string,
relays: string[] = DM_RELAYS,
): Promise<any> {
// Check if we already have a group with this user
const existingGroupId = peerGroupMap.get(recipientPubkey);
if (existingGroupId) {
const client = getMarmotClient(user.pubkey);
try {
return await client.getGroup(existingGroupId);
} catch {}
}
// Fetch recipient's key package
const kpEvent = await fetchKeyPackage(recipientPubkey, relays);
if (!kpEvent) {
throw new Error("Recipient has no MLS key package published");
}
// Use relays from the key package event so the recipient can see our commits/welcomes
const kpRelaysTag = kpEvent.tags.find((t: string[]) => t[0] === "relays");
const groupRelays = kpRelaysTag ? kpRelaysTag.slice(1) : relays;
// Create group and invite recipient
const client = getMarmotClient(user.pubkey);
const group = await client.createGroup("", { relays: groupRelays });
// Invite by key package event — handles Add proposal, Commit, Welcome delivery via NIP-59
await group.inviteByKeyPackageEvent(kpEvent);
// Register IDs and store peer mapping
registerGroupIds(group);
const mlsGroupIdHex = bytesToHex(group.id);
storePeerGroupMapping(recipientPubkey, mlsGroupIdHex);
// Subscribe to this new group's messages using nostr group ID
subscribeToGroup(getNostrGroupId(group));
return group;
}
// ---------------------------------------------------------------------------
// Sending MLS messages
// ---------------------------------------------------------------------------
/** Send a message via MLS. */
export async function sendMlsMessage(
user: any,
recipientPubkey: string,
message: string,
relays: string[] = DM_RELAYS,
): Promise<void> {
let mlsGroupIdHex = peerGroupMap.get(recipientPubkey);
if (!mlsGroupIdHex) {
const group = await startMlsConversation(user, recipientPubkey, relays);
mlsGroupIdHex = bytesToHex(group.id);
}
const client = getMarmotClient(user.pubkey);
const group = await client.getGroup(mlsGroupIdHex!);
await group.sendChatMessage(message);
// Add to local cache as a rumour-like object
const rumour = makeRumour(message, user.pubkey, recipientPubkey);
mlsRumourCache.push(rumour);
saveLocalCache();
}
// ---------------------------------------------------------------------------
// Dynamic group subscription — subscribe to a single group on existing relays
// ---------------------------------------------------------------------------
/** Subscribe to a group's messages. Uses the nostr group ID (h tag value). */
function subscribeToGroup(nostrGroupIdHex: string): void {
if (!_activeSub) return;
if (_activeSub.subscribedGroups.has(nostrGroupIdHex)) return;
_activeSub.subscribedGroups.add(nostrGroupIdHex);
const { seen, onNewRumour, closers, connectedRelays, user } = _activeSub;
const client = getMarmotClient(user.pubkey);
for (const relay of connectedRelays) {
try {
const sub = relay.subscribe(
[{ kinds: [445], "#h": [nostrGroupIdHex] }],
{
onevent: async (event: any) => {
if (seen.has(event.id)) return;
seen.add(event.id);
const rumour = await handleGroupMessageEvent(event as NostrEvent, user, client);
if (rumour) onNewRumour(rumour);
},
},
);
closers.push(() => sub.close());
} catch {}
}
}
// ---------------------------------------------------------------------------
// Subscription: listen for welcomes (kind 1059) and messages (kind 445)
// ---------------------------------------------------------------------------
/** Subscribe to MLS events for the user. Returns a close function. */
export async function subscribeToMlsMessages(
user: any,
onNewRumour: (rumour: any) => void,
relays: string[] = DM_RELAYS,
): Promise<() => void> {
const client = getMarmotClient(user.pubkey);
const inviteReader = getInviteReader(user.pubkey);
const closers: (() => void)[] = [];
const seen = new Set<string>();
const subscribedGroups = new Set<string>();
const connectedRelays: any[] = [];
// Set up active subscription state for dynamic group adds
_activeSub = { user, relays, seen, onNewRumour, subscribedGroups, closers, connectedRelays };
// Load all known groups and register their ID mappings
await client.loadAllGroups();
const allRelayUrls = new Set(relays);
for (const g of client.groups) {
registerGroupIds(g);
const r = g.relays;
if (r) r.forEach((url: string) => allRelayUrls.add(url));
}
// Listen for new invites (welcomes via NIP-59)
inviteReader.on("newInvite", async (invite) => {
try {
const { group } = await client.joinGroupFromWelcome({ welcomeRumor: invite });
registerGroupIds(group);
const mlsGroupIdHex = bytesToHex(group.id);
const senderPubkey = invite.pubkey;
storePeerGroupMapping(senderPubkey, mlsGroupIdHex);
// Subscribe to this newly joined group's messages
subscribeToGroup(getNostrGroupId(group));
} catch (e) {
console.error("[mls-relay] Failed to join from welcome:", e);
}
});
for (const url of allRelayUrls) {
try {
const relay = await Relay.connect(url);
connectedRelays.push(relay);
// Subscribe to gift wraps (kind 1059) addressed to us — welcomes arrive via NIP-59
const welcomeSub = relay.subscribe(
[{ kinds: [1059], "#p": [user.pubkey] }],
{
onevent: async (event) => {
if (seen.has(event.id)) return;
seen.add(event.id);
try {
const ingested = await inviteReader.ingestEvent(event as NostrEvent);
if (ingested) {
await inviteReader.decryptGiftWraps();
}
} catch {}
},
},
);
closers.push(() => welcomeSub.close());
// Subscribe to messages for all groups we're already in (kind 445)
const groups = client.groups;
if (groups.length > 0) {
const hTags: string[] = [];
for (const g of groups) {
const nostrId = getNostrGroupId(g);
subscribedGroups.add(nostrId);
hTags.push(nostrId);
}
const msgSub = relay.subscribe(
[{ kinds: [445], "#h": hTags }],
{
onevent: async (event) => {
if (seen.has(event.id)) return;
seen.add(event.id);
const rumour = await handleGroupMessageEvent(event as NostrEvent, user, client);
if (rumour) onNewRumour(rumour);
},
},
);
closers.push(() => msgSub.close());
}
} catch (e) {
console.error("[mls-relay] Failed to connect:", url, e);
}
}
return () => {
_activeSub = null;
inviteReader.removeAllListeners("newInvite");
closers.forEach((c) => c());
};
}
// ---------------------------------------------------------------------------
// Handle incoming kind 445 group messages
// ---------------------------------------------------------------------------
async function handleGroupMessageEvent(
event: NostrEvent,
user: any,
client: any,
): Promise<any | null> {
try {
const hTag = event.tags.find((t: string[]) => t[0] === "h");
if (!hTag) return null;
const nostrGroupIdHex = hTag[1];
// Skip already-processed events
if (processedEventIds.has(event.id)) return null;
// Resolve nostr group ID to MLS group ID for client.getGroup()
const mlsGroupIdHex = nostrIdToMlsId.get(nostrGroupIdHex);
if (!mlsGroupIdHex) return null;
let group;
try {
group = await client.getGroup(mlsGroupIdHex);
} catch {
return null;
}
// Use MarmotGroup.ingest to process the event
for await (const result of group.ingest([event])) {
if (result.kind === "processed" && result.result.kind === "applicationMessage") {
const raw = new TextDecoder().decode(result.result.message);
let content: string;
let senderPubkey: string;
try {
const rumor = JSON.parse(raw);
content = rumor.content ?? raw;
senderPubkey = rumor.pubkey ?? (groupPeerMap.get(mlsGroupIdHex) || event.pubkey);
} catch {
content = raw;
senderPubkey = groupPeerMap.get(mlsGroupIdHex) || event.pubkey;
}
// Skip self-echo — we already cached our own messages locally at send time
if (senderPubkey === user.pubkey) {
processedEventIds.add(event.id);
saveLocalCache();
await group.save();
return null;
}
const recipientPubkey = user.pubkey;
const rumour = makeRumour(content, senderPubkey, recipientPubkey);
rumour._mls = true;
rumour._groupId = mlsGroupIdHex;
mlsRumourCache.push(rumour);
processedEventIds.add(event.id);
saveLocalCache();
await group.save();
return rumour;
}
}
processedEventIds.add(event.id);
saveLocalCache();
await group.save();
return null;
} catch (e) {
console.error("[mls-relay] Failed to process group message:", event.id, e);
return null;
}
}
// ---------------------------------------------------------------------------
// Fetch historical MLS messages
// ---------------------------------------------------------------------------
/** Fetch MLS messages the user has received. Returns rumour-like objects. */
export async function getMlsMessageRumours(user: any, relays: string[] = DM_RELAYS): Promise<any[]> {
// Load from localStorage on first call
if (mlsRumourCache.length === 0) loadLocalCache();
if (mlsRumourCache.length > 0) return mlsRumourCache;
const client = getMarmotClient(user.pubkey);
await client.loadAllGroups();
const groups = client.groups;
if (groups.length === 0) return [];
// Register ID mappings and collect nostr group IDs for h tag queries
const hTags: string[] = [];
for (const g of groups) {
registerGroupIds(g);
hTags.push(getNostrGroupId(g));
}
// Also query the group's own relays, not just DM_RELAYS
const groupRelays = new Set(relays);
for (const g of groups) {
const r = g.relays;
if (r) r.forEach((url: string) => groupRelays.add(url));
}
// Fetch kind 445 events
const events = await pool.querySync([...groupRelays], {
kinds: [445],
"#h": hTags,
limit: 1000,
});
// Sort chronologically and process
events.sort((a, b) => a.created_at - b.created_at);
for (const event of events) {
const rumour = await handleGroupMessageEvent(event as NostrEvent, user, client);
if (rumour) mlsRumourCache.push(rumour);
}
return mlsRumourCache;
}
// ---------------------------------------------------------------------------
// Peer <-> Group mapping (localStorage)
// ---------------------------------------------------------------------------
function storePeerGroupMapping(peerPubkey: string, mlsGroupId: string): void {
peerGroupMap.set(peerPubkey, mlsGroupId);
groupPeerMap.set(mlsGroupId, peerPubkey);
try {
const key = "mls:peer2group";
const map = JSON.parse(localStorage.getItem(key) || "{}");
map[peerPubkey] = mlsGroupId;
localStorage.setItem(key, JSON.stringify(map));
const rkey = "mls:group2peer";
const rmap = JSON.parse(localStorage.getItem(rkey) || "{}");
rmap[mlsGroupId] = peerPubkey;
localStorage.setItem(rkey, JSON.stringify(rmap));
} catch {}
}
// Restore peer mappings from localStorage on load
function restorePeerMappings(): void {
try {
const map = JSON.parse(localStorage.getItem("mls:peer2group") || "{}");
for (const [k, v] of Object.entries(map)) {
peerGroupMap.set(k, v as string);
}
const rmap = JSON.parse(localStorage.getItem("mls:group2peer") || "{}");
for (const [k, v] of Object.entries(rmap)) {
groupPeerMap.set(k, v as string);
}
} catch {}
}
restorePeerMappings();
loadLocalCache();
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Create a rumour-like object compatible with the existing chat UI. */
function makeRumour(content: string, senderPubkey: string, recipientPubkey: string): any {
return {
id: bytesToHex(crypto.getRandomValues(new Uint8Array(32))),
kind: 14,
created_at: Math.floor(Date.now() / 1000),
pubkey: senderPubkey,
content,
tags: [["p", recipientPubkey]],
_mls: true,
};
}
|