Files
fundamentum-client/docs/V1_1B.md
T
2026-11-05 09:05:13 +01:00

11 KiB

Fundamentum v1.1b — the client in the browser, live subscribe, the JS package (design note)

Written before the v1.1b code. It turns docs/V1_1.md §4–§6 (accepted with decision 5 on 2026-11-04) into what gets built. (choice) marks where this note settles something §4–§6 left open; each says what it costs. Nothing here changes a v1 or v1.1a format.

The goal: a PWA on a phone is a Domi device. It joins the self group, sends and reads verified events, and hears about new mail without polling. Its device key and its state never leave the browser unencrypted.

Two releases:

Release Contents
v0.4.0 the client split into an async core and a blocking native driver (§1); DeviceSigner (§2); live subscribe on the relay (§3); CORS (§4)
v0.5.0 crates/wasm (fetch transport, WebCrypto signer) and the JS package @domi/fundamentum (§5), tested in a browser against a local relay

1. One core, two drivers

§4 proposed a sans-IO state machine. (choice) The core is async code generic over a transport and a signer, and Rust's async produces the state machine. A hand-written state machine for sync (several mailboxes, more pages, invites, joins, acks) would duplicate every control path the client already has, and every bug fix would have to be made twice.

pub trait Transport {
    /// `Err` only when the relay could not be reached (offline); any HTTP status is an `Ok` response.
    fn send(&self, req: HttpRequest) -> impl Future<Output = Result<HttpResponse, String>>;
}
pub struct Core<T: Transport, S: DeviceSigner> { … }   // every operation that talks to the relay or signs is `async`
pub struct Client(Core<Ureq, Subkey>);                 // native: the v0.3 API, unchanged, blocking
  • Native (Client): the API every app uses today keeps its exact signatures. Its transport (ureq) and signer (Subkey) never wait, so each call polls the core's future exactly once. There is no executor and no thread. The core never waits on anything else (no timers, no channels).
  • Browser (crates/wasm): the same core over fetch and a WebCrypto signer, driven by the browser's event loop through wasm-bindgen-futures.
  • The tests stay as they are. They drive Client, so they test the core.
  • ureq is a dependency only off wasm32. EncryptedFileStore is native only. The clock is a parameter of Core::open (std::time panics in the browser).
  • Cost: Core is generic, so a native app compiles one instance and the browser another. That is fine.

2. The device key in the browser

pub trait DeviceSigner {
    fn public(&self) -> PublicKey;
    fn sign_event_tbs(&self, tbs: &[u8]) -> impl Future<Output = Result<Signature, String>>;       // EVENT_DOMAIN ‖ tbs
    fn sign_credential_tbs(&self, tbs: &[u8]) -> impl Future<Output = Result<Signature, String>>;  // CREDENTIAL_DOMAIN ‖ tbs
}
  • tessera::Subkey implements it (native).
  • In the browser the private key is a non-extractable WebCrypto Ed25519 key, the one day-1 pairing certified. Rust prepends the domain, and JavaScript calls crypto.subtle.sign("Ed25519", key, message). JavaScript never chooses the domain, so it cannot be tricked into signing one statement as another.
  • envelope gains a two-step seal for a signer that cannot be called synchronously:
    • Envelope::to_be_signed(author, cert, schema, hlc, payload) gives the bytes to sign.
    • Envelope::with_signature(tbs, sig) finishes the envelope. It checks the signature against the author before returning, so a broken signer cannot produce an envelope that others would drop.
    • seal is unchanged.
  • No hand-written crypto: Ed25519 is WebCrypto's in the browser and ed25519-dalek everywhere it is checked.

3. Live subscribe

As in V1_1 §5, with the details settled:

  • GET /v1/live upgrades to a WebSocket.
  • Subscribing. The client's first message (text, JSON) lists up to 32 subscriptions:
    {"subscribe": [{"mailbox": "<id>", "after": 17, "key": "…", "time": "…", "nonce": "…", "sig": "…"}]}
    
    • Each one is signed like a request, with method LIVE, path /v1/mailboxes/{id}/live?after={after} and an empty body. The relay checks it like any request: time, signature, registration in that mailbox, replay, rate. A subscription that fails is answered {"mailbox", "error"}, and the others still count.
    • A WebSocket from a browser cannot carry the Tab-* headers, which is why the signatures travel in the message.
  • Notices. The relay sends {"mailbox", "seq"} when that mailbox holds a message with a higher seq than after. It sends one at once if there already is one, and then one per append. It never sends the message itself; the client fetches as it does today. Notices only say "something is new": a client that misses one loses nothing, because its next fetch starts from its cursor.
  • Removed keys. A key deregistered while subscribed gets {"mailbox", "error": "unregistered_key"}, and that subscription ends. The relay checks the registration again before every notice.
  • Keeping it open. The client sends {"ping": true} at least every 5 minutes, and the relay answers {"pong": true}. After 10 minutes with nothing from the client, the relay closes the socket.
  • Limits: 32 subscriptions per connection; 4 open subscriptions per mailbox key (the 5th is refused too_many). Rate counting is the same per key as for requests.
  • In memory only. Nothing about a socket is stored or logged. One process-wide broadcast of (mailbox, seq) feeds every socket. A socket that falls behind it is told to re-fetch everything it watches ({"mailbox", "seq": null} for each).
  • The core is sans-IO here. Core::live_subscribe() returns the signed first message for every active group, and Core::live_notice(text) names the group that has mail. The drivers own the socket. The browser package uses it; the native Client gets no socket yet (desktop apps keep polling).
  • Privacy: no worse than polling, as V1_1 §5 says. A socket that watches several mailboxes links them, as fetching them from one address already does. The JS package therefore opens one socket per group by default.

