derec-library 0.0.7

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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

//! Inbound message processing.
//!
//! [`DeRecProtocol::process`] is the single entry point for anything arriving
//! off a transport. It decides whether a message is still fresh, which of the
//! two dispatch paths it takes — encrypted channel traffic, or the plaintext
//! pairing handshake — and hands it to [`handlers`](super::handlers).

use super::context::{local, pairing_config};
use super::error::ProcessError;
use super::events::DeRecEvent;
use super::stores::borrow_stores;
use super::traits::{
    DeRecChannelStore, DeRecSecretStore, DeRecShareStore, DeRecStateStore, DeRecTransport,
    DeRecUserSecretStore,
};
use super::types::{SecretKind, SecretValue};
use super::{DeRecProtocol, handlers};
use crate::extensions::channel_store::ChannelStoreExt as _;
use crate::{Error, Result, types::ChannelId};
use derec_proto::DeRecMessage;
use prost::Message;

#[cfg(target_arch = "wasm32")]
use crate::interop::wasm::now_secs;
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::now_secs;

impl<
    Ch: DeRecChannelStore,
    Sh: DeRecShareStore,
    Ss: DeRecSecretStore,
    Us: DeRecUserSecretStore,
    T: DeRecTransport,
    St: DeRecStateStore,
> DeRecProtocol<Ch, Sh, Ss, Us, St, T>
{
    /// Feed any incoming wire bytes here regardless of which flow they belong to.
    ///
    /// The library:
    ///
    /// 1. Decodes the outer [`DeRecMessage`] envelope to read `channel_id`
    /// 2. Looks up the channel's key material to determine the message kind
    /// 3. Dispatches to the appropriate message handler based on the channel state
    /// 4. Returns the events the application should react to
    ///
    /// # Expired channel cleanup
    ///
    /// Each call first removes `Pending` channels that have exceeded the
    /// configured
    /// [`ExpiredChannelCleanup`](crate::protocol::ExpiredChannelCleanup)
    /// timeout, along with their pairing keys. Removal is **lazy**: it
    /// happens only when this function runs, so an idle node retains
    /// expired channels until the next inbound message arrives. The
    /// removed ids are discarded on this path — call
    /// [`remove_expired_channels`](Self::remove_expired_channels)
    /// directly to observe them.
    ///
    /// # Auto-accept
    ///
    /// Any [`DeRecEvent::ActionRequired`] whose action kind the configured
    /// [`AutoAcceptPolicy`](crate::protocol::AutoAcceptPolicy) opts into is replaced in place with
    /// `AutoAccepted` plus the events a manual
    /// [`accept`](Self::accept) would have produced. A failure while
    /// accepting surfaces as [`ProcessError`], the same way a manual accept
    /// would reach the caller.
    ///
    /// # Event ordering
    ///
    /// Timeout events come first — sharing-round before unpair — then the
    /// events this message produced. An application reading the returned
    /// list in order therefore sees deadlines settle before new work.
    ///
    /// A failed call still reports what it produced before failing, in
    /// [`ProcessError::events`]: the timeout events at least, and every
    /// event of the message when only the follow-up auto-publish failed.
    /// Those deadlines are already settled and are never reported again.
    ///
    /// # Security: bounding inbound message size
    ///
    /// This function does **not** enforce an upper bound on `message.len()`,
    /// and no library entry point that ingests peer wire bytes does either.
    /// Legitimate envelopes span many orders of magnitude:
    ///
    /// - Tens of bytes for empty acks / ping-class messages.
    /// - A few KB for pairing material and verification proofs.
    /// - Hundreds of KB to several MB for `StoreShareRequest` carrying a
    ///   share of a large secret.
    /// - Many MB for `ReplicaSync` envelopes carrying an entire secret
    ///   (`O(num_secrets × num_helpers × max_secret_bytes)`).
    ///
    /// Any cap tight enough to provide meaningful DoS resistance would risk
    /// silently truncating a legitimate replica sync — at which point the
    /// secret can become unrecoverable. The protocol therefore delegates
    /// inbound-size bounding to the **application's transport layer**,
    /// which knows the deployment's max secret size, helper count, and
    /// replica fan-out and can pick a ceiling that fits.
    ///
    /// Callers MUST refuse oversized envelopes upstream (e.g. enforce a
    /// max HTTP body / WebSocket frame size consistent with their
    /// configuration) before handing bytes to this function.
    ///
    /// Malformed bytes — including truncation, varint overflow, and any
    /// `prost`-level decode failure — surface as
    /// [`ProcessError`] wrapping [`Error::ProtobufDecode`]. This function
    /// never panics on adversarial input. Protobuf recursion depth is
    /// bounded by `prost`'s decoder; DeRec's schema is shallow (~3 levels),
    /// so no additional caller-side recursion limit is required.
    #[cfg_attr(
        feature = "logging",
        tracing::instrument(
            skip_all,
            fields(trace_id = tracing::field::Empty, message_len = message.len())
        )
    )]
    pub async fn process(
        &mut self,
        message: &[u8],
    ) -> std::result::Result<Vec<DeRecEvent>, ProcessError> {
        let mut events = self.run_timeout_sweeps().await;

        let handled = self.handle_message(message).await;
        let channel_id = match handled {
            Ok((channel_id, mut message_events)) => {
                events.append(&mut message_events);
                channel_id
            }
            Err((channel_id, source)) => {
                self.update_sharing_round(&mut events).await;
                return Err(ProcessError {
                    channel_id,
                    source,
                    events,
                });
            }
        };

        self.update_sharing_round(&mut events).await;

        match self.maybe_auto_publish_after_pair(&events).await {
            Ok(auto_publish_events) => {
                events.extend(auto_publish_events);
                Ok(events)
            }
            Err(source) => Err(ProcessError {
                channel_id: Some(channel_id),
                source,
                events,
            }),
        }
    }

    async fn handle_message(
        &mut self,
        message: &[u8],
    ) -> std::result::Result<(ChannelId, Vec<DeRecEvent>), (Option<ChannelId>, Error)> {
        let envelope =
            DeRecMessage::decode(message).map_err(|e| (None, Error::ProtobufDecode(e)))?;
        let channel_id = ChannelId(envelope.channel_id);
        #[cfg(feature = "logging")]
        tracing::Span::current().record("trace_id", envelope.trace_id);

        let events = self
            .process_inner(&envelope, channel_id)
            .await
            .map_err(|source| (Some(channel_id), source))?;
        let events = self
            .apply_auto_accept(events)
            .await
            .map_err(|source| (Some(channel_id), source))?;
        Ok((channel_id, events))
    }

    async fn is_pending_verification(&self, channel_id: ChannelId) -> Result<bool> {
        use crate::protocol::types::{ChannelQuery, ChannelStatus, ReplicaFilter};

        if let Some(record) = self
            .channel_store
            .load(self.secret_id, ChannelQuery::Helper { channel_id })
            .await?
            && record.status() == ChannelStatus::Pending
        {
            return Ok(true);
        }

        Ok(self
            .channel_store
            .replicas_matching(
                self.secret_id,
                ReplicaFilter {
                    status: vec![ChannelStatus::Pending],
                    ..Default::default()
                },
            )
            .await?
            .iter()
            .any(|member| member.channel_id == channel_id))
    }

    pub(super) fn is_message_expired(
        &self,
        envelope: &DeRecMessage,
        #[cfg_attr(not(feature = "logging"), allow(unused))] channel_id: ChannelId,
    ) -> bool {
        let Some(ts) = &envelope.timestamp else {
            return false;
        };
        let msg_secs = ts.seconds as u64;
        let now = now_secs();
        let age = now.saturating_sub(msg_secs);
        if age > self.timeouts.inbound_message.as_secs() {
            #[cfg(feature = "logging")]
            tracing::warn!(
                channel_id = channel_id.0,
                message_age_secs = age,
                timeout_secs = self.timeouts.inbound_message.as_secs(),
                "message discarded — older than configured timeout"
            );
            return true;
        }
        false
    }

    async fn process_inner(
        &mut self,
        message: &DeRecMessage,
        channel_id: ChannelId,
    ) -> Result<Vec<DeRecEvent>> {
        if self.is_message_expired(message, channel_id) {
            return Ok(vec![DeRecEvent::MessageIgnored {
                channel_id,
                reason: crate::protocol::IgnoreReason::Expired,
                trace_id: message.trace_id,
            }]);
        }

        if let Some(events) = self.process_channel_message(message, channel_id).await? {
            return Ok(events);
        }

        if let Some(events) = self.process_pairing_message(message, channel_id).await? {
            return Ok(events);
        }

        #[cfg(feature = "logging")]
        tracing::warn!(channel_id = channel_id.0, "no key material for channel");

        Err(Error::InvalidInput(
            "unknown channel_id: no shared key or pairing secret found",
        ))
    }

    async fn process_channel_message(
        &mut self,
        message: &DeRecMessage,
        channel_id: ChannelId,
    ) -> Result<Option<Vec<DeRecEvent>>> {
        let Some(SecretValue::SharedKey(shared_key)) = self
            .secret_store
            .load(self.secret_id, channel_id, SecretKind::SharedKey)
            .await?
        else {
            return Ok(None);
        };

        if self.is_pending_verification(channel_id).await? {
            #[cfg(feature = "logging")]
            tracing::warn!(
                channel_id = channel_id.0,
                "message ignored — channel is pending fingerprint verification"
            );
            return Ok(Some(vec![DeRecEvent::MessageIgnored {
                channel_id,
                reason: crate::protocol::IgnoreReason::PendingVerification,
                trace_id: message.trace_id,
            }]));
        }

        let events = handlers::handle(
            &mut borrow_stores!(self),
            &local!(self),
            message,
            channel_id,
            &shared_key,
            self.auto_respond_on_failure,
        )
        .await?;

        Ok(Some(events))
    }

    /// Dispatch a pairing message, trying the plaintext leg before the
    /// encrypted one.
    ///
    /// `PrePair` envelopes carry a serialized `MessageBody` directly — no
    /// shared or asymmetric key exists yet, so there is nothing to encrypt
    /// under — and therefore decode without any crypto material. ECIES
    /// ciphertext for the regular `Pair` flow will not realistically decode to
    /// a valid `PrePair*` variant; if it ever did, it falls through to the
    /// encrypted path.
    ///
    /// A `PrePairRequest` is routed only when this device holds a correlation
    /// record for the channel, and either kind will do:
    /// [`SecretKind::PairingSecret`](crate::protocol::SecretKind::PairingSecret) from a `HashedKeys` contact, whose keys
    /// the accept path publishes, or [`SecretKind::PairingContact`](crate::protocol::SecretKind::PairingContact) from a
    /// `NoKeys` one, which has no keys until the accept path generates them.
    /// Holding neither means the channel is unknown, and the message is
    /// dropped silently. A `PrePairResponse` needs the stored contact
    /// specifically, to check the keys against its binding hash.
    async fn process_pairing_message(
        &mut self,
        message: &DeRecMessage,
        channel_id: ChannelId,
    ) -> Result<Option<Vec<DeRecEvent>>> {
        use derec_proto::MessageBody;

        if let Ok(inner) = crate::derec_message::extract_inner_plaintext_message(&message.message) {
            match inner {
                MessageBody::PrePairRequest(request) => {
                    let has_pairing_secret = matches!(
                        self.secret_store
                            .load(self.secret_id, channel_id, SecretKind::PairingSecret)
                            .await?,
                        Some(SecretValue::PairingSecret(_))
                    );
                    let has_pairing_contact = matches!(
                        self.secret_store
                            .load(self.secret_id, channel_id, SecretKind::PairingContact)
                            .await?,
                        Some(SecretValue::PairingContact(_))
                    );
                    if !has_pairing_secret && !has_pairing_contact {
                        return Ok(None);
                    }
                    let events = handlers::pairing::pre_pair::on_request(
                        channel_id,
                        &request,
                        message.trace_id,
                    )?;
                    return Ok(Some(events));
                }
                MessageBody::PrePairResponse(resp) => {
                    let Some(SecretValue::PairingContact(contact)) = self
                        .secret_store
                        .load(self.secret_id, channel_id, SecretKind::PairingContact)
                        .await?
                    else {
                        return Ok(None);
                    };
                    let events = handlers::pairing::pre_pair::on_response(
                        &mut borrow_stores!(self),
                        &local!(self),
                        &pairing_config!(self),
                        channel_id,
                        &contact,
                        &resp,
                    )
                    .await?;
                    return Ok(Some(events));
                }
                _ => {} // Fall through to the encrypted Pair path.
            }
        }

        let Some(SecretValue::PairingSecret(pairing_secret)) = self
            .secret_store
            .load(self.secret_id, channel_id, SecretKind::PairingSecret)
            .await?
        else {
            return Ok(None);
        };
        let pairing_secret = pairing_secret.to_secret()?;

        let events = handlers::handle_pairing(
            &mut borrow_stores!(self),
            &local!(self),
            &pairing_config!(self),
            message,
            channel_id,
            &pairing_secret,
        )
        .await?;
        Ok(Some(events))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::builder::DeRecProtocolBuilder;
    use crate::protocol::test::{
        InMemChannelStore, InMemSecretStore, InMemShareStore, InMemStateStore,
        InMemUserSecretStore, RecordingTransport, run_async,
    };
    use crate::protocol::types::{ChannelRecord, ChannelStatus, HelperChannel, SecretValue};

    const SECRET_ID: u64 = 0xAF;
    const CHANNEL: ChannelId = ChannelId(808);
    const SHARED_KEY: crate::types::SharedKey = [9u8; 32];

    fn endpoint() -> derec_proto::TransportProtocol {
        derec_proto::TransportProtocol {
            uri: "https://peer.example.com/derec".to_owned(),
            protocol: derec_proto::Protocol::Https as i32,
        }
    }

    /// A paired channel with the peer in the **Helper** role, so an inbound
    /// request that requires an `Owner` peer fails the role gate. That is a
    /// post-decrypt failure: authenticated, and therefore answerable.
    async fn seed(channels: &mut InMemChannelStore, secrets: &mut InMemSecretStore) {
        channels
            .save(
                SECRET_ID,
                ChannelRecord::Helper(HelperChannel {
                    channel_id: CHANNEL,
                    transports: vec![endpoint()],
                    communication_info: std::collections::HashMap::new(),
                    status: ChannelStatus::Paired,
                    created_at: now_secs(),
                    peer_role: derec_proto::SenderKind::Helper,
                }),
            )
            .await
            .expect("seed channel");
        secrets
            .save(SECRET_ID, CHANNEL, SecretValue::SharedKey(SHARED_KEY))
            .await
            .expect("seed shared key");
    }

    fn build(
        channels: InMemChannelStore,
        secrets: InMemSecretStore,
        transport: RecordingTransport,
        auto_respond: bool,
    ) -> DeRecProtocol<
        InMemChannelStore,
        InMemShareStore,
        InMemSecretStore,
        InMemUserSecretStore,
        InMemStateStore,
        RecordingTransport,
    > {
        DeRecProtocolBuilder::new(SECRET_ID)
            .with_channel_store(channels)
            .with_share_store(InMemShareStore::default())
            .with_secret_store(secrets)
            .with_user_secret_store(InMemUserSecretStore::default())
            .with_transport(transport)
            .with_state_store(InMemStateStore)
            .with_own_transports(["https://owner.example.com"])
            .with_threshold(2)
            .with_auto_respond_on_failure(auto_respond)
            .build()
            .expect("test protocol builds")
    }

    /// An inbound `VerifyShareResponse` from a peer recorded as a Helper
    /// fails the role gate — a response is only legal from a Helper, so the
    /// request direction is what this channel refuses.
    fn failing_request() -> Vec<u8> {
        let timestamp = crate::derec_message::current_timestamp();
        crate::derec_message::DeRecMessageBuilder::channel()
            .channel_id(CHANNEL)
            .timestamp(timestamp)
            .message_body(derec_proto::MessageBody::VerifyShareRequest(
                derec_proto::VerifyShareRequestMessage {
                    secret_id: SECRET_ID,
                    version: 1,
                    nonce: 7,
                    timestamp: Some(timestamp),
                    reply_to_transports: Vec::new(),
                },
            ))
            .encrypt(&SHARED_KEY)
            .expect("encrypt")
            .build()
            .expect("build")
            .encode_to_vec()
    }

    /// Disabled is the default and stays silent: the failure is the caller's
    /// to act on, and the peer is told nothing.
    #[test]
    fn disabled_sends_nothing_and_still_errors() {
        run_async(async {
            let channels = InMemChannelStore::default();
            let secrets = InMemSecretStore::default();
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed(&mut c, &mut s).await;

            let mut protocol = build(channels, secrets, transport.clone(), false);
            let result = protocol.process(&failing_request()).await;

            assert!(result.is_err(), "the failure must reach the caller");
            assert!(
                transport.sent_envelopes().is_empty(),
                "nothing may go on the wire when the setting is off"
            );
        });
    }

    /// Enabled tells the peer as well — and still returns the same error, so
    /// the caller's handling does not change with the setting.
    #[test]
    fn enabled_sends_a_failure_response_and_still_errors() {
        run_async(async {
            let channels = InMemChannelStore::default();
            let secrets = InMemSecretStore::default();
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed(&mut c, &mut s).await;

            let mut protocol = build(channels, secrets, transport.clone(), true);
            let result = protocol.process(&failing_request()).await;

            assert!(
                result.is_err(),
                "the caller still gets the error that mattered"
            );

            let sent = transport.sent_envelopes();
            assert_eq!(sent.len(), 1, "exactly one failure response");

            // It is a real, decryptable VerifyShareResponse carrying a
            // non-Ok status — not merely "some bytes were sent".
            let envelope = derec_proto::DeRecMessage::decode(sent[0].as_slice())
                .expect("the response is a DeRec envelope");
            let inner = crate::derec_message::extract_inner_message(&envelope.message, &SHARED_KEY)
                .expect("the peer can decrypt it under the channel key");
            let derec_proto::MessageBody::VerifyShareResponse(response) = inner else {
                panic!("expected a VerifyShareResponse, got {inner:?}");
            };
            let status = response.result.expect("a result is present").status;
            assert_ne!(
                status,
                derec_proto::StatusEnum::Ok as i32,
                "the response must report failure"
            );
        });
    }

    /// A message that fails to decrypt is not authenticated, so nothing is
    /// sent back even with the setting on — otherwise anyone able to reach
    /// this device could use it as an oracle.
    #[test]
    fn an_undecryptable_message_is_never_answered() {
        run_async(async {
            let channels = InMemChannelStore::default();
            let secrets = InMemSecretStore::default();
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed(&mut c, &mut s).await;

            // Right channel, wrong key: decryption fails before dispatch.
            let timestamp = crate::derec_message::current_timestamp();
            let envelope = crate::derec_message::DeRecMessageBuilder::channel()
                .channel_id(CHANNEL)
                .timestamp(timestamp)
                .message_body(derec_proto::MessageBody::VerifyShareRequest(
                    derec_proto::VerifyShareRequestMessage {
                        secret_id: SECRET_ID,
                        version: 1,
                        nonce: 7,
                        timestamp: Some(timestamp),
                        reply_to_transports: Vec::new(),
                    },
                ))
                .encrypt(&[1u8; 32])
                .expect("encrypt")
                .build()
                .expect("build")
                .encode_to_vec();

            let mut protocol = build(channels, secrets, transport.clone(), true);
            let result = protocol.process(&envelope).await;

            assert!(result.is_err(), "an undecryptable message still errors");
            assert!(
                transport.sent_envelopes().is_empty(),
                "an unauthenticated message must never be answered"
            );
        });
    }

    const PAIRING_CHANNEL: ChannelId = ChannelId(4040);
    const GROUP_CHANNEL: ChannelId = ChannelId(5050);
    const SOURCE_ID: u64 = 0x5_0C;
    const DESTINATION_ID: u64 = 0xDE_57;
    const TRACE: u64 = 0x7_7ACE;

    async fn seed_member(
        channels: &mut InMemChannelStore,
        channel_id: ChannelId,
        replica_id: u64,
        role: crate::protocol::types::ReplicaRole,
        status: ChannelStatus,
    ) {
        channels
            .save(
                SECRET_ID,
                ChannelRecord::Replica(crate::protocol::types::ReplicaMember {
                    channel_id,
                    replica_id: crate::types::ReplicaId(replica_id),
                    transports: vec![endpoint()],
                    communication_info: std::collections::HashMap::new(),
                    role,
                    status,
                    created_at: now_secs(),
                }),
            )
            .await
            .expect("seed replica member");
    }

    async fn seed_unconfirmed_destination(
        channels: &mut InMemChannelStore,
        secrets: &mut InMemSecretStore,
    ) {
        use crate::protocol::types::ReplicaRole;
        seed_member(
            channels,
            PAIRING_CHANNEL,
            DESTINATION_ID,
            ReplicaRole::Destination,
            ChannelStatus::Pending,
        )
        .await;
        seed_member(
            channels,
            PAIRING_CHANNEL,
            SOURCE_ID,
            ReplicaRole::Source,
            ChannelStatus::Pending,
        )
        .await;
        secrets
            .save(
                SECRET_ID,
                PAIRING_CHANNEL,
                SecretValue::SharedKey(SHARED_KEY),
            )
            .await
            .expect("seed pairing key");
    }

    fn replica_push(channel_id: ChannelId, sent_at: prost_types::Timestamp) -> Vec<u8> {
        crate::derec_message::DeRecMessageBuilder::channel()
            .channel_id(channel_id)
            .timestamp(sent_at)
            .trace_id(TRACE)
            .message_body(derec_proto::MessageBody::StoreShareRequest(
                derec_proto::StoreShareRequestMessage {
                    secret_id: SECRET_ID,
                    version: 1,
                    timestamp: Some(sent_at),
                    replica_id: Some(SOURCE_ID),
                    ..Default::default()
                },
            ))
            .encrypt(&SHARED_KEY)
            .expect("encrypt")
            .build()
            .expect("build")
            .encode_to_vec()
    }

    fn build_with(
        channels: InMemChannelStore,
        secrets: InMemSecretStore,
        user_secrets: InMemUserSecretStore,
        transport: RecordingTransport,
    ) -> DeRecProtocol<
        InMemChannelStore,
        InMemShareStore,
        InMemSecretStore,
        InMemUserSecretStore,
        InMemStateStore,
        RecordingTransport,
    > {
        DeRecProtocolBuilder::new(SECRET_ID)
            .with_channel_store(channels)
            .with_share_store(InMemShareStore::default())
            .with_secret_store(secrets)
            .with_user_secret_store(user_secrets)
            .with_transport(transport)
            .with_state_store(InMemStateStore)
            .with_own_transports(["https://destination.example.com"])
            .with_threshold(2)
            .with_replica_id(DESTINATION_ID)
            .build()
            .expect("test protocol builds")
    }

    fn ignored(
        reason: crate::protocol::IgnoreReason,
        channel_id: ChannelId,
    ) -> impl Fn(&DeRecEvent) -> bool {
        move |e| {
            matches!(e, DeRecEvent::MessageIgnored { channel_id: c, reason: r, trace_id }
                if *c == channel_id && *r == reason && *trace_id == TRACE)
        }
    }

    #[test]
    fn an_unconfirmed_replica_destination_ignores_the_mirror_push() {
        run_async(async {
            let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
            let user_secrets = InMemUserSecretStore::default();
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed_unconfirmed_destination(&mut c, &mut s).await;
            let members_before = channels.member_rows.lock().unwrap().len();
            let secrets_before = secrets.data.lock().unwrap().len();

            let mut protocol = build_with(
                channels.clone(),
                secrets.clone(),
                user_secrets.clone(),
                transport.clone(),
            );
            let events = protocol
                .process(&replica_push(
                    PAIRING_CHANNEL,
                    crate::derec_message::current_timestamp(),
                ))
                .await
                .expect("an ignored message is not an error");

            assert!(
                events.iter().any(ignored(
                    crate::protocol::IgnoreReason::PendingVerification,
                    PAIRING_CHANNEL
                )),
                "expected MessageIgnored(PendingVerification) on the pairing channel, got {events:?}"
            );
            assert!(
                !events.iter().any(|e| matches!(
                    e,
                    DeRecEvent::ReplicaSecretInstalled { .. }
                        | DeRecEvent::ReplicaSecretReceived { .. }
                )),
                "the copy must not be installed: {events:?}"
            );
            assert!(
                transport.sent_envelopes().is_empty(),
                "nothing may be acknowledged"
            );
            assert!(
                user_secrets.data.lock().unwrap().is_empty(),
                "no snapshot may be written"
            );
            assert_eq!(
                channels.member_rows.lock().unwrap().len(),
                members_before,
                "no member may be added"
            );
            assert!(
                channels
                    .member_rows
                    .lock()
                    .unwrap()
                    .values()
                    .all(|m| m.status == ChannelStatus::Pending),
                "no member may be promoted"
            );
            assert!(
                channels.helper_rows.lock().unwrap().is_empty(),
                "no helper may be written"
            );
            assert_eq!(
                secrets.data.lock().unwrap().len(),
                secrets_before,
                "no key may be written or dropped"
            );
        });
    }

    #[test]
    fn a_pending_helper_channel_ignores_inbound_traffic() {
        run_async(async {
            let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed(&mut c, &mut s).await;
            channels
                .helper_rows
                .lock()
                .unwrap()
                .values_mut()
                .for_each(|h| h.status = ChannelStatus::Pending);

            let mut protocol = build(channels, secrets, transport.clone(), true);
            let events = protocol
                .process(&failing_request())
                .await
                .expect("ignored, not failed");

            assert!(
                events.iter().any(|e| matches!(
                    e,
                    DeRecEvent::MessageIgnored {
                        channel_id,
                        reason: crate::protocol::IgnoreReason::PendingVerification,
                        ..
                    } if *channel_id == CHANNEL
                )),
                "expected MessageIgnored(PendingVerification), got {events:?}"
            );
            assert!(
                transport.sent_envelopes().is_empty(),
                "a pending channel is answered with nothing, not even a failure status"
            );
        });
    }

    #[test]
    fn a_pending_pairing_does_not_gate_the_group_channel() {
        run_async(async {
            use crate::protocol::types::ReplicaRole;
            let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed_unconfirmed_destination(&mut c, &mut s).await;
            seed_member(
                &mut c,
                GROUP_CHANNEL,
                0x07_4E4,
                ReplicaRole::Source,
                ChannelStatus::Paired,
            )
            .await;
            s.save(SECRET_ID, GROUP_CHANNEL, SecretValue::SharedKey(SHARED_KEY))
                .await
                .expect("seed group key");

            let mut protocol = build_with(
                channels,
                secrets,
                InMemUserSecretStore::default(),
                transport,
            );
            let outcome = protocol
                .process(&replica_push(
                    GROUP_CHANNEL,
                    crate::derec_message::current_timestamp(),
                ))
                .await;

            let gated = outcome.as_ref().is_ok_and(|events| {
                events
                    .iter()
                    .any(|e| matches!(e, DeRecEvent::MessageIgnored { .. }))
            });
            assert!(
                !gated,
                "group traffic must not be gated by another channel's pairing: {outcome:?}"
            );
        });
    }

    #[test]
    fn an_expired_message_is_reported_as_ignored() {
        run_async(async {
            let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
            let transport = RecordingTransport::default();
            let (mut c, mut s) = (channels.clone(), secrets.clone());
            seed_unconfirmed_destination(&mut c, &mut s).await;

            let mut protocol = build_with(
                channels,
                secrets,
                InMemUserSecretStore::default(),
                transport.clone(),
            );
            let stale = prost_types::Timestamp {
                seconds: now_secs() as i64 - 24 * 60 * 60,
                nanos: 0,
            };
            let events = protocol
                .process(&replica_push(PAIRING_CHANNEL, stale))
                .await
                .expect("an expired message is not an error");

            assert!(
                events.iter().any(ignored(
                    crate::protocol::IgnoreReason::Expired,
                    PAIRING_CHANNEL
                )),
                "expected MessageIgnored(Expired), got {events:?}"
            );
            assert!(transport.sent_envelopes().is_empty());
        });
    }
}