Skip to content

Commit 84cd45a

Browse files
committed
Refactor: Update Hash conversions to improve consistency and error handling, adjust Bech32 encoding/decoding with detailed context
1 parent e00ad78 commit 84cd45a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

common/src/hash.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,26 @@ macro_rules! declare_hash_type_with_bech32 {
248248
}
249249

250250
impl TryFrom<Vec<u8>> for $name {
251-
type Error = Vec<u8>;
251+
type Error = anyhow::Error;
252252
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
253-
Ok(Self(Hash::try_from(vec)?))
253+
Ok(Self(
254+
Hash::try_from(vec).map_err(|e| anyhow::anyhow!("{}", hex::encode(e)))?,
255+
))
254256
}
255257
}
256258

257259
impl TryFrom<&[u8]> for $name {
258-
type Error = std::array::TryFromSliceError;
260+
type Error = anyhow::Error;
259261
fn try_from(arr: &[u8]) -> Result<Self, Self::Error> {
260-
Ok(Self(Hash::try_from(arr)?))
262+
Ok(Self(
263+
Hash::try_from(arr).map_err(|e| anyhow::anyhow!("{}", e))?,
264+
))
261265
}
262266
}
263267

264268
impl AsRef<[u8]> for $name {
265269
fn as_ref(&self) -> &[u8] {
266-
self.0.as_ref()
270+
&self.0.as_ref()
267271
}
268272
}
269273

0 commit comments

Comments
 (0)