ping_core/client.rs
1//! `MessagingClient` — top-level handle. Owns the OpenMLS provider, identity, local device,
2//! and the set of open conversations.
3//!
4//! All operations are `async`. The intent is that the FFI generators emit Swift `async`,
5//! Kotlin `suspend`, and the WASM glue exposes Promises.
6
7use openmls::framing::MlsMessageOut;
8use openmls::prelude::{
9 tls_codec::Serialize as TlsSerialize, BasicCredential, Ciphersuite, CredentialWithKey,
10 KeyPackageBuilder,
11};
12use openmls_basic_credential::SignatureKeyPair;
13use openmls_traits::OpenMlsProvider;
14use parking_lot::RwLock;
15use ping_mls_store::{PersistentMlsProvider, StorageBackend};
16use std::collections::HashMap;
17use std::sync::Arc;
18use zeroize::Zeroizing;
19
20use crate::{
21 codec,
22 conversation::{Conversation, ConversationId, ConversationMeta, MemberInfo},
23 device::{
24 CatchupAppEventEntry, CatchupConversationEntry, CatchupSnapshot, DeviceId, DeviceInfo,
25 LinkingTicket, LocalDevice, CATCHUP_SNAPSHOT_SOFT_CAP, CATCHUP_SNAPSHOT_VERSION,
26 },
27 error::{Error, Result},
28 identity::{Identity, UserId},
29 message::{IncomingMessage, MessageEnvelope, MessageKind},
30 storage::Storage,
31 sync::SyncCursor,
32 transport::Transport,
33};
34
35const DEFAULT_CIPHERSUITE: Ciphersuite = Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519;
36
37/// Whether a transport send failure is a DEFINITE server rejection (the server
38/// returned an HTTP error response) rather than an ambiguous network failure
39/// where the server may actually have applied the message.
40///
41/// This decides whether a staged Commit can be safely rolled back: only a
42/// definite rejection guarantees the server did NOT apply it. The host transport
43/// embeds the HTTP status in the error string (e.g. "network: http 409"); 4xx +
44/// known server error codes are definite, while timeouts / "fetch failed" / 5xx
45/// are ambiguous (could be a masked success). When unsure we treat it as
46/// ambiguous (return false) — merging on a masked success is recoverable, but
47/// rolling back a Commit the server DID apply strands us a step behind forever.
48fn is_definite_rejection(err: &Error) -> bool {
49 let Error::Transport(s) = err else {
50 return false;
51 };
52 let s = s.to_ascii_lowercase();
53 s.contains("http 4")
54 || s.contains("epoch_advanced")
55 || s.contains("invalid_request")
56 || s.contains("not_found")
57 || s.contains("conflict")
58 || s.contains("forbidden")
59 || s.contains("unauthorized")
60}
61
62/// Per-chat result reported by [`MessagingClient::admit_device_to_chats`].
63#[derive(Debug, Clone)]
64pub struct AdmitChatOutcome {
65 pub conversation_id: ConversationId,
66 pub status: AdmitChatStatus,
67}
68
69#[derive(Debug, Clone)]
70pub enum AdmitChatStatus {
71 /// The new device is now an MLS leaf in this chat. Both the Commit
72 /// and the addressed Welcome have been sent.
73 Admitted,
74 /// We chose not to admit (e.g. the conversation is a DeviceGroup,
75 /// which was already handled at linking-ticket build time).
76 Skipped { reason: String },
77 /// MLS or transport rejected the admission. `error` is the underlying
78 /// message — typically a `transport error: ...` or an OpenMLS error.
79 Failed { error: String },
80}
81
82#[derive(Debug)]
83pub struct ClientConfig {
84 pub identity: Identity,
85 pub device_label: String,
86 pub storage: Arc<dyn Storage>,
87 pub transport: Arc<dyn Transport>,
88 /// Wall clock in ms. Pulled from the host so we can use a synthetic clock in tests.
89 pub now_ms: u64,
90 /// [CR-4] OpenMLS-provider backend. Defaults to in-memory; iOS NSE and web SW
91 /// cold-start paths MUST pass `StorageBackend::Sqlite { path, encryption_key }`
92 /// (native) or `StorageBackend::IndexedDb { db_name }` (WASM, when that lands).
93 /// See `docs/design/CR4_CR7_PERSISTENCE.md`.
94 pub storage_backend: StorageBackend,
95 /// Optional 32-byte Ed25519 secret key the SDK should use as the
96 /// device signing key. When set AND no `LocalDevice` is yet
97 /// persisted in `storage`, the SDK constructs its first
98 /// `LocalDevice` from this key instead of generating a fresh
99 /// random one — so `device_id = SHA-256(public_key_of(secret))`
100 /// is fully determined by what the host provided.
101 ///
102 /// Use case: align the SDK's `device_id` (which it stamps into
103 /// every envelope's `sender_device` field) with an externally-
104 /// computed device id — typically `SHA-256(device_signing_pubkey)`
105 /// in the host's auth layer, where the JWT carries that same
106 /// value as its `device_id` claim. Without this alignment, a
107 /// server that validates `envelope.sender_device ==
108 /// jwt.device_id` would reject every send.
109 ///
110 /// Ignored on re-init (when storage already has a persisted
111 /// `LocalDevice`) so the device identity remains stable across
112 /// restarts.
113 pub device_signing_secret_key: Option<[u8; 32]>,
114}
115
116impl ClientConfig {
117 /// Construct a config with `StorageBackend::Memory` — convenient for tests and
118 /// the existing v0.1 in-memory flow.
119 pub fn new_in_memory(
120 identity: Identity,
121 device_label: String,
122 storage: Arc<dyn Storage>,
123 transport: Arc<dyn Transport>,
124 now_ms: u64,
125 ) -> Self {
126 Self {
127 identity,
128 device_label,
129 storage,
130 transport,
131 now_ms,
132 storage_backend: StorageBackend::Memory,
133 device_signing_secret_key: None,
134 }
135 }
136}
137
138pub struct MessagingClient {
139 pub(crate) identity: Identity,
140 pub(crate) local_device: LocalDevice,
141 pub(crate) crypto: Arc<PersistentMlsProvider>,
142 pub(crate) signing: Arc<SignatureKeyPair>,
143 pub(crate) storage: Arc<dyn Storage>,
144 pub(crate) transport: Arc<dyn Transport>,
145 conversations: RwLock<HashMap<ConversationId, Conversation>>,
146 /// Conversations detected as STRANDED during catch-up: a full page of
147 /// events was fetched but nothing could be applied (every envelope failed
148 /// to decrypt / was wrong-epoch), meaning a Commit was missed and the group
149 /// can no longer advance from local state. The host polls
150 /// [`MessagingClient::stranded_conversations`] after a sync and recovers
151 /// each (re-Welcome / same-user state snapshot). Cleared automatically once
152 /// the conversation makes progress again.
153 stranded: RwLock<std::collections::HashSet<ConversationId>>,
154}
155
156impl std::fmt::Debug for MessagingClient {
157 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158 f.debug_struct("MessagingClient")
159 .field("user_id", &self.identity.user_id().as_hex())
160 .field("device_id", &self.local_device.device_id.as_hex())
161 .field("conversation_count", &self.conversations.read().len())
162 .finish()
163 }
164}
165
166impl MessagingClient {
167 /// Initialise. Creates a new local device if none is recorded in storage; otherwise rehydrates.
168 pub async fn init(cfg: ClientConfig) -> Result<Arc<Self>> {
169 // [CR-4] OpenMLS provider is now pluggable. For `StorageBackend::Memory` this
170 // behaves like the old `OpenMlsRustCrypto::default()`. For `Sqlite`, the
171 // working set is hydrated from the on-disk blob; subsequent `checkpoint` calls
172 // flush it back. iOS NSE / web SW cold-start lives here.
173 //
174 // Use `open_async` so the WASM `StorageBackend::IndexedDb` variant can read
175 // its snapshot blob through the host-supplied `AsyncBlobStore` before
176 // returning — without this, the provider's `MemoryStorage` would be empty
177 // and `MlsGroup::load` would silently return `None` for every group on
178 // cold restart, breaking chat persistence across reloads. Native targets
179 // (Memory + Sqlite) delegate to the sync path under the hood, so the
180 // `.await` is free there.
181 let crypto = PersistentMlsProvider::open_async(cfg.storage_backend.clone())
182 .await
183 .map_err(|e| Error::Storage(format!("provider open: {e}")))?;
184 let local_device = match cfg.storage.get("device", "local").await? {
185 Some(bytes) => decode_local_device(&bytes, cfg.identity.user_id().clone())?,
186 None => {
187 // First-init path. If the host supplied a signing secret
188 // (typically to align the device_id with their auth
189 // layer), use it; otherwise mint a fresh random key.
190 // Either way, the constructed `LocalDevice` is
191 // immediately persisted so future inits load from
192 // storage without consulting the override again.
193 let dev = match cfg.device_signing_secret_key.as_ref() {
194 Some(secret) => LocalDevice::from_signing_secret(
195 cfg.identity.user_id().clone(),
196 cfg.device_label,
197 cfg.now_ms,
198 secret,
199 ),
200 None => LocalDevice::generate(
201 cfg.identity.user_id().clone(),
202 cfg.device_label,
203 cfg.now_ms,
204 ),
205 };
206 let bytes = encode_local_device(&dev)?;
207 cfg.storage.put("device", "local", bytes).await?;
208 dev
209 }
210 };
211
212 // [CR-4] MLS signing keypair MUST be stable across cold restarts — otherwise the
213 // leaf-key stored on disk no longer matches the per-client key on re-init, and any
214 // send-after-restart silently misroutes. We derive deterministically from the
215 // already-persistent `LocalDevice::signing` (Ed25519, 32 raw bytes), and the
216 // ciphersuite's signature scheme is Ed25519 too — so the device signing key and the
217 // MLS leaf signing key are the same bytes. The MLS storage provider also receives
218 // a copy via `store()` so OpenMLS-internal lookups (process_message, etc.) succeed.
219 let signing = {
220 let sk_bytes = local_device.signing.to_bytes().to_vec();
221 let pk_bytes = local_device.signing.verifying_key().to_bytes().to_vec();
222 let kp = SignatureKeyPair::from_raw(
223 DEFAULT_CIPHERSUITE.signature_algorithm(),
224 sk_bytes,
225 pk_bytes,
226 );
227 kp.store(crypto.storage()).map_err(Error::mls)?;
228 Arc::new(kp)
229 };
230
231 let client = Arc::new(Self {
232 identity: cfg.identity,
233 local_device,
234 crypto,
235 signing,
236 storage: cfg.storage,
237 transport: cfg.transport,
238 conversations: RwLock::new(HashMap::new()),
239 stranded: RwLock::new(std::collections::HashSet::new()),
240 });
241
242 client.rehydrate_conversations(cfg.now_ms).await?;
243
244 // [CR-10] Ensure the DeviceGroup exists at init, not lazily inside
245 // build_linking_ticket. Single-device users need somewhere to write
246 // personal events (drafts, read pointers, notes, vault wrapper)
247 // even before they pair a second device. Lazy creation in
248 // build_linking_ticket left them with no DG → no place for
249 // personal state to land.
250 //
251 // Idempotent — re-init after a cold restart finds the DG via
252 // rehydrate_conversations and this becomes a no-op.
253 client.ensure_device_group(cfg.now_ms).await?;
254
255 Ok(client)
256 }
257
258 /// Open a client that can DECRYPT but never persists — the notification
259 /// preview path.
260 ///
261 /// The motivating case is the iOS Notification Service Extension: a second
262 /// process, woken per push, that has to turn one inbound envelope into a
263 /// sender and a message preview for the lock screen, while the main app
264 /// keeps running and keeps writing.
265 ///
266 /// [`init`](Self::init) cannot be used for that, and not merely as a matter
267 /// of taste. Persistence here checkpoints the ENTIRE MLS working set as one
268 /// blob (`ping_mls_store`), so two writers do not merge — whichever flushes
269 /// second replaces everything the other did, discarding epochs and ratchet
270 /// state wholesale. `init` also *creates* state (minting a `LocalDevice`,
271 /// ensuring the DeviceGroup exists), which is exactly the behaviour a
272 /// read-only opener must not have.
273 ///
274 /// So this constructor is the same hydration with every writing path
275 /// removed:
276 /// * the storage backend MUST be read-only, checked here rather than
277 /// trusted, so a caller cannot reach this code with a writable store;
278 /// * a missing `LocalDevice` is an ERROR, never a fresh identity — this
279 /// process joins an existing installation or it does nothing;
280 /// * the DeviceGroup is not created. A reader that finds none simply has
281 /// no DeviceGroup, which costs it nothing (personal-state sync is the
282 /// app's job, not a notification's).
283 ///
284 /// The MLS signing keypair is still `store`d, but only into the in-memory
285 /// `MemoryStorage` the provider hands OpenMLS; with a read-only backend
286 /// that never reaches disk.
287 ///
288 /// Pair with [`preview_envelope`](Self::preview_envelope), and give it a
289 /// read-only host `Storage` too — this guards the MLS blob, not the host's
290 /// own key-value writes.
291 pub async fn open_read_only(cfg: ClientConfig) -> Result<Arc<Self>> {
292 let crypto = PersistentMlsProvider::open_async(cfg.storage_backend.clone())
293 .await
294 .map_err(|e| Error::Storage(format!("provider open: {e}")))?;
295 if !crypto.is_read_only() {
296 // Names BOTH variants: WASM (the Service Worker) uses
297 // `AsyncBlobReadOnly`, and a message that only mentioned SQLite
298 // sent a web caller looking for a backend they cannot use.
299 return Err(Error::Invalid(
300 "open_read_only requires a read-only backend \
301 (StorageBackend::SqliteReadOnly or ::AsyncBlobReadOnly)"
302 .into(),
303 ));
304 }
305
306 let local_device = match cfg.storage.get("device", "local").await? {
307 Some(bytes) => decode_local_device(&bytes, cfg.identity.user_id().clone())?,
308 None => {
309 return Err(Error::Storage(
310 "open_read_only: no LocalDevice in storage; \
311 a read-only client never mints one"
312 .into(),
313 ))
314 }
315 };
316
317 let signing = {
318 let sk_bytes = local_device.signing.to_bytes().to_vec();
319 let pk_bytes = local_device.signing.verifying_key().to_bytes().to_vec();
320 let kp = SignatureKeyPair::from_raw(
321 DEFAULT_CIPHERSUITE.signature_algorithm(),
322 sk_bytes,
323 pk_bytes,
324 );
325 kp.store(crypto.storage()).map_err(Error::mls)?;
326 Arc::new(kp)
327 };
328
329 let client = Arc::new(Self {
330 identity: cfg.identity,
331 local_device,
332 crypto,
333 signing,
334 storage: cfg.storage,
335 transport: cfg.transport,
336 conversations: RwLock::new(HashMap::new()),
337 stranded: RwLock::new(std::collections::HashSet::new()),
338 });
339
340 client.rehydrate_conversations(cfg.now_ms).await?;
341 Ok(client)
342 }
343
344 /// Decrypt ONE application envelope for display, changing nothing durable.
345 ///
346 /// The counterpart to [`open_read_only`](Self::open_read_only): applies the
347 /// envelope to this process's private copy of the MLS state and returns the
348 /// plaintext, with no flush of either the MLS blob or the host's key-value
349 /// store. The ratchet advance is deliberately thrown away — the owning
350 /// process still holds the un-advanced state and decrypts the same message
351 /// again, independently, when it next runs. Two readers of one snapshot;
352 /// neither can desynchronise the other.
353 ///
354 /// Handshake traffic returns `Ok(None)` without being processed. A Commit
355 /// moves the group to a new epoch and a Welcome joins one, and both are
356 /// state changes whose whole point is to be durable — applying either in a
357 /// process that discards its writes would burn the message for a preview
358 /// nobody could act on, and a self-removing Commit would tear down group
359 /// state the owner still needs. There is nothing to show for them anyway:
360 /// they carry no user-visible text.
361 ///
362 /// `Ok(None)` also covers "already applied" (the hydrated cursor rejects a
363 /// replay) and "not a conversation this device knows".
364 pub fn preview_envelope(
365 &self,
366 env: &MessageEnvelope,
367 now_ms: u64,
368 ) -> Result<Option<IncomingMessage>> {
369 debug_assert!(
370 self.crypto.is_read_only(),
371 "preview_envelope is only sound on a read-only provider"
372 );
373 if env.kind != MessageKind::Application {
374 return Ok(None);
375 }
376 if !self.conversations.read().contains_key(&env.conversation_id) {
377 return Ok(None);
378 }
379 let (out, _removed) = self.apply_envelope_in_memory(env, now_ms)?;
380 Ok(out)
381 }
382
383 /// [CR-10] Idempotently ensures this user's DeviceGroup exists in
384 /// `self.conversations`. Called from `init` (so single-device users
385 /// have a DG immediately) and from `build_linking_ticket` (the legacy
386 /// lazy path; still safe to call when the DG already exists, since
387 /// rehydrate_conversations would have re-attached it before init
388 /// returned).
389 ///
390 /// The DeviceGroup is a one-leaf MLS group at creation time —
391 /// `add_members` (called by `build_linking_ticket` when a second
392 /// device pairs in) is what grows it. We persist the snapshot so a
393 /// cold restart picks it up before this function runs again.
394 pub(crate) async fn ensure_device_group(self: &Arc<Self>, now_ms: u64) -> Result<()> {
395 let dg_id = device_group_id_for(self.identity.user_id());
396 if self.conversations.read().contains_key(&dg_id) {
397 return Ok(());
398 }
399 let mut new_dg = Conversation::create(
400 dg_id,
401 Some("device-group".into()),
402 self.local_device.device_id.clone(),
403 self.identity.user_id(),
404 self.crypto.clone(),
405 self.signing.clone(),
406 self.storage.clone(),
407 now_ms,
408 )?;
409 new_dg.meta.is_device_group = true;
410 new_dg.snapshot_to_storage().await?;
411 self.conversations.write().insert(dg_id, new_dg);
412 Ok(())
413 }
414
415 pub fn user_id(&self) -> UserId {
416 self.identity.user_id().clone()
417 }
418 /// Export this client's account identity (the Ed25519 seed, CBOR-wrapped —
419 /// same format `Identity::import` / `MessagingClient::init(identity_export)`
420 /// accept). SECRET. Hosts use this to TRANSFER the account identity to a
421 /// newly-linked device over the sealed linking channel, so every linked
422 /// device shares ONE `user_id` and `IncomingMessage.sender_user_id` equals
423 /// the local `user_id()` for any of the account's own devices — the basis
424 /// for cross-device self-attribution. Never log or persist in cleartext.
425 pub fn export_identity(&self) -> Zeroizing<Vec<u8>> {
426 self.identity.export()
427 }
428 pub fn device_id(&self) -> DeviceId {
429 self.local_device.device_id.clone()
430 }
431 pub fn device_info(&self, now_ms: u64) -> DeviceInfo {
432 self.local_device.info(now_ms)
433 }
434
435 /// Generate a fresh KeyPackage to publish to the directory. Hosts call this when registering
436 /// a device or topping up the directory.
437 ///
438 /// `build()` writes the private init + encryption keys into the storage
439 /// provider's working set, but ON ITS OWN that write is NOT durable: on the
440 /// WASM/AsyncBlob backend the working set only reaches IndexedDB at the next
441 /// `checkpoint_async`, so a page reload before the next state-changing op
442 /// loses the private keys while the PUBLIC KeyPackage has already been
443 /// published. Any Welcome later bound to that KeyPackage then fails with
444 /// "No matching key package was found in the key store" (breaking calls and
445 /// every invite to this device). So we checkpoint HERE, before returning the
446 /// bytes the host will publish — the published KeyPackage is durable the
447 /// instant it leaves this function. Hence `async`.
448 pub async fn fresh_key_package(&self) -> Result<Vec<u8>> {
449 self.build_key_package(false).await
450 }
451
452 /// Generate a fresh LAST-RESORT KeyPackage.
453 ///
454 /// A normal KeyPackage is single-use: once a Welcome consumes it, the
455 /// private init key is deleted and the directory entry is burned. When a
456 /// device's published pool runs dry, every invite to that device hard-fails
457 /// ("user unavailable") until it comes online and tops up — the classic
458 /// "I added them but they never got it" complaint.
459 ///
460 /// A last-resort KeyPackage (RFC 9420 §10) carries the `LastResort`
461 /// extension, signalling the server it may serve this KeyPackage MORE THAN
462 /// ONCE when no single-use KeyPackages remain. The host publishes exactly
463 /// one per device; the server keeps it as the always-available fallback so
464 /// an invite never fails purely because the pool emptied. Forward secrecy
465 /// for the joining epoch is slightly weaker (the init key is reused until
466 /// replenishment), which is the accepted RFC trade-off for availability.
467 pub async fn fresh_last_resort_key_package(&self) -> Result<Vec<u8>> {
468 self.build_key_package(true).await
469 }
470
471 async fn build_key_package(&self, last_resort: bool) -> Result<Vec<u8>> {
472 let credential_with_key = CredentialWithKey {
473 credential: BasicCredential::new(self.identity.user_id().0.clone()).into(),
474 signature_key: self.signing.public().to_vec().into(),
475 };
476 let mut builder = KeyPackageBuilder::new()
477 // Advertise the group-name extension capability so a later
478 // `set_name` (rename / avatar change via GroupContextExtensions)
479 // passes openmls' per-member capability check on every group this
480 // device joins. For a last-resort KeyPackage the leaf must also
481 // advertise the LastResort extension it carries, or openmls
482 // validation rejects the KeyPackage at add-member time. See
483 // `conversation::ping_leaf_capabilities_for`.
484 .leaf_node_capabilities(crate::conversation::ping_leaf_capabilities_for(last_resort));
485 if last_resort {
486 builder = builder.mark_as_last_resort();
487 }
488 let bundle = builder
489 .build(
490 DEFAULT_CIPHERSUITE,
491 self.crypto.as_ref(),
492 self.signing.as_ref(),
493 credential_with_key,
494 )
495 .map_err(Error::mls)?;
496 // Durably persist the freshly-generated private keys BEFORE the public
497 // KeyPackage is handed to the host to publish (see doc comment).
498 self.crypto
499 .checkpoint_async()
500 .await
501 .map_err(|e| Error::Storage(format!("key package checkpoint: {e}")))?;
502 // KeyPackages are serialized as MlsMessage(KeyPackage) per the MLS framing spec.
503 let msg: MlsMessageOut = bundle.key_package().clone().into();
504 msg.tls_serialize_detached().map_err(Error::mls)
505 }
506
507 /// Create a new conversation owned by this client (and seeded with a single member: this device).
508 pub async fn create_conversation(
509 self: &Arc<Self>,
510 name: Option<String>,
511 now_ms: u64,
512 ) -> Result<ConversationId> {
513 self.create_conversation_with_id(ConversationId::new(), name, now_ms)
514 .await
515 }
516
517 /// Create an ephemeral per-call MLS group. Identical to
518 /// [`create_conversation`] except the id carries the `0xFF 0xCC` call-group
519 /// sentinel (see [`ConversationId::new_call_group`]), so every device — the
520 /// callee joining via a name-stripped Welcome, a freshly-linked sibling —
521 /// recognises it as a call group by its id alone and keeps it out of the chat
522 /// list, with no dependence on the (creator-local) `call:` name or a
523 /// per-device registry. Hosts MUST mint call groups via this method rather
524 /// than `create_conversation(name: "call:…")` to get the structural guarantee.
525 pub async fn create_call_conversation(
526 self: &Arc<Self>,
527 name: Option<String>,
528 now_ms: u64,
529 ) -> Result<ConversationId> {
530 self.create_conversation_with_id(ConversationId::new_call_group(), name, now_ms)
531 .await
532 }
533
534 async fn create_conversation_with_id(
535 self: &Arc<Self>,
536 id: ConversationId,
537 name: Option<String>,
538 now_ms: u64,
539 ) -> Result<ConversationId> {
540 let convo = Conversation::create(
541 id,
542 name,
543 self.local_device.device_id.clone(),
544 self.identity.user_id(),
545 self.crypto.clone(),
546 self.signing.clone(),
547 self.storage.clone(),
548 now_ms,
549 )?;
550 convo.snapshot_to_storage().await?;
551 self.conversations.write().insert(id, convo);
552 Ok(id)
553 }
554
555 /// Join via a Welcome bundled in a [`MessageEnvelope`] of kind `Welcome`.
556 pub async fn join_conversation(
557 self: &Arc<Self>,
558 welcome_envelope: &MessageEnvelope,
559 now_ms: u64,
560 ) -> Result<ConversationId> {
561 if welcome_envelope.kind != MessageKind::Welcome {
562 return Err(Error::Invalid("expected Welcome envelope".into()));
563 }
564 let convo = Conversation::join(
565 &welcome_envelope.payload,
566 self.local_device.device_id.clone(),
567 self.crypto.clone(),
568 self.signing.clone(),
569 self.storage.clone(),
570 now_ms,
571 )?;
572 let id = convo.id();
573 convo.snapshot_to_storage().await?;
574 self.conversations.write().insert(id, convo);
575 // Joining (re-Welcome) recovers a previously-stranded conversation.
576 self.stranded.write().remove(&id);
577 Ok(id)
578 }
579
580 /// Conversations detected as STRANDED during catch-up — a Commit was missed
581 /// and the group can no longer advance from local state. The host should
582 /// recover each (re-Welcome from a peer, or a same-user state-snapshot
583 /// import) so messages start delivering again. The set self-clears as a
584 /// conversation makes progress or is re-joined.
585 pub fn stranded_conversations(&self) -> Vec<ConversationId> {
586 let mut ids: Vec<ConversationId> = self.stranded.read().iter().copied().collect();
587 ids.sort_by_key(|a| a.0);
588 ids
589 }
590
591 pub fn list_conversations(&self) -> Vec<ConversationMeta> {
592 self.conversations
593 .read()
594 .values()
595 .map(|c| c.meta.clone())
596 .collect()
597 }
598
599 /// Member roster for a conversation, recovered locally from the MLS
600 /// group's leaf credentials. Empty if the conversation is unknown to
601 /// this client. Lets any device (including one that just joined via a
602 /// linking Welcome) resolve a 1:1 peer's `UserId` without the
603 /// out-of-band `ping.profile` re-send.
604 pub fn members(&self, conv_id: ConversationId) -> Vec<MemberInfo> {
605 self.conversations
606 .read()
607 .get(&conv_id)
608 .map(|c| c.members())
609 .unwrap_or_default()
610 }
611
612 /// Send an application message. Returns once the envelope has been handed to the transport.
613 pub async fn send(
614 &self,
615 conv_id: ConversationId,
616 plaintext: Vec<u8>,
617 now_ms: u64,
618 ) -> Result<MessageEnvelope> {
619 // FAST-FAIL on a stranded conversation. A missed Commit forked our epoch,
620 // so anything we encrypt now is at a stale epoch: the server either 409s
621 // it (epoch occupied) or accepts a frame the peers can never decrypt — a
622 // durable-but-undelivered message. Surface `EpochStranded` immediately so
623 // the host renders "needs repair" (and recovers via
624 // `stranded_conversations()` → re-Welcome / snapshot import, which clears
625 // the mark) instead of silently 409-looping every send. Without this the
626 // SDK knew the conversation was forked but sent anyway.
627 if self.stranded.read().contains(&conv_id) {
628 return Err(Error::EpochStranded(conv_id.as_hex()));
629 }
630 let envelope = {
631 let mut guard = self.conversations.write();
632 let convo = guard
633 .get_mut(&conv_id)
634 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
635 convo.send_application(&plaintext, now_ms)?
636 };
637 self.transport.send(envelope.clone()).await?;
638 // The OpenMLS sender ratchet advances on every Application message — `seq` + `hlc`
639 // are bumped on the conversation, and the underlying group keystore stores new
640 // generation keys. Without a checkpoint here, a reload rolls back to the pre-send
641 // state and the next send re-uses an already-consumed generation that receivers
642 // silently drop. Mirrors the snapshot calls after every Commit/Welcome op.
643 //
644 // Capture the snapshot inputs UNDER the read guard, then DROP the
645 // guard (end of the `let` statement) before the async flush — never
646 // hold a `parking_lot` guard across `.await` (see
647 // `Conversation::snapshot_inputs`).
648 let snap = self
649 .conversations
650 .read()
651 .get(&conv_id)
652 .map(|c| c.snapshot_inputs())
653 .transpose()?;
654 if let Some(snap) = snap {
655 snap.flush().await?;
656 }
657 Ok(envelope)
658 }
659
660 /// Add members. The Commit goes on the wire; the Welcome should be delivered to the new
661 /// devices' inboxes (the host transport implements that — typically as a separate addressed
662 /// envelope).
663 ///
664 /// [CR-2] Each entry is `(DeviceId, KeyPackage_bytes)`. The host typically gets the
665 /// device_id from the directory at the same time it gets the KeyPackage; we use it to
666 /// record a per-conversation `device_id → leaf_index` map so [`Self::revoke_device`]
667 /// can later locate the leaf without a fresh directory lookup. The SDK does not
668 /// cryptographically verify the host's device-id claim — that's a directory policy
669 /// concern.
670 //
671 // The `conversations` lock is taken only for the SYNCHRONOUS MLS work
672 // (the add commit) and the synchronous snapshot capture, then dropped
673 // BEFORE every `.await`. We must never hold a `parking_lot` guard
674 // across an await — see `Conversation::snapshot_inputs` for why (the
675 // single-threaded wasm worker would panic in `parking_lot`'s parker
676 // stub). `parking_lot/send_guard` is still set so any guard that DOES
677 // briefly cross a yield-free boundary stays `Send`.
678 pub async fn add_members(
679 &self,
680 conv_id: ConversationId,
681 entries: Vec<(DeviceId, Vec<u8>)>,
682 now_ms: u64,
683 ) -> Result<()> {
684 // Phase 1 — stage the Commit WITHOUT merging (local epoch unchanged).
685 let staged = {
686 let mut guard = self.conversations.write();
687 let convo = guard
688 .get_mut(&conv_id)
689 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
690 convo.stage_add_members(entries, now_ms)?
691 };
692
693 // Phase 2 — send the Commit FIRST, then merge only if the server accepts
694 // it (send-then-merge). A Commit the server REJECTS is rolled back, so the
695 // local epoch can never run ahead of the server — the desync that
696 // permanently bricks a group (every later Commit 409s; peers can't decrypt
697 // our epoch). A network failure with NO response is ambiguous (the server
698 // may have applied it), so there we merge to match a possible masked
699 // success rather than strand ourselves a step behind.
700 if let Err(send_err) = self.transport.send(staged.commit.clone()).await {
701 let merged = {
702 let mut guard = self.conversations.write();
703 match guard.get_mut(&conv_id) {
704 Some(convo) if is_definite_rejection(&send_err) => {
705 let _ = convo.abort_staged();
706 false
707 }
708 Some(convo) => {
709 convo.confirm_staged(&staged, now_ms)?;
710 true
711 }
712 None => false,
713 }
714 };
715 if merged {
716 self.flush_conversation(&conv_id).await?;
717 }
718 return Err(send_err);
719 }
720
721 // Phase 3 — Commit accepted: merge locally + persist (so the advanced
722 // epoch survives a crash even if the Welcome below fails).
723 {
724 let mut guard = self.conversations.write();
725 let convo = guard
726 .get_mut(&conv_id)
727 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
728 convo.confirm_staged(&staged, now_ms)?;
729 }
730 self.flush_conversation(&conv_id).await?;
731
732 // Phase 4 — deliver the Welcome to the new members. Best-effort: they are
733 // in the group server-side now; a failed Welcome is recoverable
734 // (re-invite) and must NOT roll back the merged Commit.
735 if let Some(welcome) = staged.welcome {
736 self.transport.send(welcome).await?;
737 }
738 Ok(())
739 }
740
741 /// Change a conversation's `name` (carried in the GroupContext) and broadcast
742 /// the change to every member as an MLS GroupContextExtensions Commit. Unlike
743 /// a hydration broadcast, the new name rides MLS group STATE, so every member
744 /// — and every future joiner via the GroupInfo — converges on it. Hosts use
745 /// this to make a rename or an embedded avatar-media-id change bulletproof
746 /// (the `name` carries the `ping:meta:v1:` blob).
747 ///
748 /// No Welcome (membership is unchanged). Uses the same send-then-merge
749 /// rollback discipline as [`Self::add_members`] so a server-rejected Commit
750 /// never desyncs the local epoch. All members must have re-linked since the
751 /// group-name capability shipped (see `conversation::ping_leaf_capabilities`),
752 /// else openmls rejects the Commit.
753 pub async fn set_conversation_name(
754 &self,
755 conv_id: ConversationId,
756 name: Option<String>,
757 now_ms: u64,
758 ) -> Result<()> {
759 // Phase 1 — stage the Commit WITHOUT merging (local epoch unchanged).
760 let staged = {
761 let mut guard = self.conversations.write();
762 let convo = guard
763 .get_mut(&conv_id)
764 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
765 convo.stage_set_name(name, now_ms)?
766 };
767
768 // Phase 2 — send-then-merge (see `add_members` for the rollback rationale).
769 if let Err(send_err) = self.transport.send(staged.commit.clone()).await {
770 let merged = {
771 let mut guard = self.conversations.write();
772 match guard.get_mut(&conv_id) {
773 Some(convo) if is_definite_rejection(&send_err) => {
774 let _ = convo.abort_staged();
775 false
776 }
777 Some(convo) => {
778 convo.confirm_staged(&staged, now_ms)?;
779 true
780 }
781 None => false,
782 }
783 };
784 if merged {
785 self.flush_conversation(&conv_id).await?;
786 }
787 return Err(send_err);
788 }
789
790 // Phase 3 — Commit accepted: merge locally + persist.
791 {
792 let mut guard = self.conversations.write();
793 let convo = guard
794 .get_mut(&conv_id)
795 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
796 convo.confirm_staged(&staged, now_ms)?;
797 }
798 self.flush_conversation(&conv_id).await?;
799 Ok(())
800 }
801
802 /// Snapshot + flush a conversation's persistable state. Captures the snapshot
803 /// synchronously under the read guard, drops the guard, then awaits the flush
804 /// (never hold a `parking_lot` guard across an await — wasm parker panics).
805 async fn flush_conversation(&self, conv_id: &ConversationId) -> Result<()> {
806 let snap = self
807 .conversations
808 .read()
809 .get(conv_id)
810 .map(|c| c.snapshot_inputs())
811 .transpose()?;
812 if let Some(snap) = snap {
813 snap.flush().await?;
814 }
815 Ok(())
816 }
817
818 /// Admits `new_device_id` to every conversation in `kps_per_chat` via
819 /// the standard MLS `add_members` flow — one Commit + one Welcome per
820 /// chat. This is the SDK-side replacement for the host's previous
821 /// per-chat reconciler loop after device linking; centralising it
822 /// here means iOS/Android/web hosts all share the orchestration and
823 /// the transport's Welcome-recipient priming is automatic.
824 ///
825 /// Inputs:
826 /// - `new_device_id`: the device being admitted (matches the
827 /// `device_binding_sig` recipient in the linking ticket).
828 /// - `kps_per_chat`: one freshly-claimed KeyPackage per chat. The
829 /// host claims these via the auth-layer's per-account KP pool
830 /// (`GET /v1/devices/{accountId}`) AFTER the new device's
831 /// bootstrap has uploaded its KP batch.
832 /// - `now_ms`: wall-clock used to stamp HLCs on the emitted
833 /// envelopes.
834 ///
835 /// Per-chat failures (unknown conversation, MLS error, transport
836 /// error, etc.) are CAPTURED in the returned vec rather than
837 /// short-circuiting the whole call — losing one chat shouldn't
838 /// strand the new device on every other chat. The caller decides
839 /// whether to retry the failed entries (e.g. with a fresh KP).
840 pub async fn admit_device_to_chats(
841 &self,
842 new_device_id: DeviceId,
843 kps_per_chat: Vec<(ConversationId, Vec<u8>)>,
844 now_ms: u64,
845 ) -> Result<Vec<AdmitChatOutcome>> {
846 let mut outcomes = Vec::with_capacity(kps_per_chat.len());
847 for (conv_id, kp_bytes) in kps_per_chat {
848 // Belt-and-braces: skip the DeviceGroup. The DG was already
849 // welcomed via the linking ticket — re-adding the new
850 // device there would produce a duplicate-add Commit that
851 // BE de-dups, but the noise is avoidable.
852 let is_dg = self
853 .conversations
854 .read()
855 .get(&conv_id)
856 .map(|c| c.meta().is_device_group)
857 .unwrap_or(false);
858 if is_dg {
859 outcomes.push(AdmitChatOutcome {
860 conversation_id: conv_id,
861 status: AdmitChatStatus::Skipped {
862 reason: "device_group".to_string(),
863 },
864 });
865 continue;
866 }
867
868 // Prime the host transport with the welcome recipient BEFORE
869 // we mutate MLS state. If priming fails (non-web hosts use
870 // the default no-op), continue — the host's transport will
871 // either route some other way or surface a 4xx on the
872 // welcome send and we'll catch it below.
873 let _ = self
874 .transport
875 .set_next_welcome_recipients(conv_id, vec![new_device_id.clone()])
876 .await;
877
878 let entry = (new_device_id.clone(), kp_bytes);
879 let outcome_result = {
880 let mut guard = self.conversations.write();
881 match guard.get_mut(&conv_id) {
882 Some(convo) => convo.add_members(vec![entry], now_ms),
883 None => Err(Error::UnknownConversation(conv_id.as_hex())),
884 }
885 };
886
887 let outcome = match outcome_result {
888 Ok(o) => o,
889 Err(e) => {
890 outcomes.push(AdmitChatOutcome {
891 conversation_id: conv_id,
892 status: AdmitChatStatus::Failed {
893 error: e.to_string(),
894 },
895 });
896 continue;
897 }
898 };
899
900 if let Err(e) = self.transport.send(outcome.commit).await {
901 outcomes.push(AdmitChatOutcome {
902 conversation_id: conv_id,
903 status: AdmitChatStatus::Failed {
904 error: format!("commit send: {e}"),
905 },
906 });
907 continue;
908 }
909 if let Err(e) = self.transport.send(outcome.welcome).await {
910 outcomes.push(AdmitChatOutcome {
911 conversation_id: conv_id,
912 status: AdmitChatStatus::Failed {
913 error: format!("welcome send: {e}"),
914 },
915 });
916 continue;
917 }
918
919 // Capture the snapshot under the read guard, drop it, then
920 // flush async (never hold the lock across `.await`).
921 let snap_result = self
922 .conversations
923 .read()
924 .get(&conv_id)
925 .map(|c| c.snapshot_inputs())
926 .transpose();
927 let flush_result = match snap_result {
928 Ok(Some(snap)) => snap.flush().await,
929 Ok(None) => Ok(()),
930 Err(e) => Err(e),
931 };
932 if let Err(e) = flush_result {
933 // Snapshot failure is non-fatal for the join — the MLS adds
934 // already shipped — but record it so the host can decide
935 // whether to retry. The next successful send/process will
936 // re-snapshot anyway.
937 outcomes.push(AdmitChatOutcome {
938 conversation_id: conv_id,
939 status: AdmitChatStatus::Failed {
940 error: format!("snapshot: {e}"),
941 },
942 });
943 continue;
944 }
945
946 outcomes.push(AdmitChatOutcome {
947 conversation_id: conv_id,
948 status: AdmitChatStatus::Admitted,
949 });
950 }
951 Ok(outcomes)
952 }
953
954 pub async fn remove_members(
955 &self,
956 conv_id: ConversationId,
957 leaf_indexes: Vec<u32>,
958 now_ms: u64,
959 ) -> Result<()> {
960 // Send-then-merge — see `add_members` for the full rationale.
961 let staged = {
962 let mut guard = self.conversations.write();
963 let convo = guard
964 .get_mut(&conv_id)
965 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
966 convo.stage_remove_members(leaf_indexes, now_ms)?
967 };
968
969 if let Err(send_err) = self.transport.send(staged.commit.clone()).await {
970 let merged = {
971 let mut guard = self.conversations.write();
972 match guard.get_mut(&conv_id) {
973 Some(convo) if is_definite_rejection(&send_err) => {
974 let _ = convo.abort_staged();
975 false
976 }
977 Some(convo) => {
978 convo.confirm_staged(&staged, now_ms)?;
979 true
980 }
981 None => false,
982 }
983 };
984 if merged {
985 self.flush_conversation(&conv_id).await?;
986 }
987 return Err(send_err);
988 }
989
990 {
991 let mut guard = self.conversations.write();
992 let convo = guard
993 .get_mut(&conv_id)
994 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
995 convo.confirm_staged(&staged, now_ms)?;
996 }
997 self.flush_conversation(&conv_id).await?;
998 Ok(())
999 }
1000
1001 /// Re-admit a device, first evicting any existing leaf that duplicates the
1002 /// new KeyPackage's signature key (the phrase-restore case — see
1003 /// [`Conversation::duplicate_signature_key_leaves`] and
1004 /// `docs/specs/re-admit-device.md`). Equivalent to [`Self::add_members`] when
1005 /// there is no duplicate, so it is a strict superset — safe to prefer on the
1006 /// recovery re-admission path.
1007 ///
1008 /// It composes the two conformance-tested membership primitives —
1009 /// `remove_members` (evict the dead duplicate leaf, freeing its signing key)
1010 /// then `add_members` (admit the fresh device + ship its Welcome) — each with
1011 /// its own send-then-merge rollback, so a server-rejected Commit never leaves
1012 /// the local epoch ahead of the server. Two Commits on the rare recovery path;
1013 /// folding them into a single combined Remove+Add commit is a possible future
1014 /// optimization (kept out of scope to reuse already-vetted primitives). If the
1015 /// remove succeeds but the add fails, the device is simply un-admitted (no
1016 /// worse than before) and the caller retries.
1017 pub async fn re_admit_device(
1018 &self,
1019 conv_id: ConversationId,
1020 entry: (DeviceId, Vec<u8>),
1021 now_ms: u64,
1022 ) -> Result<()> {
1023 let dup_leaves = {
1024 let guard = self.conversations.read();
1025 let convo = guard
1026 .get(&conv_id)
1027 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1028 convo.duplicate_signature_key_leaves(&entry.1)?
1029 };
1030 if !dup_leaves.is_empty() {
1031 self.remove_members(conv_id, dup_leaves, now_ms).await?;
1032 }
1033 self.add_members(conv_id, vec![entry], now_ms).await?;
1034 Ok(())
1035 }
1036
1037 /// Leave a conversation. Broadcasts a self-Remove PROPOSAL (MLS doesn't
1038 /// allow committing your own removal — a remaining member commits it via
1039 /// [`Self::commit_pending_proposals`]). After this returns, the host should
1040 /// delete the conversation locally; the leaver remains a cryptographic
1041 /// member only until a peer commits the proposal, at which point the server
1042 /// stops delivering to this device.
1043 pub async fn leave_conversation(&self, conv_id: ConversationId, now_ms: u64) -> Result<()> {
1044 let proposal = {
1045 let mut guard = self.conversations.write();
1046 let convo = guard
1047 .get_mut(&conv_id)
1048 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1049 convo.leave_group(now_ms)?
1050 };
1051 // A proposal doesn't change the epoch, so there's nothing to roll back
1052 // on a send failure — surface the error and let the host retry.
1053 self.transport.send(proposal).await?;
1054 self.flush_conversation(&conv_id).await?;
1055 Ok(())
1056 }
1057
1058 /// Drop a conversation's ENTIRE local state with **no** network side effect.
1059 ///
1060 /// Unlike [`Self::leave_conversation`] (which broadcasts a self-Remove
1061 /// proposal so a remaining member evicts you), this is a purely LOCAL
1062 /// teardown for a group the server does not back — e.g. an invite that minted
1063 /// the MLS group locally but never completed server-side, so the backend
1064 /// 404s `fetchSince` / 403s the member roster for it. The host detects that
1065 /// authoritative "not a member" verdict and calls this so the dead group
1066 /// stops rehydrating on every restart (and re-materialising as a ghost
1067 /// conversation). No envelope is sent — there is no live group to send to.
1068 ///
1069 /// Deletes the OpenMLS group state AND the host-side snapshot rows
1070 /// (`groups/{id}/…`, `cursors/{id}`, `device_leaves/{id}`) that
1071 /// [`Self::rehydrate_conversations`] would otherwise reload, then drops the
1072 /// in-memory handle + `stranded` marker. Idempotent: an unknown id still
1073 /// best-effort purges any orphan storage rows, and calling it twice is safe.
1074 pub async fn drop_conversation_local(&self, conv_id: ConversationId) -> Result<()> {
1075 // Take the handle OUT of the map first, then delete its OpenMLS state via
1076 // the owned value — mirrors the self-removal teardown in
1077 // `process_envelope` and keeps no lock across the awaits below.
1078 let existing = self.conversations.write().remove(&conv_id);
1079 if let Some(mut convo) = existing {
1080 if let Err(e) = convo.delete_group_state() {
1081 // Best-effort: dropping the in-memory handle + purging the
1082 // snapshot rows below already makes the group non-rehydratable.
1083 tracing::warn!(error = %e, "drop_conversation_local: delete group state failed");
1084 }
1085 }
1086 self.stranded.write().remove(&conv_id);
1087
1088 // Purge the host-side snapshot rows so a restart's
1089 // `rehydrate_conversations` (which walks `groups/{id}/meta`) can't bring
1090 // the group back. Best-effort — deleting an absent key is a no-op on
1091 // every backend. Sweep the whole `groups/{id}/` prefix so any future
1092 // sub-key is covered, then the known `cursors` + `device_leaves` rows.
1093 let hex = conv_id.as_hex();
1094 if let Ok(keys) = self.storage.list_keys("groups", &format!("{hex}/")).await {
1095 for k in keys {
1096 let _ = self.storage.delete("groups", &k).await;
1097 }
1098 }
1099 let _ = self.storage.delete("cursors", &hex).await;
1100 let _ = self.storage.delete("device_leaves", &hex).await;
1101 Ok(())
1102 }
1103
1104 /// Conversations with buffered pending proposals (e.g. a peer's leave
1105 /// proposal awaiting a Commit). The host polls this after a sync and, if it
1106 /// is the designated committer, calls [`Self::commit_pending_proposals`] to
1107 /// evict the leaver. Sorted for determinism.
1108 pub fn conversations_with_pending_proposals(&self) -> Vec<ConversationId> {
1109 let guard = self.conversations.read();
1110 let mut ids: Vec<ConversationId> = guard
1111 .iter()
1112 .filter(|(_, c)| c.has_pending_proposals())
1113 .map(|(id, _)| *id)
1114 .collect();
1115 ids.sort_by_key(|a| a.0);
1116 ids
1117 }
1118
1119 /// Commit all buffered pending proposals for a conversation (evicts a peer
1120 /// who left). No-op (Ok) when nothing is pending. Send-then-merge with
1121 /// rollback like add/remove so a server-rejected Commit doesn't desync the
1122 /// epoch — on an `epoch_advanced` rejection the host should re-sync (another
1123 /// member already committed) and the pending proposal will have cleared.
1124 pub async fn commit_pending_proposals(
1125 &self,
1126 conv_id: ConversationId,
1127 now_ms: u64,
1128 ) -> Result<()> {
1129 let staged = {
1130 let mut guard = self.conversations.write();
1131 let convo = guard
1132 .get_mut(&conv_id)
1133 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1134 match convo.stage_commit_pending_proposals(now_ms)? {
1135 Some(s) => s,
1136 None => return Ok(()),
1137 }
1138 };
1139
1140 if let Err(send_err) = self.transport.send(staged.commit.clone()).await {
1141 let merged = {
1142 let mut guard = self.conversations.write();
1143 match guard.get_mut(&conv_id) {
1144 Some(convo) if is_definite_rejection(&send_err) => {
1145 let _ = convo.abort_staged();
1146 false
1147 }
1148 Some(convo) => {
1149 convo.confirm_staged(&staged, now_ms)?;
1150 true
1151 }
1152 None => false,
1153 }
1154 };
1155 if merged {
1156 self.flush_conversation(&conv_id).await?;
1157 }
1158 return Err(send_err);
1159 }
1160
1161 {
1162 let mut guard = self.conversations.write();
1163 let convo = guard
1164 .get_mut(&conv_id)
1165 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1166 convo.confirm_staged(&staged, now_ms)?;
1167 }
1168 self.flush_conversation(&conv_id).await?;
1169 Ok(())
1170 }
1171
1172 /// Process an inbound envelope coming from the transport's subscribe callback or a sync pull.
1173 /// Returns `Some` for application traffic, `None` for handshake messages (already merged).
1174 ///
1175 /// LIVE path: applies the envelope to the in-memory MLS working set and then
1176 /// durably flushes THIS envelope before returning (per-envelope crash-safety
1177 /// for streaming). The catch-up drain (`sync_conversations`) instead applies a
1178 /// whole page in-memory and flushes ONCE per page — see `apply_envelope_in_memory`.
1179 pub async fn process_envelope(
1180 &self,
1181 env: &MessageEnvelope,
1182 now_ms: u64,
1183 ) -> Result<Option<IncomingMessage>> {
1184 let (out, removed) = self.apply_envelope_in_memory(env, now_ms)?;
1185 // A self-removal already deleted the group's storage; nothing to flush.
1186 if !removed {
1187 self.flush_conversation(&env.conversation_id).await?;
1188 }
1189 Ok(out)
1190 }
1191
1192 /// Apply an inbound envelope to the IN-MEMORY MLS working set and advance the
1193 /// in-memory cursor, WITHOUT the durable checkpoint flush. Returns the
1194 /// decrypted application message (if any) and whether the conversation was
1195 /// REMOVED (a Commit that removed our own leaf tears the group down and
1196 /// deletes its storage synchronously — a removed conversation needs no flush).
1197 ///
1198 /// The write guard is taken and dropped entirely within this synchronous
1199 /// helper — NO `.await` happens while it is held. That is the wasm
1200 /// parking-panic constraint: previously the write guard was held across
1201 /// `snapshot_to_storage().await`, and on the single-threaded wasm worker a
1202 /// concurrent reader that landed while a writer was waiting made `parking_lot`
1203 /// park → panic "Parking not supported". Callers `.await` the flush AFTER this
1204 /// returns (`process_envelope` per call; `sync_conversations` once per page).
1205 ///
1206 /// Batching the flush is the drain-wedge fix: `flush` writes a FULL-state
1207 /// checkpoint (the whole MLS store as one blob), so flushing per envelope made
1208 /// a backlog drain O(N × total_state) of synchronous serialization on the
1209 /// host's single serial queue — the stall the client saw as "messages
1210 /// delivered late". Crash-safety is unchanged: `flush` persists MLS state
1211 /// before the cursor (see `ConversationSnapshot::flush`), so a crash mid-page
1212 /// re-fetches the page and `SyncCursor::is_new` dedups the already-applied
1213 /// events.
1214 fn apply_envelope_in_memory(
1215 &self,
1216 env: &MessageEnvelope,
1217 now_ms: u64,
1218 ) -> Result<(Option<IncomingMessage>, bool)> {
1219 // Welcome envelopes for unknown conversations are routed to
1220 // `join_conversation` by the caller. Here we only handle traffic for
1221 // already-open groups.
1222 let mut guard = self.conversations.write();
1223 let convo = match guard.get_mut(&env.conversation_id) {
1224 Some(c) => c,
1225 None => return Err(Error::UnknownConversation(env.conversation_id.as_hex())),
1226 };
1227 let out = convo.process(env, now_ms)?;
1228 // SELF-REMOVAL: if this Commit removed our own leaf the group is now
1229 // Inactive. Tear it down — delete the OpenMLS state + drop the handle
1230 // — so the conversation becomes UNKNOWN to this client. A later
1231 // re-invite Welcome for the same group_id is then routed to
1232 // `join_conversation` (the host only joins UNKNOWN conversations) and
1233 // re-joins from clean storage, instead of being suppressed as an
1234 // "already-joined duplicate" (the cause of the stuck-un-joined re-invite).
1235 if !convo.is_active() {
1236 let conv_id = env.conversation_id;
1237 // Best-effort storage cleanup; even if it fails, dropping the
1238 // in-memory handle already makes the conversation re-joinable.
1239 if let Err(e) = convo.delete_group_state() {
1240 tracing::warn!(error = %e, "process_envelope: delete removed group state failed");
1241 }
1242 guard.remove(&conv_id);
1243 self.stranded.write().remove(&conv_id);
1244 return Ok((out, true));
1245 }
1246 Ok((out, false))
1247 }
1248
1249 /// Catch-up sync: pull missing events for every open conversation since its cursor.
1250 /// Returns the list of newly-decrypted application messages, in apply order.
1251 pub async fn sync_conversations(&self, now_ms: u64) -> Result<Vec<IncomingMessage>> {
1252 // Snapshot the conversation IDs ONLY — not their cursors. The cursor is
1253 // re-read fresh per fetch below. Two bugs this avoids:
1254 // 1. Stale-cursor pagination: `process_envelope` advances the
1255 // conversation's cursor as it applies each page, but the OLD code
1256 // kept fetching from the cursor captured up-front — so a chat with
1257 // more than one page (256+) of backlog re-fetched the SAME first
1258 // page forever and never paged past it (catch-up silently truncated
1259 // a freshly-linked device's history, incl. group-avatar/name
1260 // hydration that lands after the first page).
1261 // 2. Join-during-sync: reading IDs fresh here (and re-reading the map
1262 // each iteration) means a conversation the host joins via a live
1263 // Welcome right before/around this call is still covered.
1264 // Iterate a sorted, de-duplicated ID set for deterministic order.
1265 let conversation_ids: Vec<ConversationId> = {
1266 let guard = self.conversations.read();
1267 let mut ids: Vec<ConversationId> = guard.keys().copied().collect();
1268 ids.sort_by_key(|a| a.0);
1269 ids
1270 };
1271
1272 let mut delivered = Vec::new();
1273 for conv_id in conversation_ids {
1274 loop {
1275 // Re-read the LIVE cursor each iteration so pagination advances
1276 // as `process_envelope` consumes pages. If the conversation was
1277 // removed mid-sync (e.g. a wipe), stop cleanly.
1278 let cursor = match self.conversations.read().get(&conv_id) {
1279 Some(c) => c.cursor.clone(),
1280 None => break,
1281 };
1282 // PER-CONVERSATION ISOLATION: a transport error on ONE
1283 // conversation (a transient 5xx, a non-404 fetch failure) must
1284 // not abort catch-up for ALL the others. Previously a single
1285 // erroring conversation propagated `?` and failed the entire
1286 // `sync_conversations` — so on a freshly-linked device, one bad
1287 // conversation (e.g. the device group, or a group mid-churn)
1288 // blocked every chat from syncing, and group name/avatar
1289 // hydration broadcasts never arrived. Log + skip this
1290 // conversation instead.
1291 let batch = match self.transport.fetch_since(conv_id, cursor, 256).await {
1292 Ok(b) => b,
1293 Err(e) => {
1294 tracing::warn!(error = %e, "sync_conversations: fetch_since failed; skipping conversation");
1295 break;
1296 }
1297 };
1298 if batch.is_empty() {
1299 break;
1300 }
1301 let mut advanced = false;
1302 let mut removed = false;
1303 for env in &batch {
1304 // PER-ENVELOPE ISOLATION: a single undecryptable / malformed
1305 // / wrong-epoch envelope must not drop the rest of the page
1306 // (or fail the sync). Skip it; the live stream / a later
1307 // epoch advance can still deliver retriable ones.
1308 //
1309 // BATCHED CHECKPOINT (drain-wedge fix): apply each envelope to
1310 // the in-memory working set WITHOUT flushing, then persist the
1311 // whole page with ONE full-state checkpoint below. This turns a
1312 // backlog drain from O(N × total_state) synchronous
1313 // serializations into O(1) per page (~100-1000× less work on
1314 // the host's single serial queue) — the stall the client saw
1315 // as "messages delivered late".
1316 match self.apply_envelope_in_memory(env, now_ms) {
1317 Ok((msg, rm)) => {
1318 // Commits / handshakes advance the in-memory cursor too
1319 // (no app message surfaced) — treat them as progress so
1320 // we keep paging instead of re-fetching the same page.
1321 advanced = true;
1322 if let Some(msg) = msg {
1323 delivered.push(msg);
1324 }
1325 if rm {
1326 // A self-removal Commit deleted the conversation
1327 // mid-page; stop applying further envelopes to it.
1328 removed = true;
1329 break;
1330 }
1331 }
1332 Err(e) => {
1333 tracing::warn!(error = %e, "sync_conversations: process_envelope failed; skipping envelope");
1334 }
1335 }
1336 }
1337 if advanced {
1338 // Progress was made — this conversation is not stranded
1339 // (clear any prior stranded mark).
1340 self.stranded.write().remove(&conv_id);
1341 }
1342 // ONE durable checkpoint for the whole page. Skipped when the
1343 // conversation was self-removed (its storage was already deleted).
1344 // Crash-safety is preserved by `flush`'s state-before-cursor
1345 // ordering: a crash before/inside this flush re-fetches the page on
1346 // restart and `SyncCursor::is_new` dedups the already-applied events.
1347 if advanced && !removed {
1348 if let Err(e) = self.flush_conversation(&conv_id).await {
1349 tracing::warn!(error = %e, "sync_conversations: batch checkpoint failed; will re-fetch on next sync");
1350 break;
1351 }
1352 }
1353 if removed {
1354 break; // conversation gone; nothing more to page
1355 }
1356 if batch.len() < 256 {
1357 break; // partial page → caught up
1358 }
1359 if !advanced {
1360 // Full page but nothing advanced the cursor (every envelope
1361 // errored / was wrong-epoch). The group can no longer advance
1362 // from local state — a Commit was missed. Mark it STRANDED so
1363 // the host can recover it (re-Welcome / snapshot import) via
1364 // `stranded_conversations()`, and bail to avoid an infinite
1365 // re-fetch loop.
1366 tracing::warn!(
1367 "sync_conversations: full page made no progress; marking conversation stranded"
1368 );
1369 self.stranded.write().insert(conv_id);
1370 break;
1371 }
1372 }
1373 }
1374 Ok(delivered)
1375 }
1376
1377 /// Rehydrate conversations from storage on startup ([CR-4]).
1378 ///
1379 /// Walks the host-side `groups` namespace for meta records, pairs each with its
1380 /// cursor + device→leaf map, and asks `Conversation::load` to re-attach to the
1381 /// underlying OpenMLS group state. The MLS state itself was persisted by the
1382 /// SQLite-backed `PersistentMlsProvider` on the previous run; this method
1383 /// reconciles the SDK-side caches with what's on disk.
1384 async fn rehydrate_conversations(self: &Arc<Self>, now_ms: u64) -> Result<()> {
1385 let metas = self.storage.list_keys("groups", "").await?;
1386 for path in metas {
1387 // path looks like "{convId}/meta"
1388 let Some((id_hex, suffix)) = path.split_once('/') else {
1389 continue;
1390 };
1391 if suffix != "meta" {
1392 continue;
1393 }
1394 let Some(meta_bytes) = self.storage.get("groups", &path).await? else {
1395 continue;
1396 };
1397 let meta: ConversationMeta = match codec::decode(&meta_bytes) {
1398 Ok(m) => m,
1399 Err(_) => continue,
1400 };
1401 let cursor_bytes = self
1402 .storage
1403 .get("cursors", id_hex)
1404 .await?
1405 .unwrap_or_default();
1406 let cursor = if cursor_bytes.is_empty() {
1407 SyncCursor::default()
1408 } else {
1409 SyncCursor::decode(&cursor_bytes).unwrap_or_default()
1410 };
1411
1412 // [CR-2] device→leaf map was persisted alongside meta + cursor.
1413 let device_leaves_bytes = self
1414 .storage
1415 .get("device_leaves", id_hex)
1416 .await?
1417 .unwrap_or_default();
1418 let device_leaves: std::collections::BTreeMap<DeviceId, u32> =
1419 if device_leaves_bytes.is_empty() {
1420 std::collections::BTreeMap::new()
1421 } else {
1422 let pairs: Vec<(DeviceId, u32)> =
1423 codec::decode(&device_leaves_bytes).unwrap_or_default();
1424 pairs.into_iter().collect()
1425 };
1426
1427 match Conversation::load(
1428 meta.id,
1429 meta.clone(),
1430 cursor,
1431 device_leaves,
1432 self.local_device.device_id.clone(),
1433 self.crypto.clone(),
1434 self.signing.clone(),
1435 self.storage.clone(),
1436 now_ms,
1437 ) {
1438 Ok(Some(convo)) => {
1439 tracing::debug!(
1440 target: "ping_core::client",
1441 convo = %id_hex,
1442 epoch = meta.epoch,
1443 "rehydrated conversation from disk"
1444 );
1445 self.conversations.write().insert(meta.id, convo);
1446 }
1447 Ok(None) => {
1448 tracing::warn!(
1449 target: "ping_core::client",
1450 convo = %id_hex,
1451 "host-side meta present but OpenMLS state missing — skipping"
1452 );
1453 }
1454 Err(e) => {
1455 tracing::warn!(
1456 target: "ping_core::client",
1457 convo = %id_hex,
1458 error = %e,
1459 "Conversation::load failed — skipping"
1460 );
1461 }
1462 }
1463 }
1464 Ok(())
1465 }
1466
1467 // ------------------- Multi-device API -------------------
1468
1469 /// Build a [`LinkingTicket`] for a new device. The caller obtains `new_device_kp` from the
1470 /// new device (e.g., via QR-encoded handshake) and is responsible for sealing the returned
1471 /// ticket against the new device's ephemeral X25519 pubkey before transmission via
1472 /// [`ping_link::seal_ticket`].
1473 ///
1474 /// [CR-13] `last_app_events` is a host-supplied list of `(conversation_id, app_event_bytes)`
1475 /// for the new device's "what you missed" UI. The SDK adds its own metas + (currently-
1476 /// empty) per-conversation MLS state and bundles everything into
1477 /// [`device::CatchupSnapshot`], CBOR-encoded into the ticket's `catchup_snapshot` field.
1478 /// Pass an empty `Vec` to suppress catchup data (the new device sees an empty
1479 /// conversation list until normal sync runs).
1480 pub async fn build_linking_ticket(
1481 self: &Arc<Self>,
1482 new_device_id: DeviceId,
1483 new_device_kp: Vec<u8>,
1484 last_app_events: Vec<(ConversationId, Vec<u8>)>,
1485 now_ms: u64,
1486 ) -> Result<LinkingTicket> {
1487 let device_binding_sig = self.identity.sign_device_binding(&new_device_id.0);
1488 let dg_id = device_group_id_for(self.identity.user_id());
1489
1490 // [CR-10] DG is eagerly created at init now, but call ensure here too so
1491 // hosts that bypass `MessagingClient::init` (mocked tests, legacy upgrade
1492 // paths) keep working.
1493 self.ensure_device_group(now_ms).await?;
1494
1495 // Admit the new device to the DeviceGroup.
1496 let outcome = {
1497 let mut conversations = self.conversations.write();
1498 // `ensure_device_group` above creates it, but return a typed error
1499 // instead of panicking (which would unwind across FFI/wasm) on the
1500 // pathological case where it's still missing.
1501 let dg = conversations.get_mut(&dg_id).ok_or_else(|| {
1502 Error::Invalid("device group missing after ensure_device_group".into())
1503 })?;
1504 // [CR-2] Record the new device's leaf in the DG so future `revoke_device`
1505 // can find it. The new_device_id we got as a parameter is the inviter's
1506 // own assertion — same trust model as the rest of `add_members`.
1507 dg.add_members(vec![(new_device_id.clone(), new_device_kp)], now_ms)?
1508 };
1509
1510 // [CR-13] Assemble the catchup snapshot: SDK-known conversation metadata + host-
1511 // supplied last-known plaintext per conversation. [CR-7] now populates
1512 // `group_state_bytes` with each group's MLS state so the new device can decrypt
1513 // historical traffic without re-Welcoming. An empty `group_state_bytes` would
1514 // mean either a group with no exportable state (shouldn't happen) or an
1515 // encoder failure (we let those propagate as errors below).
1516 let catchup_snapshot = if last_app_events.is_empty() && self.conversations.read().is_empty()
1517 {
1518 // Cheap path: nothing to snapshot, skip the encode round-trip.
1519 Vec::new()
1520 } else {
1521 // CR-7 per-group state export is O(conversations) expensive MLS
1522 // serialization AND unconsumed by current hosts (both re-admit the new
1523 // device via the post-link re-Welcome reconciler rather than importing
1524 // this state). Exporting every group's state for an account with hundreds
1525 // or thousands of conversations is wasted CPU that also blows the 256 KB
1526 // ticket cap. So budget the EXPORT itself: fill `group_state_bytes` only
1527 // while under a small byte budget, in iteration order; once exhausted, ship
1528 // the (cheap) meta with empty `group_state_bytes` and skip the expensive
1529 // export entirely — the receiver falls back to the normal re-Welcome path
1530 // for those. `encode_within_cap` below is the final guarantee the whole
1531 // snapshot fits regardless. This keeps the per-link cost bounded to a
1532 // CONSTANT (~budget) instead of growing with conversation count.
1533 const GROUP_STATE_EXPORT_BUDGET: usize = CATCHUP_SNAPSHOT_SOFT_CAP; // 64 KiB
1534 let mut group_state_budget = GROUP_STATE_EXPORT_BUDGET;
1535 let conversation_metas: Vec<CatchupConversationEntry> = {
1536 let guard = self.conversations.read();
1537 let mut metas = Vec::with_capacity(guard.len());
1538 for c in guard.values() {
1539 let group_state_bytes = if group_state_budget > 0 {
1540 let bytes = c.export_state_snapshot(now_ms)?.to_vec();
1541 if bytes.len() <= group_state_budget {
1542 group_state_budget -= bytes.len();
1543 bytes
1544 } else {
1545 // This one would overflow the budget — drop it and stop
1546 // exporting further (later conversations skip the export).
1547 group_state_budget = 0;
1548 Vec::new()
1549 }
1550 } else {
1551 Vec::new()
1552 };
1553 metas.push(CatchupConversationEntry {
1554 conversation_id: c.id(),
1555 meta: c.meta().clone(),
1556 group_state_bytes,
1557 });
1558 }
1559 metas
1560 };
1561 let last_app_events_per_conv: Vec<CatchupAppEventEntry> = last_app_events
1562 .into_iter()
1563 .map(|(conversation_id, app_event_bytes)| CatchupAppEventEntry {
1564 conversation_id,
1565 app_event_bytes,
1566 })
1567 .collect();
1568 // `encode_within_cap` (not `encode`) so a user with many/large groups can
1569 // still link: the snapshot snapshots EVERY conversation, so a big account
1570 // would otherwise blow the 256 KB ticket cap and hard-fail linking outright.
1571 // It sheds per-group MLS state (largest first) to fit — those conversations
1572 // catch up via the post-link re-Welcome reconciler the hosts already run.
1573 CatchupSnapshot {
1574 v: CATCHUP_SNAPSHOT_VERSION,
1575 conversation_metas,
1576 last_app_events_per_conv,
1577 }
1578 .encode_within_cap()?
1579 };
1580
1581 Ok(LinkingTicket {
1582 v: 1,
1583 user_id: self.identity.user_id().clone(),
1584 user_pubkey: self.identity.public_key().to_bytes().to_vec(),
1585 new_device_id,
1586 device_binding_sig,
1587 device_group_welcome: outcome.welcome.payload,
1588 catchup_snapshot,
1589 })
1590 }
1591
1592 /// Apply a received linking ticket. Joins the user's DeviceGroup; the catch-up snapshot
1593 /// (if any) is decrypted by the host using the standard per-conversation channel afterwards.
1594 pub async fn consume_linking_ticket(
1595 self: &Arc<Self>,
1596 ticket: &LinkingTicket,
1597 now_ms: u64,
1598 ) -> Result<()> {
1599 // Verify the binding the existing device made for us. (Ed25519 public keys are 32 bytes.)
1600 let pk_bytes: [u8; 32] = ticket
1601 .user_pubkey
1602 .as_slice()
1603 .try_into()
1604 .map_err(|_| Error::Identity("user_pubkey must be 32 bytes".into()))?;
1605 let user_pk = ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes)
1606 .map_err(|e| Error::Identity(format!("bad user pubkey: {e}")))?;
1607 Identity::verify_device_binding(
1608 &user_pk,
1609 &ticket.user_id,
1610 &ticket.new_device_id.0,
1611 &ticket.device_binding_sig,
1612 )?;
1613 if ticket.new_device_id != self.local_device.device_id {
1614 return Err(Error::Invalid(
1615 "ticket addressed to a different device".into(),
1616 ));
1617 }
1618
1619 let dummy_env = MessageEnvelope::new(
1620 ConversationId(device_group_id_for(&ticket.user_id).0),
1621 0,
1622 MessageKind::Welcome,
1623 self.local_device.device_id.clone(),
1624 0,
1625 crate::clock::Hlc::ZERO,
1626 ticket.device_group_welcome.clone(),
1627 );
1628 self.join_conversation(&dummy_env, now_ms).await?;
1629 Ok(())
1630 }
1631
1632 /// [CR-7] Export the MLS state snapshot for one open conversation.
1633 ///
1634 /// Thin pass-through to [`Conversation::export_state_snapshot`]. Returned bytes
1635 /// are wrapped in `Zeroizing` because they contain past epoch secrets.
1636 pub fn export_conversation_state_snapshot(
1637 &self,
1638 conv_id: ConversationId,
1639 now_ms: u64,
1640 ) -> Result<zeroize::Zeroizing<Vec<u8>>> {
1641 let guard = self.conversations.read();
1642 let convo = guard
1643 .get(&conv_id)
1644 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1645 convo.export_state_snapshot(now_ms)
1646 }
1647
1648 /// [CR-7] Import a `GroupStateSnapshot` produced by another device's
1649 /// [`Conversation::export_state_snapshot`].
1650 ///
1651 /// Replays the snapshot's entries into this client's OpenMLS provider, then
1652 /// reconstructs the `Conversation` handle via `MlsGroup::load`. After return,
1653 /// the conversation is in `list_conversations()` and `send`/`process_envelope`
1654 /// work against it normally.
1655 ///
1656 /// **Scope.** This is for the *same-user* hand-off (linking, recovery). The
1657 /// snapshot exposes the exporter's view of past epoch secrets for the target
1658 /// group; only call this when the receiving device has been authenticated to
1659 /// the same user identity (mnemonic, QR-handshake). Cross-user history transfer
1660 /// uses HPKE-sealed AppEvent re-shares (umbrella §15.6), not this method.
1661 ///
1662 /// **Sanity.** Refuses snapshots whose `group_id` doesn't match the bytes the
1663 /// receiver intends to claim — guards against host bugs that shuffle snapshots
1664 /// between groups. Refuses mismatched OpenMLS storage versions outright; no
1665 /// silent forward/back compatibility.
1666 pub async fn import_state_snapshot(
1667 self: &Arc<Self>,
1668 snapshot_bytes: &[u8],
1669 now_ms: u64,
1670 ) -> Result<ConversationId> {
1671 use crate::device::GroupStateSnapshot;
1672 let snap = GroupStateSnapshot::decode(snapshot_bytes)
1673 .map_err(|e| Error::Invalid(format!("snapshot decode: {e}")))?;
1674
1675 if snap.openmls_storage_version != openmls_traits::storage::CURRENT_VERSION {
1676 return Err(Error::Invalid(format!(
1677 "snapshot openmls_storage_version={} not supported (this SDK supports v={})",
1678 snap.openmls_storage_version,
1679 openmls_traits::storage::CURRENT_VERSION
1680 )));
1681 }
1682
1683 let conv_id = snap.group_id;
1684
1685 // Refuse if we already have an active handle for this conv — the host should
1686 // close it first, otherwise import silently overwrites in-memory state and
1687 // the existing handle becomes stale.
1688 if self.conversations.read().contains_key(&conv_id) {
1689 return Err(Error::Invalid(format!(
1690 "conversation {} already open; close before importing snapshot",
1691 conv_id.as_hex()
1692 )));
1693 }
1694
1695 // Replay raw KV pairs into the provider's working set.
1696 let entries: Vec<(Vec<u8>, Vec<u8>)> =
1697 snap.entries.into_iter().map(|e| (e.key, e.value)).collect();
1698 self.crypto
1699 .import_entries(entries)
1700 .map_err(|e| Error::Storage(format!("import entries: {e}")))?;
1701
1702 // Reconstruct the Conversation handle. `Conversation::load` will return
1703 // `Ok(None)` if OpenMLS still can't find the group — i.e. our snapshot was
1704 // incomplete or for a different storage version.
1705 let meta = ConversationMeta {
1706 id: conv_id,
1707 name: None,
1708 epoch: 0, // will be overwritten from the loaded group state in process()
1709 member_count: 0,
1710 is_device_group: false, // host can flip this via meta update if needed
1711 created_at_ms: now_ms,
1712 };
1713 let convo = Conversation::load(
1714 conv_id,
1715 meta,
1716 SyncCursor::default(),
1717 std::collections::BTreeMap::new(),
1718 self.local_device.device_id.clone(),
1719 self.crypto.clone(),
1720 self.signing.clone(),
1721 self.storage.clone(),
1722 now_ms,
1723 )?
1724 .ok_or_else(|| {
1725 Error::Invalid(
1726 "snapshot imported but OpenMLS could not load the group — snapshot may be incomplete or storage version mismatched"
1727 .into(),
1728 )
1729 })?;
1730
1731 // Pull the live epoch + member count from the loaded group so the meta we
1732 // just stubbed is consistent with what we'll observe on subsequent process_envelope.
1733 let live_epoch = convo.epoch();
1734 let live_members = convo.group.members().count() as u32;
1735 let live_name = convo.name_from_group_state();
1736 let mut convo = convo;
1737 convo.meta.epoch = live_epoch;
1738 convo.meta.member_count = live_members;
1739 // Recover the name from the loaded GroupContext state (a snapshot import
1740 // is join-equivalent; the stubbed `name: None` would otherwise stick).
1741 convo.meta.name = live_name;
1742 convo.snapshot_to_storage().await?;
1743
1744 self.conversations.write().insert(conv_id, convo);
1745 Ok(conv_id)
1746 }
1747
1748 /// Export a derived secret from one conversation's MLS exporter ([CR-8]).
1749 ///
1750 /// Thin pass-through to [`Conversation::export_secret`]. See that method's doc comment
1751 /// for the contract on `label`, `context`, length validation, and zeroization. The
1752 /// returned `Zeroizing<Vec<u8>>` is automatically wiped when dropped.
1753 pub fn export_conversation_secret(
1754 &self,
1755 conv_id: ConversationId,
1756 label: &str,
1757 context: &[u8],
1758 length: usize,
1759 ) -> Result<Zeroizing<Vec<u8>>> {
1760 let guard = self.conversations.read();
1761 let convo = guard
1762 .get(&conv_id)
1763 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1764 convo.export_secret(label, context, length)
1765 }
1766
1767 /// Revoke a device by removing its leaf from every conversation where we know its
1768 /// position ([CR-2]).
1769 ///
1770 /// Returns one Commit envelope per conversation the device was a leaf in. The host
1771 /// broadcasts each envelope to the affected conversation; the SDK has also already
1772 /// handed them to the transport via `transport.send` (idempotent broadcast is the
1773 /// host's call).
1774 ///
1775 /// **Scope.** The SDK can only resolve leaves it recorded itself — either when it
1776 /// admitted the device via [`Self::add_members`] or when this device joined as the
1777 /// target via Welcome. For peer-admitted devices the leaf index isn't locally known;
1778 /// those conversations are silently skipped. The host can fall back to
1779 /// `remove_members(leaf_index)` directly using a transport-side directory lookup if
1780 /// it needs to revoke from those conversations too. See
1781 /// `docs/architecture/multi-device.md §Device removal` for the broader flow.
1782 ///
1783 /// Conversations with no entry for `device_id` produce no envelope; an empty `Vec`
1784 /// return is a valid outcome (e.g. the device was already revoked, or was never
1785 /// added by this client).
1786 #[allow(clippy::await_holding_lock)] // see add_members for rationale
1787 pub async fn revoke_device(
1788 &self,
1789 device_id: DeviceId,
1790 now_ms: u64,
1791 ) -> Result<Vec<MessageEnvelope>> {
1792 // 1. Walk every open conversation and gather (conv_id, leaf_index) pairs where
1793 // we know `device_id` controls a leaf. Done under a read lock so we don't hold
1794 // the write lock across the per-conversation remove path.
1795 let targets: Vec<(ConversationId, u32)> = self
1796 .conversations
1797 .read()
1798 .iter()
1799 .filter_map(|(id, c)| c.leaf_index_of(&device_id).map(|leaf| (*id, leaf)))
1800 .collect();
1801
1802 // 2. For each target, emit a remove_members commit. We do this sequentially: each
1803 // one is a separate MLS epoch advance on its own group, and they don't share
1804 // state, so parallel issuance is safe but adds complexity we don't need for v1.
1805 let mut envelopes = Vec::with_capacity(targets.len());
1806 for (conv_id, leaf_index) in targets {
1807 let envelope = {
1808 let mut guard = self.conversations.write();
1809 let convo = guard
1810 .get_mut(&conv_id)
1811 .ok_or_else(|| Error::UnknownConversation(conv_id.as_hex()))?;
1812 convo.remove_members(vec![leaf_index], now_ms)?
1813 };
1814 self.transport.send(envelope.clone()).await?;
1815 if let Some(c) = self.conversations.read().get(&conv_id) {
1816 c.snapshot_to_storage().await?;
1817 }
1818 envelopes.push(envelope);
1819 }
1820
1821 // 3. Notify the auth-layer server so it can invalidate the
1822 // revoked device's KeyPackage pool, mark `auth.devices.revoked_at`,
1823 // and refuse any future envelope signed by the revoked device's
1824 // JWT. Done AFTER the MLS Commits so peers learn via MLS first
1825 // (the canonical path) and the auth layer is the eventual-
1826 // consistency cleanup. Transport failures bubble up so callers
1827 // can retry — but the MLS-side work has already shipped, so
1828 // the device is functionally revoked in every group; only the
1829 // auth-layer KeyPackage purge is pending.
1830 self.transport.revoke_device_remote(device_id).await?;
1831 Ok(envelopes)
1832 }
1833}
1834
1835fn device_group_id_for(user_id: &UserId) -> ConversationId {
1836 // Deterministic 16-byte ID derived from the user's id, prefixed so it cannot collide with
1837 // a randomly-generated ULID in normal use (ULIDs start with a millisecond timestamp).
1838 let mut bytes = [0u8; 16];
1839 bytes[0] = 0xFF;
1840 bytes[1] = 0xDC; // "DeviCe" group sentinel
1841 let h = codec::sha256(&user_id.0);
1842 bytes[2..].copy_from_slice(&h[..14]);
1843 ConversationId(bytes)
1844}
1845
1846fn encode_local_device(d: &LocalDevice) -> Result<Vec<u8>> {
1847 use serde::Serialize;
1848 #[derive(Serialize)]
1849 struct Persisted<'a> {
1850 device_id: &'a DeviceId,
1851 label: &'a str,
1852 created_at_ms: u64,
1853 #[serde(with = "serde_bytes")]
1854 signing_seed: &'a [u8],
1855 }
1856 codec::encode(&Persisted {
1857 device_id: &d.device_id,
1858 label: &d.label,
1859 created_at_ms: d.created_at_ms,
1860 signing_seed: d.signing.as_bytes(),
1861 })
1862}
1863
1864fn decode_local_device(bytes: &[u8], user_id: UserId) -> Result<LocalDevice> {
1865 use serde::Deserialize;
1866 #[derive(Deserialize)]
1867 struct Persisted {
1868 device_id: DeviceId,
1869 label: String,
1870 created_at_ms: u64,
1871 #[serde(with = "serde_bytes")]
1872 signing_seed: Vec<u8>,
1873 }
1874 let p: Persisted = codec::decode(bytes)?;
1875 let seed: [u8; 32] = p
1876 .signing_seed
1877 .as_slice()
1878 .try_into()
1879 .map_err(|_| Error::Invalid("device signing seed must be 32 bytes".into()))?;
1880 let signing = ed25519_dalek::SigningKey::from_bytes(&seed);
1881 Ok(LocalDevice {
1882 device_id: p.device_id,
1883 user_id,
1884 label: p.label,
1885 signing,
1886 created_at_ms: p.created_at_ms,
1887 })
1888}