use binary_sv2::{self, Deserialize, Serialize};
#[cfg(feature = "noise_sv2")]
use codec_sv2::{
Error, HandshakeRole, NoiseEncoder, StandardEitherFrame, StandardNoiseDecoder,
StandardSv2Frame, State,
};
#[cfg(feature = "noise_sv2")]
use framing_sv2::framing::Sv2Frame;
#[cfg(feature = "noise_sv2")]
use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey};
#[cfg(feature = "noise_sv2")]
use noise_sv2::{Initiator, Responder};
#[cfg(feature = "noise_sv2")]
use noise_sv2::{ELLSWIFT_ENCODING_SIZE, INITIATOR_EXPECTED_HANDSHAKE_MESSAGE_SIZE};
use std::convert::TryInto;
#[cfg(feature = "noise_sv2")]
use std::{
io::{Read, Write},
net::{TcpListener, TcpStream},
};
#[cfg(feature = "noise_sv2")]
const CUSTOM_MSG_TYPE: u8 = 0xff;
#[cfg(feature = "noise_sv2")]
const TCP_ADDR: &str = "127.0.0.1:3333";
#[cfg(feature = "noise_sv2")]
const AUTHORITY_PUBLIC_K: &str = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72";
#[cfg(feature = "noise_sv2")]
const AUTHORITY_PRIVATE_K: &str = "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n";
#[cfg(feature = "noise_sv2")]
const CERT_VALIDITY: std::time::Duration = std::time::Duration::from_secs(3600);
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CustomMessage {
nonce: u16,
}
#[cfg(feature = "noise_sv2")]
fn main() {
let listener_receiver = TcpListener::bind(TCP_ADDR).expect("Failed to bind TCP listener");
let mut stream_sender: TcpStream =
TcpStream::connect(TCP_ADDR).expect("Failed to connect to TCP stream");
let mut stream_receiver: TcpStream = listener_receiver
.incoming()
.next()
.expect("Failed to accept incoming TCP stream")
.expect("Failed to connect to incoming TCP stream");
let authority_public_k: Secp256k1PublicKey = AUTHORITY_PUBLIC_K
.to_string()
.try_into()
.expect("Failed to convert receiver public key to Secp256k1PublicKey");
let authority_private_k: Secp256k1SecretKey = AUTHORITY_PRIVATE_K
.to_string()
.try_into()
.expect("Failed to convert receiver private key to Secp256k1PublicKey");
#[cfg(feature = "std")]
let initiator = Initiator::from_raw_k(authority_public_k.into_bytes())
.expect("Failed to create initiator role from raw pub key");
#[cfg(not(feature = "std"))]
let initiator =
Initiator::from_raw_k_with_rng(authority_public_k.into_bytes(), &mut rand::thread_rng())
.expect("Failed to create initiator role from raw pub key");
#[cfg(feature = "std")]
let responder = Responder::from_authority_kp(
&authority_public_k.into_bytes(),
&authority_private_k.into_bytes(),
CERT_VALIDITY,
)
.expect("Failed to initialize responder from pub/key pair and/or cert");
#[cfg(not(feature = "std"))]
let responder = Responder::from_authority_kp_with_rng(
&authority_public_k.into_bytes(),
&authority_private_k.into_bytes(),
CERT_VALIDITY,
&mut rand::thread_rng(),
)
.expect("Failed to initialize responder from pub/key pair and/or cert");
let mut sender_state = State::initialized(HandshakeRole::Initiator(initiator));
let mut receiver_state = State::initialized(HandshakeRole::Responder(responder));
let first_message = sender_state
.step_0()
.expect("Initiator failed first step of handshake");
let first_message: [u8; ELLSWIFT_ENCODING_SIZE] = first_message
.get_payload_when_handshaking()
.try_into()
.expect("Handshake remote invlaid message");
#[cfg(feature = "std")]
let (second_message, receiver_state) = receiver_state
.step_1(first_message)
.expect("Responder failed second step of handshake");
#[cfg(not(feature = "std"))]
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as u32;
#[cfg(not(feature = "std"))]
let (second_message, receiver_state) = receiver_state
.step_1_with_now_rng(first_message, now, &mut rand::thread_rng())
.expect("Responder failed second step of handshake");
let second_message: [u8; INITIATOR_EXPECTED_HANDSHAKE_MESSAGE_SIZE] = second_message
.get_payload_when_handshaking()
.try_into()
.expect("Handshake remote invlaid message");
#[cfg(feature = "std")]
let sender_state = sender_state
.step_2(second_message)
.expect("Initiator failed third step of handshake");
#[cfg(not(feature = "std"))]
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as u32;
#[cfg(not(feature = "std"))]
let sender_state = sender_state
.step_2_with_now(second_message, now)
.expect("Initiator failed third step of handshake");
let mut sender_state = match sender_state {
State::Transport(c) => State::with_transport_mode(c),
_ => panic!("todo"),
};
let mut receiver_state = match receiver_state {
State::Transport(c) => State::with_transport_mode(c),
_ => panic!("todo"),
};
let nonce = 1337;
let msg = CustomMessage { nonce };
let msg_type = CUSTOM_MSG_TYPE;
let extension_type = 0;
let channel_msg = false;
let frame = StandardEitherFrame::<CustomMessage>::Sv2(
Sv2Frame::from_message(msg, msg_type, extension_type, channel_msg)
.expect("Failed to create the frame"),
);
let mut encoder = NoiseEncoder::<CustomMessage>::new();
let encoded_frame = encoder
.encode(frame, &mut sender_state)
.expect("Failed to encode the frame");
stream_sender
.write_all(&encoded_frame[..])
.expect("Failed to send the encoded frame");
let mut decoder = StandardNoiseDecoder::<CustomMessage>::new();
let mut decoded_frame;
loop {
let decoder_buf = decoder.writable();
stream_receiver
.read_exact(decoder_buf)
.expect("Failed to read the encoded frame header");
let result = decoder.next_frame(&mut receiver_state);
match result {
Ok(frame) => {
let frame: StandardSv2Frame<CustomMessage> = frame
.try_into()
.expect("Failed to decode frame into Sv2Frame");
decoded_frame = frame;
break;
}
Err(Error::MissingBytes(_)) => {}
Err(_) => panic!("Failed to decode the frame"),
}
}
let decoded_frame_header = decoded_frame
.get_header()
.expect("Failed to get the frame header");
let decoded_msg: CustomMessage = binary_sv2::from_bytes(decoded_frame.payload())
.expect("Failed to extract the message from the payload");
assert_eq!(decoded_frame_header.msg_type(), CUSTOM_MSG_TYPE);
assert_eq!(decoded_msg.nonce, nonce);
}
#[cfg(not(feature = "noise_sv2"))]
fn main() {
eprintln!("Noise feature not enabled. Skipping example.");
}