Skip to content

Commit 35b42e5

Browse files
Implement SerializeValue and DeserializeValue for Blake2bHash
1 parent 5fb0602 commit 35b42e5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

rust/catalyst-types/src/hashes.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use std::{fmt, str::FromStr};
55
use blake2b_simd::Params;
66
use displaydoc::Display;
77
use pallas_crypto::hash::Hash;
8+
use scylla::_macro_internal::{
9+
CellWriter, ColumnType, DeserializationError, DeserializeValue, FrameSlice, SerializationError,
10+
SerializeValue, TypeCheckError, WrittenCellProof,
11+
};
812
use thiserror::Error;
913

1014
/// Number of bytes in a blake2b 224 hash.
@@ -165,6 +169,29 @@ impl<'a, C, const BYTES: usize> minicbor::Decode<'a, C> for Blake2bHash<BYTES> {
165169
}
166170
}
167171

172+
impl<const BYTES: usize> SerializeValue for Blake2bHash<BYTES> {
173+
fn serialize<'b>(
174+
&self, typ: &ColumnType, writer: CellWriter<'b>,
175+
) -> Result<WrittenCellProof<'b>, SerializationError> {
176+
self.0.as_ref().serialize(typ, writer)
177+
}
178+
}
179+
180+
impl<'frame, 'metadata, const BYTES: usize> DeserializeValue<'frame, 'metadata>
181+
for Blake2bHash<BYTES>
182+
{
183+
fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> {
184+
<Vec<u8>>::type_check(typ)
185+
}
186+
187+
fn deserialize(
188+
typ: &'metadata ColumnType<'metadata>, v: Option<FrameSlice<'frame>>,
189+
) -> Result<Self, DeserializationError> {
190+
let bytes = <Vec<u8>>::deserialize(typ, v)?;
191+
Self::try_from(bytes).map_err(DeserializationError::new)
192+
}
193+
}
194+
168195
#[cfg(test)]
169196
mod tests {
170197
use super::*;

0 commit comments

Comments
 (0)