Skip to main content

zaun_community_protocol/signed/
rotation.rs

1//! `RootRotation` and `SignedRootRotation` (specification §4.6, ctx `zaun/v1/root-rotation`).
2
3mod chain;
4mod head;
5mod link;
6
7pub use chain::{claimed_genesis, verify_chain};
8pub use head::ChainHead;
9pub use link::SignedRootRotation;
10
11use super::fields::{Rule, decode_checked, encode_checked, ensure};
12use super::object::{FORMAT_V1, SignedObject, read_format};
13use crate::crypto::Context;
14use crate::encoding::{Decode, DecodeError, Encode, EncodeError, Reader, Writer};
15use crate::ids::CommunityId;
16use crate::keys::{PublicKey, Suite};
17
18/// The replacement of the community's root key, signed by the old **and** the new key.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct RootRotation {
21    /// 2: the community.
22    pub community_id: CommunityId,
23    /// 3: 1 for the first rotation, then +1.
24    pub rotation_seq: u64,
25    /// 4: the root key being retired.
26    pub old_root: PublicKey,
27    /// 5: the root key taking over.
28    pub new_root: PublicKey,
29    /// 6: the suite of `new_root`; ≥ the old key's suite.
30    pub new_suite: u8,
31    /// 7: ms.
32    pub issued_at: u64,
33}
34
35impl RootRotation {
36    fn rules(&self) -> Rule {
37        ensure(self.rotation_seq >= 1, "rotation_seq")?;
38        // SPEC: `new_suite` is a suite byte, so an unknown value is rejected (§3.1); it must
39        // also name `new_root`'s actual suite ("the suite of new_root").
40        let known = Suite::from_byte(self.new_suite).is_some();
41        ensure(
42            known && self.new_suite == self.new_root.suite().byte(),
43            "new_suite",
44        )?;
45        ensure(self.new_suite >= self.old_root.suite().byte(), "new_suite")
46    }
47}
48
49impl Encode for RootRotation {
50    fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
51        encode_checked(self.rules())?;
52        w.u8(FORMAT_V1);
53        w.put(&self.community_id)?;
54        w.u64(self.rotation_seq);
55        w.put(&self.old_root)?;
56        w.put(&self.new_root)?;
57        w.u8(self.new_suite);
58        w.u64(self.issued_at);
59        Ok(())
60    }
61}
62
63impl Decode for RootRotation {
64    fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
65        read_format(r)?;
66        let value = Self {
67            community_id: r.get()?,
68            rotation_seq: r.u64()?,
69            old_root: r.get()?,
70            new_root: r.get()?,
71            new_suite: r.u8()?,
72            issued_at: r.u64()?,
73        };
74        decode_checked(value, Self::rules)
75    }
76}
77
78impl SignedObject for RootRotation {
79    const CONTEXT: Context = Context::RootRotation;
80}