use sha2::{Digest, Sha256};
use sim_kernel::{ContentId, Symbol};
use crate::JournalError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct JournalObject {
pub id: ContentId,
pub bytes: Vec<u8>,
}
impl JournalObject {
pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
let bytes = bytes.into();
let digest: [u8; 32] = Sha256::digest(&bytes).into();
Self {
id: ContentId::from_bytes(Symbol::qualified("journal", "sha256-bytes-v1"), digest),
bytes,
}
}
pub fn verify(&self) -> Result<(), JournalError> {
if Self::from_bytes(self.bytes.clone()).id == self.id {
Ok(())
} else {
Err(JournalError::CorruptObject(self.id.clone()))
}
}
}