hbkr-rs 0.3.2

Hashblock Key Rotation
Documentation
use serde::{Deserialize, Serialize};
use serde_hex::{Compact, SerHex};

use crate::{
    basicpre::BasicPrefix, errors::KrError, identifier_prefix::IdentifierPrefix,
    key_config::KeyConfig, said::SelfAddressingPrefix, EventSemantics, EventTypeTag,
};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
pub struct LastEstablishmentData {
    #[serde(rename = "s", with = "SerHex::<Compact>")]
    pub(crate) sn: u64,
    #[serde(rename = "d")]
    pub(crate) digest: SelfAddressingPrefix,
    pub(crate) br: Vec<BasicPrefix>,
    pub(crate) ba: Vec<BasicPrefix>,
}
/// Identifier State
///
/// represents the accumulated state after applying events, based on section 13 of the paper
#[derive(Default, PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct IdentifierState {
    #[serde(rename = "i")]
    pub prefix: IdentifierPrefix,

    #[serde(rename = "s", with = "SerHex::<Compact>")]
    pub sn: u64,

    #[serde(rename = "d")]
    pub last_event_digest: SelfAddressingPrefix,

    #[serde(rename = "p")]
    pub last_previous: Option<SelfAddressingPrefix>,

    #[serde(rename = "et")]
    pub last_event_type: Option<EventTypeTag>,

    #[serde(flatten)]
    pub current: KeyConfig,

    #[serde(rename = "bt", with = "SerHex::<Compact>")]
    pub tally: u64,

    #[serde(rename = "b")]
    pub witnesses: Vec<BasicPrefix>,

    #[serde(rename = "di")]
    pub delegator: Option<IdentifierPrefix>,

    #[serde(rename = "ee")]
    pub last_est: LastEstablishmentData,
}

impl IdentifierState {
    /// Apply
    ///
    /// validates and applies the semantic rules of the event to the event state
    pub fn apply<T: EventSemantics>(self, event: &T) -> Result<Self, KrError> {
        event.apply_to(self)
    }
}