derec-library 0.0.9

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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

//! The one response every authenticated inbound request is owed.
//!
//! A request that decrypted under a channel key came from the peer that
//! channel names, so it can be answered, and it is answered exactly once.
//! An [`Obligation`] is built for each such request: in `handlers::handle`
//! once the body is known to be a request, and again from a persisted
//! [`PendingAction`] in `accept` and `reject`. Every response to that request,
//! success or failure, goes out through [`deliver`], which marks the request
//! answered *before* handing the response to the transport. A transport error
//! after that is reported, never followed by a second response.
//!
//! [`settle`] is the single place a failure is turned into what the
//! application sees:
//!
//! - already answered: [`DeRecEvent::RequestFailed`] with `responded: true`;
//! - `auto_respond_on_failure` on: the failure response is sent, then
//!   `responded: true`;
//! - off, or on with no reply target resolvable before any send attempt:
//!   `responded: false` with a [`PendingAction::RespondToFailure`] the
//!   application passes to `reject`. No token is issued when `reject` could
//!   never answer it: no reply target can exist, or the channel whose key
//!   would encrypt the response has none stored.
//!
//! Exempt messages build no obligation and keep their behaviour: anything
//! that fails before decryption, an expired message (older than the
//! `inbound_message` window, which is the library's replay bound), every
//! response body, the pairing handshake, and a replica `UnpairRequest`, which
//! is a one-way notice.

use crate::derec_message::DeRecMessageBuilder;
use crate::extensions::channel_store::ChannelStoreExt as _;
use crate::protocol::context::Local;
use crate::protocol::events::{
    DeRecEvent, FailedRequest, PendingAction, PendingActionKind, ReplyTarget,
};
use crate::protocol::stores::{StoreSet, Stores};
use crate::protocol::{DeRecSecretStore as _, DeRecTransport, SecretKind, SecretValue};
use crate::transport::TransportPolicy;
use crate::types::{ChannelId, SharedKey};
use crate::{Error, Result};
use derec_proto::{MessageBody, StatusEnum, TransportProtocol};
use prost::Message as _;
use std::sync::Mutex;

/// The memo sent with a failure that has no protocol meaning of its own.
///
/// The local error text stays in the `message` of
/// [`DeRecEvent::RequestFailed`] and never reaches the wire.
pub(crate) const GENERIC_FAILURE_MEMO: &str = "request could not be processed";

/// The memo an authenticated message is answered with when its body could
/// not be decoded or is a request the channel cannot carry.
pub(crate) const UNDECODABLE_MEMO: &str = "message body could not be decoded";

/// One authenticated inbound request and the response it is owed.
pub(crate) struct Obligation {
    /// The channel the request arrived on.
    channel_id: ChannelId,
    trace_id: u64,
    request: FailedRequest,
    /// The request's `reply_to`, narrowed to what the transport policy allows.
    reply_to: Vec<TransportProtocol>,
    state: Mutex<State>,
}

struct State {
    /// The channel whose key encrypts the response.
    channel_id: ChannelId,
    /// That channel's key, when it is already known in memory. `None` loads
    /// it from the secret store at send time.
    key: Option<SharedKey>,
    /// The endpoints the response goes to, once a flow has named them.
    endpoints: Option<Vec<TransportProtocol>>,
    /// The status and memo of the response handed to the transport, once
    /// one was.
    sent: Option<(i32, String)>,
    /// The status and memo the request's failure carries a protocol meaning
    /// for, set by the flow that knows it. Unset means [`failure_status`]
    /// decides.
    known: Option<(StatusEnum, String)>,
    /// Events a flow reached before failing that the application must still
    /// see, such as a refusal whose answer could not be sent.
    carried: Vec<DeRecEvent>,
}

impl Obligation {
    fn new(
        channel_id: ChannelId,
        key: Option<SharedKey>,
        trace_id: u64,
        request: FailedRequest,
        policy: TransportPolicy,
    ) -> Self {
        let reply_to = crate::extensions::channel_store::filter_reply_to(
            policy,
            &requested_reply_to(&request),
        );
        Self {
            channel_id,
            trace_id,
            request,
            reply_to,
            state: Mutex::new(State {
                channel_id,
                key,
                endpoints: None,
                sent: None,
                known: None,
                carried: Vec::new(),
            }),
        }
    }

