mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-22 06:11: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" }
|
multistream-select = { path = "../multistream-select" }
|
||||||
futures = { version = "0.1", features = ["use_std"] }
|
futures = { version = "0.1", features = ["use_std"] }
|
||||||
parking_lot = "0.5.3"
|
parking_lot = "0.5.3"
|
||||||
|
protobuf = "1.7"
|
||||||
quick-error = "1.2"
|
quick-error = "1.2"
|
||||||
smallvec = "0.5"
|
smallvec = "0.5"
|
||||||
tokio-io = "0.1"
|
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 multihash;
|
||||||
extern crate multistream_select;
|
extern crate multistream_select;
|
||||||
extern crate parking_lot;
|
extern crate parking_lot;
|
||||||
|
extern crate protobuf;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate quick_error;
|
extern crate quick_error;
|
||||||
extern crate smallvec;
|
extern crate smallvec;
|
||||||
@ -225,6 +226,7 @@ extern crate rand;
|
|||||||
pub extern crate multiaddr;
|
pub extern crate multiaddr;
|
||||||
|
|
||||||
mod connection_reuse;
|
mod connection_reuse;
|
||||||
|
mod keys_proto;
|
||||||
mod peer_id;
|
mod peer_id;
|
||||||
mod public_key;
|
mod public_key;
|
||||||
|
|
||||||
@ -238,7 +240,7 @@ pub use self::connection_reuse::ConnectionReuse;
|
|||||||
pub use self::multiaddr::{AddrComponent, Multiaddr};
|
pub use self::multiaddr::{AddrComponent, Multiaddr};
|
||||||
pub use self::muxing::StreamMuxer;
|
pub use self::muxing::StreamMuxer;
|
||||||
pub use self::peer_id::PeerId;
|
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::swarm::{swarm, SwarmController, SwarmFuture};
|
||||||
pub use self::transport::{MuxedTransport, Transport};
|
pub use self::transport::{MuxedTransport, Transport};
|
||||||
pub use self::upgrade::{ConnectionUpgrade, Endpoint};
|
pub use self::upgrade::{ConnectionUpgrade, Endpoint};
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
use bs58;
|
use bs58;
|
||||||
use multihash;
|
use multihash;
|
||||||
use std::{fmt, str::FromStr};
|
use std::{fmt, str::FromStr};
|
||||||
use {PublicKeyBytes, PublicKeyBytesSlice};
|
use PublicKey;
|
||||||
|
|
||||||
/// Identifier of a peer of the network.
|
/// Identifier of a peer of the network.
|
||||||
///
|
///
|
||||||
@ -41,8 +41,9 @@ impl fmt::Debug for PeerId {
|
|||||||
impl PeerId {
|
impl PeerId {
|
||||||
/// Builds a `PeerId` from a public key.
|
/// Builds a `PeerId` from a public key.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_public_key(public_key: PublicKeyBytesSlice) -> PeerId {
|
pub fn from_public_key(public_key: PublicKey) -> PeerId {
|
||||||
let data = multihash::encode(multihash::Hash::SHA2256, public_key.0)
|
let protobuf = public_key.into_protobuf_encoding();
|
||||||
|
let data = multihash::encode(multihash::Hash::SHA2256, &protobuf)
|
||||||
.expect("sha2-256 is always supported");
|
.expect("sha2-256 is always supported");
|
||||||
PeerId { multihash: data }
|
PeerId { multihash: data }
|
||||||
}
|
}
|
||||||
@ -51,9 +52,11 @@ impl PeerId {
|
|||||||
/// back the data as an error.
|
/// back the data as an error.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
|
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
|
||||||
match multihash::decode(&data) {
|
let is_valid = multihash::decode(&data).is_ok();
|
||||||
Ok(_) => Ok(PeerId { multihash: data }),
|
if is_valid {
|
||||||
Err(_) => Err(data),
|
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
|
/// 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.
|
/// 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)
|
let alg = multihash::decode(&self.multihash)
|
||||||
.expect("our inner value should always be valid")
|
.expect("our inner value should always be valid")
|
||||||
.alg;
|
.alg;
|
||||||
match multihash::encode(alg, public_key.0) {
|
match multihash::encode(alg, &public_key.clone().into_protobuf_encoding()) {
|
||||||
Ok(compare) => Some(compare == self.multihash),
|
Ok(compare) => Some(compare == self.multihash),
|
||||||
Err(multihash::Error::UnsupportedType) => None,
|
Err(multihash::Error::UnsupportedType) => None,
|
||||||
Err(_) => Some(false),
|
Err(_) => Some(false),
|
||||||
@ -103,17 +106,10 @@ impl PeerId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PublicKeyBytes> for PeerId {
|
impl From<PublicKey> for PeerId {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(pubkey: PublicKeyBytes) -> PeerId {
|
fn from(key: PublicKey) -> PeerId {
|
||||||
PublicKeyBytesSlice(&pubkey.0).into()
|
PeerId::from_public_key(key)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<PublicKeyBytesSlice<'a>> for PeerId {
|
|
||||||
#[inline]
|
|
||||||
fn from(pubkey: PublicKeyBytesSlice<'a>) -> PeerId {
|
|
||||||
PeerId::from_public_key(pubkey)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,38 +140,25 @@ impl FromStr for PeerId {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use {PeerId, PublicKeyBytes, PublicKeyBytesSlice};
|
use {PeerId, PublicKey};
|
||||||
|
|
||||||
#[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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn peer_id_is_public_key() {
|
fn peer_id_is_public_key() {
|
||||||
let key = (0 .. 2048).map(|_| -> u8 { random() }).collect::<Vec<u8>>();
|
let key = PublicKey::Rsa((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||||
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&key));
|
let peer_id = PeerId::from_public_key(key.clone());
|
||||||
assert_eq!(peer_id.is_public_key(PublicKeyBytesSlice(&key)), Some(true));
|
assert_eq!(peer_id.is_public_key(&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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn peer_id_into_bytes_then_from_bytes() {
|
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();
|
let second = PeerId::from_bytes(peer_id.clone().into_bytes()).unwrap();
|
||||||
assert_eq!(peer_id, second);
|
assert_eq!(peer_id, second);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn peer_id_to_base58_then_back() {
|
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();
|
let second: PeerId = peer_id.to_base58().parse().unwrap();
|
||||||
assert_eq!(peer_id, second);
|
assert_eq!(peer_id, second);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,9 @@
|
|||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
use PeerId;
|
use PeerId;
|
||||||
|
use keys_proto;
|
||||||
|
use protobuf::{self, Message};
|
||||||
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||||
|
|
||||||
/// Public key used by the remote.
|
/// Public key used by the remote.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@ -34,93 +37,72 @@ pub enum PublicKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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]
|
#[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 {
|
match self {
|
||||||
PublicKey::Rsa(ref data) => PublicKeyBytesSlice(data),
|
PublicKey::Rsa(data) => {
|
||||||
PublicKey::Ed25519(ref data) => PublicKeyBytesSlice(data),
|
public_key.set_Type(keys_proto::KeyType::RSA);
|
||||||
PublicKey::Secp256k1(ref data) => PublicKeyBytesSlice(data),
|
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]
|
#[inline]
|
||||||
pub fn into_raw(self) -> PublicKeyBytes {
|
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, IoError> {
|
||||||
match self {
|
let mut pubkey = protobuf::parse_from_bytes::<keys_proto::PublicKey>(bytes)
|
||||||
PublicKey::Rsa(data) => PublicKeyBytes(data),
|
.map_err(|err| {
|
||||||
PublicKey::Ed25519(data) => PublicKeyBytes(data),
|
debug!("failed to parse public key's protobuf encoding");
|
||||||
PublicKey::Secp256k1(data) => PublicKeyBytes(data),
|
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.
|
/// Builds a `PeerId` corresponding to the public key of the node.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_peer_id(&self) -> PeerId {
|
pub fn into_peer_id(self) -> PeerId {
|
||||||
self.as_raw().into()
|
self.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PublicKey> for PeerId {
|
#[cfg(test)]
|
||||||
#[inline]
|
mod tests {
|
||||||
fn from(key: PublicKey) -> PeerId {
|
use rand::random;
|
||||||
key.to_peer_id()
|
use PublicKey;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<PublicKey> for PublicKeyBytes {
|
#[test]
|
||||||
#[inline]
|
fn key_into_protobuf_then_back() {
|
||||||
fn from(key: PublicKey) -> PublicKeyBytes {
|
let key = PublicKey::Rsa((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||||
key.into_raw()
|
let second = PublicKey::from_protobuf_encoding(&key.clone().into_protobuf_encoding()).unwrap();
|
||||||
}
|
assert_eq!(key, second);
|
||||||
}
|
|
||||||
|
|
||||||
/// 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[..]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# This script regenerates the `src/structs_proto.rs` and `src/keys_proto.rs` files from
|
# This script regenerates the `src/structs_proto.rs` file from `structs.proto`.
|
||||||
# `structs.proto` and `keys.proto`.
|
|
||||||
|
|
||||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||||
apt-get update; \
|
apt-get update; \
|
||||||
apt-get install -y protobuf-compiler; \
|
apt-get install -y protobuf-compiler; \
|
||||||
cargo install --version 1 protobuf; \
|
cargo install --version 1 protobuf; \
|
||||||
protoc --rust_out . structs.proto; \
|
protoc --rust_out . structs.proto"
|
||||||
protoc --rust_out . keys.proto"
|
|
||||||
|
|
||||||
mv -f structs.rs ./src/structs_proto.rs
|
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::protocol::{IdentifyProtocolConfig, IdentifySender};
|
||||||
pub use self::transport::IdentifyTransport;
|
pub use self::transport::IdentifyTransport;
|
||||||
|
|
||||||
mod keys_proto;
|
|
||||||
mod protocol;
|
mod protocol;
|
||||||
mod structs_proto;
|
mod structs_proto;
|
||||||
mod transport;
|
mod transport;
|
||||||
|
@ -20,7 +20,6 @@
|
|||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{future, Future, Sink, Stream};
|
use futures::{future, Future, Sink, Stream};
|
||||||
use keys_proto::{KeyType as KeyTypeProtobuf, PublicKey as PublicKeyProtobuf};
|
|
||||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PublicKey};
|
use libp2p_core::{ConnectionUpgrade, Endpoint, PublicKey};
|
||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
use protobuf::Message as ProtobufMessage;
|
use protobuf::Message as ProtobufMessage;
|
||||||
@ -79,27 +78,10 @@ where
|
|||||||
.map(|addr| addr.into_bytes())
|
.map(|addr| addr.into_bytes())
|
||||||
.collect();
|
.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();
|
let mut message = structs_proto::Identify::new();
|
||||||
message.set_agentVersion(info.agent_version);
|
message.set_agentVersion(info.agent_version);
|
||||||
message.set_protocolVersion(info.protocol_version);
|
message.set_protocolVersion(info.protocol_version);
|
||||||
message.set_publicKey(public_key.write_to_bytes()
|
message.set_publicKey(info.public_key.into_protobuf_encoding());
|
||||||
.expect("protobuf writing should always be valid"));
|
|
||||||
message.set_listenAddrs(listen_addrs);
|
message.set_listenAddrs(listen_addrs);
|
||||||
message.set_observedAddr(observed_addr.to_bytes());
|
message.set_observedAddr(observed_addr.to_bytes());
|
||||||
message.set_protocols(RepeatedField::from_vec(info.protocols));
|
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 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 {
|
let info = IdentifyInfo {
|
||||||
public_key: pubkey,
|
public_key: PublicKey::from_protobuf_encoding(msg.get_publicKey())?,
|
||||||
protocol_version: msg.take_protocolVersion(),
|
protocol_version: msg.take_protocolVersion(),
|
||||||
agent_version: msg.take_agentVersion(),
|
agent_version: msg.take_agentVersion(),
|
||||||
listen_addrs: listen_addrs,
|
listen_addrs: listen_addrs,
|
||||||
@ -268,7 +229,7 @@ mod tests {
|
|||||||
use self::libp2p_tcp_transport::TcpConfig;
|
use self::libp2p_tcp_transport::TcpConfig;
|
||||||
use self::tokio_core::reactor::Core;
|
use self::tokio_core::reactor::Core;
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
use libp2p_core::{PublicKey, PublicKeyBytesSlice, Transport};
|
use libp2p_core::{PublicKey, Transport};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use {IdentifyInfo, IdentifyOutput, IdentifyProtocolConfig};
|
use {IdentifyInfo, IdentifyOutput, IdentifyProtocolConfig};
|
||||||
@ -328,7 +289,7 @@ mod tests {
|
|||||||
observed_addr,
|
observed_addr,
|
||||||
"/ip4/100.101.102.103/tcp/5000".parse().unwrap()
|
"/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.protocol_version, "proto_version");
|
||||||
assert_eq!(info.agent_version, "agent_version");
|
assert_eq!(info.agent_version, "agent_version");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -136,7 +136,7 @@ where
|
|||||||
IdentifyOutput::RemoteInfo { info, observed_addr } => {
|
IdentifyOutput::RemoteInfo { info, observed_addr } => {
|
||||||
observed = observed_addr;
|
observed = observed_addr;
|
||||||
real_addr = process_identify_info(
|
real_addr = process_identify_info(
|
||||||
&info,
|
info,
|
||||||
&*peerstore.clone(),
|
&*peerstore.clone(),
|
||||||
original_addr.clone(),
|
original_addr.clone(),
|
||||||
addr_ttl,
|
addr_ttl,
|
||||||
@ -249,7 +249,7 @@ where
|
|||||||
match identify {
|
match identify {
|
||||||
(IdentifyOutput::RemoteInfo { info, observed_addr }, a) => {
|
(IdentifyOutput::RemoteInfo { info, observed_addr }, a) => {
|
||||||
a.and_then(move |old_addr| {
|
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))
|
Ok((real_addr, old_addr, observed_addr))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -338,7 +338,7 @@ where
|
|||||||
match identify {
|
match identify {
|
||||||
(IdentifyOutput::RemoteInfo { info, observed_addr }, old_addr) => {
|
(IdentifyOutput::RemoteInfo { info, observed_addr }, old_addr) => {
|
||||||
old_addr.and_then(move |out| {
|
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))
|
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
|
// > **Note**: This function is highly-specific, but this precise behaviour is needed in multiple
|
||||||
// > different places in the code.
|
// > different places in the code.
|
||||||
fn process_identify_info<P>(
|
fn process_identify_info<P>(
|
||||||
info: &IdentifyInfo,
|
info: IdentifyInfo,
|
||||||
peerstore: P,
|
peerstore: P,
|
||||||
client_addr: Multiaddr,
|
client_addr: Multiaddr,
|
||||||
ttl: Duration,
|
ttl: Duration,
|
||||||
@ -405,7 +405,7 @@ fn process_identify_info<P>(
|
|||||||
where
|
where
|
||||||
P: Peerstore,
|
P: Peerstore,
|
||||||
{
|
{
|
||||||
let peer_id: PeerId = info.public_key.to_peer_id();
|
let peer_id: PeerId = info.public_key.into_peer_id();
|
||||||
peerstore
|
peerstore
|
||||||
.peer_or_create(&peer_id)
|
.peer_or_create(&peer_id)
|
||||||
.add_addr(client_addr, ttl);
|
.add_addr(client_addr, ttl);
|
||||||
@ -422,8 +422,8 @@ mod tests {
|
|||||||
use IdentifyTransport;
|
use IdentifyTransport;
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
||||||
use libp2p_peerstore::{PeerAccess, PeerId, Peerstore};
|
use libp2p_peerstore::{PeerAccess, Peerstore};
|
||||||
use libp2p_core::{Transport, PublicKeyBytesSlice};
|
use libp2p_core::{PeerId, PublicKey, Transport};
|
||||||
use multiaddr::{AddrComponent, Multiaddr};
|
use multiaddr::{AddrComponent, Multiaddr};
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
use std::iter;
|
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();
|
let peerstore = MemoryPeerstore::empty();
|
||||||
peerstore.peer_or_create(&peer_id).add_addr(
|
peerstore.peer_or_create(&peer_id).add_addr(
|
||||||
|
@ -402,7 +402,7 @@ mod tests {
|
|||||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
use futures::{Future, Poll, Sink, StartSend, Stream};
|
||||||
use futures::sync::mpsc;
|
use futures::sync::mpsc;
|
||||||
use kad_server::{self, KademliaIncomingRequest, KademliaServerController};
|
use kad_server::{self, KademliaIncomingRequest, KademliaServerController};
|
||||||
use libp2p_core::PublicKeyBytes;
|
use libp2p_core::PublicKey;
|
||||||
use protocol::{ConnectionType, Peer};
|
use protocol::{ConnectionType, Peer};
|
||||||
use rand;
|
use rand;
|
||||||
|
|
||||||
@ -469,7 +469,7 @@ mod tests {
|
|||||||
|
|
||||||
let random_peer_id = {
|
let random_peer_id = {
|
||||||
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
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);
|
let find_node_fut = controller_a.find_node(&random_peer_id);
|
||||||
@ -477,7 +477,7 @@ mod tests {
|
|||||||
let example_response = Peer {
|
let example_response = Peer {
|
||||||
node_id: {
|
node_id: {
|
||||||
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
||||||
PublicKeyBytes(buf).to_peer_id()
|
PublicKey::Rsa(buf).into_peer_id()
|
||||||
},
|
},
|
||||||
multiaddrs: Vec::new(),
|
multiaddrs: Vec::new(),
|
||||||
connection_ty: ConnectionType::Connected,
|
connection_ty: ConnectionType::Connected,
|
||||||
|
@ -309,7 +309,7 @@ mod tests {
|
|||||||
use self::libp2p_tcp_transport::TcpConfig;
|
use self::libp2p_tcp_transport::TcpConfig;
|
||||||
use self::tokio_core::reactor::Core;
|
use self::tokio_core::reactor::Core;
|
||||||
use futures::{Future, Sink, Stream};
|
use futures::{Future, Sink, Stream};
|
||||||
use libp2p_core::{Transport, PeerId, PublicKeyBytesSlice};
|
use libp2p_core::{Transport, PeerId, PublicKey};
|
||||||
use protocol::{ConnectionType, KadMsg, KademliaProtocolConfig, Peer};
|
use protocol::{ConnectionType, KadMsg, KademliaProtocolConfig, Peer};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@ -333,7 +333,7 @@ mod tests {
|
|||||||
test_one(KadMsg::FindNodeRes {
|
test_one(KadMsg::FindNodeRes {
|
||||||
closer_peers: vec![
|
closer_peers: vec![
|
||||||
Peer {
|
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()],
|
multiaddrs: vec!["/ip4/100.101.102.103/tcp/20105".parse().unwrap()],
|
||||||
connection_ty: ConnectionType::Connected,
|
connection_ty: ConnectionType::Connected,
|
||||||
},
|
},
|
||||||
|
@ -31,7 +31,7 @@ use futures::Stream;
|
|||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
use std::{env, mem};
|
use std::{env, mem};
|
||||||
use libp2p::core::{either::EitherOutput, upgrade};
|
use libp2p::core::{either::EitherOutput, upgrade};
|
||||||
use libp2p::core::{Multiaddr, Transport, PublicKeyBytesSlice};
|
use libp2p::core::{Multiaddr, Transport, PublicKey};
|
||||||
use libp2p::peerstore::PeerId;
|
use libp2p::peerstore::PeerId;
|
||||||
use libp2p::tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
@ -91,7 +91,7 @@ fn main() {
|
|||||||
// or substream to our server.
|
// or substream to our server.
|
||||||
let my_id = {
|
let my_id = {
|
||||||
let key = (0..2048).map(|_| rand::random::<u8>()).collect::<Vec<_>>();
|
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);
|
let (floodsub_upgrade, floodsub_rx) = libp2p::floodsub::FloodSubUpgrade::new(my_id);
|
||||||
|
@ -33,7 +33,7 @@ use libp2p::Multiaddr;
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use libp2p::core::{Transport, PublicKeyBytesSlice};
|
use libp2p::core::{Transport, PublicKey};
|
||||||
use libp2p::core::{upgrade, either::EitherOutput};
|
use libp2p::core::{upgrade, either::EitherOutput};
|
||||||
use libp2p::kad::{ConnectionType, Peer, QueryEvent};
|
use libp2p::kad::{ConnectionType, Peer, QueryEvent};
|
||||||
use libp2p::tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
@ -97,7 +97,7 @@ fn main() {
|
|||||||
// incoming connections, and that will automatically apply secio and multiplex on top
|
// incoming connections, and that will automatically apply secio and multiplex on top
|
||||||
// of any opened stream.
|
// 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);
|
println!("Local peer id is: {:?}", my_peer_id);
|
||||||
|
|
||||||
// Let's put this `transport` into a Kademlia *swarm*. The swarm will handle all the incoming
|
// 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 libp2p;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
use libp2p::{PeerId, core::PublicKeyBytesSlice};
|
use libp2p::{PeerId, core::PublicKey};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let pid = {
|
let pid = {
|
||||||
let key = (0..2048).map(|_| rand::random::<u8>()).collect::<Vec<_>>();
|
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());
|
println!("{}", pid.to_base58());
|
||||||
}
|
}
|
||||||
|
@ -148,7 +148,7 @@ mod tests {
|
|||||||
let temp_file = self::tempfile::NamedTempFile::new().unwrap();
|
let temp_file = self::tempfile::NamedTempFile::new().unwrap();
|
||||||
let peer_store = ::json_peerstore::JsonPeerstore::new(temp_file.path()).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();
|
let addr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||||
|
|
||||||
peer_store
|
peer_store
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
//! extern crate libp2p_peerstore;
|
//! extern crate libp2p_peerstore;
|
||||||
//!
|
//!
|
||||||
//! # fn main() {
|
//! # fn main() {
|
||||||
//! use libp2p_core::{PeerId, PublicKeyBytesSlice};
|
//! use libp2p_core::{PeerId, PublicKey};
|
||||||
//! use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
//! use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
||||||
//! use libp2p_peerstore::{Peerstore, PeerAccess};
|
//! use libp2p_peerstore::{Peerstore, PeerAccess};
|
||||||
//! use multiaddr::Multiaddr;
|
//! use multiaddr::Multiaddr;
|
||||||
@ -50,7 +50,7 @@
|
|||||||
//!
|
//!
|
||||||
//! // In this example we use a `MemoryPeerstore`, but you can easily swap it for another backend.
|
//! // In this example we use a `MemoryPeerstore`, but you can easily swap it for another backend.
|
||||||
//! let mut peerstore = MemoryPeerstore::empty();
|
//! 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.
|
//! // Let's write some information about a peer.
|
||||||
//! {
|
//! {
|
||||||
|
@ -33,14 +33,14 @@ macro_rules! peerstore_tests {
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use {Peerstore, PeerAccess, PeerId};
|
use {Peerstore, PeerAccess, PeerId};
|
||||||
use libp2p_core::PublicKeyBytesSlice;
|
use libp2p_core::PublicKey;
|
||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initially_empty() {
|
fn initially_empty() {
|
||||||
$($stmt;)*
|
$($stmt;)*
|
||||||
let peer_store = $create_peerstore;
|
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_eq!(peer_store.peers().count(), 0);
|
||||||
assert!(peer_store.peer(&peer_id).is_none());
|
assert!(peer_store.peer(&peer_id).is_none());
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ macro_rules! peerstore_tests {
|
|||||||
fn set_then_get_addr() {
|
fn set_then_get_addr() {
|
||||||
$($stmt;)*
|
$($stmt;)*
|
||||||
let peer_store = $create_peerstore;
|
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();
|
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));
|
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.
|
// Add an already-expired address to a peer.
|
||||||
$($stmt;)*
|
$($stmt;)*
|
||||||
let peer_store = $create_peerstore;
|
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();
|
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));
|
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() {
|
fn clear_addrs() {
|
||||||
$($stmt;)*
|
$($stmt;)*
|
||||||
let peer_store = $create_peerstore;
|
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();
|
let addr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||||
|
|
||||||
peer_store.peer_or_create(&peer_id)
|
peer_store.peer_or_create(&peer_id)
|
||||||
@ -92,7 +92,7 @@ macro_rules! peerstore_tests {
|
|||||||
fn no_update_ttl() {
|
fn no_update_ttl() {
|
||||||
$($stmt;)*
|
$($stmt;)*
|
||||||
let peer_store = $create_peerstore;
|
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 addr1 = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||||
let addr2 = "/ip4/0.0.0.1/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() {
|
fn force_update_ttl() {
|
||||||
$($stmt;)*
|
$($stmt;)*
|
||||||
let peer_store = $create_peerstore;
|
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 addr1 = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
|
||||||
let addr2 = "/ip4/0.0.0.1/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
|
#!/bin/sh
|
||||||
|
|
||||||
# This script regenerates the `src/structs_proto.rs` and `src/keys_proto.rs` files from
|
# This script regenerates the `src/structs_proto.rs` file from `structs.proto`.
|
||||||
# `structs.proto` and `keys.proto`.
|
|
||||||
|
|
||||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||||
apt-get update; \
|
apt-get update; \
|
||||||
apt-get install -y protobuf-compiler; \
|
apt-get install -y protobuf-compiler; \
|
||||||
cargo install --version 1 protobuf; \
|
cargo install --version 1 protobuf; \
|
||||||
protoc --rust_out . structs.proto; \
|
protoc --rust_out . structs.proto"
|
||||||
protoc --rust_out . keys.proto"
|
|
||||||
|
|
||||||
mv -f structs.rs ./src/structs_proto.rs
|
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::future;
|
||||||
use futures::sink::Sink;
|
use futures::sink::Sink;
|
||||||
use futures::stream::Stream;
|
use futures::stream::Stream;
|
||||||
use keys_proto::{KeyType as KeyTypeProtobuf, PublicKey as PublicKeyProtobuf};
|
|
||||||
use libp2p_core::PublicKey;
|
use libp2p_core::PublicKey;
|
||||||
use protobuf::Message as ProtobufMessage;
|
use protobuf::Message as ProtobufMessage;
|
||||||
use protobuf::parse_from_bytes as protobuf_parse_from_bytes;
|
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.
|
// Send our proposition with our nonce, public key and supported protocols.
|
||||||
.and_then(|mut context| {
|
.and_then(|mut context| {
|
||||||
let mut public_key = PublicKeyProtobuf::new();
|
context.local_public_key_in_protobuf_bytes = context.local_key.to_public_key().into_protobuf_encoding();
|
||||||
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();
|
|
||||||
|
|
||||||
let mut proposition = Propose::new();
|
let mut proposition = Propose::new();
|
||||||
proposition.set_rand(context.local_nonce.clone().to_vec());
|
proposition.set_rand(context.local_nonce.clone().to_vec());
|
||||||
@ -201,29 +186,16 @@ where
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
context.remote_public_key_in_protobuf_bytes = prop.take_pubkey();
|
context.remote_public_key_in_protobuf_bytes = prop.take_pubkey();
|
||||||
let mut pubkey = {
|
let pubkey = match PublicKey::from_protobuf_encoding(&context.remote_public_key_in_protobuf_bytes) {
|
||||||
let bytes = &context.remote_public_key_in_protobuf_bytes;
|
|
||||||
match protobuf_parse_from_bytes::<PublicKeyProtobuf>(bytes) {
|
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
debug!("failed to parse remote's proposition's pubkey protobuf");
|
debug!("failed to parse remote's proposition's pubkey protobuf");
|
||||||
return Err(SecioError::HandshakeParsingFailure);
|
return Err(SecioError::HandshakeParsingFailure);
|
||||||
},
|
},
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
context.remote_nonce = prop.take_rand();
|
context.remote_nonce = prop.take_rand();
|
||||||
context.remote_public_key = Some(match pubkey.get_Type() {
|
context.remote_public_key = Some(pubkey);
|
||||||
KeyTypeProtobuf::RSA => {
|
|
||||||
PublicKey::Rsa(pubkey.take_Data())
|
|
||||||
},
|
|
||||||
KeyTypeProtobuf::Ed25519 => {
|
|
||||||
PublicKey::Ed25519(pubkey.take_Data())
|
|
||||||
},
|
|
||||||
KeyTypeProtobuf::Secp256k1 => {
|
|
||||||
PublicKey::Secp256k1(pubkey.take_Data())
|
|
||||||
},
|
|
||||||
});
|
|
||||||
trace!("received proposition from remote ; pubkey = {:?} ; nonce = {:?}",
|
trace!("received proposition from remote ; pubkey = {:?} ; nonce = {:?}",
|
||||||
context.remote_public_key, context.remote_nonce);
|
context.remote_public_key, context.remote_nonce);
|
||||||
Ok((prop, socket, context))
|
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 bytes::{Bytes, BytesMut};
|
||||||
use futures::stream::MapErr as StreamMapErr;
|
use futures::stream::MapErr as StreamMapErr;
|
||||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
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::signature::{Ed25519KeyPair, RSAKeyPair};
|
||||||
use ring::rand::SystemRandom;
|
use ring::rand::SystemRandom;
|
||||||
use rw_stream_sink::RwStreamSink;
|
use rw_stream_sink::RwStreamSink;
|
||||||
@ -120,7 +120,6 @@ mod algo_support;
|
|||||||
mod codec;
|
mod codec;
|
||||||
mod error;
|
mod error;
|
||||||
mod handshake;
|
mod handshake;
|
||||||
mod keys_proto;
|
|
||||||
mod structs_proto;
|
mod structs_proto;
|
||||||
|
|
||||||
/// Implementation of the `ConnectionUpgrade` trait of `libp2p_core`. Automatically applies
|
/// 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.
|
/// Builds a `PeerId` corresponding to the public key of this key pair.
|
||||||
|
#[inline]
|
||||||
pub fn to_peer_id(&self) -> PeerId {
|
pub fn to_peer_id(&self) -> PeerId {
|
||||||
match self.inner {
|
self.to_public_key().into_peer_id()
|
||||||
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()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: method to save generated key on disk?
|
// TODO: method to save generated key on disk?
|
||||||
|
Reference in New Issue
Block a user