2018-05-24 00:54:08 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:21:09 +01:00
|
|
|
use crate::PublicKey;
|
2018-05-24 00:54:08 +02:00
|
|
|
use bs58;
|
2020-01-10 21:03:59 +08:00
|
|
|
use thiserror::Error;
|
2018-05-24 00:54:08 +02:00
|
|
|
use multihash;
|
2020-01-29 11:33:19 +01:00
|
|
|
use std::{convert::TryFrom, fmt, hash, str::FromStr};
|
2018-06-05 12:29:59 +02:00
|
|
|
|
2019-09-04 19:40:28 +02:00
|
|
|
/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
|
|
|
|
/// automatically used as the peer id using an identity multihash.
|
2020-02-12 15:28:40 +01:00
|
|
|
const _MAX_INLINE_KEY_LENGTH: usize = 42;
|
2019-09-04 19:40:28 +02:00
|
|
|
|
2018-05-24 00:54:08 +02:00
|
|
|
/// Identifier of a peer of the network.
|
|
|
|
///
|
|
|
|
/// The data is a multihash of the public key of the peer.
|
|
|
|
// TODO: maybe keep things in decoded version?
|
2020-01-29 11:33:19 +01:00
|
|
|
#[derive(Clone, Eq)]
|
2018-05-24 00:54:08 +02:00
|
|
|
pub struct PeerId {
|
2018-08-09 14:51:09 +02:00
|
|
|
multihash: multihash::Multihash,
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for PeerId {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-03-23 10:08:07 +01:00
|
|
|
f.debug_tuple("PeerId")
|
|
|
|
.field(&self.to_base58())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for PeerId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.to_base58().fmt(f)
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PeerId {
|
|
|
|
/// Builds a `PeerId` from a public key.
|
2019-03-11 13:42:53 +01:00
|
|
|
pub fn from_public_key(key: PublicKey) -> PeerId {
|
|
|
|
let key_enc = key.into_protobuf_encoding();
|
2019-09-04 19:40:28 +02:00
|
|
|
|
2020-01-29 11:33:19 +01:00
|
|
|
// Note: before 0.12, this was incorrectly implemented and `SHA2256` was always used.
|
|
|
|
// Starting from version 0.13, rust-libp2p accepts both hashed and non-hashed keys as
|
2020-02-12 15:28:40 +01:00
|
|
|
// input (see `from_bytes`). Starting from version 0.16 rust-libp2p will compare
|
|
|
|
// `PeerId`s of different hashes equal, which makes it possible to connect through
|
|
|
|
// secio or noise to nodes with an identity hash. Starting from version 0.17, rust-libp2p
|
|
|
|
// will switch to not hashing the key (i.e. the correct behaviour).
|
|
|
|
// In other words, rust-libp2p 0.16 is compatible with all versions of rust-libp2p.
|
|
|
|
// Rust-libp2p 0.12 and below is **NOT** compatible with rust-libp2p 0.17 and above.
|
|
|
|
let hash_algorithm = /*if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
|
2019-09-04 19:40:28 +02:00
|
|
|
multihash::Hash::Identity
|
2020-02-12 15:28:40 +01:00
|
|
|
} else {*/
|
|
|
|
multihash::Hash::SHA2256;
|
|
|
|
//};
|
2020-01-29 11:33:19 +01:00
|
|
|
|
2019-09-04 19:40:28 +02:00
|
|
|
let multihash = multihash::encode(hash_algorithm, &key_enc)
|
|
|
|
.expect("identity and sha2-256 are always supported by known public key types");
|
2018-08-09 14:51:09 +02:00
|
|
|
PeerId { multihash }
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether `data` is a valid `PeerId`. If so, returns the `PeerId`. If not, returns
|
|
|
|
/// back the data as an error.
|
|
|
|
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
|
2018-08-09 14:51:09 +02:00
|
|
|
match multihash::Multihash::from_bytes(data) {
|
2018-08-10 17:47:02 +02:00
|
|
|
Ok(multihash) => {
|
2019-09-04 19:40:28 +02:00
|
|
|
if multihash.algorithm() == multihash::Hash::SHA2256
|
|
|
|
|| multihash.algorithm() == multihash::Hash::Identity
|
|
|
|
{
|
2018-08-10 17:47:02 +02:00
|
|
|
Ok(PeerId { multihash })
|
|
|
|
} else {
|
|
|
|
Err(multihash.into_bytes())
|
|
|
|
}
|
2019-09-04 19:40:28 +02:00
|
|
|
}
|
2018-08-09 14:51:09 +02:00
|
|
|
Err(err) => Err(err.data),
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:47:02 +02:00
|
|
|
/// Turns a `Multihash` into a `PeerId`. If the multihash doesn't use the correct algorithm,
|
|
|
|
/// returns back the data as an error.
|
|
|
|
pub fn from_multihash(data: multihash::Multihash) -> Result<PeerId, multihash::Multihash> {
|
2019-11-06 16:09:15 +01:00
|
|
|
if data.algorithm() == multihash::Hash::SHA2256 || data.algorithm() == multihash::Hash::Identity {
|
2018-08-10 17:47:02 +02:00
|
|
|
Ok(PeerId { multihash: data })
|
|
|
|
} else {
|
|
|
|
Err(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:44:36 +01:00
|
|
|
/// Generates a random peer ID from a cryptographically secure PRNG.
|
|
|
|
///
|
|
|
|
/// This is useful for randomly walking on a DHT, or for testing purposes.
|
|
|
|
pub fn random() -> PeerId {
|
|
|
|
PeerId {
|
|
|
|
multihash: multihash::Multihash::random(multihash::Hash::SHA2256)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 00:54:08 +02:00
|
|
|
/// Returns a raw bytes representation of this `PeerId`.
|
2018-06-05 12:29:59 +02:00
|
|
|
///
|
|
|
|
/// Note that this is not the same as the public key of the peer.
|
2018-05-24 00:54:08 +02:00
|
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
2018-08-09 14:51:09 +02:00
|
|
|
self.multihash.into_bytes()
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a raw bytes representation of this `PeerId`.
|
2018-06-05 12:29:59 +02:00
|
|
|
///
|
|
|
|
/// Note that this is not the same as the public key of the peer.
|
2018-05-24 00:54:08 +02:00
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
2018-08-09 14:51:09 +02:00
|
|
|
self.multihash.as_bytes()
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a base-58 encoded string of this `PeerId`.
|
|
|
|
pub fn to_base58(&self) -> String {
|
2018-08-09 14:51:09 +02:00
|
|
|
bs58::encode(self.multihash.as_bytes()).into_string()
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether the public key passed as parameter matches the public key of this `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.
|
2018-06-25 17:56:36 +02:00
|
|
|
pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
|
2018-08-09 14:51:09 +02:00
|
|
|
let alg = self.multihash.algorithm();
|
2019-03-11 13:42:53 +01:00
|
|
|
let enc = public_key.clone().into_protobuf_encoding();
|
|
|
|
match multihash::encode(alg, &enc) {
|
|
|
|
Ok(h) => Some(h == self.multihash),
|
2019-09-04 19:40:28 +02:00
|
|
|
Err(multihash::EncodeError::UnsupportedType) => None,
|
|
|
|
Err(multihash::EncodeError::UnsupportedInputLength) => None,
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:33:19 +01:00
|
|
|
impl hash::Hash for PeerId {
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where
|
|
|
|
H: hash::Hasher
|
|
|
|
{
|
|
|
|
match self.multihash.algorithm() {
|
|
|
|
multihash::Hash::Identity => {
|
|
|
|
let sha256 = multihash::encode(multihash::Hash::SHA2256, self.multihash.digest())
|
|
|
|
.expect("encoding a SHA2256 multihash never fails; qed");
|
|
|
|
hash::Hash::hash(sha256.digest(), state)
|
|
|
|
},
|
|
|
|
multihash::Hash::SHA2256 => {
|
|
|
|
hash::Hash::hash(self.multihash.digest(), state)
|
|
|
|
},
|
|
|
|
_ => unreachable!("PeerId can only be built from Identity or SHA2256; qed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:56:36 +02:00
|
|
|
impl From<PublicKey> for PeerId {
|
2018-06-05 12:29:59 +02:00
|
|
|
#[inline]
|
2018-06-25 17:56:36 +02:00
|
|
|
fn from(key: PublicKey) -> PeerId {
|
|
|
|
PeerId::from_public_key(key)
|
2018-06-05 12:29:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 14:16:50 +02:00
|
|
|
impl TryFrom<Vec<u8>> for PeerId {
|
|
|
|
type Error = Vec<u8>;
|
|
|
|
|
|
|
|
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
|
|
|
PeerId::from_bytes(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<multihash::Multihash> for PeerId {
|
|
|
|
type Error = multihash::Multihash;
|
|
|
|
|
|
|
|
fn try_from(value: multihash::Multihash) -> Result<Self, Self::Error> {
|
|
|
|
PeerId::from_multihash(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:33:19 +01:00
|
|
|
impl PartialEq<PeerId> for PeerId {
|
2018-11-24 13:55:18 +01:00
|
|
|
fn eq(&self, other: &PeerId) -> bool {
|
2020-01-29 11:33:19 +01:00
|
|
|
match (self.multihash.algorithm(), other.multihash.algorithm()) {
|
|
|
|
(multihash::Hash::SHA2256, multihash::Hash::SHA2256) => {
|
|
|
|
self.multihash.digest() == other.multihash.digest()
|
|
|
|
},
|
|
|
|
(multihash::Hash::Identity, multihash::Hash::Identity) => {
|
|
|
|
self.multihash.digest() == other.multihash.digest()
|
|
|
|
},
|
|
|
|
(multihash::Hash::SHA2256, multihash::Hash::Identity) => {
|
|
|
|
multihash::encode(multihash::Hash::SHA2256, other.multihash.digest())
|
|
|
|
.map(|mh| mh == self.multihash)
|
|
|
|
.unwrap_or(false)
|
|
|
|
},
|
|
|
|
(multihash::Hash::Identity, multihash::Hash::SHA2256) => {
|
|
|
|
multihash::encode(multihash::Hash::SHA2256, self.multihash.digest())
|
|
|
|
.map(|mh| mh == other.multihash)
|
|
|
|
.unwrap_or(false)
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:33:19 +01:00
|
|
|
// TODO: The semantics of that function aren't very precise. It is possible for two `PeerId`s to
|
|
|
|
// compare equal while their bytes representation are not. Right now, this `AsRef`
|
|
|
|
// implementation is only used to define precedence over two `PeerId`s in case of a
|
|
|
|
// simultaneous connection between two nodes. Since the simultaneous connection system
|
|
|
|
// is planned to be removed (https://github.com/libp2p/rust-libp2p/issues/912), we went for
|
|
|
|
// we keeping that function with the intent of removing it as soon as possible.
|
2019-01-23 17:44:40 +01:00
|
|
|
impl AsRef<[u8]> for PeerId {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
self.as_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:33:19 +01:00
|
|
|
impl From<PeerId> for multihash::Multihash {
|
|
|
|
fn from(peer_id: PeerId) -> Self {
|
|
|
|
peer_id.multihash
|
2018-08-10 17:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:03:59 +08:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum ParseError {
|
|
|
|
#[error("base-58 decode error: {0}")]
|
|
|
|
B58(#[from] bs58::decode::Error),
|
|
|
|
#[error("decoding multihash failed")]
|
|
|
|
MultiHash,
|
2018-05-24 00:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for PeerId {
|
|
|
|
type Err = ParseError;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let bytes = bs58::decode(s).into_vec()?;
|
|
|
|
PeerId::from_bytes(bytes).map_err(|_| ParseError::MultiHash)
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 19:01:18 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-11 13:42:53 +01:00
|
|
|
use crate::{PeerId, identity};
|
2020-01-29 11:33:19 +01:00
|
|
|
use std::{convert::TryFrom as _, hash::{self, Hasher as _}};
|
2018-06-05 19:01:18 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peer_id_is_public_key() {
|
2019-03-11 13:42:53 +01:00
|
|
|
let key = identity::Keypair::generate_ed25519().public();
|
|
|
|
let peer_id = key.clone().into_peer_id();
|
2018-06-25 17:56:36 +02:00
|
|
|
assert_eq!(peer_id.is_public_key(&key), Some(true));
|
2018-06-05 19:01:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peer_id_into_bytes_then_from_bytes() {
|
2019-03-11 13:42:53 +01:00
|
|
|
let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
|
2018-06-05 19:01:18 +02:00
|
|
|
let second = PeerId::from_bytes(peer_id.clone().into_bytes()).unwrap();
|
|
|
|
assert_eq!(peer_id, second);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peer_id_to_base58_then_back() {
|
2019-03-11 13:42:53 +01:00
|
|
|
let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
|
2018-06-05 19:01:18 +02:00
|
|
|
let second: PeerId = peer_id.to_base58().parse().unwrap();
|
|
|
|
assert_eq!(peer_id, second);
|
|
|
|
}
|
2018-11-20 13:44:36 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn random_peer_id_is_valid() {
|
|
|
|
for _ in 0 .. 5000 {
|
|
|
|
let peer_id = PeerId::random();
|
|
|
|
assert_eq!(peer_id, PeerId::from_bytes(peer_id.clone().into_bytes()).unwrap());
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 11:33:19 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peer_id_identity_equal_to_sha2256() {
|
|
|
|
let random_bytes = (0..64).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
|
|
|
|
let mh1 = multihash::encode(multihash::Hash::SHA2256, &random_bytes).unwrap();
|
|
|
|
let mh2 = multihash::encode(multihash::Hash::Identity, &random_bytes).unwrap();
|
|
|
|
let peer_id1 = PeerId::try_from(mh1).unwrap();
|
|
|
|
let peer_id2 = PeerId::try_from(mh2).unwrap();
|
|
|
|
assert_eq!(peer_id1, peer_id2);
|
|
|
|
assert_eq!(peer_id2, peer_id1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peer_id_identity_hashes_equal_to_sha2256() {
|
|
|
|
let random_bytes = (0..64).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
|
|
|
|
let mh1 = multihash::encode(multihash::Hash::SHA2256, &random_bytes).unwrap();
|
|
|
|
let mh2 = multihash::encode(multihash::Hash::Identity, &random_bytes).unwrap();
|
|
|
|
let peer_id1 = PeerId::try_from(mh1).unwrap();
|
|
|
|
let peer_id2 = PeerId::try_from(mh2).unwrap();
|
|
|
|
|
|
|
|
let mut hasher1 = fnv::FnvHasher::with_key(0);
|
|
|
|
hash::Hash::hash(&peer_id1, &mut hasher1);
|
|
|
|
let mut hasher2 = fnv::FnvHasher::with_key(0);
|
|
|
|
hash::Hash::hash(&peer_id2, &mut hasher2);
|
|
|
|
|
|
|
|
assert_eq!(hasher1.finish(), hasher2.finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peer_id_equal_across_algorithms() {
|
|
|
|
use multihash::Hash;
|
|
|
|
use quickcheck::{Arbitrary, Gen};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
struct HashAlgo(Hash);
|
|
|
|
|
|
|
|
impl Arbitrary for HashAlgo {
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
|
|
|
match g.next_u32() % 4 { // make Hash::Identity more likely
|
|
|
|
0 => HashAlgo(Hash::SHA2256),
|
|
|
|
_ => HashAlgo(Hash::Identity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn property(data: Vec<u8>, algo1: HashAlgo, algo2: HashAlgo) -> bool {
|
|
|
|
let a = PeerId::try_from(multihash::encode(algo1.0, &data).unwrap()).unwrap();
|
|
|
|
let b = PeerId::try_from(multihash::encode(algo2.0, &data).unwrap()).unwrap();
|
|
|
|
|
|
|
|
if algo1 == algo2 || algo1.0 == Hash::Identity || algo2.0 == Hash::Identity {
|
|
|
|
a == b
|
|
|
|
} else {
|
|
|
|
a != b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quickcheck::quickcheck(property as fn(Vec<u8>, HashAlgo, HashAlgo) -> bool)
|
|
|
|
}
|
2018-06-05 19:01:18 +02:00
|
|
|
}
|