    /// The obligation a decrypted channel message carries, or `None` when the
    /// body is not a request owed a response.
    pub(crate) fn for_body(
        body: &MessageBody,
        channel_id: ChannelId,
        key: &SharedKey,
        trace_id: u64,
        policy: TransportPolicy,
    ) -> Option<Self> {
        let request = match body {
            MessageBody::StoreShareRequest(r) => FailedRequest::StoreShare(r.clone()),
            MessageBody::VerifyShareRequest(r) => FailedRequest::VerifyShare(r.clone()),
            MessageBody::GetSecretIdsVersionsRequest(r) => {
                FailedRequest::GetSecretIdsVersions(r.clone())
            }
            MessageBody::GetShareRequest(r) => FailedRequest::GetShare(r.clone()),
            MessageBody::UnpairRequest(r) if r.replica_id.is_none() => {
                FailedRequest::Unpair(r.clone())
            }
            MessageBody::UpdateChannelInfoRequest(r) => FailedRequest::UpdateChannelInfo(r.clone()),
            MessageBody::PairRequest(_) | MessageBody::PrePairRequest(_) => {
                FailedRequest::Undecodable
            }
            _ => return None,
        };
        Some(Self::new(channel_id, Some(*key), trace_id, request, policy))
    }

    /// The obligation of a message that decrypted under the channel key but
    /// whose body could not be decoded. Like a request the channel cannot
    /// carry, it is answered on the channel's recorded endpoints, echoing the
    /// envelope's `trace_id`.
    pub(crate) fn undecodable(
        channel_id: ChannelId,
        key: &SharedKey,
        trace_id: u64,
        policy: TransportPolicy,
    ) -> Self {
        Self::new(
            channel_id,
            Some(*key),
            trace_id,
            FailedRequest::Undecodable,
            policy,
        )
    }

    /// The obligation a persisted action still carries, or `None` for an
    /// action that answers no channel request: the pairing handshake, and a
    /// [`PendingAction::RespondToFailure`], which only `reject` resolves.
    pub(crate) fn for_action(action: &PendingAction, policy: TransportPolicy) -> Option<Self> {
        let (channel_id, key, trace_id, request) = match action {
            PendingAction::StoreShare {
                channel_id,
                request,
                shared_key,
                trace_id,
            } => (
                channel_id,
                shared_key,
                trace_id,
                FailedRequest::StoreShare(request.clone()),
            ),
            PendingAction::VerifyShare {
                channel_id,
                request,
                shared_key,
                trace_id,
            } => (
                channel_id,
                shared_key,
                trace_id,
                FailedRequest::VerifyShare(request.clone()),
            ),
            PendingAction::Discovery {
                channel_id,
                request,
                shared_key,
                trace_id,
            } => (
                channel_id,
                shared_key,
                trace_id,
                FailedRequest::GetSecretIdsVersions(request.clone()),
            ),
            PendingAction::GetShare {
                channel_id,
                request,
                shared_key,
                trace_id,
            } => (
                channel_id,
                shared_key,
                trace_id,
                FailedRequest::GetShare(request.clone()),
            ),
            PendingAction::Unpair {
                channel_id,
                request,
                shared_key,
                trace_id,
            } => (
                channel_id,
                shared_key,
                trace_id,
                FailedRequest::Unpair(request.clone()),
            ),
            PendingAction::UpdateChannelInfo {
                channel_id,
                request,
                shared_key,
                trace_id,
            } => (
                channel_id,
                shared_key,
                trace_id,
                FailedRequest::UpdateChannelInfo(request.clone()),
            ),
            PendingAction::Pairing { .. }
            | PendingAction::PrePair { .. }
            | PendingAction::RespondToFailure { .. } => return None,
        };
        Some(Self::new(
            *channel_id,
            Some(*key),
            *trace_id,
            request,
            policy,
        ))
    }

