Replace use of HexPrimitive in serde_as_consensus
What changed, and why it matters
This is a routine internal code cleanup in the rust-bitcoin library. It replaces one way of turning data into hex text with another, simpler in-house implementation. There is no indication of a security bug being fixed.
No security action required. Treat as normal maintenance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit removes the last use of HexPrimitive in primitives/src/serde_as_consensus.rs and replaces it with a small local ConsensusHex wrapper that implements Display using the existing encoding::EncoderByteIter and hex::BytesToHexIter. The behavior for human-readable serializers remains lower-case hex serialization; non-human-readable serialization is unchanged. The change appears to be preparation for a later refactor/move of the module.
Changed components
primitives/src/serde_as_consensus.rsInspect captured patch +17 / −2
diff --git a/primitives/src/serde_as_consensus.rs b/primitives/src/serde_as_consensus.rs
index e159d17d..cc6e2b8f 100644
--- a/primitives/src/serde_as_consensus.rs
+++ b/primitives/src/serde_as_consensus.rs
@@ -51,8 +51,23 @@ where
S: Serializer,
{
if s.is_human_readable() {
- // `HexPrimitive` uses `LowerHex` for `Display`.
- s.collect_str(&crate::hex_codec::HexPrimitive(value))
+ struct ConsensusHex<'a, T>(&'a T);
+
+ impl<T: Encode> fmt::Display for ConsensusHex<'_, T> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let encoder = self.0.encoder();
+ let byte_iter = encoding::EncoderByteIter::new(encoder);
+ let iter = hex::BytesToHexIter::new(byte_iter, hex::Case::Lower).flatten();
+
+ for ch in iter {
+ fmt::Display::fmt(&ch, f)?;
+ }
+
+ Ok(())
+ }
+ }
+
+ s.collect_str(&ConsensusHex(value))
} else {
// For non-human-readable formats, serialize as bytes.
let bytes = encoding::encode_to_vec(value);
Why this scored 17/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.