Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade bip324 to 0.6.0 #223

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 55 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bitcoin = { version = "0.32.4", default-features = false, features = [
"rand-std",
] }
bitcoin_hashes = "0.14.0"
bip324 = { version = "0.5.0", default-features = false, features = [
bip324 = { version = "0.6.0", default-features = false, features = [
"std",
"alloc",
"tokio",
Expand Down
57 changes: 19 additions & 38 deletions src/network/peer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
extern crate tokio;
use std::{ops::DerefMut, time::Duration};

use bip324::{Handshake, PacketReader, PacketWriter, Role};
use bip324::{AsyncProtocol, PacketReader, PacketWriter, Role};
use bitcoin::{p2p::ServiceFlags, Network};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
io::{AsyncRead, AsyncWrite, AsyncWriteExt},
select,
sync::{
mpsc::{self, Receiver, Sender},
Expand Down Expand Up @@ -39,7 +39,6 @@ use super::parsers::V2MessageParser;

const MESSAGE_TIMEOUT: u64 = 2;
const HANDSHAKE_TIMEOUT: u64 = 4;
const MAX_RESPONSE: [u8; 4111] = [0; 4111];

type MutexMessageGenerator = Mutex<Box<dyn MessageGenerator>>;

Expand Down Expand Up @@ -402,40 +401,22 @@ impl Peer {
self.dialog
.send_dialog("Initiating a handshake for encrypted messaging")
.await;
let mut public_key = [0; 64];
let mut handshake = Handshake::new(self.network, Role::Initiator, None, &mut public_key)
.map_err(|_| PeerError::HandshakeFailed)?;
self.write_bytes(writer, public_key.to_vec()).await?;
let mut remote_public_key = [0u8; 64];
let _ = reader
.read_exact(&mut remote_public_key)
.await
.map_err(|_| PeerError::Reader)?;
let mut local_garbage_terminator_message = [0u8; 36];
handshake
.complete_materials(
remote_public_key,
&mut local_garbage_terminator_message,
None,
)
.map_err(|_| PeerError::HandshakeFailed)?;
self.write_bytes(writer, local_garbage_terminator_message.to_vec())
.await?;
let mut max_response = MAX_RESPONSE;
let size = reader
.read(&mut max_response)
.await
.map_err(|_| PeerError::Reader)?;
let response = &mut max_response[..size];
handshake
.authenticate_garbage_and_version(response)
.map_err(|_| PeerError::HandshakeFailed)?;
let packet_handler = handshake
.finalize()
.map_err(|_| PeerError::HandshakeFailed)?;
self.dialog
.send_dialog("Established an encrypted connection")
.await;
Ok(packet_handler.into_split())
let handshake =
AsyncProtocol::new(self.network, Role::Initiator, None, None, reader, writer).await;
match handshake {
Ok(proto) => {
self.dialog
.send_dialog("Established an encrypted connection")
.await;
let (reader, writer) = proto.into_split();
Ok((reader.decoder(), writer.encoder()))
}
Err(e) => {
self.dialog
.send_dialog(format!("V2 handshake failed with description {e}"))
.await;
Err(PeerError::HandshakeFailed)
}
}
}
}
Loading