    /// The obligation a [`PendingAction::RespondToFailure`] token stands for.
    /// The key is loaded from `reply.channel_id` when the response is sent.
    pub(crate) fn for_failure(
        channel_id: ChannelId,
        trace_id: u64,
        reply: &ReplyTarget,
        request: FailedRequest,
        policy: TransportPolicy,
    ) -> Self {
        let obligation = Self::new(channel_id, None, trace_id, request, policy);
        {
            let mut state = obligation.lock();
            state.channel_id = reply.channel_id;
            if !reply.endpoints.is_empty() {
                state.endpoints = Some(reply.endpoints.clone());
            }
        }
        obligation
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, State> {
        self.state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    /// The request type this obligation answers.
    pub(crate) fn kind(&self) -> PendingActionKind {
        request_kind(&self.request)
    }

    /// The response goes to the policy-filtered `reply_to` when the request
    /// carried one, otherwise to `fallback`.
    ///
    /// Used by the replica flows once the member that sent the request is
    /// known, so a group channel is never answered through a helper lookup.
    pub(crate) fn answer_at(&self, fallback: &[TransportProtocol]) {
        let endpoints = if self.reply_to.is_empty() {
            fallback.to_vec()
        } else {
            self.reply_to.clone()
        };
        self.lock().endpoints = Some(endpoints);
    }

    /// The response is encrypted for `channel_id` under `key` rather than for
    /// the channel the request arrived on.
    pub(crate) fn encrypt_for(&self, channel_id: ChannelId, key: SharedKey) {
        let mut state = self.lock();
        state.channel_id = channel_id;
        state.key = Some(key);
    }

    /// Record that a response with `status` and `memo` is being handed to
    /// the transport.
    ///
    /// `false` when one already was: the caller must not send.
    fn claim(&self, status: StatusEnum, memo: &str) -> bool {
        let mut state = self.lock();
        if state.sent.is_some() {
            return false;
        }
        state.sent = Some((status as i32, memo.to_owned()));
        true
    }

    /// Answer this request's failure with `status` and `memo` instead of the
    /// mapping [`failure_status`] applies: the failure is a protocol rule the
    /// request broke, and the peer is told which.
    pub(crate) fn fail_with(&self, status: StatusEnum, memo: &str) {
        self.lock().known = Some((status, memo.to_owned()));
    }

    /// A refusal whose answer failed before it was handed to the transport:
    /// the router answers with the refusal's own status and memo, and
    /// `event` is reported ahead of the failure.
    fn carry_refusal(&self, status: StatusEnum, memo: &str, event: DeRecEvent) {
        let mut state = self.lock();
        state.known = Some((status, memo.to_owned()));
        state.carried.push(event);
    }

    /// The status and memo of the response already handed to the
    /// transport, if any.
    pub(crate) fn answered(&self) -> Option<(i32, String)> {
        self.lock().sent.clone()
    }

    /// The failure of a request whose response, with `sent`'s status and
    /// memo, was already attempted.
    fn failed_after_answer(&self, (status, memo): (i32, String), error: &Error) -> DeRecEvent {
        request_failed(
            self.channel_id,
            self.kind(),
            true,
            status,
            &memo,
            error,
            None,
        )
    }

    /// Where the response goes, with the key that encrypts it.
    ///
    /// `None` when no reply target can exist: the request arrived on a
    /// replica group channel, names no member and carries no `reply_to`, so
    /// nothing identifies which member to answer. A group channel is never
    /// resolved through the helper-channel lookup.
    async fn resolve<S: StoreSet>(
        &self,
        stores: &mut Stores<'_, S>,
        local: &Local<'_>,
    ) -> Result<Option<Target>> {
        let (channel_id, key, endpoints) = {
            let state = self.lock();
            (state.channel_id, state.key, state.endpoints.clone())
        };
        let endpoints = match endpoints {
            Some(endpoints) if !endpoints.is_empty() => endpoints,
            _ if !self.reply_to.is_empty() => self.reply_to.clone(),
            // A member is answered on its own recorded endpoints, looked up
            // by the identity it named on the channel it wrote on — never
            // through a helper-channel lookup, which a group channel has no
            // row for.
            _ if let Some(author) = replica_author(&self.request) => {
                stores
                    .channels
                    .load_replica_member(local.secret_id, self.channel_id, author)
                    .await?
                    .transports
            }
            _ if self.arrived_on_group(stores, local).await? => return Ok(None),
            _ => {
                stores
                    .channels
                    .peer_endpoints(local.secret_id, channel_id)
                    .await?
            }
        };
        let key = match key {
            Some(key) => key,
            None => match stores
                .secrets
                .load(local.secret_id, channel_id, SecretKind::SharedKey)
                .await?
            {
                Some(SecretValue::SharedKey(key)) => key,
                _ => {
                    return Err(Error::InvalidInput(
                        "channel has no shared key — not yet paired",
                    ));
                }
            },
        };
        Ok(Some((channel_id, endpoints, key)))
    }

    /// Whether the request arrived on a replica group channel.
    async fn arrived_on_group<S: StoreSet>(
        &self,
        stores: &mut Stores<'_, S>,
        local: &Local<'_>,
    ) -> Result<bool> {
        Ok(stores
            .channels
            .replicas_matching(
                local.secret_id,
                crate::protocol::types::ReplicaFilter::default(),
            )
            .await?
            .iter()
            .any(|member| member.channel_id == self.channel_id))
    }

    /// The target a token carries when none could be resolved: the channel
    /// and whatever endpoints are known. An empty list is resolved again
    /// when the token is rejected: the request's `reply_to`, then the
    /// sending member's recorded endpoints for a replica-bound request, or
    /// the channel's recorded endpoints for a helper-bound one.
    fn unresolved_target(&self) -> ReplyTarget {
        let state = self.lock();
        ReplyTarget {
            channel_id: state.channel_id,
            endpoints: state.endpoints.clone().unwrap_or_default(),
        }
    }
}

/// The channel whose key encrypts a response, the endpoints it goes to, and
/// that key.
type Target = (ChannelId, Vec<TransportProtocol>, SharedKey);

/// Hand `envelope` to the transport as the response `obligation` is owed.
/// `status` and `memo` are the result the envelope's response carries.
///
/// The request is marked answered first, so a transport error leaves it
/// answered. A request that already was is not sent again. Without an
/// obligation the envelope is sent as is.
pub(crate) async fn deliver<T: DeRecTransport>(
    obligation: Option<&Obligation>,
    transport: &T,
    endpoints: &[TransportProtocol],
    envelope: Vec<u8>,
    status: StatusEnum,
    memo: &str,
) -> Result<()> {
    if let Some(obligation) = obligation
        && !obligation.claim(status, memo)
    {
        return Ok(());
    }
    transport.send(endpoints, envelope).await
}

/// Whether a response to `obligation` was handed to the transport.
pub(crate) fn attempted(obligation: Option<&Obligation>) -> bool {
    obligation.is_some_and(|o| o.answered().is_some())
}

/// The events of a flow whose response was attempted, with `outcome`'s
/// failure appended as answered.
///
/// A failure before any response was attempted is returned as is, for the
/// router to answer.
pub(crate) fn reported(
    obligation: Option<&Obligation>,
    mut events: Vec<DeRecEvent>,
    outcome: Result<()>,
) -> Result<Vec<DeRecEvent>> {
    match (
        outcome,
        obligation.and_then(|o| o.answered().map(|sent| (o, sent))),
    ) {
        (Ok(()), _) => Ok(events),
        (Err(error), Some((obligation, sent))) => {
            events.push(obligation.failed_after_answer(sent, &error));
            Ok(events)
        }
        (Err(error), None) => Err(error),
    }
}

/// The outcome of a refusal: `event` once its answer was handed to the
/// transport, with a later failure appended as answered.
///
/// A failure before that keeps the refusal: the router answers with
/// `status` and `memo` and reports `event` ahead of the failure, so the
/// application learns of the refusal either way.
pub(crate) fn refused(
    obligation: Option<&Obligation>,
    event: DeRecEvent,
    status: StatusEnum,
    memo: &str,
    outcome: Result<()>,
) -> Result<Vec<DeRecEvent>> {
    match (outcome, obligation) {
        (Ok(()), _) => Ok(vec![event]),
        (Err(error), Some(obligation)) if let Some(sent) = obligation.answered() => {
            Ok(vec![event, obligation.failed_after_answer(sent, &error)])
        }
        (Err(error), Some(obligation)) => {
            obligation.carry_refusal(status, memo, event);
            Err(error)
        }
        (Err(error), None) => Err(error),
    }
}

/// Turn a flow's outcome into the events the application sees, answering a
/// failure once.
///
/// Never fails: an answerable failure is reported as
/// [`DeRecEvent::RequestFailed`] inside the returned events.
pub(crate) async fn settle<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    obligation: &Obligation,
    auto_respond_on_failure: bool,
    outcome: Result<Vec<DeRecEvent>>,
) -> Vec<DeRecEvent> {
    let error = match outcome {
        Ok(events) => return events,
        Err(error) => error,
    };
    let (known, mut events) = {
        let mut state = obligation.lock();
        (state.known.take(), std::mem::take(&mut state.carried))
    };
    if let Some(sent) = obligation.answered() {
        events.push(obligation.failed_after_answer(sent, &error));
        return events;
    }

    let (status, memo) = match (&known, &obligation.request) {
        (Some((status, memo)), _) => (*status, memo.as_str()),
        (None, FailedRequest::Undecodable) => (StatusEnum::FormatError, UNDECODABLE_MEMO),
        (None, _) => failure_status(&error),
    };
    let target = match obligation.resolve(stores, local).await {
        Ok(None) => {
            events.push(request_failed(
                obligation.channel_id,
                obligation.kind(),
                false,
                status as i32,
                memo,
                &error,
                None,
            ));
            return events;
        }
        Ok(Some(target)) => Ok(target),
        Err(error) => Err(error),
    };

    if auto_respond_on_failure && let Ok(target) = &target {
        let _sent = send_failure(stores, local, obligation, target, status, memo).await;
        if obligation.answered().is_some() {
            #[cfg(feature = "logging")]
            if let Err(send_error) = &_sent {
                tracing::warn!(
                    channel_id = obligation.channel_id.0,
                    error = %send_error,
                    "the failure response could not be delivered; it is not sent again"
                );
            }
            events.push(request_failed(
                obligation.channel_id,
                obligation.kind(),
                true,
                status as i32,
                memo,
                &error,
                None,
            ));
            return events;
        }
    }

    let reply = match target {
        Ok((channel_id, endpoints, _)) => ReplyTarget {
            channel_id,
            endpoints,
        },
        Err(_) => obligation.unresolved_target(),
    };
    // `reject` loads the key from the reply channel, so a token naming a
    // channel with no stored key could never be answered and is not issued.
    let token = has_stored_key(stores, local, reply.channel_id)
        .await
        .then(|| PendingAction::RespondToFailure {
            channel_id: obligation.channel_id,
            trace_id: obligation.trace_id,
            reply,
            request: obligation.request.clone(),
        });
    events.push(request_failed(
        obligation.channel_id,
        obligation.kind(),
        false,
        status as i32,
        memo,
        &error,
        token,
    ));
    events
}

/// Whether the secret store holds a shared key for `channel_id`.
///
/// A store read that fails is not proof of a missing key: the token is kept,
/// and `reject` reports the read again if it still fails.
async fn has_stored_key<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    channel_id: ChannelId,
) -> bool {
    match stores
        .secrets
        .load(local.secret_id, channel_id, SecretKind::SharedKey)
        .await
    {
        Ok(Some(SecretValue::SharedKey(_))) | Err(_) => true,
        Ok(_) => false,
    }
}

