What changed, and why it matters
This commit adds a new decoder for Bitcoin peer-to-peer network message headers in the rust-bitcoin library. It is a straightforward feature addition that exposes a way to parse v1 message headers using the existing consensus encoding framework. There is no indication of a security bug, fix, or vulnerability.
No security action required. Review as normal code/feature addition if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces V1MessageHeaderDecoder and implements the encoding::Decoder and encoding::Decodable traits for V1MessageHeader. It composes four existing decoders (4-byte magic, command string, 4-byte little-endian length, 4-byte checksum) and converts the length field from little-endian bytes to a u32. The existing impl_consensus_encoding! macro call remains. No bounds checks, memory safety, or cryptographic logic is altered beyond delegating to pre-existing primitives.
Changed components
p2p/src/message.rsV1MessageHeaderV1MessageHeaderDecoderInspect captured patch +58 / −0
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 555c5a38..7c170008 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -255,6 +255,64 @@ pub struct V1MessageHeader {
pub checksum: [u8; 4],
}
+type V1MessageHeaderInnerDecoder = encoding::Decoder4<
+ encoding::ArrayDecoder<4>,
+ CommandStringDecoder,
+ encoding::ArrayDecoder<4>,
+ encoding::ArrayDecoder<4>,
+ >;
+
+/// The Decoder for `V1MessageHeader`
+pub struct V1MessageHeaderDecoder(V1MessageHeaderInnerDecoder);
+
+impl encoding::Decoder for V1MessageHeaderDecoder {
+ type Output = V1MessageHeader;
+ type Error = V1MessageHeaderDecoderError;
+
+ #[inline]
+ fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
+ self.0.push_bytes(bytes).map_err(V1MessageHeaderDecoderError)
+ }
+
+ #[inline]
+ fn end(self) -> Result<Self::Output, Self::Error> {
+ let (magic, command, length, checksum) = self.0.end().map_err(V1MessageHeaderDecoderError)?;
+ Ok(
+ V1MessageHeader {
+ magic: Magic(magic),
+ command,
+ length: u32::from_le_bytes(length),
+ checksum
+ }
+ )
+ }
+
+ #[inline]
+ fn read_limit(&self) -> usize { self.0.read_limit() }
+}
+
+impl encoding::Decodable for V1MessageHeader {
+ type Decoder = V1MessageHeaderDecoder;
+ fn decoder() -> Self::Decoder {
+ V1MessageHeaderDecoder (encoding::Decoder4::new(
+ encoding::ArrayDecoder::<4>::new(),
+ CommandString::decoder(),
+ encoding::ArrayDecoder::<4>::new(),
+ encoding::ArrayDecoder::<4>::new()
+ ))
+ }
+}
+
+/// An error consensus decoding a [`V1MessageHeaderDecoderError`].
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct V1MessageHeaderDecoderError(<V1MessageHeaderInnerDecoder as encoding::Decoder>::Error);
+
+impl fmt::Display for V1MessageHeaderDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ internals::write_err!(f, "message header decoder error"; self.0)
+ }
+}
+
impl_consensus_encoding!(V1MessageHeader, magic, command, length, checksum);
/// A Network message using the v2 p2p protocol defined in BIP-0324.
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.