zaun_community_protocol/signed/
rotation.rs1mod 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#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct RootRotation {
21 pub community_id: CommunityId,
23 pub rotation_seq: u64,
25 pub old_root: PublicKey,
27 pub new_root: PublicKey,
29 pub new_suite: u8,
31 pub issued_at: u64,
33}
34
35impl RootRotation {
36 fn rules(&self) -> Rule {
37 ensure(self.rotation_seq >= 1, "rotation_seq")?;
38 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}