Merge rust-bitcoin/rust-bitcoin#7105: Remove derived serde impls for types in stabilising crates
What changed, and why it matters
This commit removes automatic serde (serialization/deserialization) code generation from several Rust Bitcoin library crates and replaces it with hand-written implementations. The stated goal is to reduce dependencies and make the code easier to reason about. The change is a refactoring/cleanup; it does not fix a known security bug, but any mistake in the new hand-written serde code could subtly change how data is serialized or parsed.
Treat as a routine refactoring commit. Reviewers should verify that the new explicit serde implementations preserve the exact same wire/format behavior as the previous derived implementations, especially for Version, OutPoint, Work, Target, CompactTarget, and Sequence, to avoid accidental consensus-relevant format changes.
Security signals we found
Manual serde implementations replace derived ones, increasing the surface area for serialization-format bugs
No validation logic changes are visible in the diff
No mention of security fixes, CVEs, or vulnerability reports in commit message or PR description
Change is framed as code-quality/dependency reduction work
Evidence from the diff
The merge removes the serde ‘derive’ feature from consensus_encoding, units, and primitives crates and replaces #[derive(Serialize, Deserialize)] with explicit serde trait implementations for types such as Version, Work, Target, CompactTarget, Sequence, and OutPoint. It also converts internal helper wrappers from derived transparent deserialization to manual Deserialize impls. The commit is described by the project as a dependency-graph and compile-time improvement, not a security patch.
Changed components
rust-bitcoin consensus_encoding craterust-bitcoin primitives craterust-bitcoin units crateserde feature handling in Cargo.toml manifestsInspect captured patch +219 / −41
### consensus_encoding/Cargo.toml
@@ -24,10 +24,11 @@ serde = ["alloc", "hex", "dep:serde"]
internals = { package = "bitcoin-internals", path = "../internals", version = "0.8.2" }
hex = { package = "hex-conservative", version = "1.1.0", default-features = false, features = [], optional = true }
-serde = { version = "1.0.195", default-features = false, features = ["derive"], optional = true }
+serde = { version = "1.0.195", default-features = false, optional = true }
[dev-dependencies]
hex = { package = "hex-conservative", version = "1.1.0" }
+serde = { version = "1.0.195", default-features = false, features = ["derive"] }
serde_json = "1.0.68"
[package.metadata.docs.rs]
### primitives/Cargo.toml
@@ -29,11 +29,12 @@ units = { package = "bitcoin-units", path = "../units", version = "0.7.0", defau
arbitrary = { version = "1.4.1", optional = true }
hex = { package = "hex-conservative", version = "1.1.0", default-features = false, optional = true }
-serde = { version = "1.0.195", default-features = false, features = ["derive", "alloc"], optional = true }
+serde = { version = "1.0.195", default-features = false, features = ["alloc"], optional = true }
[dev-dependencies]
serde_json = "1.0.68"
bincode = "1.3.1"
+serde = { version = "1.0.195", default-features = false, features = ["derive"] }
[package.metadata.docs.rs]
all-features = true
### primitives/src/block.rs
@@ -78,6 +78,8 @@ use encoding::{Decoder2, Encoder2, PrefixedSliceEncoder, VecDecoder};
use hashes::sha256d;
#[cfg(feature = "alloc")]
use hashes::HashEngine as _;
+#[cfg(feature = "serde")]
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "hex")]
use crate::hex_codec::HexPrimitive;
@@ -945,7 +947,6 @@ impl From<&Header> for BlockHash {
/// * [BIP-0009 - Version bits with timeout and delay](https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki) (current usage)
/// * [BIP-0034 - Block v2, Height in Coinbase](https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki)
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Version(i32);
impl Version {
@@ -1028,6 +1029,28 @@ impl Default for Version {
fn default() -> Self { Self::NO_SOFT_FORK_SIGNALLING }
}
+#[cfg(feature = "serde")]
+impl Serialize for Version {
+ #[inline]
+ fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ i32::serialize(&self.to_consensus(), s)
+ }
+}
+
+#[cfg(feature = "serde")]
+impl<'de> Deserialize<'de> for Version {
+ #[inline]
+ fn deserialize<D>(d: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok(Self::from_consensus(i32::deserialize(d)?))
+ }
+}
+
impl encoding::Encode for Version {
type Encoder<'e> = VersionEncoder<'e>;
#[inline]
### primitives/src/lib.rs
@@ -72,7 +72,6 @@ pub extern crate encoding;
#[cfg(feature = "hex")]
pub extern crate hex;
#[cfg(feature = "serde")]
-#[macro_use]
pub extern crate serde;
mod hash_types;
### primitives/src/transaction.rs
@@ -1171,13 +1171,6 @@ impl<'de> Deserialize<'de> for OutPoint {
deserializer.deserialize_str(StringVisitor)
} else {
- #[derive(Deserialize)]
- #[serde(field_identifier, rename_all = "lowercase")]
- enum Field {
- Txid,
- Vout,
- }
-
struct OutPointVisitor;
impl<'de> de::Visitor<'de> for OutPointVisitor {
@@ -1208,14 +1201,14 @@ impl<'de> Deserialize<'de> for OutPoint {
while let Some(key) = map.next_key()? {
match key {
- Field::Txid => {
+ OutPointField::Txid => {
if txid.is_some() {
return Err(de::Error::duplicate_field("txid"));
}
let bytes: [u8; 32] = map.next_value()?;
txid = Some(Txid::from_byte_array(bytes));
}
- Field::Vout => {
+ OutPointField::Vout => {
if vout.is_some() {
return Err(de::Error::duplicate_field("vout"));
}
@@ -1232,9 +1225,63 @@ impl<'de> Deserialize<'de> for OutPoint {
}
}
- const FIELDS: &[&str] = &["txid", "vout"];
- deserializer.deserialize_struct("OutPoint", FIELDS, OutPointVisitor)
+ deserializer.deserialize_struct("OutPoint", OutPointField::NAMES, OutPointVisitor)
+ }
+ }
+}
+
+/// A field of [`OutPoint`] when deserializing from a non-human-readable format.
+#[cfg(feature = "serde")]
+enum OutPointField {
+ Txid,
+ Vout,
+}
+
+#[cfg(feature = "serde")]
+impl OutPointField {
+ const NAMES: &'static [&'static str] = &["txid", "vout"];
+}
+
+#[cfg(feature = "serde")]
+impl<'de> Deserialize<'de> for OutPointField {
+ fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
+ struct FieldVisitor;
+
+ impl de::Visitor<'_> for FieldVisitor {
+ type Value = OutPointField;
+
+ fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("field identifier")
+ }
+
+ fn visit_u64<E: de::Error>(self, v: u64) -> Result<Self::Value, E> {
+ match v {
+ 0 => Ok(OutPointField::Txid),
+ 1 => Ok(OutPointField::Vout),
+ _ => Err(E::invalid_value(
+ de::Unexpected::Unsigned(v),
+ &"field index 0 <= i < 2",
+ )),
+ }
+ }
+
+ fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
+ self.visit_bytes(v.as_bytes())
+ }
+
+ fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<Self::Value, E> {
+ match v {
+ b"txid" => Ok(OutPointField::Txid),
+ b"vout" => Ok(OutPointField::Vout),
+ _ => {
+ let v = crate::prelude::String::from_utf8_lossy(v);
+ Err(E::unknown_field(&v, OutPointField::NAMES))
+ }
+ }
+ }
}
+
+ deserializer.deserialize_identifier(FieldVisitor)
}
}
@@ -1250,7 +1297,6 @@ impl<'de> Deserialize<'de> for OutPoint {
/// [BIP-0068]: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
/// [BIP-0431]: https://github.com/bitcoin/bips/blob/master/bip-0431.mediawiki
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Version(u32);
impl Version {
@@ -1315,6 +1361,28 @@ impl From<Version> for u32 {
fn from(version: Version) -> Self { version.0 }
}
+#[cfg(feature = "serde")]
+impl Serialize for Version {
+ #[inline]
+ fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ u32::serialize(&self.to_u32(), s)
+ }
+}
+
+#[cfg(feature = "serde")]
+impl<'de> Deserialize<'de> for Version {
+ #[inline]
+ fn deserialize<D>(d: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok(Self::maybe_non_standard(u32::deserialize(d)?))
+ }
+}
+
impl encoding::Encode for Version {
type Encoder<'e> = VersionEncoder<'e>;
#[inline]
### units/Cargo.toml
@@ -23,7 +23,7 @@ serde = ["dep:serde", "internals/serde"]
internals = { package = "bitcoin-internals", path = "../internals", version = "0.8.2" }
encoding = { package = "bitcoin-consensus-encoding", path = "../consensus_encoding", version = "1.0.0", default-features = false, optional = true }
-serde = { version = "1.0.195", default-features = false, features = ["derive"], optional = true }
+serde = { version = "1.0.195", default-features = false, optional = true }
arbitrary = { version = "1.4.1", optional = true }
[dev-dependencies]
### units/src/amount/serde.rs
@@ -321,9 +321,13 @@ pub mod as_sat {
where
A: SeqAccess<'de>,
{
- #[derive(Deserialize)]
- #[serde(transparent)]
- struct Wrapper<T: AmountOps>(#[serde(with = "super")] T);
+ struct Wrapper<T: AmountOps>(T);
+
+ impl<'de, T: AmountOps> Deserialize<'de> for Wrapper<T> {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ super::deserialize(d).map(Wrapper)
+ }
+ }
let mut out = Vec::with_capacity(internals::serde::cautious_size_hint::<X>(
seq.size_hint(),
@@ -501,9 +505,13 @@ pub mod as_btc {
where
A: SeqAccess<'de>,
{
- #[derive(Deserialize)]
- #[serde(transparent)]
- struct Wrapper(#[serde(with = "super")] SignedAmount);
+ struct Wrapper(SignedAmount);
+
+ impl<'de> Deserialize<'de> for Wrapper {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ super::deserialize(d).map(Wrapper)
+ }
+ }
let mut out = Vec::with_capacity(internals::serde::cautious_size_hint::<X>(
seq.size_hint(),
@@ -683,9 +691,13 @@ pub mod as_str {
where
A: SeqAccess<'de>,
{
- #[derive(Deserialize)]
- #[serde(transparent)]
- struct Wrapper(#[serde(with = "super")] SignedAmount);
+ struct Wrapper(SignedAmount);
+
+ impl<'de> Deserialize<'de> for Wrapper {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ super::deserialize(d).map(Wrapper)
+ }
+ }
let mut out = Vec::with_capacity(internals::serde::cautious_size_hint::<X>(
seq.size_hint(),
### units/src/fee_rate/serde.rs
@@ -138,9 +138,13 @@ pub mod as_sat_per_kwu_floor {
where
A: SeqAccess<'de>,
{
- #[derive(Deserialize)]
- #[serde(transparent)]
- struct Wrapper(#[serde(with = "super")] FeeRate);
+ struct Wrapper(FeeRate);
+
+ impl<'de> Deserialize<'de> for Wrapper {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ super::deserialize(d).map(Wrapper)
+ }
+ }
let mut out = Vec::with_capacity(
internals::serde::cautious_size_hint::<FeeRate>(seq.size_hint()),
@@ -274,9 +278,13 @@ pub mod as_sat_per_vb_floor {
where
A: SeqAccess<'de>,
{
- #[derive(Deserialize)]
- #[serde(transparent)]
- struct Wrapper(#[serde(with = "super")] FeeRate);
+ struct Wrapper(FeeRate);
+
+ impl<'de> Deserialize<'de> for Wrapper {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ super::deserialize(d).map(Wrapper)
+ }
+ }
let mut out = Vec::with_capacity(
internals::serde::cautious_size_hint::<FeeRate>(seq.size_hint()),
@@ -410,9 +418,13 @@ pub mod as_sat_per_vb_ceil {
where
A: SeqAccess<'de>,
{
- #[derive(Deserialize)]
- #[serde(transparent)]
- struct Wrapper(#[serde(with = "super")] FeeRate);
+ struct Wrapper(FeeRate);
+
+ impl<'de> Deserialize<'de> for Wrapper {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
+ super::deserialize(d).map(Wrapper)
+ }
+ }
let mut out = Vec::with_capacity(
internals::serde::cautious_size_hint::<FeeRate>(seq.size_hint()),
### units/src/pow.rs
@@ -9,7 +9,7 @@ use core::ops::{Add, Sub};
use arbitrary::{Arbitrary, Unstructured};
use internals::u256::U256;
#[cfg(feature = "serde")]
-use serde::{Deserialize, Serialize};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::internal_macros::impl_fmt_traits_for_u32_wrapper;
use crate::parse_int::{self, PrefixedHexError, UnprefixedHexError};
@@ -93,14 +93,35 @@ macro_rules! do_impl {
U256::from_str(s).map($ty).map_err($err_ty)
}
}
+
+ #[cfg(feature = "serde")]
+ impl Serialize for $ty {
+ #[inline]
+ fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ U256::serialize(&self.0, s)
+ }
+ }
+
+ #[cfg(feature = "serde")]
+ impl<'de> Deserialize<'de> for $ty {
+ #[inline]
+ fn deserialize<D>(d: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok($ty(U256::deserialize(d)?))
+ }
+ }
};
}
/// A 256 bit integer representing work.
///
/// Work is a measure of how difficult it is to find a hash below a given [`Target`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Work(U256);
impl Work {
@@ -159,7 +180,6 @@ impl Sub for Work {
///
/// ref: <https://en.bitcoin.it/wiki/Target>
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Target(U256);
impl Target {
@@ -318,7 +338,6 @@ impl_fmt_traits_for_u32_wrapper!(Target);
/// Equality is on the consensus encoded `u32`. Conversion to [`Target`] is lossy, so this type is
/// not ordered. To compare two targets convert them first.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompactTarget(u32);
impl CompactTarget {
@@ -382,6 +401,28 @@ impl fmt::Display for CompactTarget {
parse_int::impl_parse_str_from_int_infallible!(CompactTarget, u32, from_consensus);
+#[cfg(feature = "serde")]
+impl Serialize for CompactTarget {
+ #[inline]
+ fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ u32::serialize(&self.to_consensus_u32(), s)
+ }
+}
+
+#[cfg(feature = "serde")]
+impl<'de> Deserialize<'de> for CompactTarget {
+ #[inline]
+ fn deserialize<D>(d: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok(Self::from_consensus(u32::deserialize(d)?))
+ }
+}
+
impl TryFrom<CompactTarget> for Target {
type Error = InvalidCompactTargetError;
### units/src/sequence.rs
@@ -19,7 +19,7 @@ use core::fmt;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
#[cfg(feature = "serde")]
-use serde::{Deserialize, Serialize};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::locktime::relative::error::TimeOverflowError;
use crate::locktime::relative::{self, NumberOf512Seconds};
@@ -32,7 +32,6 @@ pub use self::error::SequenceDecoderError;
/// Bitcoin transaction input sequence number.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
-#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Sequence(u32);
impl Sequence {
@@ -246,6 +245,28 @@ impl fmt::Debug for Sequence {
parse_int::impl_parse_str_from_int_infallible!(Sequence, u32, from_consensus);
+#[cfg(feature = "serde")]
+impl Serialize for Sequence {
+ #[inline]
+ fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ u32::serialize(&self.to_consensus_u32(), s)
+ }
+}
+
+#[cfg(feature = "serde")]
+impl<'de> Deserialize<'de> for Sequence {
+ #[inline]
+ fn deserialize<D>(d: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok(Self::from_consensus(u32::deserialize(d)?))
+ }
+}
+
#[cfg(feature = "encoding")]
impl encoding::Encode for Sequence {
type Encoder<'e> = SequenceEncoder<'e>;Why this scored 19/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.