From 58710dfb9139d3411cd6b1e4aaddec799075f101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Toman?= Date: Sat, 25 Nov 2023 23:26:57 +0100 Subject: [PATCH] Implement LowerHex and UpperHex for CompactTarget --- bitcoin/src/pow.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index cc25654a..26fa7d60 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -306,6 +306,16 @@ impl Decodable for CompactTarget { } } +impl LowerHex for CompactTarget { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { LowerHex::fmt(&self.0, f) } +} + +impl UpperHex for CompactTarget { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { UpperHex::fmt(&self.0, f) } +} + /// Big-endian 256 bit integer type. // (high, low): u.0 contains the high bits, u.1 contains the low bits. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] @@ -1538,6 +1548,12 @@ mod tests { assert!(result.is_err()); } + #[test] + fn compact_target_lower_hex_and_upper_hex() { + assert_eq!(format!("{:08x}", CompactTarget(0x01D0F456)), "01d0f456"); + assert_eq!(format!("{:08X}", CompactTarget(0x01d0f456)), "01D0F456"); + } + #[test] fn target_from_compact() { // (nBits, target)