What changed, and why it matters
This commit adds a new way to serialize (convert to bytes) Bitcoin peer-to-peer network addresses in the rust-bitcoin library. It does not appear to fix or introduce a security vulnerability; it is a routine implementation change replacing one serialization method with another while preserving the documented big-endian byte order.
No security action required. Reviewers may verify that the new encoder produces byte-for-byte identical output to the previous `serialize` implementation for representative addresses, including IPv4-mapped IPv6 addresses and various ports.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces AddressEncoder using the crate’s encoding framework for the Address type. It encodes service flags, a 16-byte IPv6-compatible address, and a 2-byte port in network byte order (big-endian), matching the Bitcoin P2P protocol specification. The existing test is updated to use encoding::encode_to_vec instead of the older serialize function. No security-relevant behavior change is evident from the diff.
Changed components
rust-bitcoin p2p/src/address.rsAddress serialization/encodingInspect captured patch +34 / −1
diff --git a/p2p/src/address.rs b/p2p/src/address.rs
index 07b4fe9b..1c502d5d 100644
--- a/p2p/src/address.rs
+++ b/p2p/src/address.rs
@@ -154,6 +154,39 @@ impl ToSocketAddrs for Address {
}
}
+encoding::encoder_newtype! {
+ /// The encoder for the [`Address`] type.
+ pub struct AddressEncoder<'e>(encoding::Encoder3<
+ crate::ServiceFlagsEncoder<'e>,
+ encoding::ArrayEncoder<16>,
+ encoding::ArrayEncoder<2>
+ >);
+}
+
+impl encoding::Encodable for Address {
+ type Encoder<'e>
+ = AddressEncoder<'e>
+ where
+ Self: 'e;
+
+ fn encoder(&self) -> Self::Encoder<'_> {
+ let mut address: [u8; 16] = [0; 16];
+ for (index, value) in self.address.iter().enumerate() {
+ let arr: [u8; 2] = value.to_be_bytes();
+ address[index * 2] = arr[0];
+ address[index * 2 + 1] = arr[1];
+ }
+
+ let enc = encoding::Encoder3::new(
+ self.services.encoder(),
+ encoding::ArrayEncoder::without_length_prefix(address),
+ encoding::ArrayEncoder::without_length_prefix(self.port.to_be_bytes()),
+ );
+
+ AddressEncoder::new(enc)
+ }
+}
+
type AddressInnerDecoder = encoding::Decoder3<
crate::ServiceFlagsDecoder,
encoding::ArrayDecoder<16>,
@@ -947,7 +980,7 @@ mod test {
#[test]
fn serialize_address() {
assert_eq!(
- serialize(&Address {
+ encoding::encode_to_vec(&Address {
services: ServiceFlags::NETWORK,
address: [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001],
port: 8333
Why this scored 17/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.