Merge rust-bitcoin/rust-bitcoin#7126: Add `Vec` support to `XOnlyPublicKey` serde impls
What changed, and why it matters
This commit adds new helper code for serializing and deserializing lists of Bitcoin x-only public keys using the serde framework. It is a feature addition, not a bug fix, and there is no indication it addresses a security vulnerability.
No security action required. Treat as a normal feature addition. Reviewers may optionally verify the new serde helpers compile and behave correctly under the `alloc` feature.
Security signals we found
No security-relevant signals detected in the diff or commit metadata.
Change is additive only and does not alter existing serialization behavior.
No mention of vulnerability, CVE, security fix, or bug in commit title/message.
Evidence from the diff
The change introduces two new vec submodules under x_only_serde::with_parity and x_only_serde::without_parity in crypto/src/key.rs. These provide serialize/deserialize functions for &[XOnlyPublicKey] and Vec<XOnlyPublicKey>, mirroring the existing opt helpers for Option<XOnlyPublicKey>. The implementation delegates to FullPublicKey or raw secp256k1::XOnlyPublicKey serde logic and is gated behind the alloc feature. No existing behavior is modified.
Changed components
rust-bitcoin/crypto/src/key.rsx_only_serde moduleXOnlyPublicKey serde helpersInspect captured patch +70 / −0
### crypto/src/key.rs
@@ -1311,6 +1311,7 @@ pub mod x_only_serde {
//! The provided modules can be used as follows:
//!
//! ```
+ //! # #[cfg(feature = "alloc")]
//! # #[cfg(feature = "hex")] {
//! use bitcoin_crypto::key::XOnlyPublicKey;
//! use serde::{Deserialize, Serialize};
@@ -1321,6 +1322,8 @@ pub mod x_only_serde {
//! pub key: XOnlyPublicKey,
//! #[serde(with = "bitcoin_crypto::key::x_only_serde::without_parity::opt")]
//! pub maybe_key: Option<XOnlyPublicKey>,
+ //! #[serde(with = "bitcoin_crypto::key::x_only_serde::with_parity::vec")]
+ //! pub keys: Vec<XOnlyPublicKey>,
//! }
//! # }
//! ```
@@ -1381,6 +1384,38 @@ pub mod x_only_serde {
Option::<FullPublicKey>::deserialize(d).map(|key| key.map(XOnlyPublicKey::from))
}
}
+
+ #[cfg(feature = "alloc")]
+ pub mod vec {
+ //! Serialize and deserialize [`Vec<XOnlyPublicKey>`] including their parity.
+ //!
+ //! Use with `#[serde(with = "bitcoin_crypto::key::x_only_serde::with_parity::vec")]`.
+
+ use alloc::vec::Vec;
+
+ use serde::{Deserialize, Deserializer, Serializer};
+
+ use crate::key::{FullPublicKey, XOnlyPublicKey};
+
+ /// Serializes each key in `keys` as a [`FullPublicKey`] using its parity.
+ #[inline]
+ pub fn serialize<S: Serializer>(
+ keys: &[XOnlyPublicKey],
+ s: S,
+ ) -> Result<S::Ok, S::Error> {
+ s.collect_seq(keys.iter().map(|key| key.to_public_key()))
+ }
+
+ /// Deserializes a sequence of [`FullPublicKey`]s into [`XOnlyPublicKey`]s retaining
+ /// their parity.
+ #[inline]
+ pub fn deserialize<'d, D: Deserializer<'d>>(
+ d: D,
+ ) -> Result<Vec<XOnlyPublicKey>, D::Error> {
+ let keys = Vec::<FullPublicKey>::deserialize(d)?;
+ Ok(keys.into_iter().map(XOnlyPublicKey::from).collect())
+ }
+ }
}
pub mod without_parity {
@@ -1438,6 +1473,41 @@ pub mod x_only_serde {
.map(|key| key.map(|key| XOnlyPublicKey::from_secp(key, Parity::Even)))
}
}
+
+ #[cfg(feature = "alloc")]
+ pub mod vec {
+ //! Serialize and deserialize [`Vec<XOnlyPublicKey>`] without their parity.
+ //!
+ //! Use with `#[serde(with = "bitcoin_crypto::key::x_only_serde::without_parity::vec")]`.
+
+ use alloc::vec::Vec;
+
+ use secp256k1::Parity;
+ use serde::{Deserialize, Deserializer, Serializer};
+
+ use crate::key::XOnlyPublicKey;
+
+ /// Serializes the x-coordinate of each key in `keys`, discarding their parity.
+ #[inline]
+ pub fn serialize<S: Serializer>(
+ keys: &[XOnlyPublicKey],
+ s: S,
+ ) -> Result<S::Ok, S::Error> {
+ s.collect_seq(keys.iter().map(|key| key.to_inner()))
+ }
+
+ /// Deserializes a sequence of x-coordinates into [`XOnlyPublicKey`]s with even parity.
+ #[inline]
+ pub fn deserialize<'d, D: Deserializer<'d>>(
+ d: D,
+ ) -> Result<Vec<XOnlyPublicKey>, D::Error> {
+ let keys = Vec::<secp256k1::XOnlyPublicKey>::deserialize(d)?;
+ Ok(keys
+ .into_iter()
+ .map(|key| XOnlyPublicKey::from_secp(key, Parity::Even))
+ .collect())
+ }
+ }
}
}
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.