Skip to content

Commit

Permalink
WIP public and finish deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed May 10, 2024
1 parent 9151382 commit de68754
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate std;
mod chacha20poly1305;
mod fschacha20poly1305;
mod hkdf;
mod v2;
pub mod serde;

use core::fmt;

Expand Down
20 changes: 16 additions & 4 deletions protocol/src/v2.rs → protocol/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,28 @@ pub fn serialize_with_alloc(msg: NetworkMessage) -> Result<alloc::vec::Vec<u8>,
pub fn deserialize(buffer: &[u8]) -> Result<NetworkMessage, Error> {
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(),
}),
}
}
Expand Down

0 comments on commit de68754

Please sign in to comment.