zaun_community_protocol/client/owner/
revise.rs1use crate::client::BuildError;
4use crate::command::Validate;
5use crate::command::community::DescriptorPublish;
6use crate::crypto::{self, SecretKey};
7use crate::keys::PublicKey;
8use crate::signed::{
9 CommunityDescriptor, DelegationRef, HostDelegation, RootRotation, Signed, SignedObject,
10 SignedRootRotation,
11};
12
13#[must_use]
16pub fn next_revision(current: &CommunityDescriptor, now_ms: u64) -> CommunityDescriptor {
17 CommunityDescriptor {
18 revision: current.revision.saturating_add(1),
19 issued_at: now_ms,
20 ..current.clone()
21 }
22}
23
24pub fn rotate_root(
30 current: &CommunityDescriptor,
31 old: &SecretKey,
32 new: &SecretKey,
33 now_ms: u64,
34) -> Result<SignedRootRotation, BuildError> {
35 let old_root = old.public_key();
36 if old_root != current.root_public_key {
37 return Err(BuildError::WrongRootKey);
38 }
39 let last_seq = match current.rotations.last() {
41 Some(r) => {
42 RootRotation::decode_unverified(&r.bytes)
43 .map_err(|e| BuildError::Signed(e.into()))?
44 .rotation_seq
45 }
46 None => 0,
47 };
48 let new_root = new.public_key();
49 let rotation = RootRotation {
50 community_id: current.community_id,
51 rotation_seq: last_seq.saturating_add(1),
52 new_suite: new_root.suite().byte(),
53 old_root,
54 new_root,
55 issued_at: now_ms,
56 };
57 Ok(SignedRootRotation::sign(&rotation, old, new)?)
58}
59
60#[must_use]
64pub fn rotated_revision(
65 current: &CommunityDescriptor,
66 rotation: SignedRootRotation,
67 new_root: &PublicKey,
68 now_ms: u64,
69) -> CommunityDescriptor {
70 CommunityDescriptor {
71 root_public_key: new_root.clone(),
72 rotations: vec![rotation],
73 ..next_revision(current, now_ms)
74 }
75}
76
77pub fn resign_delegation(
83 new_root: &SecretKey,
84 delegation: &HostDelegation,
85) -> Result<Signed<HostDelegation>, BuildError> {
86 Ok(delegation.clone().sign(new_root)?)
87}
88
89pub fn publish_body(
96 mut descriptor: CommunityDescriptor,
97 root: &SecretKey,
98 delegation: Option<&Signed<HostDelegation>>,
99 icon: Option<Vec<u8>>,
100) -> Result<(Signed<CommunityDescriptor>, DescriptorPublish), BuildError> {
101 if root.public_key() != descriptor.root_public_key {
102 return Err(BuildError::WrongRootKey);
103 }
104 if let Some(d) = delegation {
105 descriptor.delegation = Some(DelegationRef {
106 epoch: d.object().epoch,
107 delegation_hash: d.hash(),
108 });
109 }
110 if let Some(bytes) = &icon {
111 descriptor.icon = Some(crypto::content_hash(bytes));
112 }
113 let signed = descriptor.sign(root)?;
114 let body = DescriptorPublish {
115 descriptor: signed.to_json(),
116 delegation: delegation.map(Signed::to_json),
117 icon,
118 };
119 body.validate().map_err(BuildError::Body)?;
120 Ok((signed, body))
121}