Skip to main content

zaun_community_protocol/client/owner/
revise.rs

1//! Descriptor revisions and root rotation (§4.1, §4.6, §8.1 `community.descriptor.publish`).
2
3use 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/// A copy of `current` with the next revision and `issued_at = now_ms`, for the owner to
14/// edit and pass to [`publish_body`].
15#[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
24/// Signs the rotation from the current root `old` to `new` (§4.6): `rotation_seq` follows
25/// the descriptor's latest rotation, and both keys sign.
26///
27/// # Errors
28/// [`BuildError::WrongRootKey`] when `old` is not `current.root_public_key`; signing errors.
29pub 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    // The owner's own descriptor: its embedded rotation was signed by this owner.
40    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/// The next revision after `rotation`: the new root key and `rotations = [rotation]`
61/// (§4.1 field 5). Sign it with the **new** key via [`publish_body`], together with
62/// [`resign_delegation`]: the delegation is verified under the current root (§4.3 rule 3).
63#[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
77/// Re-signs an existing delegation under a new root key. The bytes, hence the
78/// `delegation_hash` and epoch, are unchanged.
79///
80/// # Errors
81/// Signing errors.
82pub fn resign_delegation(
83    new_root: &SecretKey,
84    delegation: &HostDelegation,
85) -> Result<Signed<HostDelegation>, BuildError> {
86    Ok(delegation.clone().sign(new_root)?)
87}
88
89/// Signs `descriptor` with `root` (its current root key) and builds the
90/// `community.descriptor.publish` body. A given `delegation` is referenced by the descriptor
91/// (epoch and hash) and sent; a given `icon` sets the descriptor's icon hash and is sent.
92///
93/// # Errors
94/// [`BuildError::WrongRootKey`], field rule, signing or body (icon size) errors.
95pub 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}