use crate::{
collections::Map,
operation::{CgkaOperation, CgkaOperationGraph},
pcs_key::PcsKey,
};
use alloc::vec::Vec;
use keyhive_crypto::digest::Digest;
#[derive(Debug)]
pub(crate) struct VerifiedRootSecret {
op_hash: Digest<CgkaOperation>,
secret: PcsKey,
}
impl VerifiedRootSecret {
pub(crate) fn verify(
graph: &CgkaOperationGraph,
op_hash: Digest<CgkaOperation>,
secret: PcsKey,
) -> Option<Self> {
(graph.update_root_share_key(&op_hash)? == secret.0.share_key())
.then_some(Self { op_hash, secret })
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct RootSecrets(Map<Digest<CgkaOperation>, PcsKey>);
impl RootSecrets {
pub(crate) fn insert(&mut self, verified: VerifiedRootSecret) {
self.0.insert(verified.op_hash, verified.secret);
}
pub(crate) fn get(&self, op_hash: &Digest<CgkaOperation>) -> Option<PcsKey> {
self.0.get(op_hash).copied()
}
pub(crate) fn iter(&self) -> impl Iterator<Item = (Digest<CgkaOperation>, PcsKey)> + '_ {
self.0.iter().map(|(op_hash, secret)| (*op_hash, *secret))
}
pub(crate) fn merge_from(&mut self, other: &Self) -> Vec<(Digest<CgkaOperation>, PcsKey)> {
let mut added = Vec::new();
for (op_hash, secret) in other.iter() {
if self.0.insert(op_hash, secret) != Some(secret) {
added.push((op_hash, secret));
}
}
added
}
#[cfg(test)]
pub(crate) fn clear(&mut self) {
self.0.clear();
}
#[cfg(test)]
pub(crate) fn remove(&mut self, op_hash: &Digest<CgkaOperation>) {
self.0.remove(op_hash);
}
}