/// Answer the request `obligation` stands for with `status` and `memo`, using
/// that request type's own response.
pub(crate) async fn respond_to_failure<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    obligation: &Obligation,
    status: StatusEnum,
    memo: &str,
) -> Result<()> {
    let target = obligation
        .resolve(stores, local)
        .await?
        .ok_or(Error::InvalidInput(
            "the request names no member of the group channel it arrived on",
        ))?;
    send_failure(stores, local, obligation, &target, status, memo).await
}

async fn send_failure<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    obligation: &Obligation,
    (channel_id, endpoints, key): &Target,
    status: StatusEnum,
    memo: &str,
) -> Result<()> {
    use crate::extensions::message_body::MessageBodyExt as _;

    let mut body = failure_body(local, &obligation.request, status, memo);
    let timestamp = crate::derec_message::current_timestamp();
    body.set_timestamp(timestamp);
    let envelope = DeRecMessageBuilder::channel()
        .channel_id(*channel_id)
        .timestamp(timestamp)
        .message_body(body)
        .trace_id(obligation.trace_id)
        .encrypt(key)?
        .build()?
        .encode_to_vec();
    deliver(
        Some(obligation),
        stores.transport,
        endpoints,
        envelope,
        status,
        memo,
    )
    .await
}

/// The response `request`'s type answers a failure with. A body that could
/// not be decoded or a request the channel cannot carry names no request
/// type the channel answers, so it is answered with an
/// `ErrorResponseMessage`.
fn failure_body(
    local: &Local<'_>,
    request: &FailedRequest,
    status: StatusEnum,
    memo: &str,
) -> MessageBody {
    use crate::protocol::handlers;
    match request {
        FailedRequest::StoreShare(r) => handlers::sharing::failure_response(local, r, status, memo),
        FailedRequest::VerifyShare(r) => handlers::verification::failure_response(r, status, memo),
        FailedRequest::GetSecretIdsVersions(r) => {
            handlers::discovery::failure_response(local, r, status, memo)
        }
        FailedRequest::GetShare(r) => handlers::recovery::failure_response(local, r, status, memo),
        FailedRequest::Unpair(_) => handlers::unpairing::failure_response(status, memo),
        FailedRequest::UpdateChannelInfo(_) => {
            handlers::update_channel_info::failure_response(status, memo)
        }
        FailedRequest::Undecodable => {
            MessageBody::ErrorResponse(derec_proto::ErrorResponseMessage {
                result: Some(derec_proto::DeRecResult {
                    status: status as i32,
                    memo: memo.to_owned(),
                }),
                timestamp: None,
            })
        }
    }
}

