zaun_community_protocol/signed/
validity.rs1mod pair;
5
6pub use pair::{ValidPair, validate_pair};
7
8use super::descriptor::{CommunityDescriptor, DelegationRef};
9use super::error::{SignedError, ValidityError};
10use super::object::Signed;
11use super::rotation::{ChainHead, SignedRootRotation, claimed_genesis, verify_chain};
12use crate::crypto::content_hash;
13use crate::ids::CommunityId;
14use crate::json::SignedJson;
15
16pub const CLOCK_SKEW_MS: u64 = 300_000;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct AcceptedState {
22 pub revision: u64,
24 pub descriptor_hash: [u8; 32],
26 pub delegation: Option<DelegationRef>,
28 pub rotation_seq: u64,
30 pub root_key_hash: [u8; 32],
33 pub min_suite: u8,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct ValidDescriptor {
40 descriptor: Signed<CommunityDescriptor>,
41 chain: ChainHead,
42}
43
44impl ValidDescriptor {
45 #[must_use]
47 pub const fn descriptor(&self) -> &Signed<CommunityDescriptor> {
48 &self.descriptor
49 }
50
51 #[must_use]
53 pub const fn chain(&self) -> &ChainHead {
54 &self.chain
55 }
56
57 #[must_use]
60 pub fn accepted_state(&self, previous: Option<&AcceptedState>) -> AcceptedState {
61 let d = self.descriptor.object();
62 AcceptedState {
63 revision: d.revision,
64 descriptor_hash: self.descriptor.hash(),
65 delegation: previous.and_then(|p| p.delegation),
66 rotation_seq: self.chain.rotation_seq(),
67 root_key_hash: content_hash(self.chain.key().as_bytes()),
68 min_suite: d.min_suite,
69 }
70 }
71}
72
73pub fn validate_descriptor(
79 community_id: &CommunityId,
80 descriptor: &SignedJson,
81 chain: &[SignedRootRotation],
82 accepted: Option<&AcceptedState>,
83) -> Result<ValidDescriptor, ValidityError> {
84 let genesis = claimed_genesis(&descriptor.bytes, chain)
85 .map_err(|e| ValidityError::Descriptor(SignedError::Decode(e)))?;
86 let chain_head = verify_chain(community_id, &genesis, chain)?;
87 let floor = accepted.map_or(1, |a| a.min_suite);
88 let signed = Signed::<CommunityDescriptor>::from_json(descriptor, chain_head.key(), floor)
89 .map_err(ValidityError::Descriptor)?;
90 let d = signed.object();
91 if d.community_id != *community_id {
92 return Err(ValidityError::Community);
93 }
94 if d.root_public_key != *chain_head.key() {
95 return Err(ValidityError::RootKey);
96 }
97 if d.rotations.last().map(|r| &r.bytes) != chain.last().map(|r| &r.bytes) {
102 return Err(ValidityError::EmbeddedRotation);
103 }
104 if chain_head.suite() < d.min_suite {
105 return Err(ValidityError::SuiteBelowMinimum);
106 }
107 if let Some(a) = accepted {
108 if d.revision < a.revision {
109 return Err(ValidityError::RevisionRollback);
110 }
111 if d.revision == a.revision && signed.hash() != a.descriptor_hash {
112 return Err(ValidityError::RevisionConflict);
113 }
114 let then = chain_head
117 .key_at(a.rotation_seq)
118 .ok_or(ValidityError::ChainRollback)?;
119 if content_hash(then.as_bytes()) != a.root_key_hash {
120 return Err(ValidityError::RootKeyFork);
121 }
122 if d.min_suite < a.min_suite {
123 return Err(ValidityError::MinSuiteRollback);
124 }
125 }
126 Ok(ValidDescriptor {
127 descriptor: signed,
128 chain: chain_head,
129 })
130}