4. CORS

A PWA at fiscus.… calling tabellarius.… needs CORS. (choice) The relay answers every request with Access-Control-Allow-Origin: *, and a preflight (OPTIONS) with the methods, the content-type and tab-* headers, and max-age one day.

  • * is safe here because nothing is authorised by a cookie or by the origin. Every request is authorised only by its signature, and browsers never send cookies to a * origin.
  • Caddy strips Origin, Referer and User-Agent before the relay sees them. The relay never learns which app a request came from. (The browser still sends Origin, so the TLS endpoint sees it, as it sees the address. Caddy's access log stays off.)
  • The CORS handling is ~20 lines in the relay, tested, not a new dependency.
  • Alternative: proxy the relay under each app's own origin (fiscus.…/tabellarius/). No CORS, but every app's vhost then knows which app talks to the relay, and the relay would need one configuration per app.

5. The JS package (@domi/fundamentum, v0.5.0)

  • crates/wasm (wasm-bindgen) wraps Core<Fetch, WebCryptoSigner>. packages/fundamentum holds the generated module and a small hand-written index.js. Each PWA serves it itself; nothing comes from a CDN.
  • API (mirrors the Rust one):
    • Fundamentum.open({relay, cert, key, state}) takes the cert text, the non-extractable CryptoKey and the previously saved state.
    • Then: selfGroup, openGroup, groups, invite, join, send, sync, log, members, remove, applyRevocation, backup, restore, import.
    • subscribe(onChange) opens the sockets and runs sync when a notice arrives.
  • State: after every call that changes it, the package hands the encrypted state to onSave(bytes). It is encrypted with a non-extractable WebCrypto AES-GCM key (V1_1 §4), and the app keeps it in IndexedDB.
    • The plaintext exists only inside the page, which already holds the wasm memory that contains it. This is the same trust boundary as the desktop app's process.
  • Calls are serialised: the package queues them, so two syncs never run at once. If one still overlaps, the core answers busy instead of panicking.
  • The browser build: wasm32-unknown-unknown, with the browser's randomness (getrandom wasm_js) and OpenMLS's js feature, built in WSL with nix (rustup targets are not installed on the dev machine). It is checked by a test page against a local relay: a native CLI device and a browser device join one group, exchange events both ways, and the browser hears the live notice.

Status

v0.4.0:

  • Core<T, S> with Transport and DeviceSigner. Client is the native driver, and its API is unchanged.
  • The two-step seal: Envelope::to_be_signed and with_signature.
  • /v1/live, with Core::live_subscribe and live_notice.
  • CORS, and Caddy stripping Origin, Referer and User-Agent.

Tested against the in-process relay:

  • A core whose transport and signer really wait pairs and syncs with a native device.
  • A device hears that its group has mail, and later that it was removed.
  • The relay's checks and limits on subscriptions hold.
  • The preflight works.

Not built for wasm32 yet; that comes with crates/wasm in v0.5.0.

v0.5.0: crates/wasm and packages/fundamentum (index.js; pkg/ is built by build.sh in nix develop .#wasm and is not in git). The wasm module is 3.1 MB.

The browser check (packages/fundamentum/README.md) passed on 2026-11-04 in the desktop app's Chromium, against a local relay, cross-origin:

  • A native CLI device and a browser device joined one self group. The browser's credential and events were signed by its non-extractable WebCrypto key; exportKey refused with InvalidAccessError.
  • Events went both ways, and the browser heard the CLI's event live and synced it.
  • The sealed state reopened to the same log. It does not show the group id, and a wrong AES key does not open it.

Seen while testing it: a browser sends a CORS preflight (OPTIONS) before each relay request, because the URL changes (?after=N) and preflights are cached per URL. That doubles the round trips. The answers are small and unsigned, and do not count against the rate limit.

  • Possible later: put after into a signed header so the URL stays the same. That would be a protocol change.

Not checked:

  • A real phone.
  • WebKit/Firefox. Ed25519 in WebCrypto is recent there.
  • The live server (it needs the nixos-rebuild for v0.4.0's relay).

Not in v1.1b

  • Pairing a phone end to end (the PWA asking the factor service for its cert, as day 1 did for the Custos web vault). This needs the live server.
  • A native WebSocket driver for desktop apps.
  • Service-worker push while the PWA is closed: no third-party push service (Domi rule), so a closed PWA syncs when it is opened.