Move XOnlyPublicKey serde to the other serde impls
What changed, and why it matters
This commit simply moves the code that handles serializing and deserializing XOnlyPublicKey values (when the optional serde feature is enabled) from one location in the file to another location alongside similar implementations. The actual behavior of the code is unchanged; it is a pure code organization or cleanup change with no security relevance.
No action required; this is a non-functional refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff removes the serde Serialize/Deserialize implementations for XOnlyPublicKey from near the struct definition and reinserts them later in the file, adjacent to other serde implementations such as those for WifKey and LegacyPublicKey. The implementation code is byte-for-byte identical: serialization delegates to the inner secp256k1::XOnlyPublicKey, and deserialization wraps the result with Self::from_secp(…, Parity::Even). No logic changes, no feature-gate changes, and no API changes.
Changed components
crypto/src/key.rsInspect captured patch +23 / −23
diff --git a/crypto/src/key.rs b/crypto/src/key.rs
index 138e60bb..e601565a 100644
--- a/crypto/src/key.rs
+++ b/crypto/src/key.rs
@@ -476,29 +476,6 @@ impl fmt::Display for XOnlyPublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.as_inner().fmt(f) }
}
-// XOnlyPublicKey should serialize/deserialize identically to the inner type.
-#[cfg(feature = "serde")]
-impl Serialize for XOnlyPublicKey {
- #[inline]
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- <secp256k1::XOnlyPublicKey as Serialize>::serialize(self.as_inner(), serializer)
- }
-}
-
-#[cfg(feature = "serde")]
-impl<'de> Deserialize<'de> for XOnlyPublicKey {
- #[inline]
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'de>,
- {
- Ok(Self::from_secp(secp256k1::XOnlyPublicKey::deserialize(deserializer)?, Parity::Even))
- }
-}
-
impl Keypair {
/// Generates a new random key pair.
///
@@ -1280,6 +1257,29 @@ impl<'de> serde::Deserialize<'de> for WifKey {
}
}
+// XOnlyPublicKey should serialize/deserialize identically to the inner type.
+#[cfg(feature = "serde")]
+impl Serialize for XOnlyPublicKey {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ <secp256k1::XOnlyPublicKey as Serialize>::serialize(self.as_inner(), serializer)
+ }
+}
+
+#[cfg(feature = "serde")]
+impl<'de> Deserialize<'de> for XOnlyPublicKey {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok(Self::from_secp(secp256k1::XOnlyPublicKey::deserialize(deserializer)?, Parity::Even))
+ }
+}
+
#[cfg(feature = "serde")]
#[allow(clippy::collapsible_else_if)] // Aids readability.
impl serde::Serialize for LegacyPublicKey {
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.