mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 01:01:34 +00:00
Use stronger typing for the public key of identify (#236)
* Use stronger typing for the public key of identify * Add PartialEq/Eq implementations * Fix tests * Also change is_public_key()
This commit is contained in:
@ -20,7 +20,7 @@
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{future, Future, Sink, Stream};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PublicKeyBytes};
|
||||
use log::Level;
|
||||
use multiaddr::Multiaddr;
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
@ -83,7 +83,7 @@ where
|
||||
let mut message = structs_proto::Identify::new();
|
||||
message.set_agentVersion(info.agent_version);
|
||||
message.set_protocolVersion(info.protocol_version);
|
||||
message.set_publicKey(info.public_key);
|
||||
message.set_publicKey(info.public_key.0);
|
||||
message.set_listenAddrs(listen_addrs);
|
||||
message.set_observedAddr(observed_addr.to_bytes());
|
||||
message.set_protocols(RepeatedField::from_vec(info.protocols));
|
||||
@ -100,8 +100,8 @@ where
|
||||
/// Information sent from the listener to the dialer.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IdentifyInfo {
|
||||
/// Public key of the node in the DER format.
|
||||
pub public_key: Vec<u8>,
|
||||
/// Public key of the node.
|
||||
pub public_key: PublicKeyBytes,
|
||||
/// Version of the "global" protocol, eg. `ipfs/1.0.0` or `polkadot/1.0.0`.
|
||||
pub protocol_version: String,
|
||||
/// Name and version of the client. Can be thought as similar to the `User-Agent` header
|
||||
@ -213,7 +213,7 @@ fn parse_proto_msg(msg: BytesMut) -> Result<(IdentifyInfo, Multiaddr), IoError>
|
||||
let observed_addr = bytes_to_multiaddr(msg.take_observedAddr())?;
|
||||
|
||||
let info = IdentifyInfo {
|
||||
public_key: msg.take_publicKey(),
|
||||
public_key: PublicKeyBytes(msg.take_publicKey()),
|
||||
protocol_version: msg.take_protocolVersion(),
|
||||
agent_version: msg.take_agentVersion(),
|
||||
listen_addrs: listen_addrs,
|
||||
@ -235,7 +235,7 @@ mod tests {
|
||||
use self::libp2p_tcp_transport::TcpConfig;
|
||||
use self::tokio_core::reactor::Core;
|
||||
use futures::{Future, Stream};
|
||||
use libp2p_core::Transport;
|
||||
use libp2p_core::{Transport, PublicKeyBytes};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use {IdentifyInfo, IdentifyOutput, IdentifyProtocolConfig};
|
||||
@ -263,7 +263,7 @@ mod tests {
|
||||
.and_then(|identify| match identify {
|
||||
IdentifyOutput::Sender { sender, .. } => sender.send(
|
||||
IdentifyInfo {
|
||||
public_key: vec![1, 2, 3, 4, 5, 7],
|
||||
public_key: PublicKeyBytes(vec![1, 2, 3, 4, 5, 7]),
|
||||
protocol_version: "proto_version".to_owned(),
|
||||
agent_version: "agent_version".to_owned(),
|
||||
listen_addrs: vec![
|
||||
@ -295,7 +295,7 @@ mod tests {
|
||||
observed_addr,
|
||||
"/ip4/100.101.102.103/tcp/5000".parse().unwrap()
|
||||
);
|
||||
assert_eq!(info.public_key, &[1, 2, 3, 4, 5, 7]);
|
||||
assert_eq!(info.public_key.0, &[1, 2, 3, 4, 5, 7]);
|
||||
assert_eq!(info.protocol_version, "proto_version");
|
||||
assert_eq!(info.agent_version, "agent_version");
|
||||
assert_eq!(
|
||||
|
@ -390,7 +390,7 @@ fn process_identify_info<P>(
|
||||
where
|
||||
P: Peerstore,
|
||||
{
|
||||
let peer_id = PeerId::from_public_key(&info.public_key);
|
||||
let peer_id: PeerId = info.public_key.to_peer_id();
|
||||
peerstore
|
||||
.peer_or_create(&peer_id)
|
||||
.add_addr(client_addr, ttl);
|
||||
@ -408,7 +408,7 @@ mod tests {
|
||||
use futures::{Future, Stream};
|
||||
use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
||||
use libp2p_peerstore::{PeerAccess, PeerId, Peerstore};
|
||||
use libp2p_core::Transport;
|
||||
use libp2p_core::{Transport, PublicKeyBytesSlice};
|
||||
use multiaddr::{AddrComponent, Multiaddr};
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
@ -450,7 +450,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
let peer_id = PeerId::from_public_key(&vec![1, 2, 3, 4]);
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3, 4]));
|
||||
|
||||
let peerstore = MemoryPeerstore::empty();
|
||||
peerstore.peer_or_create(&peer_id).add_addr(
|
||||
|
Reference in New Issue
Block a user