penumbra_txhash/
effect_hash.rsuse penumbra_proto::{penumbra::core::txhash::v1 as pb, DomainType, Message, Name};
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct EffectHash(pub [u8; 64]);
fn create_personalized_state(personalization: &str) -> blake2b_simd::State {
let mut state = blake2b_simd::State::new();
let length = personalization.len() as u64;
state.update(&length.to_le_bytes());
state.update(personalization.as_bytes());
state
}
impl EffectHash {
pub fn as_bytes(&self) -> &[u8; 64] {
&self.0
}
pub fn from_proto_effecting_data<M: Message + Name>(message: &M) -> EffectHash {
let mut state = create_personalized_state(&M::type_url());
state.update(&message.encode_to_vec());
EffectHash(*state.finalize().as_array())
}
}
impl Default for EffectHash {
fn default() -> Self {
Self([0u8; 64])
}
}
impl std::fmt::Debug for EffectHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("EffectHash")
.field(&hex::encode(self.0))
.finish()
}
}
impl DomainType for EffectHash {
type Proto = pb::EffectHash;
}
impl From<EffectHash> for pb::EffectHash {
fn from(msg: EffectHash) -> Self {
Self {
inner: msg.0.to_vec(),
}
}
}
impl TryFrom<pb::EffectHash> for EffectHash {
type Error = anyhow::Error;
fn try_from(value: pb::EffectHash) -> Result<Self, Self::Error> {
Ok(Self(value.inner.try_into().map_err(|_| {
anyhow::anyhow!("incorrect length for effect hash")
})?))
}
}
impl AsRef<[u8]> for EffectHash {
fn as_ref(&self) -> &[u8] {
&self.0
}
}