/// The status and memo a failure is answered with.
///
/// A failure with a protocol meaning keeps its own status and memo. Anything
/// else — a store backend, an internal invariant, a primitive — is `FAIL`
/// with [`GENERIC_FAILURE_MEMO`], so no local error text reaches the peer.
pub(crate) fn failure_status(error: &Error) -> (StatusEnum, &'static str) {
    match error {
        Error::RoleMismatch { .. } => (
            StatusEnum::Rejected,
            "the sender's role does not permit this request",
        ),
        Error::NoUsableEndpoint { .. } => (
            StatusEnum::UnsupportedTransportProtocol,
            "no transport endpoint in the request is supported",
        ),
        Error::Transport(_) => (
            StatusEnum::UnsupportedTransportProtocol,
            "a transport endpoint in the request was refused",
        ),
        _ => (StatusEnum::Fail, GENERIC_FAILURE_MEMO),
    }
}

/// A [`DeRecEvent::RequestFailed`] for `error`, answered (or to be
/// answered) with `status` and `memo`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn request_failed(
    channel_id: ChannelId,
    kind: PendingActionKind,
    responded: bool,
    status: i32,
    memo: &str,
    error: &Error,
    action: Option<PendingAction>,
) -> DeRecEvent {
    DeRecEvent::RequestFailed {
        channel_id,
        action_kind: kind,
        responded,
        status,
        memo: memo.to_owned(),
        error_code: crate::error_codes::classify(error).1.name().to_owned(),
        message: error.to_string(),
        action,
    }
}

