oxabl_cache_crypto/
trust.rs1use oxabl_cache_format::{Namespace, PublicKey};
4
5use crate::dictionary::Dictionary;
6use crate::error::{Error, Result};
7use crate::record_entry::Entry;
8use crate::record_open::OpenedRecord;
9use crate::roster_verify::VerifiedRoster;
10use crate::segment_open::SegmentExpectation;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct Provenance {
15 pub tier: Namespace,
16 pub signer: PublicKey,
17 pub epoch: u32,
18 pub roster_version: u64,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct AcceptedRecord {
24 entry: Entry,
25 provenance: Provenance,
26}
27
28impl OpenedRecord {
29 pub fn accept(self, roster: &VerifiedRoster, reader: &PublicKey) -> Result<AcceptedRecord> {
35 let signer = self.entry.signer;
36 let accepted = match self.context.namespace {
37 Namespace::Trusted => roster.is_ci_key(&signer),
38 Namespace::Personal => signer == *reader && roster.member_by_sign_key(reader).is_some(),
39 };
40 if !accepted {
41 return Err(Error::SignerNotAccepted);
42 }
43 if self.entry.fields.roster_version > roster.version() {
44 return Err(Error::RosterVersionAhead);
45 }
46 Ok(AcceptedRecord {
47 provenance: Provenance {
48 tier: self.context.namespace,
49 signer,
50 epoch: self.context.epoch,
51 roster_version: self.entry.fields.roster_version,
52 },
53 entry: self.entry,
54 })
55 }
56}
57
58impl AcceptedRecord {
59 pub fn entry(&self) -> &Entry {
60 &self.entry
61 }
62
63 pub fn provenance(&self) -> Provenance {
64 self.provenance
65 }
66
67 pub fn segment_expectation<'a>(
70 &self,
71 dictionary: Option<&'a Dictionary>,
72 ) -> Result<SegmentExpectation<'a>> {
73 let named = self.entry.fields.dict_id;
74 if named != dictionary.map(Dictionary::id) {
75 return Err(Error::Mismatch("dictionary"));
76 }
77 Ok(SegmentExpectation {
78 id: self.entry.fields.segment_id,
79 sealed_len: u64::from(self.entry.fields.segment_len),
80 plain_len: self.entry.fields.plain_len,
81 dictionary,
82 })
83 }
84}