Repository Public
1/**2 * SP Message client engine: identity, devices, MLS groups, sync, outbox, attachments.3 * It has no dependency on the DOM so the same code runs in the browser and in the integration tests.4 */5import { b64, equalBytes, fromUtf8, randomBytes, unb64, utf8, uuid, uuidBytes } from "./bytes"6import {7 decryptChunk, edPublic, edSign, encryptChunk, FILE_CHUNK, hkdf, hpkeOpen, hpkeSeal, linkCode, linkInfo, linkKeygen,8 newRecoveryKey, open, openBackup, seal, sealBackup, sessionBindMessage, signCert, TAG, verifyCert,9} from "./crypto"10import * as mls from "./mls"11import type { Store } from "./store"12import {13 ApiError, defaultPrefs, type Content, type ConvMeta, type DirUser, type FileRef, type Me, type Msg, type NewContent,14 type Pin, type Prefs, type ServerMsg, type Task, type Transport,15} from "./types"1617type DeviceSecret = { userId: string; deviceId: string; signatureSeed: Uint8Array; identitySeed: Uint8Array; identityKey: Uint8Array; cert: Uint8Array }18type PendingLink = { deviceId: string; signatureSeed: Uint8Array; linkPriv: Uint8Array; linkPub: Uint8Array; label: string }19type StoredKp = mls.StoredKeyPackage & { lastResort?: boolean }20type Outgoing = { key: string; conv: string; content: Content; transient: boolean; tries: number }2122export type Conv = ConvMeta & {23 state?: mls.GroupState24 serverMembers: { user_id: string; role: string }[]25 delivered: Record<string, number>26 resync: string[]27 lastSeq: number28 messages: Msg[]29 typing: Record<string, number>30 warnings: string[] // users whose identity key differs from the pinned one31 missing: string[]32 strangers: string[]33 waitingForKeys: boolean34}3536export type CallSignal = { conv: string; from: { userId: string; deviceId: string }; content: Extract<Content, { t: "call" }>; at: number }3738const str = (v: unknown, max: number) => (typeof v === "string" ? v.slice(0, max) : "")39const num = (v: unknown) => (typeof v === "number" && isFinite(v) && v >= 0 ? v : undefined)40/** Attachment descriptors are written by other members: keep only well-formed fields. */41function cleanFiles(files: unknown): FileRef[] | undefined {42 if (!Array.isArray(files)) return undefined43 const out: FileRef[] = []44 for (const f of files.slice(0, 20)) {45 if (!f || typeof f !== "object") continue46 const x = f as Record<string, unknown>47 if (typeof x.id !== "string" || !/^[0-9a-f-]{36}$/.test(x.id) || typeof x.k !== "string" || typeof x.p !== "string") continue48 const size = num(x.size), chunks = num(x.chunks)49 if (size === undefined || !chunks || chunks > 100000) continue50 out.push({51 id: x.id, k: x.k, p: x.p, name: str(x.name, 255).replace(/[\\/\u0000-\u001f]/g, "_") || "file", mime: /^[\w.+-]+\/[\w.+-]+$/.test(str(x.mime, 100)) ? str(x.mime, 100) : "application/octet-stream",52 size, chunks, w: num(x.w), h: num(x.h), dur: num(x.dur), voice: x.voice === true,53 thumb: typeof x.thumb === "string" && x.thumb.length < 60000 && /^[A-Za-z0-9+/=]+$/.test(x.thumb) ? x.thumb : undefined,54 wave: Array.isArray(x.wave) ? x.wave.slice(0, 96).map((n) => (typeof n === "number" ? Math.max(0, Math.min(1, n)) : 0)) : undefined,55 })56 }57 return out.length ? out : undefined58}5960const TRANSIENT_CALL_OPS = new Set(["offer", "answer", "ice", "join", "leave", "decline"])61const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))6263export class Engine {64 convs = new Map<string, Conv>()65 users = new Map<string, DirUser>()66 pins = new Map<string, Pin>()67 prefs: Prefs = { ...defaultPrefs }68 drafts: Record<string, string> = {}69 notes: Record<string, string> = {}70 online = false71 ready = false72 activeConv: string | null = null73 focused = true74 pendingDevices: { id: string; label: string; code: string }[] = []75 notices: { id: string; text: string; at: number }[] = []76 onCall: ((s: CallSignal) => void) | null = null77 onNotify: ((conv: Conv, msg: Msg) => void) | null = null78 /** Set when the server refuses this device or session; the interface must sign out or re-enrol. */79 fatal: string | null = null80 version = 08182 private listeners = new Set<() => void>()83 private queues = new Map<string, Promise<unknown>>()84 private sock: ReturnType<Transport["socket"]> | null = null85 private syncing: Promise<void> | null = null86 private syncAgain = false87 private pumping = false88 private pumpAgain = false89 private outSeq = 090 private rejectedPackages = 091 private timers: ReturnType<typeof setInterval>[] = []92 private selfKey!: Uint8Array93 private ackTimers = new Map<string, ReturnType<typeof setTimeout>>()94 private reconcileTimers = new Map<string, ReturnType<typeof setTimeout>>()95 private stopped = false96 private serverOffset = 09798 constructor(public t: Transport, public store: Store, public me: Me, public dev: DeviceSecret) {}99100 get deviceId() {101 return this.dev.deviceId102 }103 now() {104 return Date.now() + this.serverOffset105 }106107 // ------------------------------------------------------------------ change notification108 subscribe = (fn: () => void) => {109 this.listeners.add(fn)110 return () => this.listeners.delete(fn)111 }112 private emitScheduled = false113 emit() {114 this.version++115 if (this.emitScheduled) return116 this.emitScheduled = true117 queueMicrotask(() => {118 this.emitScheduled = false119 for (const l of this.listeners) l()120 })121 }122 notice(text: string) {123 this.notices.push({ id: uuid(), text, at: Date.now() })124 if (this.notices.length > 20) this.notices.shift()125 this.emit()126 }127128 // ------------------------------------------------------------------ static enrolment helpers129 /** Loads the device secret stored in this browser, if any. */130 static async loadDevice(store: Store, me: Me): Promise<DeviceSecret | null> {131 const d = await store.get<DeviceSecret>("secret/device")132 return d && d.userId === me.id ? d : null133 }134135 /** First device of an account: creates the identity key, the device key, and the recovery key. */136 static async createIdentity(t: Transport, store: Store, me: Me, label: string): Promise<{ dev: DeviceSecret; recoveryKey: string }> {137 const identitySeed = randomBytes(32), signatureSeed = randomBytes(32)138 const identityKey = edPublic(identitySeed), signatureKey = edPublic(signatureSeed)139 const deviceId = uuid()140 const cert = await signCert(identitySeed, me.id, deviceId, signatureKey)141 const recoveryKey = newRecoveryKey()142 const backup = await sealBackup(recoveryKey, me.id, identitySeed)143 await t.request("POST", "/api/identity", {144 identity_key: b64(identityKey), device_id: deviceId, label, signature_key: b64(signatureKey), cert: b64(cert), backup: b64(backup),145 })146 const dev: DeviceSecret = { userId: me.id, deviceId, signatureSeed, identitySeed, identityKey, cert }147 await store.set("secret/device", dev)148 return { dev, recoveryKey }149 }150151 /** Additional device, approval path: registers as pending and returns the code to compare. */152 static async requestLink(t: Transport, store: Store, me: Me, label: string): Promise<{ code: string; deviceId: string }> {153 let p = await store.get<PendingLink>("secret/pending-link")154 if (!p) {155 const signatureSeed = randomBytes(32)156 const kp = await linkKeygen()157 p = { deviceId: uuid(), signatureSeed, linkPriv: kp.privateKey, linkPub: kp.publicKey, label }158 await store.set("secret/pending-link", p)159 await t.request("POST", "/api/devices", { device_id: p.deviceId, label, signature_key: b64(edPublic(signatureSeed)), link_pub: b64(p.linkPub) })160 }161 return { code: await linkCode(me.id, p.deviceId, p.linkPub, edPublic(p.signatureSeed)), deviceId: p.deviceId }162 }163 static async cancelLink(t: Transport, store: Store) {164 const p = await store.get<PendingLink>("secret/pending-link")165 if (p) await t.request("DELETE", `/api/devices/${p.deviceId}`).catch(() => {})166 await store.del("secret/pending-link")167 }168 /** Polled by the new device: once approved, opens the sealed identity key and checks everything. */169 static async completeLink(t: Transport, store: Store, me: Me): Promise<DeviceSecret | null> {170 const p = await store.get<PendingLink>("secret/pending-link")171 if (!p) return null172 const list = await t.request<any[]>("GET", "/api/devices")173 const mine = list.find((d) => d.id === p.deviceId)174 if (!mine) {175 await store.del("secret/pending-link")176 throw new Error("The request was declined or has expired.")177 }178 if (mine.status !== "active" || !mine.link_payload || !mine.cert || !me.identity_key) return null179 const identitySeed = await hpkeOpen(p.linkPriv, linkInfo(me.id, p.deviceId), unb64(mine.link_payload))180 const identityKey = edPublic(identitySeed)181 if (!equalBytes(identityKey, unb64(me.identity_key))) throw new Error("The identity key received does not match this account.")182 const cert = unb64(mine.cert)183 if (!(await verifyCert(identityKey, me.id, p.deviceId, edPublic(p.signatureSeed), cert))) throw new Error("Invalid device certificate.")184 const dev: DeviceSecret = { userId: me.id, deviceId: p.deviceId, signatureSeed: p.signatureSeed, identitySeed, identityKey, cert }185 await store.set("secret/device", dev)186 await store.del("secret/pending-link")187 return dev188 }189 /** Additional device, recovery path: the recovery key opens the backup of the identity key. */190 static async recover(t: Transport, store: Store, me: Me, recoveryKey: string, label: string): Promise<DeviceSecret> {191 if (!me.identity_key) throw new Error("This account has no encryption identity yet.")192 const { blob } = await t.request<{ blob: string }>("GET", "/api/backup")193 let identitySeed: Uint8Array194 try {195 identitySeed = await openBackup(recoveryKey, me.id, unb64(blob))196 } catch {197 throw new Error("This recovery key is not correct.")198 }199 const identityKey = edPublic(identitySeed)200 if (!equalBytes(identityKey, unb64(me.identity_key))) throw new Error("The backup does not match this account.")201 const signatureSeed = randomBytes(32), deviceId = uuid()202 const cert = await signCert(identitySeed, me.id, deviceId, edPublic(signatureSeed))203 await Engine.cancelLink(t, store)204 await t.request("POST", "/api/devices", { device_id: deviceId, label, signature_key: b64(edPublic(signatureSeed)), cert: b64(cert) })205 const dev: DeviceSecret = { userId: me.id, deviceId, signatureSeed, identitySeed, identityKey, cert }206 await store.set("secret/device", dev)207 return dev208 }209 /** After a new sign-in in a browser that already holds a device: proves possession of the device key. */210 static async bindSession(t: Transport, dev: DeviceSecret, sessionId: string) {211 const signature = await edSign(dev.signatureSeed, sessionBindMessage(sessionId, dev.deviceId))212 return t.request<{ status: string }>("POST", "/api/session/device", { device_id: dev.deviceId, signature: b64(signature) })213 }214215 // ------------------------------------------------------------------ lifecycle216 async start() {217 this.selfKey = await hkdf(this.dev.identitySeed, "spm-self-v1")218 for (const [, p] of await this.store.list<Pin & { user: string }>("pin/")) this.pins.set(p.user, p)219 this.pins.set(this.me.id, { ik: b64(this.dev.identityKey), verified: true, since: 0 })220 this.prefs = { ...defaultPrefs, ...((await this.store.get<Prefs>("prefs")) ?? {}) }221 this.drafts = (await this.store.get<Record<string, string>>("drafts")) ?? {}222 this.notes = (await this.store.get<Record<string, string>>("notes")) ?? {}223 for (const [, u] of await this.store.list<DirUser>("user/")) this.users.set(u.id, u)224 for (const [, meta] of await this.store.list<ConvMeta>("c/")) {225 const raw = await this.store.get<Uint8Array>(`secret/g/${meta.id}`)226 let state: mls.GroupState | undefined227 try {228 state = raw ? mls.deserialize(raw) : undefined229 } catch {230 state = undefined231 }232 this.convs.set(meta.id, { ...meta, state, serverMembers: [], delivered: {}, resync: [], lastSeq: meta.cursor, messages: [], typing: {}, warnings: [], missing: [], strangers: [], waitingForKeys: !state })233 }234 for (const [, m] of await this.store.list<Msg>("m/")) this.convs.get(m.conv)?.messages.push(m)235 for (const c of this.convs.values()) this.sortMessages(c)236 this.ready = true237 this.emit()238 this.connect()239 this.timers.push(setInterval(() => this.tick(), 5000))240 }241242 stop() {243 this.stopped = true244 this.timers.forEach(clearInterval)245 this.sock?.close()246 }247248 private connect() {249 if (this.stopped) return250 this.sock = this.t.socket(251 (m) => void this.onSocket(m).catch((e) => console.warn("socket handler", e?.message)),252 (open) => {253 this.online = open254 this.emit()255 if (!open && !this.stopped) setTimeout(() => this.connect(), 1500 + Math.random() * 2000)256 },257 )258 }259260 private async onSocket(m: any) {261 switch (m.t) {262 case "hello":263 this.serverOffset = m.server_time - Date.now()264 await this.sync()265 break266 case "msg":267 await this.onPush(m)268 break269 case "welcome":270 case "membership":271 case "removed":272 case "directory":273 case "device-added":274 if (m.t === "device-added" && m.via === "recovery") this.notice(`A new device was added to your account with the recovery key: ${m.label ?? ""}`)275 void this.sync()276 break277 case "device-pending":278 await this.refreshPendingDevices()279 break280 case "delivered": {281 const c = this.convs.get(m.conversation)282 if (c) {283 c.delivered[m.user] = Math.max(c.delivered[m.user] ?? 0, m.seq)284 this.emit()285 }286 break287 }288 case "typing": {289 const c = this.convs.get(m.conversation)290 if (c) {291 if (m.on) c.typing[m.user] = Date.now()292 else delete c.typing[m.user]293 this.emit()294 }295 break296 }297 case "presence": {298 const u = this.users.get(m.user)299 if (u) {300 u.online = m.online301 this.emit()302 }303 break304 }305 case "self":306 await this.pullSelf()307 break308 }309 }310311 private tick() {312 const now = this.now()313 let changed = false314 for (const c of this.convs.values()) {315 for (const [u, at] of Object.entries(c.typing)) if (Date.now() - at > 6000) (delete c.typing[u], (changed = true))316 const expired = c.messages.filter((m) => m.expiresAt && m.expiresAt <= now)317 if (expired.length) {318 c.messages = c.messages.filter((m) => !expired.includes(m))319 for (const m of expired) void this.store.del(`m/${c.id}/${m.id}`)320 changed = true321 }322 }323 if (changed) this.emit()324 void this.runScheduled()325 if (this.online) void this.pump()326 }327328 /** Serialises all work on one conversation: MLS state must advance strictly in order. */329 private queue<T>(conv: string, fn: () => Promise<T>): Promise<T> {330 const prev = this.queues.get(conv) ?? Promise.resolve()331 const next = prev.catch(() => {}).then(fn)332 this.queues.set(conv, next)333 return next334 }335336 // ------------------------------------------------------------------ persistence337 private async saveConv(c: Conv) {338 const meta: ConvMeta = {339 id: c.id, kind: c.kind, cursor: c.cursor, name: c.name, ttl: c.ttl, unread: c.unread, lastTs: c.lastTs, myRead: c.myRead,340 read: c.read, tasks: c.tasks, muted: c.muted, left: c.left, createdAt: c.createdAt,341 }342 await this.store.set(`c/${c.id}`, meta)343 }344 private async saveState(c: Conv) {345 if (c.state) await this.store.set(`secret/g/${c.id}`, mls.serialize(c.state))346 else await this.store.del(`secret/g/${c.id}`)347 }348 private saveMsg(m: Msg) {349 m.rev = (m.rev ?? 0) + 1350 return this.store.set(`m/${m.conv}/${m.id}`, m)351 }352 private sortMessages(c: Conv) {353 c.messages.sort((a, b) => (a.seq ?? 1e15) - (b.seq ?? 1e15) || a.ts - b.ts)354 }355356 // ------------------------------------------------------------------ directory and identity pinning357 private async ingestUsers(list: DirUser[]) {358 for (const u of list) {359 if (u.identity_key && u.id !== this.me.id) {360 const pin = this.pins.get(u.id)361 if (!pin) {362 // Trust on first use; the person can later verify the safety number.363 await this.setPin(u.id, { ik: u.identity_key, verified: false, since: Date.now() })364 } else if (pin.ik !== u.identity_key && pin.changed !== u.identity_key) {365 await this.setPin(u.id, { ...pin, changed: u.identity_key })366 this.notice(`The security key of ${u.display_name} has changed.`)367 } else if (pin.ik === u.identity_key && pin.changed) {368 await this.setPin(u.id, { ...pin, changed: undefined })369 }370 }371 this.users.set(u.id, u)372 await this.store.set(`user/${u.id}`, u)373 }374 }375 private async setPin(userId: string, pin: Pin) {376 this.pins.set(userId, pin)377 await this.store.set(`pin/${userId}`, { ...pin, user: userId })378 }379 /** Devices of a user whose certificate verifies under the identity key we have pinned for them. */380 private async trustedDevices(userId: string): Promise<string[]> {381 const u = this.users.get(userId), pin = this.pins.get(userId)382 if (!u || !pin) return []383 const ik = unb64(pin.ik)384 const out: string[] = []385 for (const d of u.devices)386 if (d.status === "active" && d.cert && (await verifyCert(ik, userId, d.id, unb64(d.signature_key), unb64(d.cert)))) out.push(d.id)387 return out388 }389 async setVerified(userId: string, verified: boolean) {390 const pin = this.pins.get(userId)391 if (pin) await this.setPin(userId, { ...pin, verified })392 this.emit()393 }394 /** Accepts a changed identity key after the person has been warned. Verification status is reset. */395 async acceptIdentityChange(userId: string) {396 const pin = this.pins.get(userId)397 if (!pin?.changed) return398 await this.setPin(userId, { ik: pin.changed, verified: false, since: Date.now() })399 for (const c of this.convs.values()) {400 this.checkWarnings(c)401 if (c.missing.includes(userId)) await this.reAdd(c.id, userId).catch((e) => console.warn("re-add:", e?.message))402 else this.scheduleReconcile(c.id, 200)403 }404 this.emit()405 }406 /**407 * Explicitly puts a member account back into the encrypted group after all of its devices were408 * removed (identity reset, account re-activated). Never done automatically.409 */410 async reAdd(convId: string, userId: string) {411 const c = this.convs.get(convId)412 if (!c?.state) return413 await this.lookup([userId])414 await this.queue(convId, async () => {415 if (!c.state || !c.serverMembers.some((m) => m.user_id === userId)) return416 const expected = new Map<string, string>()417 const inTree = new Set(mls.members(c.state).map((m) => m.deviceId))418 for (const d of await this.trustedDevices(userId)) if (!inTree.has(d)) expected.set(d, userId)419 const kps = await this.claim([...expected.keys()], expected)420 if (!kps.length) throw new Error(`No verified device is available for ${this.userName(userId)}.`)421 const out = await mls.commit(c.state, kps.map((k) => k.data), [])422 await this.postCommit(c, out, { welcomeTo: kps.map((k) => k.deviceId) })423 this.checkWarnings(c)424 })425 this.emit()426 }427 private checkWarnings(c: Conv) {428 if (!c.state) return429 const bad = new Set<string>()430 for (const m of mls.members(c.state)) {431 const pin = this.pins.get(m.userId)432 if (pin && pin.ik !== b64(m.identityKey)) bad.add(m.userId)433 }434 for (const m of c.serverMembers) if (this.pins.get(m.user_id)?.changed) bad.add(m.user_id)435 c.warnings = [...bad]436 const inTree = new Set(mls.members(c.state).map((m) => m.userId))437 // listed as members by the server but holding no key in the group: they cannot read anything until re-added by a person438 c.missing = c.serverMembers.map((m) => m.user_id).filter((u) => !inTree.has(u) && this.users.get(u)?.devices.length)439 // holding a key in the group without being listed as a member (removed by the next reconciliation)440 c.strangers = c.serverMembers.length ? [...inTree].filter((u) => !c.serverMembers.some((m) => m.user_id === u)) : []441 }442 userName(id: string) {443 return id === this.me.id ? this.me.display_name : (this.users.get(id)?.display_name ?? "Unknown")444 }445 convTitle(c: Conv) {446 if (c.kind === "group") return c.name || "Group"447 const other = c.serverMembers.find((m) => m.user_id !== this.me.id)?.user_id ?? (c.state ? mls.members(c.state).find((m) => m.userId !== this.me.id)?.userId : undefined)448 return other ? this.userName(other) : "Conversation"449 }450 peerOf(c: Conv): string | null {451 if (c.kind !== "dm") return null452 return c.serverMembers.find((m) => m.user_id !== this.me.id)?.user_id ?? null453 }454455 // ------------------------------------------------------------------ sync456 sync(): Promise<void> {457 if (this.syncing) {458 this.syncAgain = true459 return this.syncing460 }461 this.syncing = this.doSync()462 .catch((e) => {463 if (e instanceof ApiError && (e.status === 401 || e.code === "device_not_active" || e.code === "no_device" || e.code === "account_not_active")) {464 this.fatal = e.code465 this.stop()466 this.emit()467 } else console.warn("sync failed:", e?.message)468 })469 .finally(() => {470 this.syncing = null471 if (this.syncAgain) {472 this.syncAgain = false473 void this.sync()474 }475 })476 return this.syncing477 }478479 private async doSync() {480 const s = await this.t.request<any>("GET", "/api/sync")481 this.serverOffset = s.server_time - Date.now()482 await this.ingestUsers(s.users)483 const meU = (s.users as DirUser[]).find((u) => u.id === this.me.id)484 if (meU) this.me = { ...this.me, display_name: meU.display_name }485486 const seen = new Set<string>()487 for (const sc of s.conversations) {488 seen.add(sc.id)489 let c = this.convs.get(sc.id)490 if (!c) {491 c = {492 id: sc.id, kind: sc.kind, cursor: 0, unread: 0, lastTs: new Date(sc.created_at).getTime(), myRead: 0, read: {}, tasks: [],493 createdAt: new Date(sc.created_at).getTime(), serverMembers: [], delivered: {}, resync: [], lastSeq: 0, messages: [], typing: {}, warnings: [], missing: [], strangers: [], waitingForKeys: true,494 }495 this.convs.set(sc.id, c)496 }497 c.left = false498 c.serverMembers = sc.members499 c.delivered = sc.delivered500 c.resync = sc.resync501 c.lastSeq = sc.last_seq502 }503 for (const c of this.convs.values())504 if (!seen.has(c.id) && !c.left) {505 c.left = true506 c.state = undefined507 await this.saveState(c)508 await this.saveConv(c)509 }510511 // Welcomes: join the groups other devices added us to.512 const done: number[] = []513 for (const w of s.welcomes as { id: number; conversation_id: string; after_seq: number; data: string }[]) {514 const c = this.convs.get(w.conversation_id)515 if (c) await this.queue(c.id, () => this.acceptWelcome(c, unb64(w.data), w.after_seq))516 done.push(w.id)517 }518 if (done.length) await this.t.request("POST", "/api/welcomes/ack", { ids: done })519 await this.replenishKeyPackages()520521 await Promise.all(522 [...this.convs.values()].filter((c) => !c.left).map(async (c) => {523 await this.queue(c.id, () => this.catchUp(c))524 this.checkWarnings(c)525 this.scheduleReconcile(c.id)526 }),527 )528 await this.pullSelf()529 await this.refreshPendingDevices()530 this.emit()531 void this.pump()532 }533534 private async acceptWelcome(c: Conv, welcome: Uint8Array, afterSeq: number) {535 // A welcome never replaces a working group state: it is only accepted by a device that holds none536 // (new device, device removed and re-added, or device that asked to be re-synchronised).537 if (c.state && mls.isActive(c.state)) return538 let kp: StoredKp | undefined539 for (const ref of mls.welcomeRefs(welcome)) {540 kp = await this.store.get<StoredKp>(`secret/kp/${ref}`)541 if (kp) break542 }543 if (!kp) return // addressed to a key package this browser no longer holds544 let joined: mls.GroupState545 try {546 joined = await mls.join(welcome, kp)547 } catch (e) {548 console.warn("welcome rejected:", (e as Error).message)549 return550 }551 // The group must be the one this conversation id names, and a direct conversation may only contain its two accounts.552 const listed = new Set(c.serverMembers.map((m) => m.user_id))553 if (!equalBytes(mls.groupId(joined), uuidBytes(c.id)) || (c.kind === "dm" && mls.members(joined).some((m) => !listed.has(m.userId)))) {554 console.warn("welcome rejected: it does not match the conversation")555 return556 }557 c.state = joined558 if (!kp.lastResort) await this.store.del(`secret/kp/${kp.ref}`)559 c.cursor = afterSeq560 c.waitingForKeys = false561 await this.store.del(`secret/pc/${c.id}`)562 await this.saveState(c)563 await this.saveConv(c)564 this.scheduleAck(c)565 }566567 private async replenishKeyPackages() {568 const { count } = await this.t.request<{ count: number }>("GET", "/api/keypackages/count")569 const hasLast = (await this.store.list<StoredKp>("secret/kp/")).some(([, k]) => k.lastResort)570 if (count >= 10 && hasLast) return571 const fresh: StoredKp[] = []572 for (let i = count; i < 25; i++) fresh.push(await mls.newKeyPackage(this.dev))573 let last: StoredKp | undefined574 if (!hasLast) last = { ...(await mls.newKeyPackage(this.dev)), lastResort: true }575 for (const k of [...fresh, ...(last ? [last] : [])]) await this.store.set(`secret/kp/${k.ref}`, k)576 await this.t.request("POST", "/api/keypackages", { packages: fresh.map((k) => b64(k.publicBytes)), last_resort: last ? b64(last.publicBytes) : undefined })577 }578579 private async catchUp(c: Conv) {580 if (!c.state) {581 c.waitingForKeys = true582 return583 }584 for (;;) {585 if (c.cursor >= c.lastSeq) break586 const page = await this.t.request<ServerMsg[]>("GET", `/api/conversations/${c.id}/messages?after=${c.cursor}`)587 if (!page.length) {588 c.cursor = c.lastSeq589 break590 }591 for (const m of page) await this.processOne(c, m)592 await this.saveState(c)593 await this.saveConv(c)594 if (page.length < 200) break595 }596 this.scheduleAck(c)597 }598599 private async onPush(m: ServerMsg & { conversation: string }) {600 const c = this.convs.get(m.conversation)601 if (!c) return void this.sync()602 await this.queue(c.id, async () => {603 c.lastSeq = Math.max(c.lastSeq, m.seq)604 if (!c.state || m.seq <= c.cursor) return605 if (m.seq === c.cursor + 1) {606 await this.processOne(c, m)607 await this.saveState(c)608 await this.saveConv(c)609 this.scheduleAck(c)610 } else await this.catchUp(c)611 })612 this.emit()613 }614615 private scheduleAck(c: Conv) {616 if (this.ackTimers.has(c.id)) return617 this.ackTimers.set(618 c.id,619 setTimeout(() => {620 this.ackTimers.delete(c.id)621 void this.t.request("POST", `/api/conversations/${c.id}/ack`, { seq: c.cursor }).catch(() => {})622 }, 250),623 )624 }625626 /** Applies one stored message, in sequence order. Never throws: a bad message must not block the queue. */627 private async processOne(c: Conv, m: ServerMsg) {628 if (m.seq <= c.cursor) return629 try {630 if (m.sender_device === this.deviceId) {631 if (m.kind === "commit") {632 const pc = await this.store.get<{ clientId: string; state: Uint8Array }>(`secret/pc/${c.id}`)633 if (pc && pc.clientId === m.client_id) {634 // The commit was accepted but this browser stopped before recording it.635 c.state = mls.deserialize(pc.state)636 await this.store.del(`secret/pc/${c.id}`)637 }638 } else {639 const local = c.messages.find((x) => x.id === m.client_id)640 if (local && !local.seq) {641 local.seq = m.seq642 local.status = "sent"643 await this.saveMsg(local)644 }645 }646 } else if (c.state) {647 const before = m.kind === "commit" ? mls.members(c.state) : []648 const r = await mls.receive(c.state, unb64(m.data))649 if (r.kind === "commit") {650 await this.store.del(`secret/pc/${c.id}`)651 if (r.removedSelf) {652 c.state = undefined653 c.waitingForKeys = true654 } else {655 c.state = r.state656 this.membershipEvents(c, before, mls.members(r.state), m)657 this.checkWarnings(c)658 }659 } else {660 c.state = r.state661 const content = JSON.parse(fromUtf8(r.plaintext)) as Content662 const pin = this.pins.get(r.sender.userId)663 const untrusted = !!pin && pin.ik !== b64(r.sender.identityKey)664 await this.apply(c, { userId: r.sender.userId, deviceId: r.sender.deviceId }, content, m.seq, m.created_at, untrusted)665 }666 }667 } catch (e) {668 console.warn(`conversation ${c.id} seq ${m.seq} (${m.kind}):`, (e as Error).message)669 if (m.kind === "commit" && m.sender_device !== this.deviceId) {670 // Without this commit the group state cannot advance: ask the others to re-add this device.671 c.state = undefined672 c.waitingForKeys = true673 await this.store.del(`secret/pc/${c.id}`)674 void this.t.request("POST", `/api/conversations/${c.id}/resync`).catch(() => {})675 }676 }677 c.cursor = m.seq678 }679680 private membershipEvents(c: Conv, before: mls.Member[], after: mls.Member[], m: ServerMsg) {681 const b = new Set(before.map((x) => x.userId)), a = new Set(after.map((x) => x.userId))682 const added = [...a].filter((u) => !b.has(u)), removed = [...b].filter((u) => !a.has(u))683 const push = (type: string, users: string[]) => {684 const msg: Msg = { id: `${m.client_id}-${type}`, conv: c.id, seq: m.seq, from: m.sender_user, dev: m.sender_device, ts: m.created_at, kind: "sys", body: "", sys: { type, users } }685 if (!c.messages.some((x) => x.id === msg.id)) {686 c.messages.push(msg)687 void this.saveMsg(msg)688 }689 }690 if (added.length) push("added", added)691 if (removed.length) push("removed", removed)692 const newDevs = after.filter((x) => b.has(x.userId) && !before.some((y) => y.deviceId === x.deviceId))693 if (newDevs.length) push("device", [...new Set(newDevs.map((x) => x.userId))])694 }695696 // ------------------------------------------------------------------ applying decrypted content697 private async apply(c: Conv, from: { userId: string; deviceId: string }, content: Content, seq: number | undefined, ts: number, untrusted = false) {698 const mine = from.userId === this.me.id699 const find = (id: string) => c.messages.find((x) => x.id === id)700 switch (content.t) {701 case "text": {702 if (find(content.id)) return703 if (typeof content.body !== "string" || content.body.length > 20000) return704 content.files = cleanFiles(content.files)705 if (content.reply && (typeof content.reply.id !== "string" || typeof content.reply.from !== "string" || typeof content.reply.excerpt !== "string")) content.reply = undefined706 if (content.fwd && typeof content.fwd.from !== "string") content.fwd = undefined707 if (content.ttl !== undefined && !(typeof content.ttl === "number" && content.ttl > 0)) content.ttl = undefined708 const msg: Msg = {709 id: content.id, conv: c.id, seq, from: from.userId, dev: from.deviceId, ts, kind: "text", body: content.body, reply: content.reply,710 fwd: content.fwd, files: content.files, status: seq ? "sent" : "sending", untrusted: untrusted || undefined,711 expiresAt: content.ttl ? ts + content.ttl * 1000 : undefined,712 }713 c.messages.push(msg)714 c.lastTs = ts715 if (!mine && !(this.activeConv === c.id && this.focused)) {716 c.unread++717 if (!c.muted) this.onNotify?.(c, msg)718 }719 await this.saveMsg(msg)720 break721 }722 case "edit": {723 const x = find(content.target)724 if (x && x.from === from.userId && !x.deleted && typeof content.body === "string") {725 x.body = content.body.slice(0, 20000)726 x.edited = ts727 await this.saveMsg(x)728 }729 break730 }731 case "del": {732 const x = find(content.target)733 if (x && x.from === from.userId) {734 x.deleted = true735 x.body = ""736 x.files = undefined737 x.reply = undefined738 await this.saveMsg(x)739 }740 break741 }742 case "react": {743 const x = find(content.target)744 if (x && typeof content.emoji === "string" && content.emoji.length <= 16) {745 const r = (x.reactions ??= {})746 const set = new Set(r[content.emoji] ?? [])747 if (content.on) set.add(from.userId)748 else set.delete(from.userId)749 if (set.size) r[content.emoji] = [...set]750 else delete r[content.emoji]751 await this.saveMsg(x)752 }753 break754 }755 case "pin": {756 const x = find(content.target)757 if (x) {758 x.pinned = !!content.on759 await this.saveMsg(x)760 }761 break762 }763 case "read": {764 const s = Number(content.seq) || 0765 if (mine) {766 if (s > c.myRead) {767 c.myRead = s768 c.unread = c.messages.filter((x) => x.kind === "text" && x.from !== this.me.id && (x.seq ?? 0) > s).length769 }770 } else c.read[from.userId] = Math.max(c.read[from.userId] ?? 0, s)771 break772 }773 case "meta": {774 const sys: Msg = { id: content.id, conv: c.id, seq, from: from.userId, dev: from.deviceId, ts, kind: "sys", body: "", sys: { type: "meta" } }775 if (typeof content.name === "string" && c.kind === "group" && content.name !== c.name) {776 c.name = content.name.slice(0, 80)777 sys.sys = { type: "renamed", text: c.name }778 } else if (typeof content.ttl === "number" && content.ttl !== (c.ttl ?? 0)) {779 c.ttl = content.ttl || undefined780 sys.sys = { type: "ttl", text: String(content.ttl) }781 } else break782 if (!find(sys.id)) {783 c.messages.push(sys)784 await this.saveMsg(sys)785 }786 break787 }788 case "task": {789 if (content.op === "add" && typeof content.title === "string" && !c.tasks.some((t) => t.id === content.task))790 c.tasks.push({ id: content.task, title: content.title.slice(0, 300), done: false, by: from.userId, due: content.due, ts } satisfies Task)791 else if (content.op === "toggle") c.tasks = c.tasks.map((t) => (t.id === content.task ? { ...t, done: !t.done } : t))792 else if (content.op === "del") c.tasks = c.tasks.filter((t) => t.id !== content.task)793 break794 }795 case "call": {796 if (content.op === "ring" && !find(content.id)) {797 const msg: Msg = { id: content.id, conv: c.id, seq, from: from.userId, dev: from.deviceId, ts, kind: "call", body: "", sys: { type: "call", call: content.call, video: content.video } }798 c.messages.push(msg)799 c.lastTs = ts800 await this.saveMsg(msg)801 }802 if (seq !== undefined) this.onCall?.({ conv: c.id, from, content, at: ts })803 break804 }805 }806 this.sortMessages(c)807 this.emit()808 }809810 // ------------------------------------------------------------------ sending811 /** Queues a message. It is shown immediately, stored in the outbox, and delivered as soon as possible. */812 async send(convId: string, content: NewContent, opts: { id?: string } = {}): Promise<string> {813 const c = this.convs.get(convId)814 if (!c) throw new Error("unknown conversation")815 const full = { ...content, id: opts.id ?? uuid(), ts: this.now() } as Content816 if (full.t === "text" && c.ttl && !full.ttl) full.ttl = c.ttl817 const transient = full.t === "read" || (full.t === "call" && TRANSIENT_CALL_OPS.has(full.op))818 await this.apply(c, { userId: this.me.id, deviceId: this.deviceId }, full, undefined, full.ts)819 const key = `o/${String(full.ts).padStart(15, "0")}-${String(this.outSeq++).padStart(8, "0")}-${full.id}`820 await this.store.set(key, { key, conv: convId, content: full, transient, tries: 0 } satisfies Outgoing)821 await this.saveConv(c)822 void this.pump()823 return full.id824 }825826 /** Sends everything waiting in the outbox, oldest first. Stops at the first network failure. */827 async pump() {828 if (!this.online) return829 if (this.pumping) {830 this.pumpAgain = true831 return832 }833 this.pumping = true834 try {835 this.pumpAgain = false836 for (const [key, o] of await this.store.list<Outgoing>("o/")) {837 const c = this.convs.get(o.conv)838 if (!c || c.left) {839 await this.store.del(key)840 continue841 }842 if (!c.state) continue843 const result = await this.queue(c.id, () => this.sendOne(c, o))844 if (result === "retry") break845 if (result === "done" || result === "drop") await this.store.del(key)846 }847 } finally {848 this.pumping = false849 }850 if (this.pumpAgain) void this.pump()851 }852853 private async sendOne(c: Conv, o: Outgoing): Promise<"done" | "drop" | "retry" | "later"> {854 for (let attempt = 0; attempt < 3; attempt++) {855 if (!c.state) return "later"856 const enc = await mls.encrypt(c.state, utf8(JSON.stringify(o.content)))857 c.state = enc.state858 await this.saveState(c) // the ratchet has advanced: persist before anything can fail859 try {860 const ttl = o.content.t === "text" ? o.content.ttl : undefined861 const r = await this.t.request<{ seq: number }>("POST", `/api/conversations/${c.id}/messages`, {862 epoch: mls.epochOf(c.state), client_id: o.content.id, data: b64(enc.data), transient: o.transient, expires_in: ttl,863 })864 if (r.seq === c.cursor + 1) {865 c.cursor = r.seq866 await this.saveConv(c)867 }868 c.lastSeq = Math.max(c.lastSeq, r.seq)869 const local = c.messages.find((x) => x.id === o.content.id)870 if (local) {871 local.seq = r.seq872 local.status = "sent"873 await this.saveMsg(local)874 this.sortMessages(c)875 }876 this.emit()877 return "done"878 } catch (e) {879 if (e instanceof ApiError && e.code === "stale_epoch") {880 c.lastSeq = Math.max(c.lastSeq, c.cursor + 1)881 await this.catchUp(c)882 continue883 }884 if (e instanceof ApiError && e.status >= 400 && e.status < 500 && e.status !== 429) {885 const local = c.messages.find((x) => x.id === o.content.id)886 if (local) {887 local.status = "failed"888 await this.saveMsg(local)889 }890 this.emit()891 return "drop"892 }893 return "retry"894 }895 }896 return "retry"897 }898899 /** Records the outcome of a call on its history entry (answered, missed, declined, duration:N). */900 annotateCall(convId: string, callId: string, text: string) {901 const c = this.convs.get(convId)902 const m = c?.messages.find((x) => x.kind === "call" && x.sys?.call === callId)903 if (!c || !m || !m.sys) return904 if (m.sys.text?.startsWith("duration:") || (m.sys.text === "answered" && !text.startsWith("duration:"))) return905 if (text === "missed" && m.from !== this.me.id && m.sys.text !== "missed") c.unread++906 m.sys.text = text907 void this.saveMsg(m)908 void this.saveConv(c)909 this.emit()910 }911912 typing(convId: string, on: boolean) {913 this.sock?.send({ t: "typing", conversation: convId, on })914 }915916 /** Marks everything received so far as read, and tells the other side if read receipts are enabled. */917 async markRead(convId: string) {918 const c = this.convs.get(convId)919 if (!c) return920 const last = c.messages.reduce((n, m) => Math.max(n, m.seq ?? 0), 0)921 const had = c.unread922 c.unread = 0923 if (last > c.myRead) {924 c.myRead = last925 await this.saveConv(c)926 if (this.prefs.readReceipts && c.state) await this.send(convId, { t: "read", seq: last })927 } else if (had) await this.saveConv(c)928 if (had) this.emit()929 }930931 // ------------------------------------------------------------------ group changes932 /** Verified key packages for the given devices. Devices without a valid package are skipped. */933 private async claim(deviceIds: string[], expected: Map<string, string>): Promise<{ deviceId: string; data: Uint8Array }[]> {934 if (!deviceIds.length) return []935 const got = await this.t.request<{ device_id: string; data: string }[]>("POST", "/api/keypackages/claim", { device_ids: deviceIds })936 const out: { deviceId: string; data: Uint8Array }[] = []937 for (const g of got) {938 const data = unb64(g.data)939 const id = await mls.keyPackageIdentity(data)940 const userId = expected.get(g.device_id)941 const pin = userId ? this.pins.get(userId) : undefined942 // The server chose this package: accept it only if it is certified by the identity key we pinned.943 const reason = !id ? "invalid certificate or encoding" : id.deviceId !== g.device_id ? "device mismatch" : id.userId !== userId ? "account mismatch"944 : !pin ? "no pinned identity" : pin.ik !== b64(id.identityKey) ? "identity key differs from the pinned one" : null945 if (reason) {946 this.rejectedPackages++947 console.warn(`key package for device ${g.device_id.slice(0, 8)} refused: ${reason}`)948 continue949 }950 out.push({ deviceId: g.device_id, data })951 }952 return out953 }954955 private async postCommit(c: Conv, out: mls.CommitOut, extra: { welcomeTo: string[]; add_users?: string[]; remove_users?: string[]; resynced?: string[] }) {956 const clientId = uuid()957 const epoch = mls.epochOf(c.state!)958 await this.store.set(`secret/pc/${c.id}`, { clientId, state: mls.serialize(out.state) })959 let r: { seq: number }960 try {961 r = await this.t.request<{ seq: number }>("POST", `/api/conversations/${c.id}/commit`, {962 epoch, client_id: clientId, commit: b64(out.commit),963 welcomes: out.welcome ? extra.welcomeTo.map((d) => ({ device_id: d, data: b64(out.welcome!) })) : [],964 add_users: extra.add_users ?? [], remove_users: extra.remove_users ?? [], resynced: extra.resynced ?? [],965 })966 } catch (e) {967 await this.store.del(`secret/pc/${c.id}`)968 throw e969 }970 c.state = out.state971 if (r.seq === c.cursor + 1) c.cursor = r.seq972 c.lastSeq = Math.max(c.lastSeq, r.seq)973 await this.saveState(c)974 await this.saveConv(c)975 await this.store.del(`secret/pc/${c.id}`)976 }977978 private scheduleReconcile(convId: string, delay = 400 + Math.random() * 2200) {979 if (this.reconcileTimers.has(convId)) return980 this.reconcileTimers.set(981 convId,982 setTimeout(() => {983 this.reconcileTimers.delete(convId)984 const c = this.convs.get(convId)985 if (c) void this.queue(convId, () => this.reconcile(c)).catch((e) => console.warn("reconcile:", e?.message))986 }, delay),987 )988 }989990 /**991 * Brings the MLS group in line with the directory: adds certified devices of member accounts that992 * are missing, removes devices that were revoked and leaves of accounts that left.993 */994 private async reconcile(c: Conv, tries = 0): Promise<void> {995 if (!c.state || c.left || !mls.isActive(c.state)) return996 const current = mls.members(c.state)997 const memberUsers = new Set(c.serverMembers.map((m) => m.user_id))998 if (!memberUsers.has(this.me.id)) return999 const expected = new Map<string, string>()1000 const add: string[] = [], removeLeaves: number[] = [], resynced: string[] = []1001 let missingDirectory = false1002 for (const u of memberUsers) {1003 if (!this.users.has(u)) {1004 missingDirectory = true1005 continue1006 }1007 // The server's member list is not trusted for additions: only accounts that already hold a leaf in the1008 // authenticated MLS tree get their new devices added here. Adding a new account is always an explicit action.1009 const trusted = current.some((m) => m.userId === u) ? await this.trustedDevices(u) : []1010 for (const d of trusted) {1011 expected.set(d, u)1012 const inTree = current.some((m) => m.deviceId === d)1013 if (!inTree) add.push(d)1014 else if (c.resync.includes(d) && d !== this.deviceId) {1015 removeLeaves.push(current.find((m) => m.deviceId === d)!.leafIndex)1016 add.push(d)1017 resynced.push(d)1018 }1019 }1020 const u2 = this.users.get(u)!1021 const activeIds = new Set(u2.devices.filter((d) => d.status === "active").map((d) => d.id))1022 for (const m of current) if (m.userId === u && !activeIds.has(m.deviceId) && m.deviceId !== this.deviceId) removeLeaves.push(m.leafIndex)1023 }1024 for (const m of current) if (!memberUsers.has(m.userId)) removeLeaves.push(m.leafIndex)1025 if (missingDirectory) void this.lookup([...memberUsers].filter((u) => !this.users.has(u)))1026 if (!add.length && !removeLeaves.length) return1027 const refusedBefore = this.rejectedPackages1028 const kps = await this.claim(add, expected)1029 const uniqueRemoves = [...new Set(removeLeaves)]1030 if (!kps.length && !uniqueRemoves.length) {1031 // Nothing usable. If packages were handed out but refused, wait much longer: every claim consumes a1032 // one-time package of that device, and retrying quickly would drain them.1033 this.scheduleReconcile(c.id, this.rejectedPackages > refusedBefore ? 60_000 : 4000)1034 return1035 }1036 const epochBefore = mls.epochOf(c.state)1037 try {1038 const out = await mls.commit(c.state, kps.map((k) => k.data), uniqueRemoves)1039 await this.postCommit(c, out, { welcomeTo: kps.map((k) => k.deviceId), resynced })1040 this.checkWarnings(c)1041 this.emit()1042 } catch (e) {1043 if (e instanceof ApiError && e.code === "epoch_conflict") {1044 c.lastSeq = Math.max(c.lastSeq, c.cursor + 1)1045 await this.catchUp(c)1046 if (tries < 3 && c.state && mls.epochOf(c.state) !== epochBefore) return this.reconcile(c, tries + 1)1047 return1048 }1049 throw e1050 }1051 }10521053 async lookup(ids: string[]) {1054 if (!ids.length) return1055 await this.ingestUsers(await this.t.request<DirUser[]>("POST", "/api/users/lookup", { ids }))1056 this.emit()1057 }10581059 /** Creates a conversation with the given accounts and adds all of their certified devices. */1060 async createConversation(kind: "dm" | "group", userIds: string[], name?: string): Promise<string> {1061 const others = [...new Set(userIds)].filter((u) => u !== this.me.id)1062 if (kind === "dm") {1063 const existing = [...this.convs.values()].find((c) => c.kind === "dm" && !c.left && c.serverMembers.some((m) => m.user_id === others[0]))1064 if (existing) return existing.id1065 }1066 await this.lookup([...others, this.me.id])1067 const expected = new Map<string, string>()1068 for (const u of [...others, this.me.id]) for (const d of await this.trustedDevices(u)) if (d !== this.deviceId) expected.set(d, u)1069 for (const u of others)1070 if (![...expected.values()].includes(u)) throw new Error(`${this.userName(u)} has not finished setting up encryption yet.`)1071 const id = uuid()1072 const state = await mls.newGroup(uuidBytes(id), this.dev)1073 const kps = await this.claim([...expected.keys()], expected)1074 for (const u of others)1075 if (!kps.some((k) => expected.get(k.deviceId) === u)) throw new Error(`No verified device is available for ${this.userName(u)}.`)1076 const out = await mls.commit(state, kps.map((k) => k.data), [])1077 try {1078 await this.t.request("POST", "/api/conversations", {1079 id, kind, member_ids: others, client_id: uuid(), commit: b64(out.commit),1080 welcomes: kps.map((k) => ({ device_id: k.deviceId, data: b64(out.welcome!) })),1081 })1082 } catch (e) {1083 if (e instanceof ApiError && e.code === "dm_exists") {1084 await this.sync()1085 return e.message1086 }1087 throw e1088 }1089 const now = this.now()1090 const c: Conv = {1091 id, kind, cursor: 1, unread: 0, lastTs: now, myRead: 0, read: {}, tasks: [], createdAt: now, state: out.state, name: kind === "group" ? name : undefined,1092 serverMembers: [{ user_id: this.me.id, role: "owner" }, ...others.map((u) => ({ user_id: u, role: kind === "dm" ? "owner" : "member" }))],1093 delivered: {}, resync: [], lastSeq: 1, messages: [], typing: {}, warnings: [], missing: [], strangers: [], waitingForKeys: false,1094 }1095 this.convs.set(id, c)1096 await this.saveState(c)1097 await this.saveConv(c)1098 if (kind === "group" && name) await this.send(id, { t: "meta", name })1099 this.emit()1100 return id1101 }11021103 async addMembers(convId: string, userIds: string[]) {1104 const c = this.convs.get(convId)1105 if (!c?.state || c.kind !== "group") throw new Error("not a group")1106 await this.lookup(userIds)1107 await this.queue(convId, async () => {1108 const expected = new Map<string, string>()1109 for (const u of userIds) for (const d of await this.trustedDevices(u)) expected.set(d, u)1110 const kps = await this.claim([...expected.keys()], expected)1111 const usable = userIds.filter((u) => kps.some((k) => expected.get(k.deviceId) === u))1112 if (!usable.length) throw new Error("None of these people has a verified device available.")1113 const out = await mls.commit(c.state!, kps.map((k) => k.data), [])1114 await this.postCommit(c, out, { welcomeTo: kps.map((k) => k.deviceId), add_users: usable })1115 for (const u of usable) if (!c.serverMembers.some((m) => m.user_id === u)) c.serverMembers.push({ user_id: u, role: "member" })1116 const msg: Msg = { id: uuid(), conv: c.id, from: this.me.id, dev: this.deviceId, ts: this.now(), seq: c.cursor, kind: "sys", body: "", sys: { type: "added", users: usable } }1117 c.messages.push(msg)1118 await this.saveMsg(msg)1119 })1120 if (c.name) await this.send(convId, { t: "meta", name: c.name }) // new members have no history: repeat the group name1121 this.emit()1122 }11231124 async removeMember(convId: string, userId: string) {1125 const c = this.convs.get(convId)1126 if (!c?.state || c.kind !== "group") throw new Error("not a group")1127 await this.queue(convId, async () => {1128 const leaves = mls.members(c.state!).filter((m) => m.userId === userId).map((m) => m.leafIndex)1129 const out = await mls.commit(c.state!, [], leaves)1130 await this.postCommit(c, out, { welcomeTo: [], remove_users: [userId] })1131 c.serverMembers = c.serverMembers.filter((m) => m.user_id !== userId)1132 const msg: Msg = { id: uuid(), conv: c.id, from: this.me.id, dev: this.deviceId, ts: this.now(), seq: c.cursor, kind: "sys", body: "", sys: { type: "removed", users: [userId] } }1133 c.messages.push(msg)1134 await this.saveMsg(msg)1135 })1136 this.emit()1137 }11381139 async leave(convId: string) {1140 const c = this.convs.get(convId)1141 if (!c) return1142 await this.t.request("POST", `/api/conversations/${convId}/leave`)1143 c.left = true1144 c.state = undefined1145 await this.saveState(c)1146 await this.saveConv(c)1147 this.emit()1148 }11491150 async deleteLocal(convId: string) {1151 const c = this.convs.get(convId)1152 if (!c) return1153 for (const m of c.messages) await this.store.del(`m/${convId}/${m.id}`)1154 c.messages = []1155 if (c.left) {1156 await this.store.del(`c/${convId}`)1157 this.convs.delete(convId)1158 }1159 this.emit()1160 }11611162 // ------------------------------------------------------------------ devices1163 async refreshPendingDevices() {1164 const list = await this.t.request<any[]>("GET", "/api/devices").catch(() => [])1165 const out: Engine["pendingDevices"] = []1166 for (const d of list)1167 if (d.status === "pending" && d.link_pub)1168 out.push({ id: d.id, label: d.label, code: await linkCode(this.me.id, d.id, unb64(d.link_pub), unb64(d.signature_key)) })1169 this.pendingDevices = out1170 this.emit()1171 }1172 /** Run on a trusted device after the person compared the code: certifies the new device and hands it the identity key. */1173 async approveDevice(deviceId: string) {1174 const list = await this.t.request<any[]>("GET", "/api/devices")1175 const d = list.find((x) => x.id === deviceId && x.status === "pending")1176 if (!d) throw new Error("This request no longer exists.")1177 const cert = await signCert(this.dev.identitySeed, this.me.id, deviceId, unb64(d.signature_key))1178 const payload = await hpkeSeal(unb64(d.link_pub), linkInfo(this.me.id, deviceId), this.dev.identitySeed)1179 await this.t.request("POST", `/api/devices/${deviceId}/approve`, { cert: b64(cert), link_payload: b64(payload) })1180 await this.refreshPendingDevices()1181 }1182 async rejectDevice(deviceId: string) {1183 await this.t.request("DELETE", `/api/devices/${deviceId}`)1184 await this.refreshPendingDevices()1185 }1186 /** Replaces the recovery key: the old one stops working. */1187 async rotateRecoveryKey(): Promise<string> {1188 const key = newRecoveryKey()1189 await this.t.request("PUT", "/api/backup", { blob: b64(await sealBackup(key, this.me.id, this.dev.identitySeed)) })1190 return key1191 }11921193 // ------------------------------------------------------------------ data shared between my own devices1194 private async putSelf(key: string, value: unknown) {1195 const data = await seal(this.selfKey, utf8(JSON.stringify(value)), utf8(key))1196 await this.t.request("PUT", `/api/self/${encodeURIComponent(key)}`, { data: b64(data) }).catch(() => {})1197 }1198 private async pullSelf() {1199 const rows = await this.t.request<{ key: string; data: string }[]>("GET", "/api/self").catch(() => [])1200 for (const r of rows) {1201 try {1202 const v = JSON.parse(fromUtf8(await open(this.selfKey, unb64(r.data), utf8(r.key))))1203 if (r.key === "prefs") this.prefs = { ...defaultPrefs, ...v }1204 else if (r.key === "drafts") this.drafts = v1205 else if (r.key === "notes") this.notes = v1206 } catch {1207 /* a blob sealed under a previous identity: ignore */1208 }1209 }1210 await this.store.set("prefs", this.prefs)1211 await this.store.set("drafts", this.drafts)1212 await this.store.set("notes", this.notes)1213 this.emit()1214 }1215 async setPrefs(p: Partial<Prefs>) {1216 this.prefs = { ...this.prefs, ...p }1217 await this.store.set("prefs", this.prefs)1218 this.emit()1219 await this.putSelf("prefs", this.prefs)1220 }1221 private draftTimer: ReturnType<typeof setTimeout> | null = null1222 setDraft(convId: string, text: string) {1223 if (text) this.drafts[convId] = text1224 else delete this.drafts[convId]1225 if (this.draftTimer) clearTimeout(this.draftTimer)1226 this.draftTimer = setTimeout(() => {1227 void this.store.set("drafts", this.drafts)1228 void this.putSelf("drafts", this.drafts)1229 }, 1200)1230 }1231 async setNote(convId: string, text: string) {1232 if (text) this.notes[convId] = text1233 else delete this.notes[convId]1234 await this.store.set("notes", this.notes)1235 await this.putSelf("notes", this.notes)1236 this.emit()1237 }1238 async toggleBookmark(convId: string, msgId: string) {1239 const m = this.convs.get(convId)?.messages.find((x) => x.id === msgId)1240 if (!m) return1241 m.bookmarked = !m.bookmarked1242 await this.saveMsg(m)1243 this.emit()1244 }1245 async setMuted(convId: string, muted: boolean) {1246 const c = this.convs.get(convId)1247 if (!c) return1248 c.muted = muted1249 await this.saveConv(c)1250 this.emit()1251 }12521253 // ------------------------------------------------------------------ scheduled messages (kept on this device until due)1254 async schedule(convId: string, content: NewContent, at: number) {1255 const id = uuid()1256 await this.store.set(`sched/${id}`, { id, conv: convId, content, at })1257 this.emit()1258 return id1259 }1260 async scheduled(): Promise<{ id: string; conv: string; content: NewContent; at: number }[]> {1261 return (await this.store.list<{ id: string; conv: string; content: NewContent; at: number }>("sched/")).map(([, v]) => v).sort((a, b) => a.at - b.at)1262 }1263 async cancelScheduled(id: string) {1264 await this.store.del(`sched/${id}`)1265 this.emit()1266 }1267 private async runScheduled() {1268 for (const s of await this.scheduled())1269 if (s.at <= this.now()) {1270 await this.store.del(`sched/${s.id}`)1271 if (this.convs.get(s.conv)) await this.send(s.conv, s.content)1272 }1273 }12741275 // ------------------------------------------------------------------ search (runs on this device, over decrypted history)1276 search(query: string): Msg[] {1277 const q = query.trim().toLowerCase()1278 if (q.length < 2) return []1279 const out: Msg[] = []1280 for (const c of this.convs.values())1281 for (const m of c.messages)1282 if (m.kind === "text" && !m.deleted && (m.body.toLowerCase().includes(q) || m.files?.some((f) => f.name.toLowerCase().includes(q)))) out.push(m)1283 return out.sort((a, b) => b.ts - a.ts).slice(0, 200)1284 }12851286 // ------------------------------------------------------------------ attachments1287 /**1288 * Encrypts a file in chunks and uploads it. Chunks already stored on the server are skipped, and1289 * each chunk is retried, so an interrupted transfer resumes where it stopped.1290 */1291 async upload(1292 convId: string, data: Blob, info: Omit<FileRef, "id" | "k" | "p" | "size" | "chunks">,1293 onProgress?: (fraction: number) => void, signal?: { aborted: boolean }, expiresIn?: number,1294 ): Promise<FileRef> {1295 const key = randomBytes(32), prefix = randomBytes(8)1296 const chunks = Math.max(1, Math.ceil(data.size / FILE_CHUNK))1297 const cipherSize = data.size + chunks * TAG1298 const { id } = await this.t.request<{ id: string }>("POST", "/api/files", { size: cipherSize, chunk_size: FILE_CHUNK + TAG, chunk_count: chunks })1299 let have = new Set<number>()1300 for (let i = 0; i < chunks; i++) {1301 if (signal?.aborted) {1302 await this.t.request("DELETE", `/api/files/${id}`).catch(() => {})1303 throw new Error("cancelled")1304 }1305 if (have.has(i)) continue1306 const plain = new Uint8Array(await data.slice(i * FILE_CHUNK, Math.min(data.size, (i + 1) * FILE_CHUNK)).arrayBuffer())1307 const ct = await encryptChunk(key, prefix, i, i === chunks - 1, plain)1308 for (let attempt = 0; ; attempt++) {1309 try {1310 await this.t.putBytes(`/api/files/${id}/chunks/${i}`, ct)1311 break1312 } catch (e) {1313 if (e instanceof ApiError && e.status < 500 && e.status !== 429) throw e1314 if (attempt >= 8) throw new Error("The upload could not be completed. Check the connection and try again.")1315 await sleep(Math.min(15000, 800 * 2 ** attempt))1316 have = new Set((await this.t.request<{ received: number[] }>("GET", `/api/files/${id}`).catch(() => ({ received: [] }))).received)1317 if (have.has(i)) break1318 }1319 }1320 onProgress?.((i + 1) / chunks)1321 }1322 await this.t.request("POST", `/api/files/${id}/complete`, { conversation_id: convId, expires_in: expiresIn })1323 return { ...info, id, k: b64(key), p: b64(prefix), size: data.size, chunks }1324 }13251326 private blobCache = new Map<string, Blob>()1327 /** Downloads and decrypts an attachment. Any modified, missing or reordered chunk makes it fail. */1328 async download(ref: FileRef, onProgress?: (fraction: number) => void): Promise<Blob> {1329 const cached = this.blobCache.get(ref.id)1330 if (cached) return cached1331 const key = unb64(ref.k), prefix = unb64(ref.p)1332 const parts: BlobPart[] = []1333 for (let i = 0; i < ref.chunks; i++) {1334 let ct: Uint8Array | undefined1335 for (let attempt = 0; !ct; attempt++) {1336 try {1337 ct = await this.t.getBytes(`/api/files/${ref.id}/chunks/${i}`)1338 } catch (e) {1339 if ((e instanceof ApiError && e.status < 500) || attempt >= 5) throw e1340 await sleep(800 * 2 ** attempt)1341 }1342 }1343 parts.push((await decryptChunk(key, prefix, i, i === ref.chunks - 1, ct)) as unknown as BlobPart)1344 onProgress?.((i + 1) / ref.chunks)1345 }1346 const blob = new Blob(parts, { type: ref.mime || "application/octet-stream" })1347 if (blob.size !== ref.size) throw new Error("attachment size mismatch")1348 if (blob.size < 64 * 1024 * 1024) {1349 this.blobCache.set(ref.id, blob)1350 if (this.blobCache.size > 40) this.blobCache.delete(this.blobCache.keys().next().value!)1351 }1352 return blob1353 }1354 async shareFile(ref: FileRef, convId: string) {1355 await this.t.request("POST", `/api/files/${ref.id}/share`, { conversation_id: convId })1356 }13571358 /** Forwards a message to another conversation, with attribution to the original author. */1359 async forward(msg: Msg, toConv: string) {1360 for (const f of msg.files ?? []) await this.shareFile(f, toConv)1361 await this.send(toConv, { t: "text", body: msg.body, files: msg.files, fwd: { from: this.userName(msg.from) } })1362 }13631364 // ------------------------------------------------------------------ sign-out1365 /** Removes every local secret and message from this browser. */1366 async wipe() {1367 this.stop()1368 await this.store.clear()1369 }1370}1371