Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Upgrade to libp2p v0.19 - Changes the default PeerId representation #6064

Merged
merged 10 commits into from
May 18, 2020
37 changes: 28 additions & 9 deletions client/network/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use libp2p::{
};
#[cfg(not(target_os = "unknown"))]
use libp2p::{tcp, dns, websocket};
use libp2p::core::{self, upgrade, transport::boxed::Boxed, transport::OptionalTransport, muxing::StreamMuxerBox};
use libp2p::core::{self, either::{EitherError, EitherOutput}, upgrade, transport::boxed::Boxed, transport::OptionalTransport, muxing::StreamMuxerBox};
use std::{io, sync::Arc, time::Duration, usize};

pub use self::bandwidth::BandwidthSinks;
Expand All @@ -42,14 +42,22 @@ pub fn build_transport(
) -> (Boxed<(PeerId, StreamMuxerBox), io::Error>, Arc<bandwidth::BandwidthSinks>) {
// Build configuration objects for encryption mechanisms.
let noise_config = {
let noise_keypair = noise::Keypair::<noise::X25519>::new().into_authentic(&keypair)
// For more information about this panic, see in "On the Importance of Checking
// Cryptographic Protocols for Faults" by Dan Boneh, Richard A. DeMillo,
// and Richard J. Lipton.
// For more information about these two panics, see in "On the Importance of
// Checking Cryptographic Protocols for Faults" by Dan Boneh, Richard A. DeMillo,
// and Richard J. Lipton.
let noise_keypair_legacy = noise::Keypair::<noise::X25519>::new().into_authentic(&keypair)
.expect("can only fail in case of a hardware bug; since this signing is performed only \
once and at initialization, we're taking the bet that the inconvenience of a very \
rare panic here is basically zero");
noise::NoiseConfig::ix(noise_keypair)
let noise_keypair_spec = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&keypair)
.expect("can only fail in case of a hardware bug; since this signing is performed only \
once and at initialization, we're taking the bet that the inconvenience of a very \
rare panic here is basically zero");

core::upgrade::SelectUpgrade::new(
noise::NoiseConfig::ix(noise_keypair_legacy),
noise::NoiseConfig::xx(noise_keypair_spec)
romanb marked this conversation as resolved.
Show resolved Hide resolved
)
};

// Build configuration objects for multiplexing mechanisms.
Expand Down Expand Up @@ -97,11 +105,22 @@ pub fn build_transport(
// Encryption
let transport = transport.and_then(move |stream, endpoint| {
core::upgrade::apply(stream, noise_config, endpoint, upgrade::Version::V1)
.and_then(|(remote_id, out)| async move {
let remote_key = match remote_id {
noise::RemoteIdentity::IdentityKey(key) => key,
.map_err(|err|
err.map_err(|err| match err {
EitherError::A(err) => err,
EitherError::B(err) => err,
})
)
.and_then(|result| async move {
let remote_key = match &result {
EitherOutput::First((noise::RemoteIdentity::IdentityKey(key), _)) => key.clone(),
EitherOutput::Second((noise::RemoteIdentity::IdentityKey(key), _)) => key.clone(),
_ => return Err(upgrade::UpgradeError::Apply(noise::NoiseError::InvalidKey))
};
let out = match result {
EitherOutput::First((_, o)) => o,
EitherOutput::Second((_, o)) => o,
};
Ok((out, remote_key.into_peer_id()))
})
});
Expand Down