From 653224387d8c09e461aabe632dbad6d59caf8f05 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 5 Mar 2026 22:10:50 -0800 Subject: Add MLS messaging with marmot-ts, NIP-17 fallback Integrate MLS (Messaging Layer Security) via marmot-ts for encrypted group messaging. Falls back to NIP-17 when the recipient doesn't have an MLS key package. Includes optimistic UI for sent messages, local caching of decrypted rumours, dynamic group subscriptions, and self-echo filtering. Fix SSR crash in nostr settings page. Co-Authored-By: Claude Opus 4.6 --- src/lib/messaging.ts | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/lib/messaging.ts (limited to 'src/lib/messaging.ts') diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts new file mode 100644 index 00000000..fdb03f90 --- /dev/null +++ b/src/lib/messaging.ts @@ -0,0 +1,109 @@ +/** + * Unified messaging facade: MLS (marmot) with NIP-17 fallback. + * + * Drop-in replacement for the nip17 send/subscribe/getMessageRumours API. + * Checks if the recipient supports MLS (has kind 443 key package) and + * uses MLS when available, otherwise falls back to NIP-17. + */ + +import * as nip17 from "$lib/nip17"; +import { + recipientSupportsMls, + sendMlsMessage, + subscribeToMlsMessages, + getMlsMessageRumours, + publishKeyPackage, +} from "$lib/mls-relay"; + +// Re-export unchanged functions from nip17 +export { + decrypt, + getPreferredRelays, + cacheRumour, + getCachedChatList, + setCachedChatList, + getCachedUserInfo, + setCachedUserInfo, +} from "$lib/nip17"; + +// Cache for MLS support checks +const mlsSupportCache = new Map(); + +async function checkMlsSupport(pubkey: string): Promise { + if (mlsSupportCache.has(pubkey)) return mlsSupportCache.get(pubkey)!; + try { + const supported = await recipientSupportsMls(pubkey); + mlsSupportCache.set(pubkey, supported); + return supported; + } catch { + return false; + } +} + +/** Send a message — MLS if both parties support it, NIP-17 otherwise. */ +export const send = async (message: string, user: any, recipient: any, expiryDays?: number) => { + if (user.pubkey !== recipient.pubkey && !expiryDays) { + try { + const peerSupports = await checkMlsSupport(recipient.pubkey); + if (peerSupports) { + await sendMlsMessage(user, recipient.pubkey, message); + return; + } + } catch (e) { + console.warn("[messaging] MLS send failed, falling back to NIP-17:", e); + } + } + + return nip17.send(message, user, recipient, expiryDays); +}; + +/** Get all message rumours — combines NIP-17 and MLS sources. */ +export const getMessageRumours = async (user: any): Promise => { + const [nip17Rumours, mlsRumours] = await Promise.allSettled([ + nip17.getMessageRumours(user), + getMlsMessageRumours(user), + ]); + + const rumours: any[] = []; + if (nip17Rumours.status === "fulfilled") rumours.push(...nip17Rumours.value); + if (mlsRumours.status === "fulfilled") rumours.push(...mlsRumours.value); + + // Dedupe by id + const seen = new Set(); + return rumours.filter((r) => { + if (seen.has(r.id)) return false; + seen.add(r.id); + return true; + }); +}; + +/** Subscribe to both NIP-17 and MLS incoming messages. Returns a close function. */ +export const subscribeToMessages = async ( + user: any, + onNewRumour: (rumour: any) => void, +): Promise<() => void> => { + const closers: (() => void)[] = []; + + // NIP-17 subscription + try { + const closeNip17 = await nip17.subscribeToMessages(user, onNewRumour); + closers.push(closeNip17); + } catch (e) { + console.error("[messaging] NIP-17 subscription failed:", e); + } + + // MLS subscription + try { + const closeMls = await subscribeToMlsMessages(user, onNewRumour); + closers.push(closeMls); + } catch (e) { + console.error("[messaging] MLS subscription failed:", e); + } + + // Publish our key package so others can start MLS conversations with us + publishKeyPackage(user).catch((e) => + console.warn("[messaging] Failed to publish MLS key package:", e), + ); + + return () => closers.forEach((c) => c()); +}; -- cgit v1.2.3