//! Checking a block against the CID that claims to address it. use crate::cid::{Cid, Multihash}; #[derive(Debug, thiserror::Error)] pub enum BlockError { #[error("block hashes to {computed}, not the {claimed} it is filed under")] CidMismatch { claimed: Cid, computed: Cid }, #[error("block is addressed by {found:?}, but atproto only uses sha2-256")] UnsupportedMultihash { found: Multihash }, } /// Check that `block`'s contents really are for its cid. pub fn verify_block(claimed: &Cid, block: &[u8]) -> Result<(), BlockError> { let found = claimed.multihash_type(); if !matches!(found, Multihash::Sha2256) { return Err(BlockError::UnsupportedMultihash { found }); } let computed = Cid::digest_sha2(claimed.codec(), block); if computed == *claimed { Ok(()) } else { Err(BlockError::CidMismatch { claimed: *claimed, computed, }) } }