From 8932cd1d89e8640e93e70513aeb2197c5817a858 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Tue, 15 Oct 2024 12:51:33 +0200 Subject: [PATCH] feat: `RlpNode::as_hash` --- src/nodes/rlp.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/nodes/rlp.rs b/src/nodes/rlp.rs index 6f54531..c858327 100644 --- a/src/nodes/rlp.rs +++ b/src/nodes/rlp.rs @@ -89,6 +89,16 @@ impl RlpNode { pub fn as_slice(&self) -> &[u8] { &self.0 } + + /// Returns hash if this is an RLP-encoded hash + #[inline] + pub fn as_hash(&self) -> Option { + if self.len() == B256::len_bytes() + 1 { + Some(B256::from_slice(&self.0[1..])) + } else { + None + } + } } #[cfg(feature = "arbitrary")]