zaun_community_protocol/client/member/
moved.rs1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct ReauthTarget {
21 pub community_id: CommunityId,
23 pub host_id: HostId,
25 pub delegation_epoch: u64,
27 pub host_challenge: Challenge,
29}
30
31pub 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#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct Moved {
61 pub pair: ValidPair,
63 pub host_id: HostId,
65 pub host_public_key: PublicKey,
67 pub host_addresses: Vec<String>,
69 pub delegation_epoch: u64,
71}
72
73pub 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}