[client] F21: MLS 1:1 over OpenMLS, a mailbox per contact, invites with a derived welcome mailbox

This commit is contained in:
2026-11-14 13:23:25 +01:00
parent 8517b0bbd9
commit 4a4e82e0e9
6 changed files with 2697 additions and 16 deletions
+153
View File
@@ -0,0 +1,153 @@
//! How the client reaches a relay. The library makes no network calls itself: an app gives it a
//! [`Transport`] (HTTP in Amicitia, F22). [`MemoryTransport`] is a stand-in with the relay's rules,
//! for tests and for running the client on a device without a relay.
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tabellarius_protocol::{
blob_hash, CreateInvite, CreateMailbox, InviteId, MailboxId, Sha256Hash, Token,
};
/// Why the relay said no. Carries nothing of the request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransportError {
/// 404: unknown, wrong token, expired, or already claimed.
NotFound,
/// 409: the id is taken.
Conflict,
/// Anything else: the network, a 5xx, a body that did not parse.
Failed,
}
/// The relay's operations (`PROTOCOL.md` §8), one method each.
pub trait Transport {
fn create_mailbox(&self, req: &CreateMailbox) -> Result<(), TransportError>;
fn post(&self, id: &MailboxId, post: &Token, blob: &[u8]) -> Result<(), TransportError>;
/// The waiting blobs, oldest first.
fn fetch(&self, id: &MailboxId, fetch: &Token) -> Result<Vec<Vec<u8>>, TransportError>;
fn acknowledge(
&self,
id: &MailboxId,
fetch: &Token,
hash: &Sha256Hash,
) -> Result<(), TransportError>;
fn create_invite(&self, req: &CreateInvite) -> Result<(), TransportError>;
/// The KeyPackage, once.
fn claim_invite(&self, id: &InviteId, secret: &Token) -> Result<Vec<u8>, TransportError>;
}
#[derive(Default)]
struct Boxes {
mailboxes: HashMap<MailboxId, (Sha256Hash, Sha256Hash, Vec<Vec<u8>>)>,
invites: HashMap<InviteId, (Sha256Hash, Vec<u8>)>,
}
/// An in-memory relay with the same rules as the real one (no expiry: there is no clock here).
/// Clone it to give two clients the same relay.
#[derive(Clone, Default)]
pub struct MemoryTransport(Arc<Mutex<Boxes>>);
impl MemoryTransport {
fn boxes(&self) -> std::sync::MutexGuard<'_, Boxes> {
self.0.lock().unwrap_or_else(|p| p.into_inner())
}
/// How many blobs wait on the relay, in all mailboxes: what was not acknowledged.
pub fn waiting_blobs(&self) -> usize {
self.boxes()
.mailboxes
.values()
.map(|(_, _, b)| b.len())
.sum()
}
/// Every byte the relay holds, for privacy tests: ids, hashes, blobs and KeyPackages.
pub fn everything_stored(&self) -> Vec<u8> {
let b = self.boxes();
let mut out = Vec::new();
for (id, (post, fetch, blobs)) in &b.mailboxes {
out.extend_from_slice(id.as_bytes());
out.extend_from_slice(post.as_bytes());
out.extend_from_slice(fetch.as_bytes());
blobs.iter().for_each(|blob| out.extend_from_slice(blob));
}
for (id, (hash, kp)) in &b.invites {
out.extend_from_slice(id.as_bytes());
out.extend_from_slice(hash.as_bytes());
out.extend_from_slice(kp);
}
out
}
}
impl Transport for MemoryTransport {
fn create_mailbox(&self, req: &CreateMailbox) -> Result<(), TransportError> {
let mut b = self.boxes();
if b.mailboxes.contains_key(&req.id) {
return Err(TransportError::Conflict);
}
b.mailboxes.insert(
req.id,
(req.post_token_hash, req.fetch_token_hash, Vec::new()),
);
Ok(())
}
fn post(&self, id: &MailboxId, post: &Token, blob: &[u8]) -> Result<(), TransportError> {
match self.boxes().mailboxes.get_mut(id) {
Some((hash, _, blobs)) if *hash == post.hash() => {
blobs.push(blob.to_vec());
Ok(())
}
_ => Err(TransportError::NotFound),
}
}
fn fetch(&self, id: &MailboxId, fetch: &Token) -> Result<Vec<Vec<u8>>, TransportError> {
match self.boxes().mailboxes.get(id) {
Some((_, hash, blobs)) if *hash == fetch.hash() => Ok(blobs.clone()),
_ => Err(TransportError::NotFound),
}
}
fn acknowledge(
&self,
id: &MailboxId,
fetch: &Token,
hash: &Sha256Hash,
) -> Result<(), TransportError> {
match self.boxes().mailboxes.get_mut(id) {
Some((_, f, blobs)) if *f == fetch.hash() => {
let at = blobs
.iter()
.position(|b| blob_hash(b) == *hash)
.ok_or(TransportError::NotFound)?;
blobs.remove(at);
Ok(())
}
_ => Err(TransportError::NotFound),
}
}
fn create_invite(&self, req: &CreateInvite) -> Result<(), TransportError> {
let mut b = self.boxes();
if b.invites.contains_key(&req.id) {
return Err(TransportError::Conflict);
}
b.invites
.insert(req.id, (req.secret_hash, req.key_package.bytes().to_vec()));
Ok(())
}
fn claim_invite(&self, id: &InviteId, secret: &Token) -> Result<Vec<u8>, TransportError> {
let mut b = self.boxes();
match b.invites.get(id) {
Some((hash, _)) if *hash == secret.hash() => b
.invites
.remove(id)
.map(|(_, kp)| kp)
.ok_or(TransportError::NotFound),
_ => Err(TransportError::NotFound),
}
}
}