diff --git a/zebra-chain/src/block/hash.rs b/zebra-chain/src/block/hash.rs index f3163dfe..ef789d26 100644 --- a/zebra-chain/src/block/hash.rs +++ b/zebra-chain/src/block/hash.rs @@ -22,6 +22,12 @@ use super::Header; #[cfg_attr(test, derive(Arbitrary))] pub struct Hash(pub [u8; 32]); +impl fmt::Display for Hash { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&hex::encode(&self.0)) + } +} + impl fmt::Debug for Hash { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("BlockHeaderHash") diff --git a/zebra-chain/src/block/tests/prop.rs b/zebra-chain/src/block/tests/prop.rs index 1879d736..d10305b5 100644 --- a/zebra-chain/src/block/tests/prop.rs +++ b/zebra-chain/src/block/tests/prop.rs @@ -17,6 +17,13 @@ proptest! { prop_assert_eq![hash, other_hash]; } + #[test] + fn block_hash_display_fromstr_roundtrip(hash in any::()) { + let display = format!("{}", hash); + let parsed = display.parse::().expect("hash should parse"); + prop_assert_eq!(hash, parsed); + } + #[test] fn blockheader_roundtrip(header in any::
()) { let bytes = header.zcash_serialize_to_vec()?; diff --git a/zebra-chain/src/transaction/hash.rs b/zebra-chain/src/transaction/hash.rs index a919c13c..cc526b35 100644 --- a/zebra-chain/src/transaction/hash.rs +++ b/zebra-chain/src/transaction/hash.rs @@ -27,6 +27,12 @@ impl<'a> From<&'a Transaction> for Hash { } } +impl fmt::Display for Hash { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&hex::encode(&self.0)) + } +} + impl fmt::Debug for Hash { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("TransactionHash") diff --git a/zebra-chain/src/transaction/tests/prop.rs b/zebra-chain/src/transaction/tests/prop.rs index 2f944e7d..29548bbb 100644 --- a/zebra-chain/src/transaction/tests/prop.rs +++ b/zebra-chain/src/transaction/tests/prop.rs @@ -14,6 +14,13 @@ proptest! { prop_assert_eq![tx, tx2]; } + #[test] + fn transaction_hash_display_fromstr_roundtrip(hash in any::()) { + let display = format!("{}", hash); + let parsed = display.parse::().expect("hash should parse"); + prop_assert_eq!(hash, parsed); + } + #[test] fn locktime_roundtrip(locktime in any::()) { let mut bytes = Cursor::new(Vec::new());