Skip to main content

coven_format/
floor_pointer.rs

1//! The floor pointer: the small record at `<store>/floor` that names the newest
2//! floor body (D8, D9). Its clear prefix carries the store key, the first kept
3//! slot and the body's writer, number and digest, so a joiner holding no key
4//! can find the body and read its key copies. Its encrypted header carries the
5//! signer.
6
7use crate::error::{exact_length, Error};
8use crate::path::ObjectPath;
9use crate::value::Slot;
10use crate::wire::{decode_frame_value, wire_struct, Decoder, Encoder, Wire};
11use coven_foundation::id_source::{DeviceId, KeyId, StoreId};
12
13/// The clear routing of a floor pointer: kind 42, format, then
14/// `key | first | device | number | digest`.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct FloorPointerPrefix {
17    /// Store key sealing the pointer's header.
18    pub key: KeyId,
19    /// First kept slot; guides key discovery until the floor authenticates.
20    pub first: Slot,
21    /// The device that wrote the body.
22    pub device: DeviceId,
23    /// The body's number among that device's floor bodies.
24    pub number: Slot,
25    /// The digest of the body's complete bytes, signature included.
26    pub body: [u8; 32],
27}
28
29impl FloorPointerPrefix {
30    /// The exact length of the prefix.
31    pub const LENGTH: usize = 3 + 16 + 8 + 8 + 8 + 32;
32
33    /// Encode the exact associated-data prefix.
34    pub fn encode(&self) -> Result<Vec<u8>, Error> {
35        let mut out = Encoder::new();
36        out.bytes(&crate::FormatVersion::CURRENT.prefix(crate::kinds::FLOOR_POINTER))?;
37        self.key.put(&mut out)?;
38        self.first.put(&mut out)?;
39        self.device.put(&mut out)?;
40        self.number.put(&mut out)?;
41        self.body.put(&mut out)?;
42        Ok(out.bytes)
43    }
44
45    /// Read exactly the prefix.
46    pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
47        crate::sealed::prefix(bytes, crate::kinds::FLOOR_POINTER)?;
48        exact_length(bytes.len(), Self::LENGTH)?;
49        let mut input = Decoder::new(&bytes[3..])?;
50        let prefix = Self {
51            key: KeyId::get(&mut input)?,
52            first: <Slot as Wire>::get(&mut input)?,
53            device: DeviceId::get(&mut input)?,
54            number: <Slot as Wire>::get(&mut input)?,
55            body: <[u8; 32]>::get(&mut input)?,
56        };
57        input.finish()?;
58        Ok(prefix)
59    }
60
61    /// The path of the body this pointer names in `store`.
62    pub fn body_path(&self, store: StoreId) -> ObjectPath {
63        ObjectPath::floor_body(store, self.device, self.number.into())
64    }
65}
66
67/// The pointer's encrypted header: the signer and the slot its history is
68/// applied through. Verify the signer against the membership at `at`.
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
70pub struct FloorPointerHeader {
71    /// Device signing the pointer in the state at `at`.
72    pub author: DeviceId,
73    /// Signer's applied slot, covering the body's announcements.
74    pub at: u64,
75}
76wire_struct!(FloorPointerHeader, author, at);
77
78impl FloorPointerHeader {
79    /// Encode the kind-19 header frame.
80    pub fn encode(&self) -> Result<Vec<u8>, Error> {
81        crate::encode_frame(crate::kinds::FLOOR_POINTER_HEADER, self)
82    }
83    /// Decode exactly the kind-19 header frame.
84    pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
85        decode_frame_value(bytes, crate::kinds::FLOOR_POINTER_HEADER, "floor pointer header")
86    }
87}
88
89#[cfg(test)]
90#[path = "floor_pointer_tests.rs"]
91mod tests;