From de68754e541b1bd4c71882ab07e147bd35df1dbe Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Fri, 10 May 2024 12:20:01 -0700 Subject: [PATCH] WIP public and finish deserialization --- protocol/src/lib.rs | 2 +- protocol/src/{v2.rs => serde.rs} | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) rename protocol/src/{v2.rs => serde.rs} (91%) diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index a392705..56e4a9d 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -11,7 +11,7 @@ extern crate std; mod chacha20poly1305; mod fschacha20poly1305; mod hkdf; -mod v2; +pub mod serde; use core::fmt; diff --git a/protocol/src/v2.rs b/protocol/src/serde.rs similarity index 91% rename from protocol/src/v2.rs rename to protocol/src/serde.rs index 8fb224f..2942815 100644 --- a/protocol/src/v2.rs +++ b/protocol/src/serde.rs @@ -248,16 +248,28 @@ pub fn serialize_with_alloc(msg: NetworkMessage) -> Result, pub fn deserialize(buffer: &[u8]) -> Result { let short_id = buffer[0]; match short_id { - // zero-byte means the command is encoded in the next 12 bytes. + // Zero-byte means the command is encoded in the next 12 bytes. 0u8 => { let mut command_buffer = &buffer[1..13]; let command = CommandString::consensus_decode(&mut command_buffer) .map_err(|_| Error::Deserialize)?; - match command { - "verack" => {} + let mut payload_buffer = &buffer[13..]; + // There are a handful of "known" messages which don't use a short ID, otherwise Unknown. + match command.as_ref() { + "verack" => Ok(NetworkMessage::Verack), + "sendheaders" => Ok(NetworkMessage::SendHeaders), + "getaddr" => Ok(NetworkMessage::GetAddr), + "wtxidrelay" => Ok(NetworkMessage::WtxidRelay), + "addrv2" => Ok(NetworkMessage::SendAddrV2), + "alert" => Ok(NetworkMessage::Alert(Decodable::consensus_decode( + &mut payload_buffer, + ))), + "reject" => Ok(NetworkMessage::Reject(Decodable::consensus_decode( + &mut payload_buffer, + ))), _ => Ok(NetworkMessage::Unknown { command, - payload: buffer[13..].to_vec(), + payload: payload_buffer.to_vec(), }), } }