1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
//! Higher-level protocol orchestrator for the DeRec protocol.
//!
//! This module provides [`DeRecProtocol`], a stateful orchestrator that wraps the
//! core protocol flows. The caller supplies concrete implementations of:
//!
//! - [`DeRecChannelStore`] — paired-channel record storage
//! - [`DeRecShareStore`] — secret share storage
//! - [`DeRecSecretStore`] — cryptographic key storage
//! - [`DeRecUserSecretStore`] — secret-snapshot storage for replica auto-publish
//! - [`DeRecTransport`] — outbound message delivery
//!
//! The application feeds incoming wire bytes to [`DeRecProtocol::process`] and
//! reacts to the returned [`DeRecEvent`] values. All routing, state persistence,
//! and reply sending are handled internally.
//!
//! # Flows
//!
//! Each `DeRecProtocol::start(DeRecFlow::…)` entry point drives one
//! protocol flow:
//!
//! - **Pairing** — establish a channel + derive a shared key with a peer.
//! - **ProtectSecret** (sharing) — VSS-split the current secret to every
//! paired Helper and ship the full secret to every paired Replica.
//! - **VerifyShares** — challenge a Helper to prove it still holds a
//! specific stored share via a SHA-384 commitment (see
//! [`StateItem::PendingVerification`](crate::protocol::types::StateItem)
//! for the orchestrator-owned request/response binding row in the
//! state store).
//! - **Discovery** — ask a Helper which `(secret_id, version)` tuples it
//! currently holds for us. Frequently the precursor to `RecoverSecret`
//! but useful for routine inventory too.
//! - **RecoverSecret** — collect enough Helper shares to reconstruct an
//! earlier secret version.
//! - **Restore** — [`DeRecProtocol::restore`] commits a recovered
//! [`crate::protocol::types::Secret`] into a fresh protocol instance:
//! reseats canonical helper / replica channels at the recovered
//! version and wipes the throwaway recovery-mode channels. Not a
//! [`DeRecFlow`] variant — called once, directly on the protocol,
//! after a `SecretRecovered` event surfaces.
//! - **UpdateChannelInfo** — broadcast updated `communication_info`
//! and/or `supported_transports` to one or more paired peers. Either
//! side may initiate. The accompanying setters
//! [`DeRecProtocol::set_communication_info`] and
//! [`DeRecProtocol::set_own_transports`] update local state first; the
//! flow then announces the change. The endpoint-changeover discipline
//! on `set_own_transports` is required reading before broadcasting a
//! transport update — both endpoints must remain reachable through
//! the changeover or in-flight traffic will be lost.
//! - **Unpair** — Owner-initiated channel teardown. Ack semantics are
//! governed by [`DeRecProtocolBuilder::with_unpair_ack`](crate::protocol::DeRecProtocolBuilder::with_unpair_ack).
//!
//! See [`DeRecFlow`] for the per-variant role requirements and field
//! semantics.
//!
//! # Reserved `CommunicationInfo` keys
//!
//! `CommunicationInfo` is an opaque app-defined string map *except* for
//! entries under the `derec.*` namespace, which the library reserves
//! for its own use (e.g. carrying the sender's `replica_id` on
//! replica-mode pairing envelopes). Apps SHOULD NOT use this namespace;
//! the orchestrator silently auto-injects, extracts, and strips
//! `derec.*` entries at the protocol boundary. See
//! [`utils::reserved_keys`] for the current set of keys and their wire
//! encoding.
pub
pub
pub
pub
pub
pub
pub
pub
pub
/// In-memory store and transport doubles shared by the unit tests
/// throughout this module. Lives at the `protocol` level because the
/// doubles implement the [`traits`] store interfaces and the rigs build
/// a whole [`DeRecProtocol`] — neither is specific to [`handlers`].
pub
pub use ;
use TransportProtocol;
pub use ;
use HashMap;
pub use ;
pub use ;
pub use ;
pub use ;
pub use RestoreError;
use cratenow_secs;
use cratenow_secs;
/// Internal state of the single secret container managed by the protocol.
///
/// Created on the first `ProtectSecret` flow and reused (with incrementing
/// version) on every subsequent call.
/// Higher-level DeRec protocol orchestrator.
///
/// Generic over:
/// - `ChannelStore` — paired channel storage ([`DeRecChannelStore`])
/// - `ShareStore` — share storage ([`DeRecShareStore`])
/// - `SecretStore` — secret storage ([`DeRecSecretStore`])
/// - `Transport` — outbound transport ([`DeRecTransport`])
///
/// The caller provides concrete implementations; the library imposes no
/// runtime or I/O requirements.
///
/// # Lifecycle
///
/// ```text
/// DeRecProtocolBuilder::new(secret_id).<setters>.build()?
/// │
/// ├── create_contact / start(Pairing) → pairing
/// ├── start(ProtectSecret) → sharing
/// ├── start(VerifyShares) → verification
/// ├── start(Pairing { Owner }) (recovery re-pair)
/// │ └── start(Discovery) → discovery (emits SecretsDiscovered)
/// ├── start(RecoverSecret) → recovery (emits SecretRecovered)
/// │ └── restore(&secret, version) → commit recovered secret into canonical state
/// ├── start(UpdateChannelInfo) → endpoint/info update (either side)
/// ├── start(Unpair) → unpair (Owner-initiated; ack
/// │ semantics governed by
/// │ [`DeRecProtocolBuilder::with_unpair_ack`](crate::protocol::DeRecProtocolBuilder::with_unpair_ack))
/// ├── start(ReplicaDiscovery) → replica catch-up (replica-only; asks the
/// │ group whether this device
/// │ is behind)
/// └── start(UnpairReplica) → replica removal (replica-only; voluntary
/// departure or eviction)
///
/// loop { process(incoming_bytes) → Vec<DeRecEvent> }
///
/// tick() → Vec<DeRecEvent> (time-driven only; no inbound message required)
/// ```
///
/// See [`DeRecFlow`] for the full set of orchestrator entry points
/// and the role each requires on the targeted channel(s).