Fix the public key format in the IdentifyInfo (#264)

* Fix the public key format in the IdentifyInfo

* Move PublicKey type to core

* Oops
This commit is contained in:
Pierre Krieger
2018-06-25 14:54:55 +02:00
committed by GitHub
parent de39e321e9
commit 795961b15e
10 changed files with 808 additions and 132 deletions

View File

@ -226,6 +226,7 @@ pub extern crate multiaddr;
mod connection_reuse;
mod peer_id;
mod public_key;
pub mod either;
pub mod muxing;
@ -236,7 +237,8 @@ pub mod upgrade;
pub use self::connection_reuse::ConnectionReuse;
pub use self::multiaddr::{AddrComponent, Multiaddr};
pub use self::muxing::StreamMuxer;
pub use self::peer_id::{PeerId, PublicKeyBytes, PublicKeyBytesSlice};
pub use self::peer_id::PeerId;
pub use self::public_key::{PublicKey, PublicKeyBytes, PublicKeyBytesSlice};
pub use self::swarm::{swarm, SwarmController, SwarmFuture};
pub use self::transport::{MuxedTransport, Transport};
pub use self::upgrade::{ConnectionUpgrade, Endpoint};

View File

@ -21,56 +21,7 @@
use bs58;
use multihash;
use std::{fmt, str::FromStr};
/// 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[..]
}
}
use {PublicKeyBytes, PublicKeyBytesSlice};
/// Identifier of a peer of the network.
///

126
core/src/public_key.rs Normal file
View File

@ -0,0 +1,126 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use PeerId;
/// Public key used by the remote.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PublicKey {
/// DER format.
Rsa(Vec<u8>),
/// Format = ???
// TODO: ^
Ed25519(Vec<u8>),
/// Format = ???
// TODO: ^
Secp256k1(Vec<u8>),
}
impl PublicKey {
/// Turns this public key into a raw representation.
#[inline]
pub fn as_raw(&self) -> PublicKeyBytesSlice {
match self {
PublicKey::Rsa(ref data) => PublicKeyBytesSlice(data),
PublicKey::Ed25519(ref data) => PublicKeyBytesSlice(data),
PublicKey::Secp256k1(ref data) => PublicKeyBytesSlice(data),
}
}
/// Turns this public key into a raw representation.
#[inline]
pub fn into_raw(self) -> PublicKeyBytes {
match self {
PublicKey::Rsa(data) => PublicKeyBytes(data),
PublicKey::Ed25519(data) => PublicKeyBytes(data),
PublicKey::Secp256k1(data) => PublicKeyBytes(data),
}
}
/// Builds a `PeerId` corresponding to the public key of the node.
#[inline]
pub fn to_peer_id(&self) -> PeerId {
self.as_raw().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[..]
}
}