Skip to main content

oxabl_cache_format/
dictionary_pointer.rs

1//! `OXDP` dictionary pointer.
2
3use crate::cursor::Cursor;
4use crate::error::Result;
5use crate::frame::{read_header, write_header};
6use crate::ids::{CacheId, PublicKey, SegmentId, Signature};
7use crate::writer::Writer;
8
9const MAGIC: [u8; 4] = *b"OXDP";
10
11/// Length of an encoded pointer.
12pub const DICTIONARY_POINTER_LEN: usize = 6 + 16 + 4 + 8 + 32 + 32 + 64;
13
14/// Names the dictionary segment of a cache, signed by a `ci` or `owner` key.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct DictionaryPointer {
17    pub cache_id: CacheId,
18    pub epoch: u32,
19    pub version: u64,
20    pub dict_segment_id: SegmentId,
21    pub signer: PublicKey,
22    pub signature: Signature,
23}
24
25impl DictionaryPointer {
26    /// Every byte before the signature: what it covers, after the caller's domain-separated
27    /// prefix.
28    pub fn signed_bytes(&self) -> Vec<u8> {
29        let mut w = Writer::with_capacity(DICTIONARY_POINTER_LEN);
30        write_header(&mut w, MAGIC, 0);
31        w.bytes(&self.cache_id.0);
32        w.u32(self.epoch);
33        w.u64(self.version);
34        w.bytes(&self.dict_segment_id.0);
35        w.bytes(&self.signer);
36        w.into_vec()
37    }
38
39    pub fn encode(&self) -> Vec<u8> {
40        let mut out = self.signed_bytes();
41        out.extend_from_slice(&self.signature);
42        out
43    }
44
45    pub fn decode(bytes: &[u8]) -> Result<Self> {
46        let mut c = Cursor::new(bytes);
47        read_header(&mut c, MAGIC, 0)?;
48        let pointer = Self {
49            cache_id: CacheId(c.array()?),
50            epoch: c.u32()?,
51            version: c.u64()?,
52            dict_segment_id: SegmentId(c.array()?),
53            signer: c.array()?,
54            signature: c.array()?,
55        };
56        c.finish()?;
57        Ok(pointer)
58    }
59}