Simplify and extend formatting of `Signature`
What changed, and why it matters
This is a small code cleanup in how Taproot signatures are printed or formatted as text/hex. It removes a tiny bit of duplicated logic by reusing an existing serialization helper and adds two new formatting traits (LowerHex and UpperHex). There is no security issue visible in the change.
No security action needed. Review as normal code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors fmt::Display for Signature in crypto/src/taproot.rs to delegate to SerializedSignature’s existing display implementation via self.serialize(). It also adds fmt::LowerHex and fmt::UpperHex implementations that delegate the same way. The serde derive line is simplified to use fully-qualified names, removing an import. The behavior is intended to be equivalent: the prior code hex-encoded exactly 64 bytes when the sighash type was default and 65 bytes otherwise, which matches what SerializedSignature does.
Changed components
crypto/src/taproot.rsInspect captured patch +14 / −9
diff --git a/crypto/src/taproot.rs b/crypto/src/taproot.rs
index 23174cbe..02558183 100644
--- a/crypto/src/taproot.rs
+++ b/crypto/src/taproot.rs
@@ -14,8 +14,6 @@ use core::str::FromStr;
use arbitrary::{Arbitrary, Unstructured};
use internals::array::ArrayExt;
use internals::impl_to_hex_from_lower_hex;
-#[cfg(feature = "serde")]
-use serde::{Deserialize, Serialize};
pub use self::into_iter::IntoIter;
use crate::hex;
@@ -29,7 +27,7 @@ const MAX_LEN: usize = 65; // 64 for sig, 1B sighash flag
/// A BIP-0340-0341 serialized Taproot signature with the corresponding hash type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Signature {
/// The underlying schnorr signature.
pub signature: secp256k1::schnorr::Signature,
@@ -98,12 +96,19 @@ impl Signature {
impl fmt::Display for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- if self.sighash_type == TapSighashType::Default {
- // default sighash type, don't add extra sighash byte
- hex_unstable::fmt_hex_exact!(f, 64, (*self).serialize(), hex_unstable::Case::Lower)
- } else {
- hex_unstable::fmt_hex_exact!(f, 65, (*self).serialize(), hex_unstable::Case::Lower)
- }
+ fmt::Display::fmt(&self.serialize(), f)
+ }
+}
+
+impl fmt::LowerHex for Signature {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::LowerHex::fmt(&self.serialize(), f)
+ }
+}
+
+impl fmt::UpperHex for Signature {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::UpperHex::fmt(&self.serialize(), f)
}
}
Why this scored 15/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.