mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-21 22:01:34 +00:00
The PeerId is the hash of the protobuf encoding (#276)
This commit is contained in:
@ -13,6 +13,7 @@ multihash = { path = "../multihash" }
|
||||
multistream-select = { path = "../multistream-select" }
|
||||
futures = { version = "0.1", features = ["use_std"] }
|
||||
parking_lot = "0.5.3"
|
||||
protobuf = "1.7"
|
||||
quick-error = "1.2"
|
||||
smallvec = "0.5"
|
||||
tokio-io = "0.1"
|
||||
|
12
core/regen_structs_proto.sh
Executable file
12
core/regen_structs_proto.sh
Executable file
@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/structs_proto.rs` and `src/keys_proto.rs` files from
|
||||
# `structs.proto` and `keys.proto`.
|
||||
|
||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 1 protobuf; \
|
||||
protoc --rust_out . keys.proto"
|
||||
|
||||
mv -f keys.rs ./src/keys_proto.rs
|
@ -213,6 +213,7 @@ extern crate log;
|
||||
extern crate multihash;
|
||||
extern crate multistream_select;
|
||||
extern crate parking_lot;
|
||||
extern crate protobuf;
|
||||
#[macro_use]
|
||||
extern crate quick_error;
|
||||
extern crate smallvec;
|
||||
@ -225,6 +226,7 @@ extern crate rand;
|
||||
pub extern crate multiaddr;
|
||||
|
||||
mod connection_reuse;
|
||||
mod keys_proto;
|
||||
mod peer_id;
|
||||
mod public_key;
|
||||
|
||||
@ -238,7 +240,7 @@ pub use self::connection_reuse::ConnectionReuse;
|
||||
pub use self::multiaddr::{AddrComponent, Multiaddr};
|
||||
pub use self::muxing::StreamMuxer;
|
||||
pub use self::peer_id::PeerId;
|
||||
pub use self::public_key::{PublicKey, PublicKeyBytes, PublicKeyBytesSlice};
|
||||
pub use self::public_key::PublicKey;
|
||||
pub use self::swarm::{swarm, SwarmController, SwarmFuture};
|
||||
pub use self::transport::{MuxedTransport, Transport};
|
||||
pub use self::upgrade::{ConnectionUpgrade, Endpoint};
|
||||
|
@ -21,7 +21,7 @@
|
||||
use bs58;
|
||||
use multihash;
|
||||
use std::{fmt, str::FromStr};
|
||||
use {PublicKeyBytes, PublicKeyBytesSlice};
|
||||
use PublicKey;
|
||||
|
||||
/// Identifier of a peer of the network.
|
||||
///
|
||||
@ -41,8 +41,9 @@ impl fmt::Debug for PeerId {
|
||||
impl PeerId {
|
||||
/// Builds a `PeerId` from a public key.
|
||||
#[inline]
|
||||
pub fn from_public_key(public_key: PublicKeyBytesSlice) -> PeerId {
|
||||
let data = multihash::encode(multihash::Hash::SHA2256, public_key.0)
|
||||
pub fn from_public_key(public_key: PublicKey) -> PeerId {
|
||||
let protobuf = public_key.into_protobuf_encoding();
|
||||
let data = multihash::encode(multihash::Hash::SHA2256, &protobuf)
|
||||
.expect("sha2-256 is always supported");
|
||||
PeerId { multihash: data }
|
||||
}
|
||||
@ -51,9 +52,11 @@ impl PeerId {
|
||||
/// back the data as an error.
|
||||
#[inline]
|
||||
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
|
||||
match multihash::decode(&data) {
|
||||
Ok(_) => Ok(PeerId { multihash: data }),
|
||||
Err(_) => Err(data),
|
||||
let is_valid = multihash::decode(&data).is_ok();
|
||||
if is_valid {
|
||||
Ok(PeerId { multihash: data })
|
||||
} else {
|
||||
Err(data)
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,11 +94,11 @@ impl PeerId {
|
||||
///
|
||||
/// Returns `None` if this `PeerId`s hash algorithm is not supported when encoding the
|
||||
/// given public key, otherwise `Some` boolean as the result of an equality check.
|
||||
pub fn is_public_key(&self, public_key: PublicKeyBytesSlice) -> Option<bool> {
|
||||
pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
|
||||
let alg = multihash::decode(&self.multihash)
|
||||
.expect("our inner value should always be valid")
|
||||
.alg;
|
||||
match multihash::encode(alg, public_key.0) {
|
||||
match multihash::encode(alg, &public_key.clone().into_protobuf_encoding()) {
|
||||
Ok(compare) => Some(compare == self.multihash),
|
||||
Err(multihash::Error::UnsupportedType) => None,
|
||||
Err(_) => Some(false),
|
||||
@ -103,17 +106,10 @@ impl PeerId {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PublicKeyBytes> for PeerId {
|
||||
impl From<PublicKey> for PeerId {
|
||||
#[inline]
|
||||
fn from(pubkey: PublicKeyBytes) -> PeerId {
|
||||
PublicKeyBytesSlice(&pubkey.0).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<PublicKeyBytesSlice<'a>> for PeerId {
|
||||
#[inline]
|
||||
fn from(pubkey: PublicKeyBytesSlice<'a>) -> PeerId {
|
||||
PeerId::from_public_key(pubkey)
|
||||
fn from(key: PublicKey) -> PeerId {
|
||||
PeerId::from_public_key(key)
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,38 +140,25 @@ impl FromStr for PeerId {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rand::random;
|
||||
use {PeerId, PublicKeyBytes, PublicKeyBytesSlice};
|
||||
|
||||
#[test]
|
||||
fn pubkey_as_slice_to_owned() {
|
||||
let key = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||
assert_eq!(key.clone().as_slice().to_owned(), key);
|
||||
}
|
||||
use {PeerId, PublicKey};
|
||||
|
||||
#[test]
|
||||
fn peer_id_is_public_key() {
|
||||
let key = (0 .. 2048).map(|_| -> u8 { random() }).collect::<Vec<u8>>();
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&key));
|
||||
assert_eq!(peer_id.is_public_key(PublicKeyBytesSlice(&key)), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pubkey_to_peer_id() {
|
||||
let key = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||
let peer_id = key.to_peer_id();
|
||||
assert_eq!(peer_id.is_public_key(key.as_slice()), Some(true));
|
||||
let key = PublicKey::Rsa((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||
let peer_id = PeerId::from_public_key(key.clone());
|
||||
assert_eq!(peer_id.is_public_key(&key), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn peer_id_into_bytes_then_from_bytes() {
|
||||
let peer_id = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect()).to_peer_id();
|
||||
let peer_id = PublicKey::Rsa((0 .. 2048).map(|_| -> u8 { random() }).collect()).into_peer_id();
|
||||
let second = PeerId::from_bytes(peer_id.clone().into_bytes()).unwrap();
|
||||
assert_eq!(peer_id, second);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn peer_id_to_base58_then_back() {
|
||||
let peer_id = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect()).to_peer_id();
|
||||
let peer_id = PublicKey::Rsa((0 .. 2048).map(|_| -> u8 { random() }).collect()).into_peer_id();
|
||||
let second: PeerId = peer_id.to_base58().parse().unwrap();
|
||||
assert_eq!(peer_id, second);
|
||||
}
|
||||
|
@ -19,6 +19,9 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use PeerId;
|
||||
use keys_proto;
|
||||
use protobuf::{self, Message};
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
|
||||
/// Public key used by the remote.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@ -34,93 +37,72 @@ pub enum PublicKey {
|
||||
}
|
||||
|
||||
impl PublicKey {
|
||||
/// Turns this public key into a raw representation.
|
||||
/// Encodes the public key as a protobuf message.
|
||||
///
|
||||
/// Used at various locations in the wire protocol of libp2p.
|
||||
#[inline]
|
||||
pub fn as_raw(&self) -> PublicKeyBytesSlice {
|
||||
pub fn into_protobuf_encoding(self) -> Vec<u8> {
|
||||
let mut public_key = keys_proto::PublicKey::new();
|
||||
match self {
|
||||
PublicKey::Rsa(ref data) => PublicKeyBytesSlice(data),
|
||||
PublicKey::Ed25519(ref data) => PublicKeyBytesSlice(data),
|
||||
PublicKey::Secp256k1(ref data) => PublicKeyBytesSlice(data),
|
||||
}
|
||||
PublicKey::Rsa(data) => {
|
||||
public_key.set_Type(keys_proto::KeyType::RSA);
|
||||
public_key.set_Data(data);
|
||||
},
|
||||
PublicKey::Ed25519(data) => {
|
||||
public_key.set_Type(keys_proto::KeyType::Ed25519);
|
||||
public_key.set_Data(data);
|
||||
},
|
||||
PublicKey::Secp256k1(data) => {
|
||||
public_key.set_Type(keys_proto::KeyType::Secp256k1);
|
||||
public_key.set_Data(data);
|
||||
},
|
||||
};
|
||||
|
||||
public_key
|
||||
.write_to_bytes()
|
||||
.expect("protobuf writing should always be valid")
|
||||
}
|
||||
|
||||
/// Turns this public key into a raw representation.
|
||||
/// Decodes the public key from a protobuf message.
|
||||
///
|
||||
/// Used at various locations in the wire protocol of libp2p.
|
||||
#[inline]
|
||||
pub fn into_raw(self) -> PublicKeyBytes {
|
||||
match self {
|
||||
PublicKey::Rsa(data) => PublicKeyBytes(data),
|
||||
PublicKey::Ed25519(data) => PublicKeyBytes(data),
|
||||
PublicKey::Secp256k1(data) => PublicKeyBytes(data),
|
||||
}
|
||||
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, IoError> {
|
||||
let mut pubkey = protobuf::parse_from_bytes::<keys_proto::PublicKey>(bytes)
|
||||
.map_err(|err| {
|
||||
debug!("failed to parse public key's protobuf encoding");
|
||||
IoError::new(IoErrorKind::InvalidData, err)
|
||||
})?;
|
||||
|
||||
Ok(match pubkey.get_Type() {
|
||||
keys_proto::KeyType::RSA => {
|
||||
PublicKey::Rsa(pubkey.take_Data())
|
||||
},
|
||||
keys_proto::KeyType::Ed25519 => {
|
||||
PublicKey::Ed25519(pubkey.take_Data())
|
||||
},
|
||||
keys_proto::KeyType::Secp256k1 => {
|
||||
PublicKey::Secp256k1(pubkey.take_Data())
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Builds a `PeerId` corresponding to the public key of the node.
|
||||
#[inline]
|
||||
pub fn to_peer_id(&self) -> PeerId {
|
||||
self.as_raw().into()
|
||||
pub fn into_peer_id(self) -> PeerId {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PublicKey> for PeerId {
|
||||
#[inline]
|
||||
fn from(key: PublicKey) -> PeerId {
|
||||
key.to_peer_id()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PublicKey> for PublicKeyBytes {
|
||||
#[inline]
|
||||
fn from(key: PublicKey) -> PublicKeyBytes {
|
||||
key.into_raw()
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw bytes of a public key.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PublicKeyBytes(pub Vec<u8>);
|
||||
|
||||
impl PublicKeyBytes {
|
||||
/// Turns this into a `PublicKeyBytesSlice`.
|
||||
#[inline]
|
||||
pub fn as_slice(&self) -> PublicKeyBytesSlice {
|
||||
PublicKeyBytesSlice(&self.0)
|
||||
}
|
||||
|
||||
/// Turns this into a `PeerId`.
|
||||
#[inline]
|
||||
pub fn to_peer_id(&self) -> PeerId {
|
||||
self.as_slice().into()
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw bytes of a public key.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct PublicKeyBytesSlice<'a>(pub &'a [u8]);
|
||||
|
||||
impl<'a> PublicKeyBytesSlice<'a> {
|
||||
/// Turns this into a `PublicKeyBytes`.
|
||||
#[inline]
|
||||
pub fn to_owned(&self) -> PublicKeyBytes {
|
||||
PublicKeyBytes(self.0.to_owned())
|
||||
}
|
||||
|
||||
/// Turns this into a `PeerId`.
|
||||
#[inline]
|
||||
pub fn to_peer_id(&self) -> PeerId {
|
||||
PeerId::from_public_key(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PartialEq<PublicKeyBytes> for PublicKeyBytesSlice<'a> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &PublicKeyBytes) -> bool {
|
||||
self.0 == &other.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PartialEq<PublicKeyBytesSlice<'a>> for PublicKeyBytes {
|
||||
#[inline]
|
||||
fn eq(&self, other: &PublicKeyBytesSlice<'a>) -> bool {
|
||||
self.0 == &other.0[..]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rand::random;
|
||||
use PublicKey;
|
||||
|
||||
#[test]
|
||||
fn key_into_protobuf_then_back() {
|
||||
let key = PublicKey::Rsa((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||
let second = PublicKey::from_protobuf_encoding(&key.clone().into_protobuf_encoding()).unwrap();
|
||||
assert_eq!(key, second);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/structs_proto.rs` and `src/keys_proto.rs` files from
|
||||
# `structs.proto` and `keys.proto`.
|
||||
# This script regenerates the `src/structs_proto.rs` file from `structs.proto`.
|
||||
|
||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 1 protobuf; \
|
||||
protoc --rust_out . structs.proto; \
|
||||
protoc --rust_out . keys.proto"
|
||||
protoc --rust_out . structs.proto"
|
||||
|
||||
mv -f structs.rs ./src/structs_proto.rs
|
||||
mv -f keys.rs ./src/keys_proto.rs
|
||||
|
@ -81,7 +81,6 @@ pub use self::protocol::{IdentifyInfo, IdentifyOutput};
|
||||
pub use self::protocol::{IdentifyProtocolConfig, IdentifySender};
|
||||
pub use self::transport::IdentifyTransport;
|
||||
|
||||
mod keys_proto;
|
||||
mod protocol;
|
||||
mod structs_proto;
|
||||
mod transport;
|
||||
|
@ -20,7 +20,6 @@
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{future, Future, Sink, Stream};
|
||||
use keys_proto::{KeyType as KeyTypeProtobuf, PublicKey as PublicKeyProtobuf};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PublicKey};
|
||||
use multiaddr::Multiaddr;
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
@ -79,27 +78,10 @@ where
|
||||
.map(|addr| addr.into_bytes())
|
||||
.collect();
|
||||
|
||||
let mut public_key = PublicKeyProtobuf::new();
|
||||
match info.public_key {
|
||||
PublicKey::Rsa(data) => {
|
||||
public_key.set_Type(KeyTypeProtobuf::RSA);
|
||||
public_key.set_Data(data);
|
||||
},
|
||||
PublicKey::Ed25519(data) => {
|
||||
public_key.set_Type(KeyTypeProtobuf::Ed25519);
|
||||
public_key.set_Data(data);
|
||||
},
|
||||
PublicKey::Secp256k1(data) => {
|
||||
public_key.set_Type(KeyTypeProtobuf::Secp256k1);
|
||||
public_key.set_Data(data);
|
||||
},
|
||||
};
|
||||
|
||||
let mut message = structs_proto::Identify::new();
|
||||
message.set_agentVersion(info.agent_version);
|
||||
message.set_protocolVersion(info.protocol_version);
|
||||
message.set_publicKey(public_key.write_to_bytes()
|
||||
.expect("protobuf writing should always be valid"));
|
||||
message.set_publicKey(info.public_key.into_protobuf_encoding());
|
||||
message.set_listenAddrs(listen_addrs);
|
||||
message.set_observedAddr(observed_addr.to_bytes());
|
||||
message.set_protocols(RepeatedField::from_vec(info.protocols));
|
||||
@ -224,29 +206,8 @@ fn parse_proto_msg(msg: BytesMut) -> Result<(IdentifyInfo, Multiaddr), IoError>
|
||||
};
|
||||
|
||||
let observed_addr = bytes_to_multiaddr(msg.take_observedAddr())?;
|
||||
|
||||
let pubkey = {
|
||||
let mut pubkey = protobuf_parse_from_bytes::<PublicKeyProtobuf>(msg.get_publicKey())
|
||||
.map_err(|err| {
|
||||
debug!("failed to parse remote's infos' pubkey protobuf");
|
||||
IoError::new(IoErrorKind::InvalidData, err)
|
||||
})?;
|
||||
|
||||
match pubkey.get_Type() {
|
||||
KeyTypeProtobuf::RSA => {
|
||||
PublicKey::Rsa(pubkey.take_Data())
|
||||
},
|
||||
KeyTypeProtobuf::Ed25519 => {
|
||||
PublicKey::Ed25519(pubkey.take_Data())
|
||||
},
|
||||
KeyTypeProtobuf::Secp256k1 => {
|
||||
PublicKey::Secp256k1(pubkey.take_Data())
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
let info = IdentifyInfo {
|
||||
public_key: pubkey,
|
||||
public_key: PublicKey::from_protobuf_encoding(msg.get_publicKey())?,
|
||||
protocol_version: msg.take_protocolVersion(),
|
||||
agent_version: msg.take_agentVersion(),
|
||||
listen_addrs: listen_addrs,
|
||||
@ -268,7 +229,7 @@ mod tests {
|
||||
use self::libp2p_tcp_transport::TcpConfig;
|
||||
use self::tokio_core::reactor::Core;
|
||||
use futures::{Future, Stream};
|
||||
use libp2p_core::{PublicKey, PublicKeyBytesSlice, Transport};
|
||||
use libp2p_core::{PublicKey, Transport};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use {IdentifyInfo, IdentifyOutput, IdentifyProtocolConfig};
|
||||
@ -328,7 +289,7 @@ mod tests {
|
||||
observed_addr,
|
||||
"/ip4/100.101.102.103/tcp/5000".parse().unwrap()
|
||||
);
|
||||
assert_eq!(info.public_key.as_raw(), PublicKeyBytesSlice(&[1, 2, 3, 4, 5, 7]));
|
||||
assert_eq!(info.public_key, PublicKey::Ed25519(vec![1, 2, 3, 4, 5, 7]));
|
||||
assert_eq!(info.protocol_version, "proto_version");
|
||||
assert_eq!(info.agent_version, "agent_version");
|
||||
assert_eq!(
|
||||
|
@ -136,7 +136,7 @@ where
|
||||
IdentifyOutput::RemoteInfo { info, observed_addr } => {
|
||||
observed = observed_addr;
|
||||
real_addr = process_identify_info(
|
||||
&info,
|
||||
info,
|
||||
&*peerstore.clone(),
|
||||
original_addr.clone(),
|
||||
addr_ttl,
|
||||
@ -249,7 +249,7 @@ where
|
||||
match identify {
|
||||
(IdentifyOutput::RemoteInfo { info, observed_addr }, a) => {
|
||||
a.and_then(move |old_addr| {
|
||||
let real_addr = process_identify_info(&info, &*peerstore, old_addr.clone(), addr_ttl)?;
|
||||
let real_addr = process_identify_info(info, &*peerstore, old_addr.clone(), addr_ttl)?;
|
||||
Ok((real_addr, old_addr, observed_addr))
|
||||
})
|
||||
}
|
||||
@ -338,7 +338,7 @@ where
|
||||
match identify {
|
||||
(IdentifyOutput::RemoteInfo { info, observed_addr }, old_addr) => {
|
||||
old_addr.and_then(move |out| {
|
||||
let real_addr = process_identify_info(&info, &*peerstore, out, addr_ttl)?;
|
||||
let real_addr = process_identify_info(info, &*peerstore, out, addr_ttl)?;
|
||||
Ok((real_addr, observed_addr, connec))
|
||||
})
|
||||
}
|
||||
@ -397,7 +397,7 @@ fn multiaddr_to_peerid(addr: Multiaddr) -> Result<PeerId, Multiaddr> {
|
||||
// > **Note**: This function is highly-specific, but this precise behaviour is needed in multiple
|
||||
// > different places in the code.
|
||||
fn process_identify_info<P>(
|
||||
info: &IdentifyInfo,
|
||||
info: IdentifyInfo,
|
||||
peerstore: P,
|
||||
client_addr: Multiaddr,
|
||||
ttl: Duration,
|
||||
@ -405,7 +405,7 @@ fn process_identify_info<P>(
|
||||
where
|
||||
P: Peerstore,
|
||||
{
|
||||
let peer_id: PeerId = info.public_key.to_peer_id();
|
||||
let peer_id: PeerId = info.public_key.into_peer_id();
|
||||
peerstore
|
||||
.peer_or_create(&peer_id)
|
||||
.add_addr(client_addr, ttl);
|
||||
@ -422,8 +422,8 @@ mod tests {
|
||||
use IdentifyTransport;
|
||||
use futures::{Future, Stream};
|
||||
use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
||||
use libp2p_peerstore::{PeerAccess, PeerId, Peerstore};
|
||||
use libp2p_core::{Transport, PublicKeyBytesSlice};
|
||||
use libp2p_peerstore::{PeerAccess, Peerstore};
|
||||
use libp2p_core::{PeerId, PublicKey, Transport};
|
||||
use multiaddr::{AddrComponent, Multiaddr};
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
@ -466,7 +466,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3, 4]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3, 4]));
|
||||
|
||||
let peerstore = MemoryPeerstore::empty();
|
||||
peerstore.peer_or_create(&peer_id).add_addr(
|
||||
|
@ -402,7 +402,7 @@ mod tests {
|
||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
||||
use futures::sync::mpsc;
|
||||
use kad_server::{self, KademliaIncomingRequest, KademliaServerController};
|
||||
use libp2p_core::PublicKeyBytes;
|
||||
use libp2p_core::PublicKey;
|
||||
use protocol::{ConnectionType, Peer};
|
||||
use rand;
|
||||
|
||||
@ -469,7 +469,7 @@ mod tests {
|
||||
|
||||
let random_peer_id = {
|
||||
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
||||
PublicKeyBytes(buf).to_peer_id()
|
||||
PublicKey::Rsa(buf).into_peer_id()
|
||||
};
|
||||
|
||||
let find_node_fut = controller_a.find_node(&random_peer_id);
|
||||
@ -477,7 +477,7 @@ mod tests {
|
||||
let example_response = Peer {
|
||||
node_id: {
|
||||
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
||||
PublicKeyBytes(buf).to_peer_id()
|
||||
PublicKey::Rsa(buf).into_peer_id()
|
||||
},
|
||||
multiaddrs: Vec::new(),
|
||||
connection_ty: ConnectionType::Connected,
|
||||
|
@ -309,7 +309,7 @@ mod tests {
|
||||
use self::libp2p_tcp_transport::TcpConfig;
|
||||
use self::tokio_core::reactor::Core;
|
||||
use futures::{Future, Sink, Stream};
|
||||
use libp2p_core::{Transport, PeerId, PublicKeyBytesSlice};
|
||||
use libp2p_core::{Transport, PeerId, PublicKey};
|
||||
use protocol::{ConnectionType, KadMsg, KademliaProtocolConfig, Peer};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
@ -333,7 +333,7 @@ mod tests {
|
||||
test_one(KadMsg::FindNodeRes {
|
||||
closer_peers: vec![
|
||||
Peer {
|
||||
node_id: PeerId::from_public_key(PublicKeyBytesSlice(&[93, 80, 12, 250])),
|
||||
node_id: PeerId::from_public_key(PublicKey::Rsa(vec![93, 80, 12, 250])),
|
||||
multiaddrs: vec!["/ip4/100.101.102.103/tcp/20105".parse().unwrap()],
|
||||
connection_ty: ConnectionType::Connected,
|
||||
},
|
||||
|
@ -31,7 +31,7 @@ use futures::Stream;
|
||||
use futures::future::Future;
|
||||
use std::{env, mem};
|
||||
use libp2p::core::{either::EitherOutput, upgrade};
|
||||
use libp2p::core::{Multiaddr, Transport, PublicKeyBytesSlice};
|
||||
use libp2p::core::{Multiaddr, Transport, PublicKey};
|
||||
use libp2p::peerstore::PeerId;
|
||||
use libp2p::tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
@ -91,7 +91,7 @@ fn main() {
|
||||
// or substream to our server.
|
||||
let my_id = {
|
||||
let key = (0..2048).map(|_| rand::random::<u8>()).collect::<Vec<_>>();
|
||||
PeerId::from_public_key(PublicKeyBytesSlice(&key))
|
||||
PeerId::from_public_key(PublicKey::Rsa(key))
|
||||
};
|
||||
|
||||
let (floodsub_upgrade, floodsub_rx) = libp2p::floodsub::FloodSubUpgrade::new(my_id);
|
||||
|
@ -33,7 +33,7 @@ use libp2p::Multiaddr;
|
||||
use std::env;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use libp2p::core::{Transport, PublicKeyBytesSlice};
|
||||
use libp2p::core::{Transport, PublicKey};
|
||||
use libp2p::core::{upgrade, either::EitherOutput};
|
||||
use libp2p::kad::{ConnectionType, Peer, QueryEvent};
|
||||
use libp2p::tcp::TcpConfig;
|
||||
@ -97,7 +97,7 @@ fn main() {
|
||||
// incoming connections, and that will automatically apply secio and multiplex on top
|
||||
// of any opened stream.
|
||||
|
||||
let my_peer_id = PeerId::from_public_key(PublicKeyBytesSlice(include_bytes!("test-rsa-public-key.der")));
|
||||
let my_peer_id = PeerId::from_public_key(PublicKey::Rsa(include_bytes!("test-rsa-public-key.der").to_vec()));
|
||||
println!("Local peer id is: {:?}", my_peer_id);
|
||||
|
||||
// Let's put this `transport` into a Kademlia *swarm*. The swarm will handle all the incoming
|
||||
|
@ -21,12 +21,12 @@
|
||||
extern crate libp2p;
|
||||
extern crate rand;
|
||||
|
||||
use libp2p::{PeerId, core::PublicKeyBytesSlice};
|
||||
use libp2p::{PeerId, core::PublicKey};
|
||||
|
||||
fn main() {
|
||||
let pid = {
|
||||
let key = (0..2048).map(|_| rand::random::<u8>()).collect::<Vec<_>>();
|
||||
PeerId::from_public_key(PublicKeyBytesSlice(&key))
|
||||
PeerId::from_public_key(PublicKey::Rsa(key))
|
||||
};
|
||||
println!("{}", pid.to_base58());
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ mod tests {
|
||||
let temp_file = self::tempfile::NamedTempFile::new().unwrap();
|
||||
let peer_store = ::json_peerstore::JsonPeerstore::new(temp_file.path()).unwrap();
|
||||
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
let addr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
|
||||
peer_store
|
||||
|
@ -42,7 +42,7 @@
|
||||
//! extern crate libp2p_peerstore;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! use libp2p_core::{PeerId, PublicKeyBytesSlice};
|
||||
//! use libp2p_core::{PeerId, PublicKey};
|
||||
//! use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
||||
//! use libp2p_peerstore::{Peerstore, PeerAccess};
|
||||
//! use multiaddr::Multiaddr;
|
||||
@ -50,7 +50,7 @@
|
||||
//!
|
||||
//! // In this example we use a `MemoryPeerstore`, but you can easily swap it for another backend.
|
||||
//! let mut peerstore = MemoryPeerstore::empty();
|
||||
//! let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3, 4]));
|
||||
//! let peer_id = PeerId::from_public_key(PublicKey::Rsa(vec![1, 2, 3, 4]));
|
||||
//!
|
||||
//! // Let's write some information about a peer.
|
||||
//! {
|
||||
|
@ -33,14 +33,14 @@ macro_rules! peerstore_tests {
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use {Peerstore, PeerAccess, PeerId};
|
||||
use libp2p_core::PublicKeyBytesSlice;
|
||||
use libp2p_core::PublicKey;
|
||||
use multiaddr::Multiaddr;
|
||||
|
||||
#[test]
|
||||
fn initially_empty() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
assert_eq!(peer_store.peers().count(), 0);
|
||||
assert!(peer_store.peer(&peer_id).is_none());
|
||||
}
|
||||
@ -49,7 +49,7 @@ macro_rules! peerstore_tests {
|
||||
fn set_then_get_addr() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
let addr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(5000));
|
||||
@ -63,7 +63,7 @@ macro_rules! peerstore_tests {
|
||||
// Add an already-expired address to a peer.
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
let addr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(0));
|
||||
@ -77,7 +77,7 @@ macro_rules! peerstore_tests {
|
||||
fn clear_addrs() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
let addr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id)
|
||||
@ -92,7 +92,7 @@ macro_rules! peerstore_tests {
|
||||
fn no_update_ttl() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
|
||||
let addr1 = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
let addr2 = "/ip4/0.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
@ -113,7 +113,7 @@ macro_rules! peerstore_tests {
|
||||
fn force_update_ttl() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&[1, 2, 3]));
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3]));
|
||||
|
||||
let addr1 = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
let addr2 = "/ip4/0.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
|
||||
|
@ -1,15 +0,0 @@
|
||||
enum KeyType {
|
||||
RSA = 0;
|
||||
Ed25519 = 1;
|
||||
Secp256k1 = 2;
|
||||
}
|
||||
|
||||
message PublicKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}
|
||||
|
||||
message PrivateKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}
|
@ -1,14 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/structs_proto.rs` and `src/keys_proto.rs` files from
|
||||
# `structs.proto` and `keys.proto`.
|
||||
# This script regenerates the `src/structs_proto.rs` file from `structs.proto`.
|
||||
|
||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 1 protobuf; \
|
||||
protoc --rust_out . structs.proto; \
|
||||
protoc --rust_out . keys.proto"
|
||||
protoc --rust_out . structs.proto"
|
||||
|
||||
mv -f structs.rs ./src/structs_proto.rs
|
||||
mv -f keys.rs ./src/keys_proto.rs
|
||||
|
@ -27,7 +27,6 @@ use futures::Future;
|
||||
use futures::future;
|
||||
use futures::sink::Sink;
|
||||
use futures::stream::Stream;
|
||||
use keys_proto::{KeyType as KeyTypeProtobuf, PublicKey as PublicKeyProtobuf};
|
||||
use libp2p_core::PublicKey;
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
use protobuf::parse_from_bytes as protobuf_parse_from_bytes;
|
||||
@ -145,21 +144,7 @@ where
|
||||
|
||||
// Send our proposition with our nonce, public key and supported protocols.
|
||||
.and_then(|mut context| {
|
||||
let mut public_key = PublicKeyProtobuf::new();
|
||||
public_key.set_Data(context.local_key.to_public_key().into_raw().0);
|
||||
match context.local_key.inner {
|
||||
SecioKeyPairInner::Rsa { .. } => {
|
||||
public_key.set_Type(KeyTypeProtobuf::RSA);
|
||||
},
|
||||
SecioKeyPairInner::Ed25519 { .. } => {
|
||||
public_key.set_Type(KeyTypeProtobuf::Ed25519);
|
||||
},
|
||||
#[cfg(feature = "secp256k1")]
|
||||
SecioKeyPairInner::Secp256k1 { .. } => {
|
||||
public_key.set_Type(KeyTypeProtobuf::Secp256k1);
|
||||
},
|
||||
}
|
||||
context.local_public_key_in_protobuf_bytes = public_key.write_to_bytes().unwrap();
|
||||
context.local_public_key_in_protobuf_bytes = context.local_key.to_public_key().into_protobuf_encoding();
|
||||
|
||||
let mut proposition = Propose::new();
|
||||
proposition.set_rand(context.local_nonce.clone().to_vec());
|
||||
@ -201,29 +186,16 @@ where
|
||||
}
|
||||
};
|
||||
context.remote_public_key_in_protobuf_bytes = prop.take_pubkey();
|
||||
let mut pubkey = {
|
||||
let bytes = &context.remote_public_key_in_protobuf_bytes;
|
||||
match protobuf_parse_from_bytes::<PublicKeyProtobuf>(bytes) {
|
||||
let pubkey = match PublicKey::from_protobuf_encoding(&context.remote_public_key_in_protobuf_bytes) {
|
||||
Ok(p) => p,
|
||||
Err(_) => {
|
||||
debug!("failed to parse remote's proposition's pubkey protobuf");
|
||||
return Err(SecioError::HandshakeParsingFailure);
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
context.remote_nonce = prop.take_rand();
|
||||
context.remote_public_key = Some(match pubkey.get_Type() {
|
||||
KeyTypeProtobuf::RSA => {
|
||||
PublicKey::Rsa(pubkey.take_Data())
|
||||
},
|
||||
KeyTypeProtobuf::Ed25519 => {
|
||||
PublicKey::Ed25519(pubkey.take_Data())
|
||||
},
|
||||
KeyTypeProtobuf::Secp256k1 => {
|
||||
PublicKey::Secp256k1(pubkey.take_Data())
|
||||
},
|
||||
});
|
||||
context.remote_public_key = Some(pubkey);
|
||||
trace!("received proposition from remote ; pubkey = {:?} ; nonce = {:?}",
|
||||
context.remote_public_key, context.remote_nonce);
|
||||
Ok((prop, socket, context))
|
||||
|
@ -1,593 +0,0 @@
|
||||
// This file is generated. Do not edit
|
||||
// @generated
|
||||
|
||||
// https://github.com/Manishearth/rust-clippy/issues/702
|
||||
#![allow(unknown_lints)]
|
||||
#![allow(clippy)]
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
#![allow(box_pointers)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(missing_docs)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(trivial_casts)]
|
||||
#![allow(unsafe_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_results)]
|
||||
|
||||
use protobuf::Message as Message_imported_for_functions;
|
||||
use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions;
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct PublicKey {
|
||||
// message fields
|
||||
Type: ::std::option::Option<KeyType>,
|
||||
Data: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
// see codegen.rs for the explanation why impl Sync explicitly
|
||||
unsafe impl ::std::marker::Sync for PublicKey {}
|
||||
|
||||
impl PublicKey {
|
||||
pub fn new() -> PublicKey {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
pub fn default_instance() -> &'static PublicKey {
|
||||
static mut instance: ::protobuf::lazy::Lazy<PublicKey> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const PublicKey,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(PublicKey::new)
|
||||
}
|
||||
}
|
||||
|
||||
// required .KeyType Type = 1;
|
||||
|
||||
pub fn clear_Type(&mut self) {
|
||||
self.Type = ::std::option::Option::None;
|
||||
}
|
||||
|
||||
pub fn has_Type(&self) -> bool {
|
||||
self.Type.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_Type(&mut self, v: KeyType) {
|
||||
self.Type = ::std::option::Option::Some(v);
|
||||
}
|
||||
|
||||
pub fn get_Type(&self) -> KeyType {
|
||||
self.Type.unwrap_or(KeyType::RSA)
|
||||
}
|
||||
|
||||
fn get_Type_for_reflect(&self) -> &::std::option::Option<KeyType> {
|
||||
&self.Type
|
||||
}
|
||||
|
||||
fn mut_Type_for_reflect(&mut self) -> &mut ::std::option::Option<KeyType> {
|
||||
&mut self.Type
|
||||
}
|
||||
|
||||
// required bytes Data = 2;
|
||||
|
||||
pub fn clear_Data(&mut self) {
|
||||
self.Data.clear();
|
||||
}
|
||||
|
||||
pub fn has_Data(&self) -> bool {
|
||||
self.Data.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_Data(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.Data = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_Data(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.Data.is_none() {
|
||||
self.Data.set_default();
|
||||
}
|
||||
self.Data.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_Data(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.Data.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_Data(&self) -> &[u8] {
|
||||
match self.Data.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
fn get_Data_for_reflect(&self) -> &::protobuf::SingularField<::std::vec::Vec<u8>> {
|
||||
&self.Data
|
||||
}
|
||||
|
||||
fn mut_Data_for_reflect(&mut self) -> &mut ::protobuf::SingularField<::std::vec::Vec<u8>> {
|
||||
&mut self.Data
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for PublicKey {
|
||||
fn is_initialized(&self) -> bool {
|
||||
if self.Type.is_none() {
|
||||
return false;
|
||||
}
|
||||
if self.Data.is_none() {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_proto2_enum_with_unknown_fields_into(wire_type, is, &mut self.Type, 1, &mut self.unknown_fields)?
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.Data)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(v) = self.Type {
|
||||
my_size += ::protobuf::rt::enum_size(1, v);
|
||||
}
|
||||
if let Some(ref v) = self.Data.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &v);
|
||||
}
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(v) = self.Type {
|
||||
os.write_enum(1, v.value())?;
|
||||
}
|
||||
if let Some(ref v) = self.Data.as_ref() {
|
||||
os.write_bytes(2, &v)?;
|
||||
}
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
::protobuf::MessageStatic::descriptor_static(None::<Self>)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::MessageStatic for PublicKey {
|
||||
fn new() -> PublicKey {
|
||||
PublicKey::new()
|
||||
}
|
||||
|
||||
fn descriptor_static(_: ::std::option::Option<PublicKey>) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_option_accessor::<_, ::protobuf::types::ProtobufTypeEnum<KeyType>>(
|
||||
"Type",
|
||||
PublicKey::get_Type_for_reflect,
|
||||
PublicKey::mut_Type_for_reflect,
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"Data",
|
||||
PublicKey::get_Data_for_reflect,
|
||||
PublicKey::mut_Data_for_reflect,
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<PublicKey>(
|
||||
"PublicKey",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for PublicKey {
|
||||
fn clear(&mut self) {
|
||||
self.clear_Type();
|
||||
self.clear_Data();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for PublicKey {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for PublicKey {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct PrivateKey {
|
||||
// message fields
|
||||
Type: ::std::option::Option<KeyType>,
|
||||
Data: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
// see codegen.rs for the explanation why impl Sync explicitly
|
||||
unsafe impl ::std::marker::Sync for PrivateKey {}
|
||||
|
||||
impl PrivateKey {
|
||||
pub fn new() -> PrivateKey {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
pub fn default_instance() -> &'static PrivateKey {
|
||||
static mut instance: ::protobuf::lazy::Lazy<PrivateKey> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const PrivateKey,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(PrivateKey::new)
|
||||
}
|
||||
}
|
||||
|
||||
// required .KeyType Type = 1;
|
||||
|
||||
pub fn clear_Type(&mut self) {
|
||||
self.Type = ::std::option::Option::None;
|
||||
}
|
||||
|
||||
pub fn has_Type(&self) -> bool {
|
||||
self.Type.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_Type(&mut self, v: KeyType) {
|
||||
self.Type = ::std::option::Option::Some(v);
|
||||
}
|
||||
|
||||
pub fn get_Type(&self) -> KeyType {
|
||||
self.Type.unwrap_or(KeyType::RSA)
|
||||
}
|
||||
|
||||
fn get_Type_for_reflect(&self) -> &::std::option::Option<KeyType> {
|
||||
&self.Type
|
||||
}
|
||||
|
||||
fn mut_Type_for_reflect(&mut self) -> &mut ::std::option::Option<KeyType> {
|
||||
&mut self.Type
|
||||
}
|
||||
|
||||
// required bytes Data = 2;
|
||||
|
||||
pub fn clear_Data(&mut self) {
|
||||
self.Data.clear();
|
||||
}
|
||||
|
||||
pub fn has_Data(&self) -> bool {
|
||||
self.Data.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_Data(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.Data = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_Data(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.Data.is_none() {
|
||||
self.Data.set_default();
|
||||
}
|
||||
self.Data.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_Data(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.Data.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_Data(&self) -> &[u8] {
|
||||
match self.Data.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
fn get_Data_for_reflect(&self) -> &::protobuf::SingularField<::std::vec::Vec<u8>> {
|
||||
&self.Data
|
||||
}
|
||||
|
||||
fn mut_Data_for_reflect(&mut self) -> &mut ::protobuf::SingularField<::std::vec::Vec<u8>> {
|
||||
&mut self.Data
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for PrivateKey {
|
||||
fn is_initialized(&self) -> bool {
|
||||
if self.Type.is_none() {
|
||||
return false;
|
||||
}
|
||||
if self.Data.is_none() {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_proto2_enum_with_unknown_fields_into(wire_type, is, &mut self.Type, 1, &mut self.unknown_fields)?
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.Data)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(v) = self.Type {
|
||||
my_size += ::protobuf::rt::enum_size(1, v);
|
||||
}
|
||||
if let Some(ref v) = self.Data.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &v);
|
||||
}
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(v) = self.Type {
|
||||
os.write_enum(1, v.value())?;
|
||||
}
|
||||
if let Some(ref v) = self.Data.as_ref() {
|
||||
os.write_bytes(2, &v)?;
|
||||
}
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
::protobuf::MessageStatic::descriptor_static(None::<Self>)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::MessageStatic for PrivateKey {
|
||||
fn new() -> PrivateKey {
|
||||
PrivateKey::new()
|
||||
}
|
||||
|
||||
fn descriptor_static(_: ::std::option::Option<PrivateKey>) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_option_accessor::<_, ::protobuf::types::ProtobufTypeEnum<KeyType>>(
|
||||
"Type",
|
||||
PrivateKey::get_Type_for_reflect,
|
||||
PrivateKey::mut_Type_for_reflect,
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"Data",
|
||||
PrivateKey::get_Data_for_reflect,
|
||||
PrivateKey::mut_Data_for_reflect,
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<PrivateKey>(
|
||||
"PrivateKey",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for PrivateKey {
|
||||
fn clear(&mut self) {
|
||||
self.clear_Type();
|
||||
self.clear_Data();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for PrivateKey {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for PrivateKey {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,PartialEq,Eq,Debug,Hash)]
|
||||
pub enum KeyType {
|
||||
RSA = 0,
|
||||
Ed25519 = 1,
|
||||
Secp256k1 = 2,
|
||||
}
|
||||
|
||||
impl ::protobuf::ProtobufEnum for KeyType {
|
||||
fn value(&self) -> i32 {
|
||||
*self as i32
|
||||
}
|
||||
|
||||
fn from_i32(value: i32) -> ::std::option::Option<KeyType> {
|
||||
match value {
|
||||
0 => ::std::option::Option::Some(KeyType::RSA),
|
||||
1 => ::std::option::Option::Some(KeyType::Ed25519),
|
||||
2 => ::std::option::Option::Some(KeyType::Secp256k1),
|
||||
_ => ::std::option::Option::None
|
||||
}
|
||||
}
|
||||
|
||||
fn values() -> &'static [Self] {
|
||||
static values: &'static [KeyType] = &[
|
||||
KeyType::RSA,
|
||||
KeyType::Ed25519,
|
||||
KeyType::Secp256k1,
|
||||
];
|
||||
values
|
||||
}
|
||||
|
||||
fn enum_descriptor_static(_: ::std::option::Option<KeyType>) -> &'static ::protobuf::reflect::EnumDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::EnumDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::EnumDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
::protobuf::reflect::EnumDescriptor::new("KeyType", file_descriptor_proto())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::marker::Copy for KeyType {
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for KeyType {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Enum(self.descriptor())
|
||||
}
|
||||
}
|
||||
|
||||
static file_descriptor_proto_data: &'static [u8] = b"\
|
||||
\n\nkeys.proto\"=\n\tPublicKey\x12\x1c\n\x04Type\x18\x01\x20\x02(\x0e2\
|
||||
\x08.KeyTypeR\x04type\x12\x12\n\x04Data\x18\x02\x20\x02(\x0cR\x04data\">\
|
||||
\n\nPrivateKey\x12\x1c\n\x04Type\x18\x01\x20\x02(\x0e2\x08.KeyTypeR\x04t\
|
||||
ype\x12\x12\n\x04Data\x18\x02\x20\x02(\x0cR\x04data*.\n\x07KeyType\x12\
|
||||
\x07\n\x03RSA\x10\0\x12\x0b\n\x07Ed25519\x10\x01\x12\r\n\tSecp256k1\x10\
|
||||
\x02J\xdf\x03\n\x06\x12\x04\0\0\x0e\x01\n\n\n\x02\x05\0\x12\x04\0\0\x04\
|
||||
\x01\n\n\n\x03\x05\0\x01\x12\x03\0\x05\x0c\n\x0b\n\x04\x05\0\x02\0\x12\
|
||||
\x03\x01\x02\n\n\x0c\n\x05\x05\0\x02\0\x01\x12\x03\x01\x02\x05\n\x0c\n\
|
||||
\x05\x05\0\x02\0\x02\x12\x03\x01\x08\t\n\x0b\n\x04\x05\0\x02\x01\x12\x03\
|
||||
\x02\x02\x0e\n\x0c\n\x05\x05\0\x02\x01\x01\x12\x03\x02\x02\t\n\x0c\n\x05\
|
||||
\x05\0\x02\x01\x02\x12\x03\x02\x0c\r\n\x0b\n\x04\x05\0\x02\x02\x12\x03\
|
||||
\x03\x02\x10\n\x0c\n\x05\x05\0\x02\x02\x01\x12\x03\x03\x02\x0b\n\x0c\n\
|
||||
\x05\x05\0\x02\x02\x02\x12\x03\x03\x0e\x0f\n\n\n\x02\x04\0\x12\x04\x06\0\
|
||||
\t\x01\n\n\n\x03\x04\0\x01\x12\x03\x06\x08\x11\n\x0b\n\x04\x04\0\x02\0\
|
||||
\x12\x03\x07\x02\x1c\n\x0c\n\x05\x04\0\x02\0\x04\x12\x03\x07\x02\n\n\x0c\
|
||||
\n\x05\x04\0\x02\0\x06\x12\x03\x07\x0b\x12\n\x0c\n\x05\x04\0\x02\0\x01\
|
||||
\x12\x03\x07\x13\x17\n\x0c\n\x05\x04\0\x02\0\x03\x12\x03\x07\x1a\x1b\n\
|
||||
\x0b\n\x04\x04\0\x02\x01\x12\x03\x08\x02\x1a\n\x0c\n\x05\x04\0\x02\x01\
|
||||
\x04\x12\x03\x08\x02\n\n\x0c\n\x05\x04\0\x02\x01\x05\x12\x03\x08\x0b\x10\
|
||||
\n\x0c\n\x05\x04\0\x02\x01\x01\x12\x03\x08\x11\x15\n\x0c\n\x05\x04\0\x02\
|
||||
\x01\x03\x12\x03\x08\x18\x19\n\n\n\x02\x04\x01\x12\x04\x0b\0\x0e\x01\n\n\
|
||||
\n\x03\x04\x01\x01\x12\x03\x0b\x08\x12\n\x0b\n\x04\x04\x01\x02\0\x12\x03\
|
||||
\x0c\x02\x1c\n\x0c\n\x05\x04\x01\x02\0\x04\x12\x03\x0c\x02\n\n\x0c\n\x05\
|
||||
\x04\x01\x02\0\x06\x12\x03\x0c\x0b\x12\n\x0c\n\x05\x04\x01\x02\0\x01\x12\
|
||||
\x03\x0c\x13\x17\n\x0c\n\x05\x04\x01\x02\0\x03\x12\x03\x0c\x1a\x1b\n\x0b\
|
||||
\n\x04\x04\x01\x02\x01\x12\x03\r\x02\x1a\n\x0c\n\x05\x04\x01\x02\x01\x04\
|
||||
\x12\x03\r\x02\n\n\x0c\n\x05\x04\x01\x02\x01\x05\x12\x03\r\x0b\x10\n\x0c\
|
||||
\n\x05\x04\x01\x02\x01\x01\x12\x03\r\x11\x15\n\x0c\n\x05\x04\x01\x02\x01\
|
||||
\x03\x12\x03\r\x18\x19\
|
||||
";
|
||||
|
||||
static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::descriptor::FileDescriptorProto,
|
||||
};
|
||||
|
||||
fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto {
|
||||
::protobuf::parse_from_bytes(file_descriptor_proto_data).unwrap()
|
||||
}
|
||||
|
||||
pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
|
||||
unsafe {
|
||||
file_descriptor_proto_lazy.get(|| {
|
||||
parse_descriptor_proto()
|
||||
})
|
||||
}
|
||||
}
|
@ -105,7 +105,7 @@ use asn1_der::{DerObject, traits::FromDerEncoded, traits::FromDerObject};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::stream::MapErr as StreamMapErr;
|
||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
||||
use libp2p_core::{PeerId, PublicKey, PublicKeyBytesSlice};
|
||||
use libp2p_core::{PeerId, PublicKey};
|
||||
use ring::signature::{Ed25519KeyPair, RSAKeyPair};
|
||||
use ring::rand::SystemRandom;
|
||||
use rw_stream_sink::RwStreamSink;
|
||||
@ -120,7 +120,6 @@ mod algo_support;
|
||||
mod codec;
|
||||
mod error;
|
||||
mod handshake;
|
||||
mod keys_proto;
|
||||
mod structs_proto;
|
||||
|
||||
/// Implementation of the `ConnectionUpgrade` trait of `libp2p_core`. Automatically applies
|
||||
@ -249,23 +248,9 @@ impl SecioKeyPair {
|
||||
}
|
||||
|
||||
/// Builds a `PeerId` corresponding to the public key of this key pair.
|
||||
#[inline]
|
||||
pub fn to_peer_id(&self) -> PeerId {
|
||||
match self.inner {
|
||||
SecioKeyPairInner::Rsa { ref public, .. } => {
|
||||
PublicKeyBytesSlice(&public).into()
|
||||
},
|
||||
SecioKeyPairInner::Ed25519 { ref key_pair } => {
|
||||
PublicKeyBytesSlice(key_pair.public_key_bytes()).into()
|
||||
},
|
||||
#[cfg(feature = "secp256k1")]
|
||||
SecioKeyPairInner::Secp256k1 { ref private } => {
|
||||
let secp = secp256k1::Secp256k1::with_caps(secp256k1::ContextFlag::None);
|
||||
let pubkey = secp256k1::key::PublicKey::from_secret_key(&secp, private)
|
||||
.expect("wrong secp256k1 private key ; type safety violated");
|
||||
let pubkey_bytes = pubkey.serialize();
|
||||
PublicKeyBytesSlice(&pubkey_bytes).into()
|
||||
},
|
||||
}
|
||||
self.to_public_key().into_peer_id()
|
||||
}
|
||||
|
||||
// TODO: method to save generated key on disk?
|
||||
|
Reference in New Issue
Block a user