zaun_community_protocol/client/owner/
genesis.rs1use super::{DescriptorContent, HostInfo};
4use crate::client::BuildError;
5use crate::command::Validate;
6use crate::command::community::Create;
7use crate::command::object::NewProfile;
8use crate::crypto::{self, SecretKey};
9use crate::ids::{Challenge, CommunityId};
10use crate::keys::PublicKey;
11use crate::limits::PROTOCOL_VERSION;
12use crate::signed::{
13 CommunityDescriptor, DelegationRef, HostDelegation, MemberKeyRegistration, Purpose, Scopes,
14 Signed, SignedObject,
15};
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct Genesis {
20 pub descriptor: Signed<CommunityDescriptor>,
22 pub delegation: Signed<HostDelegation>,
24}
25
26impl Genesis {
27 #[must_use]
29 pub fn community_id(&self) -> CommunityId {
30 self.descriptor.object().community_id
31 }
32}
33
34pub fn delegate(
39 root: &SecretKey,
40 community_id: CommunityId,
41 host: &HostInfo,
42 epoch: u64,
43 expires_at: Option<u64>,
44 now_ms: u64,
45) -> Result<Signed<HostDelegation>, BuildError> {
46 let delegation = HostDelegation {
47 community_id,
48 host_id: host.host_id(),
49 host_public_key: host.public_key.clone(),
50 host_addresses: host.addresses.clone(),
51 scopes: Scopes::V1_HOST,
52 epoch,
53 not_before: now_ms,
54 expires_at,
55 issued_at: now_ms,
56 };
57 Ok(delegation.sign(root)?)
58}
59
60pub fn genesis(
66 root: &SecretKey,
67 owner_member_key: &PublicKey,
68 content: DescriptorContent,
69 host: &HostInfo,
70 now_ms: u64,
71) -> Result<Genesis, BuildError> {
72 let root_public_key = root.public_key();
73 let community_id = crypto::community_id(&root_public_key);
74 let delegation = delegate(root, community_id, host, 1, None, now_ms)?;
75 let descriptor = CommunityDescriptor {
76 community_id,
77 revision: 1,
78 root_public_key,
79 rotations: Vec::new(),
80 min_suite: 1,
81 name: content.name,
82 description: content.description,
83 icon: content.icon,
84 languages: content.languages,
85 categories: content.categories,
86 rules: content.rules,
87 moderation_policy: content.moderation_policy,
88 operator_contact: content.operator_contact,
89 abuse_contact: content.abuse_contact,
90 admission_summary: content.admission_summary,
91 discovery: content.discovery,
92 protocol_min: PROTOCOL_VERSION,
93 protocol_max: PROTOCOL_VERSION,
94 owner_member_key: owner_member_key.clone(),
95 delegation: Some(DelegationRef {
96 epoch: 1,
97 delegation_hash: delegation.hash(),
98 }),
99 public_channel_links: Vec::new(),
100 issued_at: now_ms,
101 };
102 let descriptor = descriptor.sign(root)?;
103 Ok(Genesis {
104 descriptor,
105 delegation,
106 })
107}
108
109pub fn create_body(
115 genesis: &Genesis,
116 member: &SecretKey,
117 host_challenge: Challenge,
118 display_name: impl Into<String>,
119 now_ms: u64,
120) -> Result<Create, BuildError> {
121 let member_public_key = member.public_key();
122 if member_public_key != genesis.descriptor.object().owner_member_key {
123 return Err(BuildError::OwnerKey);
124 }
125 let registration = MemberKeyRegistration {
126 purpose: Purpose::CreateCommunity,
127 community_id: genesis.community_id(),
128 host_id: genesis.delegation.object().host_id,
129 host_challenge,
130 member_public_key: member_public_key.clone(),
131 issued_at: now_ms,
132 };
133 let registration = registration.sign(member)?;
134 let body = Create {
135 descriptor: genesis.descriptor.to_json(),
136 delegation: genesis.delegation.to_json(),
137 member_public_key,
138 registration: registration.to_json(),
139 profile: NewProfile {
140 display_name: display_name.into(),
141 },
142 };
143 body.validate().map_err(BuildError::Body)?;
144 Ok(body)
145}