derec-library 0.0.6

Rust SDK for the DeRec protocol, including native and WebAssembly bindings.
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

use super::super::{
    DeRecChannelStore, DeRecEvent, DeRecSecretStore, DeRecShareStore, DeRecTransport,
    MissingPolicy, PendingAction, SecretKind, SecretValue,
};
use super::replicas::discovery as replica;
use crate::extensions::channel_store::ChannelStoreExt as _;
use crate::extensions::message_body::{MessageBodyExt as _, Route};
use crate::{
    Error, Result,
    derec_message::current_timestamp,
    primitives::discovery::{
        request,
        response::{self, SecretVersionEntry, VersionEntry},
    },
    protocol::{
        context::{Exchange, Local, Round},
        stores::{StoreSet, Stores},
        types::Target,
    },
    types::{ChannelId, SharedKey},
};
use derec_proto::{
    DeRecResult, GetSecretIdsVersionsRequestMessage, GetSecretIdsVersionsResponseMessage,
    MessageBody, SenderKind, StatusEnum, StoreShareRequestMessage,
};
use prost::Message;

#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(trace_id = exchange.trace_id, channel_id = exchange.channel_id.0))
)]
pub(in crate::protocol) async fn handle<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    exchange: &Exchange<'_>,
    inner: MessageBody,
) -> Result<Vec<DeRecEvent>> {
    let channel_id = exchange.channel_id;
    match (inner.route(), inner) {
        (Route::Replica(author), inner) => {
            replica::handle(stores, local, exchange, author, inner).await
        }
        (_, inner) => {
            let expected = match &inner {
                MessageBody::GetSecretIdsVersionsRequest(_) => SenderKind::Owner,
                _ => SenderKind::Helper,
            };
            stores
                .channels
                .require_role(local.secret_id, &[channel_id], expected)
                .await?;

            match inner {
                MessageBody::GetSecretIdsVersionsRequest(request) => on_request(exchange, request),
                MessageBody::GetSecretIdsVersionsResponse(response) => {
                    on_response(exchange, &response)
                }
                _ => Err(Error::Invariant(
                    "unexpected MessageBody variant in discovery handler",
                )),
            }
        }
    }
}

/// Dispatch a discovery request to each targeted channel.
///
/// The target is narrowed by [`Target::filter`] to channels this `secret_id`
/// actually holds — a caller-supplied id may name an unpaired channel, which
/// would otherwise trip the shared-key invariant below.
///
/// Dispatch failure is isolated per channel and surfaced as
/// `DiscoveryFailed` rather than short-circuiting the fan-out, so one
/// unreachable helper cannot suppress the rest.
#[cfg_attr(feature = "logging", tracing::instrument(skip_all, fields(trace_id = round.trace_id)))]
pub(in crate::protocol) async fn start<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    target: Target,
    round: &Round<'_>,
) -> Result<Vec<DeRecEvent>> {
    let known: Vec<ChannelId> = stores
        .channels
        .helpers_matching(
            local.secret_id,
            crate::protocol::types::HelperFilter {
                ids: target.ids(),
                ..Default::default()
            },
        )
        .await?
        .iter()
        .map(|ch| ch.channel_id)
        .collect();

    let channel_ids = target.filter(&known);

    let keys = stores
        .secrets
        .load_many(
            local.secret_id,
            &channel_ids,
            SecretKind::SharedKey,
            MissingPolicy::Fail,
        )
        .await?;

    let events = dispatch_all(stores, local, keys, round).await;

    #[cfg(feature = "logging")]
    tracing::info!("discovery requests dispatched");

    Ok(events)
}

