Skip to main content

oxabl_cache_crypto/
record_context.rs

1//! What a record is bound to: cache, epoch, namespace kind and lookup id.
2
3use oxabl_cache_format::{CacheId, LookupId, Namespace};
4
5use crate::domain::{AD_RECORD, SIG_ENTRY, prefixed};
6
7/// The identity of a record slot. A record opens only under the context it was sealed for.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct RecordContext {
10    pub cache_id: CacheId,
11    pub epoch: u32,
12    pub namespace: Namespace,
13    pub lookup_id: LookupId,
14}
15
16impl RecordContext {
17    fn tail(&self) -> Vec<u8> {
18        let mut out = Vec::with_capacity(16 + 4 + 1 + 32);
19        out.extend_from_slice(&self.cache_id.0);
20        out.extend_from_slice(&self.epoch.to_be_bytes());
21        out.push(self.namespace.to_u8());
22        out.extend_from_slice(&self.lookup_id.0);
23        out
24    }
25
26    /// `"oxabl-cache v1 record" ‖ cache_id ‖ epoch ‖ ns_kind ‖ lookup_id`.
27    pub(crate) fn associated_data(&self) -> Vec<u8> {
28        prefixed(AD_RECORD, &self.tail())
29    }
30
31    /// The signed transcript of an entry: its domain prefix, this context and the payload.
32    pub(crate) fn entry_transcript(&self, signed_payload: &[u8]) -> Vec<u8> {
33        let mut out = prefixed(SIG_ENTRY, &self.tail());
34        out.extend_from_slice(signed_payload);
35        out
36    }
37}