fn request_kind(request: &FailedRequest) -> PendingActionKind {
    match request {
        FailedRequest::StoreShare(_) => PendingActionKind::StoreShare,
        FailedRequest::VerifyShare(_) => PendingActionKind::VerifyShare,
        FailedRequest::GetSecretIdsVersions(_) => PendingActionKind::Discovery,
        FailedRequest::GetShare(_) => PendingActionKind::GetShare,
        FailedRequest::Unpair(_) => PendingActionKind::Unpair,
        FailedRequest::UpdateChannelInfo(_) => PendingActionKind::UpdateChannelInfo,
        FailedRequest::Undecodable => PendingActionKind::Failed,
    }
}

/// The member that sent a replica-bound request, which is answered on that
/// member's own endpoints and never through a helper-channel lookup.
fn replica_author(request: &FailedRequest) -> Option<u64> {
    match request {
        FailedRequest::StoreShare(r) => r.replica_id,
        FailedRequest::GetSecretIdsVersions(r) => r.replica_id,
        FailedRequest::GetShare(r) => r.replica_id,
        // A replica `UnpairRequest` is a one-way notice and builds no
        // obligation, so an unpair here is always helper-bound.
        FailedRequest::Unpair(_)
        | FailedRequest::VerifyShare(_)
        | FailedRequest::UpdateChannelInfo(_)
        | FailedRequest::Undecodable => None,
    }
}

fn requested_reply_to(request: &FailedRequest) -> Vec<TransportProtocol> {
    use crate::extensions::advertised_endpoints::reply_to_owned;
    match request {
        FailedRequest::StoreShare(r) => reply_to_owned(r),
        FailedRequest::VerifyShare(r) => reply_to_owned(r),
        FailedRequest::GetSecretIdsVersions(r) => reply_to_owned(r),
        FailedRequest::GetShare(r) => reply_to_owned(r),
        FailedRequest::Unpair(r) => reply_to_owned(r),
        FailedRequest::UpdateChannelInfo(_) | FailedRequest::Undecodable => Vec::new(),
    }
}