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

start() / resume()

Establishes the WebSocket connection and performs descriptor exchange; resume() reconnects an interrupted session and retrieves queued deliveries that arrived during disconnection.

requestMediation()

Requests mediation service from the anchor, resolving to 'granted' or 'denied'.

updateKeylist(add, remove)

Registers or removes recipient keys for which the mediator queues inbound messages.

pickup(opts)

Drains the mediator queue in batches across all registered keys and acknowledges decrypted deliveries, purging them from the mediator inbox.

sendForward(did, message)

Transmits a DIDComm v2 message via the mediator using authcrypt when the recipient’s X25519 key is known, or anoncrypt otherwise.

reconcile(filter, items)

Executes NIP-77 negentropy set reconciliation over the active connection, returning the sets of identifiers missing from each side.

close()

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 over BroadcastChannel and 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_PICKUP in 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

keys, did

Node, key-agreement, and transport keys encoded as multikeys; did:nostr, did:key, and did:peer parsers and DID document builders.

event, encoding

NIP-01 event ID computation, signing, and verification; shared hex, UTF-8, base64url, and canonical JSON codecs used across @fleetmesh/* packages.

nip44, seal, giftwrap

NIP-44 v2 encryption and NIP-59 sealing and gift-wrapping primitives.

didcomm

DIDComm v2 authcrypt and anoncrypt envelope codecs.

descriptor

Serialization and parsing of kind 11801 node descriptors.

mediation, mediationMessages

Leaf-side mediation protocol: requests, grants, keylist updates, and message pickup.

negentropy

NIP-77 negentropy set reconciliation protocol.

relay

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.