Remove default XOnlyPublicKey serde impl
What changed, and why it matters
This commit removes the automatic serialization/deserialization support for the XOnlyPublicKey type in the rust-bitcoin library. Previously, serializing this type would silently drop the 'parity' bit (a small piece of metadata indicating whether the full public key was even or odd). Now users must explicitly choose a serialization format from a new module. This is a design cleanup that prevents accidental data loss when the type is serialized, but it is not a fix for an active security vulnerability.
Review downstream code that serializes or deserializes XOnlyPublicKey. It will now fail to compile unless an explicit serializer from key::x_only_serde is used. This is a breaking API change, not an urgent security patch, so plan a normal dependency update and migration.
Security signals we found
Lossy default serialization removed
Parity metadata no longer silently dropped on serde round-trip
API change from implicit to explicit serialization format
Evidence from the diff
The commit deletes the default serde::Serialize and serde::Deserialize implementations for the wrapper XOnlyPublicKey type. The wrapper previously delegated to the underlying secp256k1::XOnlyPublicKey, which only encodes the 32-byte x-coordinate and therefore discards the wrapper’s Parity field. A new x_only_serde module already provides explicit, opt-in serialization strategies. The change forces callers to choose a representation rather than getting a lossy default.
Changed components
crypto/src/key.rsXOnlyPublicKey serde supportx_only_serde moduleInspect captured patch +3 / −23
### crypto/src/key.rs
@@ -57,6 +57,9 @@ mod x_only_public_key {
/// A Bitcoin Schnorr X-only public key used for BIP-0340 signatures.
///
/// This type also holds the parity of the full public key.
+ ///
+ /// This type does not implement `serde` traits, since the x-only encoding does not include
+ /// the parity. Use one of the modules in `key::x_only_serde` instead.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct XOnlyPublicKey {
inner: secp256k1::XOnlyPublicKey,
@@ -1296,18 +1299,6 @@ impl<'de> serde::Deserialize<'de> for WifKey {
}
}
-// XOnlyPublicKey should serialize/deserialize identically to the inner type.
-#[cfg(feature = "serde")]
-impl serde::Serialize for XOnlyPublicKey {
- #[inline]
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: serde::Serializer,
- {
- <secp256k1::XOnlyPublicKey as serde::Serialize>::serialize(&self.to_inner(), serializer)
- }
-}
-
#[cfg(feature = "serde")]
pub mod x_only_serde {
//! Serde serialization and deserialization support for x-only public keys.
@@ -1447,17 +1438,6 @@ pub mod x_only_serde {
.map(|key| key.map(|key| XOnlyPublicKey::from_secp(key, Parity::Even)))
}
}
- }
- }
-
-#[cfg(feature = "serde")]
-impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
- #[inline]
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: serde::Deserializer<'de>,
- {
- Ok(Self::from_secp(secp256k1::XOnlyPublicKey::deserialize(deserializer)?, Parity::Even))
}
}
Why this scored 20/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.