primitives: Use hashes format implementations
What changed, and why it matters
This is a small internal code cleanup in the rust-bitcoin library. It changes how Bitcoin hash types (like transaction IDs) are printed as text, making them reuse formatting code from a lower-level module instead of defining it separately. There is no indication this fixes a security bug or introduces a vulnerability.
No security action required. Reviewers may optionally verify that the underlying `hashes` crate's `LowerHex`/`UpperHex`/`Display` implementations still produce the expected reversed/upper/lower formatting for each hash type, but the commit itself does not warrant a security response.
Security signals we found
No security-relevant signals detected in the diff or commit metadata.
Change is a pure refactoring of display/formatting trait implementations.
No changes to input parsing, cryptographic operations, memory handling, or consensus-critical code paths.
Evidence from the diff
The commit refactors primitives/src/hash_types/*.rs to delegate LowerHex, UpperHex, and Display formatting to the underlying hashes crate implementations, rather than using the hex_unstable::impl_fmt_traits! macro. It also makes Debug formatting unconditional (no longer gated by the absence of the hex feature). The change is behavior-preserving by design: the commit message states the goal is to reuse the existing correct formatting logic, including byte reversal and case handling. No functional change to hashing, serialization, or consensus logic is visible in the diff.
Changed components
primitives/src/hash_types/generic.rsprimitives/src/hash_types/ntxid.rsprimitives/src/hash_types/txid.rsprimitives/src/hash_types/witness_commitment.rsprimitives/src/hash_types/wtxid.rsInspect captured patch +12 / −10
diff --git a/primitives/src/hash_types/generic.rs b/primitives/src/hash_types/generic.rs
index da61f877..70ffd9d5 100644
--- a/primitives/src/hash_types/generic.rs
+++ b/primitives/src/hash_types/generic.rs
@@ -29,11 +29,18 @@ super::impl_serde!(HashType, LEN);
super::impl_bytelike_traits!(HashType, LEN);
#[cfg(feature = "hex")]
-hex_unstable::impl_fmt_traits! {
- #[display_backward(REVERSE)]
- impl fmt_traits for HashType {
- const LENGTH: usize = LEN;
- }
+impl fmt::LowerHex for HashType {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
+}
+
+#[cfg(feature = "hex")]
+impl fmt::UpperHex for HashType {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
+}
+
+#[cfg(feature = "hex")]
+impl fmt::Display for HashType {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}
#[cfg(feature = "hex")]
@@ -50,7 +57,6 @@ impl str::FromStr for HashType {
}
}
-#[cfg(not(feature = "hex"))]
impl fmt::Debug for HashType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&self.0, f) }
}
diff --git a/primitives/src/hash_types/ntxid.rs b/primitives/src/hash_types/ntxid.rs
index df7a22bc..1e81106a 100644
--- a/primitives/src/hash_types/ntxid.rs
+++ b/primitives/src/hash_types/ntxid.rs
@@ -2,7 +2,6 @@
//! The `Txid` type.
-#[cfg(not(feature = "hex"))]
use core::fmt;
#[cfg(feature = "hex")]
use core::str;
diff --git a/primitives/src/hash_types/txid.rs b/primitives/src/hash_types/txid.rs
index b0d9fbf1..880684c1 100644
--- a/primitives/src/hash_types/txid.rs
+++ b/primitives/src/hash_types/txid.rs
@@ -4,7 +4,6 @@
//!
//! In order to print and parse txids enable the "hex" feature.
-#[cfg(not(feature = "hex"))]
use core::fmt;
#[cfg(feature = "hex")]
use core::str;
diff --git a/primitives/src/hash_types/witness_commitment.rs b/primitives/src/hash_types/witness_commitment.rs
index f4332e96..e6697191 100644
--- a/primitives/src/hash_types/witness_commitment.rs
+++ b/primitives/src/hash_types/witness_commitment.rs
@@ -2,7 +2,6 @@
//! The `WitnessCommitment` type.
-#[cfg(not(feature = "hex"))]
use core::fmt;
#[cfg(feature = "hex")]
use core::str;
diff --git a/primitives/src/hash_types/wtxid.rs b/primitives/src/hash_types/wtxid.rs
index 482623f5..cdb27517 100644
--- a/primitives/src/hash_types/wtxid.rs
+++ b/primitives/src/hash_types/wtxid.rs
@@ -4,7 +4,6 @@
//!
//! In order to print and parse txids enable the "hex" feature.
-#[cfg(not(feature = "hex"))]
use core::fmt;
#[cfg(feature = "hex")]
use core::str;
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.