1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::Error;
#[derive(Clone, Copy, Eq, PartialEq, packable::Packable, derive_more::From, derive_more::AsRef)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MerkleRoot([u8; Self::LENGTH]);
impl MerkleRoot {
pub const LENGTH: usize = 32;
pub fn new(bytes: [u8; Self::LENGTH]) -> Self {
Self::from(bytes)
}
pub fn null() -> Self {
Self::from([0u8; Self::LENGTH])
}
}
impl core::ops::Deref for MerkleRoot {
type Target = [u8; Self::LENGTH];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl core::fmt::Display for MerkleRoot {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", prefix_hex::encode(self.0))
}
}
impl core::fmt::Debug for MerkleRoot {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "MerkleRoot({})", self)
}
}
impl core::str::FromStr for MerkleRoot {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(
prefix_hex::decode::<[u8; Self::LENGTH]>(s).map_err(Error::HexError)?,
))
}
}