Skip to content

Commit

Permalink
Move handshake_message_to_frame to..
Browse files Browse the repository at this point in the history
`HandShakeFrame` as `HandShake::from_message`
  • Loading branch information
jbesraa committed Jul 16, 2024
1 parent 302670d commit 07f18c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 9 additions & 3 deletions protocols/v2/codec-sv2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use noise_sv2::{self, Initiator, NoiseCodec, Responder};

pub use buffer_sv2;

pub use framing_sv2::{self, framing::handshake_message_to_frame as h2f};
pub use framing_sv2::{self};

#[cfg(feature = "noise_sv2")]
#[derive(Debug)]
Expand All @@ -49,7 +49,10 @@ impl State {
pub fn step_0(&mut self) -> core::result::Result<HandShakeFrame, Error> {
match self {
Self::HandShake(h) => match h {
HandshakeRole::Initiator(i) => i.step_0().map_err(|e| e.into()).map(h2f),
HandshakeRole::Initiator(i) => i
.step_0()
.map_err(|e| e.into())
.map(HandShakeFrame::from_message),
HandshakeRole::Responder(_) => Err(Error::InvalidStepForResponder),
},
_ => Err(Error::NotInHandShakeState),
Expand All @@ -64,7 +67,10 @@ impl State {
Self::HandShake(h) => match h {
HandshakeRole::Responder(r) => {
let (message, codec) = r.step_1(re_pub)?;
Ok((h2f(message), Self::Transport(codec)))
Ok((
HandShakeFrame::from_message(message),
Self::Transport(codec),
))
}
HandshakeRole::Initiator(_) => Err(Error::InvalidStepForInitiator),
},
Expand Down
20 changes: 10 additions & 10 deletions protocols/v2/framing-sv2/src/framing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ impl HandShakeFrame {
Ok(Self::from_bytes_unchecked(bytes))
}

/// Returns a `HandShakeFrame` from a generic byte array
#[allow(clippy::useless_conversion)]
pub fn from_message<T: AsRef<[u8]>>(message: T) -> HandShakeFrame {
let mut payload = Vec::new();
payload.extend_from_slice(message.as_ref());
HandShakeFrame {
payload: payload.into(),
}
}

#[inline]
pub fn from_bytes_unchecked(bytes: Slice) -> Self {
Self { payload: bytes }
Expand Down Expand Up @@ -239,16 +249,6 @@ impl<T: Serialize + GetSize, B: AsMut<[u8]> + AsRef<[u8]>> TryFrom<Frame<T, B>>
}
}

/// Returns a `HandShakeFrame` from a generic byte array
#[allow(clippy::useless_conversion)]
pub fn handshake_message_to_frame<T: AsRef<[u8]>>(message: T) -> HandShakeFrame {
let mut payload = Vec::new();
payload.extend_from_slice(message.as_ref());
HandShakeFrame {
payload: payload.into(),
}
}

/// Basically a boolean bit filter for `extension_type`.
/// Takes an `extension_type` represented as a `u16` and a boolean flag (`channel_msg`).
/// If `channel_msg` is true, it sets the most significant bit of `extension_type` to 1,
Expand Down

0 comments on commit 07f18c7

Please sign in to comment.