moonproof-wallet 0.2.1

Native Rust wallet for the Midnight blockchain — HD derivation, coin balancing, Zswap/unshielded/dust transacting
Documentation
//! The dust trees' roots, in the encoding the indexer reports per block.
//!
//! The node checks a dust spend's proof against the roots it recorded at
//! the spend's `ctime`, so a spend must be dated at a block whose roots
//! the local trees hold. Comparing the local roots with a block's
//! reported ones is how a wallet knows it may date a spend there.

use moonproof_ledger::serialize::Serializable;

use super::DustState;

/// Hex of each dust tree's root, as the ledger's untagged serialization
/// encodes the root digest. An empty string stands for a tree without a
/// root.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DustTreeRoots {
    pub commitment: String,
    pub generation: String,
}

impl DustTreeRoots {
    /// The roots `state`'s local trees hold.
    #[must_use]
    pub fn of(state: &DustState) -> Self {
        let local = state.local();
        Self {
            commitment: encode(local.commitment_tree.root()),
            generation: encode(local.generating_tree.root()),
        }
    }
}

fn encode<T: Serializable>(root: Option<T>) -> String {
    root.map_or_else(String::new, |root| {
        let mut bytes = Vec::with_capacity(root.serialized_size());
        root.serialize(&mut bytes)
            .expect("serializing into a Vec cannot fail");
        hex::encode(bytes)
    })
}