Exchange public key WIP

This commit is contained in:
folex
2020-03-16 21:00:06 +03:00
parent 912b4d7021
commit 13c48c0f19
4 changed files with 31 additions and 17 deletions

View File

@@ -1752,6 +1752,7 @@ impl From<kbucket::EntryView<kbucket::Key<PeerId>, Contact>> for KadPeer {
fn from(e: kbucket::EntryView<kbucket::Key<PeerId>, Contact>) -> KadPeer {
let Contact { addresses, public_key } = e.node.value;
KadPeer {
public_key,
node_id: e.node.key.into_preimage(),
multiaddrs: addresses.into_vec(),
connection_ty: match e.status {

View File

@@ -18,7 +18,8 @@
use crate::Addresses;
use libp2p_core::{PublicKey, Multiaddr};
use libp2p_core::{Multiaddr};
use libp2p_core::identity::ed25519::PublicKey;
#[derive(Clone)]
pub struct Contact {

View File

@@ -63,7 +63,9 @@ message Message {
// used to signal the sender's connection capabilities to the peer
ConnectionType connection = 3;
bytes publicKey = 4;
oneof publicKey {
bytes bytes = 4;
}
}
// defines what type of message it is.

View File

@@ -26,20 +26,23 @@
//! to poll the underlying transport for incoming messages, and the `Sink` component
//! 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::{io, iter};
use bytes::BytesMut;
use codec::UviBytes;
use futures::prelude::*;
use futures_codec::Framed;
use prost::Message;
use unsigned_varint::codec;
use wasm_timer::Instant;
use libp2p_core::{Multiaddr, PeerId};
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.
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.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KadPeer {
pub public_key: PublicKey,
pub public_key: Option<PublicKey>,
/// Identifier of the peer.
pub node_id: PeerId,
/// 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"))?
.into();
let public_key = PublicKey::decode(peer.public_key.as_slice())
.map_err(|e|
invalid_data(format!("invalid public key: {}", e).as_str())
)?;
let public_key = match peer.public_key {
Some(proto::message::peer::PublicKey::Bytes(bytes)) => {
PublicKey::decode(bytes.as_slice())
.map(Some)
.map_err(|e|
invalid_data(format!("invalid public key: {}", e).as_str())
)
}
None => Ok(None),
}?;
Ok(KadPeer {
public_key,
@@ -130,6 +139,7 @@ impl TryFrom<proto::message::Peer> for KadPeer {
impl Into<proto::message::Peer> for KadPeer {
fn into(self) -> proto::message::Peer {
use proto::message::peer::PublicKey;
proto::message::Peer {
id: self.node_id.into_bytes(),
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();
ct as i32
},
public_key: self.public_key.encode().to_vec()
public_key: self.public_key.map(|pk| PublicKey::Bytes(pk.encode().to_vec()))
}
}
}