mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-08-01 01:11:58 +00:00
Exchange public key WIP
This commit is contained in:
@@ -1752,6 +1752,7 @@ impl From<kbucket::EntryView<kbucket::Key<PeerId>, Contact>> for KadPeer {
|
|||||||
fn from(e: kbucket::EntryView<kbucket::Key<PeerId>, Contact>) -> KadPeer {
|
fn from(e: kbucket::EntryView<kbucket::Key<PeerId>, Contact>) -> KadPeer {
|
||||||
let Contact { addresses, public_key } = e.node.value;
|
let Contact { addresses, public_key } = e.node.value;
|
||||||
KadPeer {
|
KadPeer {
|
||||||
|
public_key,
|
||||||
node_id: e.node.key.into_preimage(),
|
node_id: e.node.key.into_preimage(),
|
||||||
multiaddrs: addresses.into_vec(),
|
multiaddrs: addresses.into_vec(),
|
||||||
connection_ty: match e.status {
|
connection_ty: match e.status {
|
||||||
|
@@ -18,7 +18,8 @@
|
|||||||
|
|
||||||
|
|
||||||
use crate::Addresses;
|
use crate::Addresses;
|
||||||
use libp2p_core::{PublicKey, Multiaddr};
|
use libp2p_core::{Multiaddr};
|
||||||
|
use libp2p_core::identity::ed25519::PublicKey;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Contact {
|
pub struct Contact {
|
||||||
|
@@ -63,7 +63,9 @@ message Message {
|
|||||||
// used to signal the sender's connection capabilities to the peer
|
// used to signal the sender's connection capabilities to the peer
|
||||||
ConnectionType connection = 3;
|
ConnectionType connection = 3;
|
||||||
|
|
||||||
bytes publicKey = 4;
|
oneof publicKey {
|
||||||
|
bytes bytes = 4;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// defines what type of message it is.
|
// defines what type of message it is.
|
||||||
|
@@ -26,20 +26,23 @@
|
|||||||
//! to poll the underlying transport for incoming messages, and the `Sink` component
|
//! to poll the underlying transport for incoming messages, and the `Sink` component
|
||||||
//! is used to send messages to remote peers.
|
//! is used to send messages to remote peers.
|
||||||
|
|
||||||
use bytes::BytesMut;
|
|
||||||
use codec::UviBytes;
|
|
||||||
use crate::dht_proto as proto;
|
|
||||||
use crate::record::{self, Record};
|
|
||||||
use futures::prelude::*;
|
|
||||||
use futures_codec::Framed;
|
|
||||||
use libp2p_core::{Multiaddr, PeerId};
|
|
||||||
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
|
||||||
use prost::Message;
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, time::Duration};
|
use std::{borrow::Cow, convert::TryFrom, time::Duration};
|
||||||
use std::{io, iter};
|
use std::{io, iter};
|
||||||
|
|
||||||
|
use bytes::BytesMut;
|
||||||
|
use codec::UviBytes;
|
||||||
|
use futures::prelude::*;
|
||||||
|
use futures_codec::Framed;
|
||||||
|
use prost::Message;
|
||||||
use unsigned_varint::codec;
|
use unsigned_varint::codec;
|
||||||
use wasm_timer::Instant;
|
use wasm_timer::Instant;
|
||||||
|
|
||||||
|
use libp2p_core::{Multiaddr, PeerId};
|
||||||
use libp2p_core::identity::ed25519::PublicKey;
|
use libp2p_core::identity::ed25519::PublicKey;
|
||||||
|
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||||
|
|
||||||
|
use crate::dht_proto as proto;
|
||||||
|
use crate::record::{self, Record};
|
||||||
|
|
||||||
/// The protocol name used for negotiating with multistream-select.
|
/// The protocol name used for negotiating with multistream-select.
|
||||||
pub const DEFAULT_PROTO_NAME: &[u8] = b"/ipfs/kad/1.0.0";
|
pub const DEFAULT_PROTO_NAME: &[u8] = b"/ipfs/kad/1.0.0";
|
||||||
@@ -84,7 +87,7 @@ impl Into<proto::message::ConnectionType> for KadConnectionType {
|
|||||||
/// Information about a peer, as known by the sender.
|
/// Information about a peer, as known by the sender.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct KadPeer {
|
pub struct KadPeer {
|
||||||
pub public_key: PublicKey,
|
pub public_key: Option<PublicKey>,
|
||||||
/// Identifier of the peer.
|
/// Identifier of the peer.
|
||||||
pub node_id: PeerId,
|
pub node_id: PeerId,
|
||||||
/// The multiaddresses that the sender think can be used in order to reach the peer.
|
/// The multiaddresses that the sender think can be used in order to reach the peer.
|
||||||
@@ -114,10 +117,16 @@ impl TryFrom<proto::message::Peer> for KadPeer {
|
|||||||
.ok_or_else(|| invalid_data("unknown connection type"))?
|
.ok_or_else(|| invalid_data("unknown connection type"))?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
let public_key = PublicKey::decode(peer.public_key.as_slice())
|
let public_key = match peer.public_key {
|
||||||
.map_err(|e|
|
Some(proto::message::peer::PublicKey::Bytes(bytes)) => {
|
||||||
invalid_data(format!("invalid public key: {}", e).as_str())
|
PublicKey::decode(bytes.as_slice())
|
||||||
)?;
|
.map(Some)
|
||||||
|
.map_err(|e|
|
||||||
|
invalid_data(format!("invalid public key: {}", e).as_str())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
None => Ok(None),
|
||||||
|
}?;
|
||||||
|
|
||||||
Ok(KadPeer {
|
Ok(KadPeer {
|
||||||
public_key,
|
public_key,
|
||||||
@@ -130,6 +139,7 @@ impl TryFrom<proto::message::Peer> for KadPeer {
|
|||||||
|
|
||||||
impl Into<proto::message::Peer> for KadPeer {
|
impl Into<proto::message::Peer> for KadPeer {
|
||||||
fn into(self) -> proto::message::Peer {
|
fn into(self) -> proto::message::Peer {
|
||||||
|
use proto::message::peer::PublicKey;
|
||||||
proto::message::Peer {
|
proto::message::Peer {
|
||||||
id: self.node_id.into_bytes(),
|
id: self.node_id.into_bytes(),
|
||||||
addrs: self.multiaddrs.into_iter().map(|a| a.to_vec()).collect(),
|
addrs: self.multiaddrs.into_iter().map(|a| a.to_vec()).collect(),
|
||||||
@@ -137,7 +147,7 @@ impl Into<proto::message::Peer> for KadPeer {
|
|||||||
let ct: proto::message::ConnectionType = self.connection_ty.into();
|
let ct: proto::message::ConnectionType = self.connection_ty.into();
|
||||||
ct as i32
|
ct as i32
|
||||||
},
|
},
|
||||||
public_key: self.public_key.encode().to_vec()
|
public_key: self.public_key.map(|pk| PublicKey::Bytes(pk.encode().to_vec()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user