pub mod delegated;
pub mod signatory;
use crate::error::Error;
use crate::prefix::{IdentifierPrefix, SelfAddressingPrefix};
use delegated::DelegatedIdentifierState;
use signatory::Signatory;
#[derive(Default, PartialEq, Debug, Clone)]
pub struct IdentifierState {
pub prefix: IdentifierPrefix,
pub sn: u64,
pub last: SelfAddressingPrefix,
pub current: Signatory,
pub next: SelfAddressingPrefix,
pub delegated_keys: Vec<DelegatedIdentifierState>,
pub tally: u64,
pub witnesses: Vec<IdentifierPrefix>,
}
impl IdentifierState {
pub fn verify<T: Verifiable>(&self, message: &T) -> Result<bool, Error> {
message.verify_against(self)
}
fn apply<T: EventSemantics>(self, event: &T) -> Result<Self, Error> {
event.apply_to(self)
}
pub fn verify_and_apply<T: EventSemantics + Verifiable>(
self,
event_message: &T,
) -> Result<Self, Error> {
let next = self.apply(event_message)?;
if next.verify(event_message)? {
Ok(next)
} else {
Err(Error::SemanticError("Verification Failure".to_string()))
}
}
}
pub trait EventSemantics {
fn apply_to(&self, state: IdentifierState) -> Result<IdentifierState, Error> {
Ok(state)
}
}
pub trait Verifiable {
fn verify_against(&self, state: &IdentifierState) -> Result<bool, Error>;
}