diff --git a/src/lib.rs b/src/lib.rs index 10bd413..8c5e4af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,3 +100,17 @@ impl Display for NodeHash { write!(f, "NodeHash({:?})", &self.bytes) } } + +impl From<[u8; 32]> for NodeHash { + #[inline] + fn from(bytes: [u8; 32]) -> Self { + Self::new(bytes) + } +} + +impl From<&[u8; 32]> for NodeHash { + #[inline] + fn from(bytes: &[u8; 32]) -> Self { + Self::new(*bytes) + } +} diff --git a/src/transaction/nodes.rs b/src/transaction/nodes.rs index bc56327..b5f4c63 100644 --- a/src/transaction/nodes.rs +++ b/src/transaction/nodes.rs @@ -38,6 +38,26 @@ impl From> for Option { } } +impl From> for Option<[u8; 32]> { + #[inline] + fn from(value: TrieRoot) -> Self { + match value { + TrieRoot::Empty => None, + TrieRoot::Node(hash) => Some(hash.bytes), + } + } +} + +impl From> for TrieRoot { + #[inline] + fn from(hash: Option<[u8; 32]>) -> Self { + match hash { + Some(hash) => Self::Node(NodeHash::new(hash)), + None => Self::Empty, + } + } +} + /// A unmodified Node #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]