/// Answer a peer's discovery request with the catalog of secrets held
/// for it across every linked channel.
///
/// Entries are grouped by `secret_id`, then keyed by `version`. A helper
/// holds exactly one share per `(secret_id, version)` — a second write
/// carrying different content is refused with `VERSION_CONFLICT` rather
/// than stored alongside — so a version identifies a share unambiguously
/// and needs no writer to disambiguate it.
#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(trace_id = exchange.trace_id, channel_id = exchange.channel_id.0))
)]
pub(in crate::protocol) async fn accept<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    exchange: &Exchange<'_>,
    request: &GetSecretIdsVersionsRequestMessage,
) -> Result<Vec<DeRecEvent>> {
    let channel_id = exchange.channel_id;
    let secret_id = local.secret_id;

    let linked_ids = stores
        .channels
        .linked_channels(secret_id, channel_id)
        .await?;
    let all_shares = stores.shares.load_all(secret_id, &linked_ids).await?;

    let secret_list = catalog(all_shares);

    let resp = response::produce(channel_id, &secret_list, exchange.shared_key)?;

    let envelope = crate::derec_message::apply_trace_id(&resp.envelope, exchange.trace_id)?;
    let endpoint = stores
        .channels
        .resolve_response_endpoints(
            secret_id,
            channel_id,
            &crate::extensions::advertised_endpoints::reply_to_owned(request),
        )
        .await?;
    stores.transport.send(&endpoint, envelope).await?;

    #[cfg(feature = "logging")]
    tracing::info!("discovery response sent");

    Ok(vec![DeRecEvent::NoOp])
}

#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(trace_id = exchange.trace_id, channel_id = exchange.channel_id.0))
)]
pub(in crate::protocol) async fn reject<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    exchange: &Exchange<'_>,
    request: &GetSecretIdsVersionsRequestMessage,
    status: StatusEnum,
    memo: &str,
) -> Result<()> {
    let response = GetSecretIdsVersionsResponseMessage {
        result: Some(DeRecResult {
            status: status as i32,
            memo: memo.to_owned(),
        }),
        secret_list: Vec::new(),
        timestamp: Some(current_timestamp()),
        // Answer on the path the request arrived on: a member asking gets a
        // member's answer, a helper exchange stays helper-bound.
        replica_id: local.replica_id.filter(|_| request.replica_id.is_some()),
    };

    crate::extensions::channel_store::send_channel_message(
        stores.channels,
        stores.transport,
        local.secret_id,
        exchange.channel_id,
        MessageBody::GetSecretIdsVersionsResponse(response),
        exchange.shared_key,
        exchange.trace_id,
        &crate::extensions::advertised_endpoints::reply_to_owned(request),
    )
    .await
}

#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(trace_id = exchange.trace_id, channel_id = exchange.channel_id.0))
)]
fn on_request(
    exchange: &Exchange<'_>,
    request: GetSecretIdsVersionsRequestMessage,
) -> Result<Vec<DeRecEvent>> {
    Ok(vec![DeRecEvent::ActionRequired {
        channel_id: exchange.channel_id,
        action: PendingAction::Discovery {
            channel_id: exchange.channel_id,
            request,
            shared_key: *exchange.shared_key,
            trace_id: exchange.trace_id,
        },
    }])
}

#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(trace_id = exchange.trace_id, channel_id = exchange.channel_id.0))
)]
fn on_response(
    exchange: &Exchange<'_>,
    response: &GetSecretIdsVersionsResponseMessage,
) -> Result<Vec<DeRecEvent>> {
    let result = response::process(response)?;

    #[cfg(feature = "logging")]
    tracing::info!(
        secrets_count = result.secret_list.len(),
        "secrets discovered"
    );

    Ok(vec![DeRecEvent::SecretsDiscovered {
        channel_id: exchange.channel_id,
        secrets: result.secret_list,
    }])
}

/// Send a request to every resolved channel, reporting each outcome.
///
/// Group a helper's stored shares into the per-secret version catalog a
/// discovery response reports.
///
/// One entry per secret, each listing every version held. Versions are
/// collected through a `BTreeMap` so the response is ordered and reproducible
/// rather than following store iteration order.
///
/// A version's description is read from the request that stored it. A share
/// whose bytes do not decode contributes an empty description rather than
/// failing the catalog: the version is still held, and refusing to report it
/// would hide state the peer needs to see. The first description wins, so a
/// re-store under the same version does not rewrite what was already reported.
fn catalog(all_shares: Vec<crate::protocol::types::Share>) -> Vec<SecretVersionEntry> {
    let mut secret_map: std::collections::HashMap<u64, std::collections::BTreeMap<u32, String>> =
        std::collections::HashMap::new();

    for share in all_shares {
        let description = StoreShareRequestMessage::decode(share.bytes.as_slice())
            .map(|msg| msg.version_description)
            .unwrap_or_default();
        secret_map
            .entry(share.secret_id)
            .or_default()
            .entry(share.version)
            .or_insert(description);
    }

    secret_map
        .into_iter()
        .map(|(secret_id, versions)| SecretVersionEntry {
            secret_id,
            versions: versions
                .into_iter()
                .map(|(version, description)| VersionEntry {
                    version,
                    description,
                })
                .collect(),
        })
        .collect()
}

