Skip to main content

zaun_community_protocol/client/
owner.rs

1//! Helpers for a community owner, the holder of the root key (§4, ADR-0005, ADR-0011): the
2//! genesis descriptor and host delegation, `community.create`, root rotation and descriptor
3//! revisions. The root key never leaves the owner's device; only signed objects do.
4
5mod content;
6mod genesis;
7mod revise;
8
9pub use content::DescriptorContent;
10pub use genesis::{Genesis, create_body, delegate, genesis};
11pub use revise::{next_revision, publish_body, resign_delegation, rotate_root, rotated_revision};
12
13use super::BuildError;
14use crate::crypto::{self, SecretKey};
15use crate::ids::{HostId, RngError};
16use crate::keys::PublicKey;
17
18/// Generates a fresh root key (§3.2): the community's identity. Store it sealed; it cannot
19/// be recovered (ADR-0011).
20///
21/// # Errors
22/// [`RngError`] when the OS CSPRNG fails.
23pub fn generate_root_key() -> Result<SecretKey, RngError> {
24    SecretKey::generate()
25}
26
27/// What an owner needs about a host to delegate to it (§4.2).
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct HostInfo {
30    /// The host key.
31    pub public_key: PublicKey,
32    /// 1–4 SimpleX contact addresses of the host.
33    pub addresses: Vec<String>,
34}
35
36impl HostInfo {
37    /// Wraps a host key and addresses known to be authentic.
38    #[must_use]
39    pub const fn new(public_key: PublicKey, addresses: Vec<String>) -> Self {
40        Self {
41            public_key,
42            addresses,
43        }
44    }
45
46    /// From the host's advertised `HostId`, key and addresses: the key must derive the id.
47    ///
48    /// # Errors
49    /// [`BuildError::HostId`] when it does not.
50    pub fn from_advertised(
51        host_id: HostId,
52        public_key: PublicKey,
53        addresses: Vec<String>,
54    ) -> Result<Self, BuildError> {
55        if crypto::host_id(&public_key) == host_id {
56            Ok(Self::new(public_key, addresses))
57        } else {
58            Err(BuildError::HostId)
59        }
60    }
61
62    /// The host's id.
63    #[must_use]
64    pub fn host_id(&self) -> HostId {
65        crypto::host_id(&self.public_key)
66    }
67}