use crate::{
InternalError, InternalErrorOrigin, dto::component_registry::ComponentRuntimeDirectoryAuthority,
};
use sha2::{Digest, Sha256};
pub struct ComponentRuntimeOps;
impl ComponentRuntimeOps {
pub fn directory_authority_hash(
authority: &ComponentRuntimeDirectoryAuthority,
) -> Result<[u8; 32], InternalError> {
const DOMAIN: &[u8] = b"canic.component-runtime.directory-authority.v1";
let payload = candid::encode_one(authority).map_err(|error| {
InternalError::invariant(
InternalErrorOrigin::Ops,
format!("Component runtime Directory authority cannot be encoded: {error}"),
)
})?;
let mut hasher = Sha256::new();
hasher.update(DOMAIN);
hasher.update((payload.len() as u64).to_be_bytes());
hasher.update(payload);
Ok(hasher.finalize().into())
}
}