use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use vex_core::Hash;
use crate::error::AnchorError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnchorMetadata {
pub tenant_id: String,
pub event_count: u64,
pub timestamp: DateTime<Utc>,
pub description: Option<String>,
}
impl AnchorMetadata {
pub fn new(tenant_id: impl Into<String>, event_count: u64) -> Self {
Self {
tenant_id: tenant_id.into(),
event_count,
timestamp: Utc::now(),
description: None,
}
}
pub fn with_description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnchorReceipt {
pub backend: String,
pub root_hash: String,
pub anchor_id: String,
pub anchored_at: DateTime<Utc>,
pub proof: Option<String>,
pub metadata: AnchorMetadata,
}
impl AnchorReceipt {
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(self)
}
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
}
#[async_trait]
pub trait AnchorBackend: Send + Sync + std::fmt::Debug {
async fn anchor(
&self,
root: &Hash,
metadata: AnchorMetadata,
) -> Result<AnchorReceipt, AnchorError>;
async fn verify(&self, receipt: &AnchorReceipt) -> Result<bool, AnchorError>;
fn name(&self) -> &str;
async fn is_healthy(&self) -> bool;
}