Skip to main content

zaun_community_protocol/client/member/
moved.rs

1//! Following a community to its new host (migration.md, "How clients learn about the new
2//! host" and "Members re-authenticating at the new host").
3
4use crate::command::Validate;
5use crate::command::migration::Reauth;
6use crate::crypto::SecretKey;
7use crate::event::DescriptorUpdated;
8use crate::ids::{Challenge, CommunityId, HostId};
9use crate::keys::PublicKey;
10use crate::signed::{
11    AcceptedState, CommunityDescriptor, MemberReauth, SignedObject, SignedRootRotation, ValidPair,
12    ValidityError, validate_pair,
13};
14
15use super::BuildError;
16
17/// Where a member re-authenticates: the community, the new host, the delegation epoch its
18/// verified welcome proved, and that welcome's challenge.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct ReauthTarget {
21    /// The community.
22    pub community_id: CommunityId,
23    /// The new host.
24    pub host_id: HostId,
25    /// The epoch of the delegation that names the new host.
26    pub delegation_epoch: u64,
27    /// `host_challenge` from this connection's latest verified welcome.
28    pub host_challenge: Challenge,
29}
30
31/// The `migration.reauth` body: a `MemberReauth` signed with the member's existing key.
32/// Send it with `target.community_id` as the envelope `community_id`.
33///
34/// # Errors
35/// Signing errors.
36pub fn reauth_body(
37    member: &SecretKey,
38    target: ReauthTarget,
39    now_ms: u64,
40) -> Result<Reauth, BuildError> {
41    let signed = MemberReauth {
42        community_id: target.community_id,
43        host_id: target.host_id,
44        delegation_epoch: target.delegation_epoch,
45        host_challenge: target.host_challenge,
46        member_public_key: member.public_key(),
47        issued_at: now_ms,
48    }
49    .sign(member)?;
50    let body = Reauth {
51        member_public_key: member.public_key(),
52        reauth: signed.to_json(),
53    };
54    body.validate().map_err(BuildError::Body)?;
55    Ok(body)
56}
57
58/// A verified move: the new pair (store its state) and how to reach the new host.
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct Moved {
61    /// The verified descriptor and delegation; [`ValidPair::state`] is the state to store.
62    pub pair: ValidPair,
63    /// The new host.
64    pub host_id: HostId,
65    /// Its key: the welcome's `host_public_key` must equal it.
66    pub host_public_key: PublicKey,
67    /// Its SimpleX addresses, signed by the root key.
68    pub host_addresses: Vec<String>,
69    /// The new delegation's epoch.
70    pub delegation_epoch: u64,
71}
72
73/// Checks a `community.descriptor_updated` event against the client's stored state (ยง4.3,
74/// with anti-rollback: a lower revision or delegation epoch is refused) and says whether the
75/// community moved to another host. `chain` is the rotation chain the client holds; a
76/// rotation embedded in the new descriptor extends it, as at the host.
77///
78/// `Ok(None)`: valid, and still delegated to `current_host` (a descriptor edit).
79///
80/// # Errors
81/// [`ValidityError`] for a pair that is not valid, or older than `accepted`. A descriptor
82/// without a delegation (suspension) is [`ValidityError::NoDelegation`].
83pub fn moved(
84    community_id: &CommunityId,
85    event: &DescriptorUpdated,
86    chain: &[SignedRootRotation],
87    accepted: &AcceptedState,
88    current_host: HostId,
89    now_ms: u64,
90) -> Result<Option<Moved>, ValidityError> {
91    let mut links = chain.to_vec();
92    if let Ok(candidate) = CommunityDescriptor::decode_unverified(&event.descriptor.bytes)
93        && let Some(link) = candidate.rotations.last()
94        && links.last().map(|l| &l.bytes) != Some(&link.bytes)
95    {
96        links.push(link.clone());
97    }
98    let pair = validate_pair(
99        community_id,
100        &event.descriptor,
101        &event.delegation,
102        &links,
103        Some(accepted),
104        now_ms,
105    )?;
106    let g = pair.delegation().object();
107    if g.host_id == current_host {
108        return Ok(None);
109    }
110    Ok(Some(Moved {
111        host_id: g.host_id,
112        host_public_key: g.host_public_key.clone(),
113        host_addresses: g.host_addresses.clone(),
114        delegation_epoch: g.epoch,
115        pair,
116    }))
117}