Minimal integration example
The Quickstart shows what a DeRecProtocol instance looks like in isolation. This page shows the lifecycle: an Owner pairs with two Helpers, the channels rotate to their long-term ids, and one ProtectSecret round runs end-to-end. Every println! corresponds to an event you would surface to your application.
The example is complete: the code blocks below, concatenated into one main.rs, compile and run against derec-library 0.0.10. It covers the Owner ↔ Helper case only — enough to see the start / process / accept triangle in action. It is a stripped-down version of the canonical fixture at smoke-tests/rust/src/protocol.rs; once you've understood this page, that file is where you go next for the fuller worked example (verification, recovery and restore, replica-mode pairings and Secret sync, UpdateChannelInfo, unpairing).
What we are building
Three actors live in the same process:
Alice — Owner. Creates a contact for each Helper, accepts their pairing requests, and publishes a secret via
DeRecFlow::ProtectSecret.Bob and Carol — Helpers. Each scans one of Alice's contacts, drives
DeRecFlow::Pairing, and accepts the inbound share when it arrives.
Two Helpers, because a split needs the paired Helpers' total weight (one subshare each by default) to reach the threshold, and the threshold can be no lower than 2. Below the threshold, ProtectSecret runs no split and the Helpers receive nothing.
The "transport" is a shared outbox per peer: the library hands each outbound message to the peer's send, which queues (uri, bytes); a driver loop drains the queues and feeds each message into the process(...) of the peer serving that URI. In a real deployment this would be HTTPS or gRPC — the library opens no socket and doesn't care. See Transports for the push model and the recommended HTTPS and gRPC conventions.
Project setup
[dependencies]
derec-library = "0.0.10"
derec-proto = "0.0.10"
tokio = { version = "1", features = ["macros", "rt"] }derec-proto is a direct dependency because the example names wire types (TransportProtocol, ContactMode, SenderKind) that derec-library does not re-export.
The store traits (in-memory)
Five HashMap-backed stores. Every method returns Box::pin(std::future::ready(...)) — sync work, async-shaped signature. The ready helper just shortens that.
The channel store holds two kinds of record, keyed differently. A ChannelRecord::Helper(HelperChannel) is keyed by (secret_id, channel_id); a ChannelRecord::Replica(ReplicaMember) by (secret_id, replica_id), because every member of a replica group shares one channel_id. load and remove take a ChannelQuery naming which one. helpers and replicas take a filter; returning every row and ignoring it is also correct, because the library re-applies it.
The transport
DeRecTransport::send receives every endpoint the peer advertised, in the peer's order. Rather than implement that contract by hand, implement SendOne — deliver to one endpoint — and wrap it in SequentialFailover, which tries the endpoints in order and stops at the first success. Here, "delivering" means appending to the outbox.
Wiring a Peer
Bundles a DeRecProtocol instance with its outbox and URI so the driver can route bytes. All seven with_* calls shown before with_threshold are required: build() does not type-check until every store and the transport are set, and it returns Error::InvalidInput when the own-transport list is missing or empty. with_stores(DeRecStores { .. }) sets the five stores in one call instead.
The event loop
deliver feeds bytes into a peer's process() and auto-accepts every ActionRequired. drive routes every queued message to the peer that serves its URI, repeating until nothing is in flight — that's all the "network" an in-process demo needs.
A production loop differs in two ways. It decides per ActionRequired whether to accept or reject (or configures with_auto_accept). And when process fails it does not panic: the ProcessError carries the events settled before the failure in events, which must be handled like a successful call's, and the failing channel in channel_id.
A request that arrives on an authenticated channel and then fails is not a process error: it is the RequestFailed event handled above, and the call's other events are kept. Rejecting each token exactly once is the application's part; a token nobody answers leaves its sender to time out. See RequestFailed.
The full handshake + protect round
What you should see
The long-term channel ids and the fingerprint are random, so yours differ. The relative order of Bob's and Carol's lines can vary between runs, because the channel store is a HashMap.
What this leaves out
The example above covers pairing through one publish round. The canonical fixture at smoke-tests/rust/src/protocol.rs extends it with:
HashedKeys and NoKeys pairing, including
verify_fingerprinton aPendingchannel;a
DeRecFlow::VerifySharesround, withShareVerified(which carriessubsharesandthreshold),ShareVerifyRejectedandVerifySharesFailed { reason: NoProof };DeRecFlow::Discoveryfollowed byDeRecFlow::RecoverSecret, theSecretRecoveredevent, andrestore(...)to rebuild the recovered device into an operational Owner — plus a Helper that lacks the requested version and a corrupted share;replica-mode pairings, Secret sync (
ReplicaSecretReceived,ReplicaSecretAcked),DeRecFlow::ReplicaDiscovery, and the group-key handover to a new member;DeRecFlow::Unpairunder the defaultUnpairAck::Required;DeRecFlow::UpdateChannelInfowith mid-flow endpoint changeover, andwith_auto_reply_to;with_auto_accept, parameter-range negotiation, and expired-channel cleanup.
Read it as the next step. Every flow follows the same start / process / accept triangle this example demonstrates.
Last updated