/// One event per target, in the order the targets were resolved. A failure is
/// isolated to its own target: it becomes a `DiscoveryFailed` and the fan-out
/// continues, so one unreachable helper cannot suppress the rest.
async fn dispatch_all<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    keys: Vec<(ChannelId, SecretValue)>,
    round: &Round<'_>,
) -> Vec<DeRecEvent> {
    let mut events = Vec::with_capacity(keys.len());
    for (channel_id, value) in keys {
        let SecretValue::SharedKey(shared_key) = value else {
            events.push(DeRecEvent::DiscoveryFailed {
                channel_id,
                error: "channel has no shared key".to_owned(),
            });
            continue;
        };

        match dispatch_one(stores, local, channel_id, &shared_key, round).await {
            Ok(()) => {
                events.push(DeRecEvent::DiscoveryStarted {
                    channel_id,
                    trace_id: round.trace_id,
                });
                #[cfg(feature = "logging")]
                tracing::debug!(channel_id = channel_id.0, "discovery request sent");
            }
            Err(e) => {
                events.push(DeRecEvent::DiscoveryFailed {
                    channel_id,
                    error: e.to_string(),
                });
                #[cfg(feature = "logging")]
                tracing::warn!(
                    channel_id = channel_id.0,
                    error = %e,
                    "discovery request dispatch failed"
                );
            }
        }
    }
    events
}

async fn dispatch_one<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    channel_id: ChannelId,
    shared_key: &SharedKey,
    round: &Round<'_>,
) -> Result<()> {
    let endpoint = stores
        .channels
        .peer_endpoints(local.secret_id, channel_id)
        .await?;
    let msg = request::produce(channel_id, shared_key, round.reply_to)?;
    let envelope = crate::derec_message::apply_trace_id(&msg.envelope, round.trace_id)?;
    stores.transport.send(&endpoint, envelope).await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::types::Share;

    fn share(secret_id: u64, version: u32, description: &str) -> Share {
        Share {
            secret_id,
            version,
            bytes: StoreShareRequestMessage {
                version_description: description.to_owned(),
                ..Default::default()
            }
            .encode_to_vec(),
        }
    }

    /// Versions are reported in order regardless of the order the store
    /// returned them, so a peer diffing two catalogs sees a stable list.
    #[test]
    fn versions_are_reported_in_order() {
        let catalog = catalog(vec![
            share(1, 3, "third"),
            share(1, 1, "first"),
            share(1, 2, "second"),
        ]);

        let versions: Vec<u32> = catalog[0].versions.iter().map(|v| v.version).collect();
        assert_eq!(versions, vec![1, 2, 3]);
    }

    /// One entry per secret, each carrying only its own versions.
    #[test]
    fn each_secret_gets_its_own_entry() {
        let mut catalog = catalog(vec![share(1, 1, ""), share(2, 7, ""), share(1, 2, "")]);
        catalog.sort_by_key(|e| e.secret_id);

        assert_eq!(catalog.len(), 2);
        assert_eq!(catalog[0].secret_id, 1);
        assert_eq!(catalog[0].versions.len(), 2);
        assert_eq!(catalog[1].secret_id, 2);
        assert_eq!(catalog[1].versions.len(), 1);
    }

    /// A share whose bytes do not decode is still held, so it is still
    /// reported — with an empty description rather than not at all. Dropping
    /// it would hide a version the peer needs to know about.
    #[test]
    fn an_undecodable_share_is_still_catalogued() {
        let catalog = catalog(vec![Share {
            secret_id: 1,
            version: 4,
            bytes: vec![0xFF, 0xFF, 0xFF],
        }]);

        assert_eq!(catalog[0].versions[0].version, 4);
        assert_eq!(catalog[0].versions[0].description, "");
    }

    /// A re-store under a version already present keeps the first
    /// description, so the catalog does not flip between reports.
    #[test]
    fn the_first_description_for_a_version_wins() {
        let catalog = catalog(vec![share(1, 1, "original"), share(1, 1, "later")]);

        assert_eq!(catalog[0].versions.len(), 1);
        assert_eq!(catalog[0].versions[0].description, "original");
    }
}