Skip to main content

zaun_community_protocol/client/
config.rs

1//! Session configuration: who the host must be, which community it must prove, and the
2//! caller's persisted verifier state.
3
4use super::{CLIENT_CAPABILITIES, DEFAULT_TEXT_BUDGET, RetryPolicy};
5use crate::command::object::Binding;
6use crate::crypto;
7use crate::envelope::VersionRange;
8use crate::ids::{CommunityId, HostId};
9use crate::json::SignedJson;
10use crate::keys::PublicKey;
11use crate::signed::{AcceptedState, SignedRootRotation};
12
13/// The host the client expects to reach. The welcome's host key must match (§4.5, §8.6).
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum ExpectedHost {
16    /// A known host key (from a delegation the owner signed, or a stored session).
17    Key(PublicKey),
18    /// A host id (from an invitation link or the host's advertised info).
19    Id(HostId),
20}
21
22impl ExpectedHost {
23    /// Whether `key` is this host.
24    #[must_use]
25    pub fn matches(&self, key: &PublicKey) -> bool {
26        match self {
27            Self::Key(expected) => expected == key,
28            Self::Id(id) => crypto::host_id(key) == *id,
29        }
30    }
31}
32
33/// Everything a [`super::Session`] needs besides the transport.
34#[derive(Debug, Clone)]
35pub struct SessionConfig {
36    /// The host the welcome must come from.
37    pub host: ExpectedHost,
38    /// The community the host must prove (invitation target, or the bound community).
39    /// `None` only for an unbound connection with no target (before `community.create`).
40    pub community_id: Option<CommunityId>,
41    /// The connection's binding from an earlier session, if any (restart persistence).
42    pub binding: Option<Binding>,
43    /// The caller's stored anti-rollback state for the community (§4.3 rule 5).
44    pub accepted: Option<AcceptedState>,
45    /// The full rotation chain from the genesis key, if the caller has it (§4.6).
46    pub rotations: Vec<SignedRootRotation>,
47    /// The signed descriptor and delegation of the community, as verified before (a stored
48    /// [`crate::signed::ValidPair`]): the hello names it and the host may leave it out of the
49    /// welcome; it is then verified again as if the host had sent it (§6.2).
50    pub held_pair: Option<(SignedJson, SignedJson)>,
51    /// The versions offered in `session.hello`.
52    pub versions: VersionRange,
53    /// The capabilities offered in `session.hello`.
54    pub capabilities: Vec<String>,
55    /// Per-message text budget for framing (§5.1), from the transport adapter.
56    pub text_budget: usize,
57    /// Retry schedule (§7.3).
58    pub retry: RetryPolicy,
59    /// Ack after this many received frames (§10.5: at most 8).
60    pub ack_every_frames: u32,
61    /// Ack after this long while frames arrive (§10.5: at most 30 s).
62    pub ack_every_ms: u64,
63}
64
65impl SessionConfig {
66    /// Defaults for `host` and `community_id`: version 1, `frag`, the §7.3 schedule and the
67    /// §10.5 ack cadence (8 frames / 30 s).
68    #[must_use]
69    pub fn new(host: ExpectedHost, community_id: Option<CommunityId>) -> Self {
70        Self {
71            host,
72            community_id,
73            binding: None,
74            accepted: None,
75            rotations: Vec::new(),
76            held_pair: None,
77            versions: VersionRange::CURRENT,
78            capabilities: CLIENT_CAPABILITIES
79                .iter()
80                .map(|c| (*c).to_owned())
81                .collect(),
82            text_budget: DEFAULT_TEXT_BUDGET,
83            retry: RetryPolicy::SPEC,
84            ack_every_frames: 8,
85            ack_every_ms: 30_000,
86        }
87    }
88}