use async_trait::async_trait;
use std::sync::Arc;
use mithril_common::logging::LoggerExtensions;
use crate::certificate_client::fetch::InternalCertificateRetriever;
use crate::certificate_client::{fetch, verify};
use crate::{MithrilCertificate, MithrilCertificateListItem, MithrilResult};
pub struct CertificateClient {
pub(super) aggregator_requester: Arc<dyn CertificateAggregatorRequest>,
pub(super) retriever: Arc<InternalCertificateRetriever>,
pub(super) verifier: Arc<dyn CertificateVerifier>,
}
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
pub trait CertificateAggregatorRequest: Send + Sync {
async fn list_latest(&self) -> MithrilResult<Vec<MithrilCertificateListItem>>;
async fn get_by_hash(&self, hash: &str) -> MithrilResult<Option<MithrilCertificate>>;
}
impl CertificateClient {
pub fn new(
aggregator_requester: Arc<dyn CertificateAggregatorRequest>,
verifier: Arc<dyn CertificateVerifier>,
logger: slog::Logger,
) -> Self {
let _logger = logger.new_with_component_name::<Self>();
let retriever = Arc::new(InternalCertificateRetriever::new(
aggregator_requester.clone(),
));
Self {
aggregator_requester,
retriever,
verifier,
}
}
pub async fn list(&self) -> MithrilResult<Vec<MithrilCertificateListItem>> {
fetch::list(self).await
}
pub async fn get(&self, certificate_hash: &str) -> MithrilResult<Option<MithrilCertificate>> {
fetch::get(self, certificate_hash).await
}
pub async fn verify_chain(&self, certificate_hash: &str) -> MithrilResult<MithrilCertificate> {
verify::verify_chain(self, certificate_hash).await
}
}
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
pub trait CertificateVerifier: Sync + Send {
async fn verify_chain(&self, certificate: &MithrilCertificate) -> MithrilResult<()>;
}
#[cfg(feature = "unstable")]
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
pub trait CertificateVerifierCache: Sync + Send {
async fn store_validated_certificate(
&self,
certificate_hash: &str,
previous_certificate_hash: &str,
) -> MithrilResult<()>;
async fn get_previous_hash(&self, certificate_hash: &str) -> MithrilResult<Option<String>>;
async fn reset(&self) -> MithrilResult<()>;
}