2. @fleetmesh/leaf#
@fleetmesh/leaf implements the FleetMesh leaf profile in TypeScript, enabling a browser or progressive web application (PWA) to participate in the mesh as a mediated leaf. A leaf maintains a single wss:// connection to its mediator (an anchor-class node), exchanges gift-wrapped DIDComm v2 envelopes over standard Nostr relay frames, reconciles event sets using NIP-77 negentropy, and maintains a bounded local store. Leaves do not forward traffic, mediate for other peers, issue grants, or publish conformance reports.
The normative behavior is defined in the FleetMesh specification sections “The leaf profile and mobile operational limits” and “Mediation is how a phone gets an address”. For a guided walkthrough, see Browser Leaf Application.
2.1. Installation#
npm install @fleetmesh/leaf@next
Runtime dependencies are limited to exact-pinned versions of @noble/ciphers, @noble/curves, @noble/hashes, and @scure/base. Source files under src/ use erasable TypeScript syntax exclusively, allowing Node.js to execute them directly without a build step.
2.2. Sessions#
LeafSession manages a single leaf connection to one mediator on any JavaScript runtime providing a WebSocket implementation. It requires the leaf’s node key (secp256k1, hex-encoded), its key-agreement key (X25519), and the mediator’s node key and wss:// endpoint.
Method |
Description |
|---|---|
|
Establishes the WebSocket connection and performs descriptor exchange; |
|
Requests mediation service from the anchor, resolving to |
|
Registers or removes recipient keys for which the mediator queues inbound messages. |
|
Drains the mediator queue in batches across all registered keys and acknowledges decrypted deliveries, purging them from the mediator inbox. |
|
Transmits a DIDComm v2 message via the mediator using authcrypt when the recipient’s X25519 key is known, or anoncrypt otherwise. |
|
Executes NIP-77 negentropy set reconciliation over the active connection, returning the sets of identifiers missing from each side. |
|
Terminates the WebSocket connection. |
Mediation grants and keylists persist across restarts through the state constructor option and the onState callback. Unsolicited inbound messages are delivered to onMessage, and undecryptable or rejected envelopes are reported exclusively to onDrop along with the rejection reason.
2.3. Browser Runtime#
startBrowserLeaf({ mediator }) initializes a browser-managed leaf session and returns a LeafHub coordinator:
Single connection per origin: Open tabs contend for a Web Lock; the lock holder acts as leader and maintains the sole
wss://session, while follower tabs communicate with the leader overBroadcastChanneland assume leadership if the leader closes. Service workers do not host the WebSocket connection because browsers terminate idle service workers after approximately 30 seconds.IndexedDB persistence (
LeafStore): Stores identity key material encrypted under a non-extractable WebCrypto key, alongside the active mediation grant, keylist, mediator metadata, and an age-evicted local inbox.Foreground networking and push wake-up: The leader drains the mediator queue on startup, when the page becomes visible, and when a service worker dispatches
SW_PICKUPin response to a Web Push event. (Anchor nodes do not currently emit Web Push notifications.)
hub.on(listener) subscribes to HubEvent notifications (role, status, and delivered). Calling pickup, updateKeylist, sendForward, or reconcile on a LeafHub proxies the operation through the active leader tab.
2.4. Modules#
The package root exports LeafSession, SessionError, startBrowserLeaf, SW_PICKUP, LeafHub, LeafStore, DB_NAME, and the following module namespaces:
Namespace |
Contents |
|---|---|
|
Node, key-agreement, and transport keys encoded as multikeys; |
|
NIP-01 event ID computation, signing, and verification; shared hex, UTF-8, base64url, and canonical JSON codecs used across |
|
NIP-44 v2 encryption and NIP-59 sealing and gift-wrapping primitives. |
|
DIDComm v2 authcrypt and anoncrypt envelope codecs. |
|
Serialization and parsing of kind |
|
Leaf-side mediation protocol: requests, grants, keylist updates, and message pickup. |
|
NIP-77 negentropy set reconciliation protocol. |
|
Nostr WebSocket relay client transport. |
2.5. Conformance#
The Python reference implementation in mesh-py/core/ is authoritative. Every cryptographic and protocol primitive is verified against mesh-py/core/vectors/*.json directly from the sibling repository checkout. The test suite runs in Node.js and, via Vitest browser mode on Playwright, in Chromium, Firefox, and WebKit (npm run test:browser).
npm run e2e launches a local meshnode relay server and anchor instance on localhost and verifies a full LeafSession lifecycle over WebSocket: descriptor exchange, mediation grant, keylist updates, batched forward pickup, delivery acknowledgments, and session resumption after disconnect. npm run test:browser-e2e executes this end-to-end suite inside each browser engine.
scripts/negentropy-harness.ts implements the standard negentropy conformance harness protocol, enabling web/site/scripts/interop-negentropy.py to verify reconciliation interoperability directly against the Python reference implementation.