mwc_libp2p_core/
peer_id.rs

1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use crate::PublicKey;
22use multihash::{Code, Error, Multihash, MultihashDigest};
23use rand::Rng;
24use std::{convert::TryFrom, fmt, str::FromStr};
25use thiserror::Error;
26use std::hash::Hash;
27use sha3::{Digest, Sha3_256};
28use data_encoding::BASE32;
29
30/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
31/// automatically used as the peer id using an identity multihash.
32const MAX_INLINE_KEY_LENGTH: usize = 42;
33
34/// Identifier of a peer of the network.
35///
36/// The data is a multihash of the public key of the peer.
37#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
38pub struct PeerId {
39    multihash: Multihash,
40}
41
42impl fmt::Debug for PeerId {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        let address_str = match self.as_onion_address() {
45            Ok(onion_addr) => onion_addr,
46            Err(_) => self.to_base58(),
47        };
48        f.debug_tuple("PeerId")
49            .field(&address_str)
50            .finish()
51    }
52}
53
54impl fmt::Display for PeerId {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self.as_onion_address() {
57            Ok(onion_addr) => write!(f, "{}", onion_addr ),
58            Err(_) => write!(f, "{}", self.to_base58() ),
59        }
60    }
61}
62
63#[derive(Debug, Error)]
64pub enum ParseError {
65    #[error("base-58 decode error: {0}")]
66    B58(#[from] bs58::decode::Error),
67    #[error("decoding multihash failed")]
68    MultiHash,
69    #[error("PeerId doesn't have Dalek Public Key")]
70    NotFoundDalekPK,
71    #[error("PeerId Error: {0}")]
72    GenericError(String),
73}
74
75impl PeerId {
76    /// Builds a `PeerId` from a public key.
77    pub fn from_public_key(key: PublicKey) -> PeerId {
78        let key_enc = key.into_protobuf_encoding();
79
80        let hash_algorithm = if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
81            Code::Identity
82        } else {
83            Code::Sha2_256
84        };
85
86        let multihash = hash_algorithm.digest(&key_enc);
87
88        PeerId { multihash }
89    }
90
91    pub fn get_address(&self) -> Result<String, ParseError> {
92        self.as_onion_address()
93    }
94
95    /// Parses a `PeerId` from bytes.
96    pub fn from_bytes(data: &[u8]) -> Result<PeerId, Error> {
97        Ok(PeerId::from_multihash(Multihash::from_bytes(&data)?)
98            .map_err(|mh| Error::UnsupportedCode(mh.code()))?)
99    }
100
101    /// Tries to turn a `Multihash` into a `PeerId`.
102    ///
103    /// If the multihash does not use a valid hashing algorithm for peer IDs,
104    /// or the hash value does not satisfy the constraints for a hashed
105    /// peer ID, it is returned as an `Err`.
106    pub fn from_multihash(multihash: Multihash) -> Result<PeerId, Multihash> {
107        match Code::try_from(multihash.code()) {
108            Ok(Code::Sha2_256) => Ok(PeerId { multihash }),
109            Ok(Code::Identity) if multihash.digest().len() <= MAX_INLINE_KEY_LENGTH
110                => Ok(PeerId { multihash }),
111            _ => Err(multihash)
112        }
113    }
114
115    /// Generates a random peer ID from a cryptographically secure PRNG.
116    ///
117    /// This is useful for randomly walking on a DHT, or for testing purposes.
118    pub fn random() -> PeerId {
119        let peer_id = rand::thread_rng().gen::<[u8; 32]>();
120        PeerId {
121            multihash: Multihash::wrap(Code::Identity.into(), &peer_id)
122                .expect("The digest size is never too large")
123        }
124    }
125
126    /// Returns a raw bytes representation of this `PeerId`.
127    pub fn to_bytes(&self) -> Vec<u8> {
128        self.multihash.to_bytes()
129    }
130
131    /// Currently to_hash_bytes & to_bytes are the same. But if in case we will need
132    /// to add some fields into PeerId, there will be a difference
133    pub fn to_hash_bytes(&self) -> Vec<u8> {
134        self.multihash.to_bytes()
135    }
136
137    /// Returns a base-58 encoded string of this `PeerId`.
138    pub fn to_base58(&self) -> String {
139        bs58::encode(self.to_bytes()).into_string()
140    }
141
142    /// Checks whether the public key passed as parameter matches the public key of this `PeerId`.
143    ///
144    /// Returns `None` if this `PeerId`s hash algorithm is not supported when encoding the
145    /// given public key, otherwise `Some` boolean as the result of an equality check.
146    pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
147        let alg = Code::try_from(self.multihash.code())
148            .expect("Internal multihash is always a valid `Code`");
149        let enc = public_key.clone().into_protobuf_encoding();
150        Some(alg.digest(&enc) == self.multihash)
151    }
152
153    pub fn as_dalek_pubkey(&self) -> Result<ed25519_dalek::PublicKey, ParseError> {
154        match Code::try_from(self.multihash.code()) {
155            Ok(Code::Identity) => {
156                let pk = PublicKey::from_protobuf_encoding( self.multihash.digest() )
157                    .map_err(|e| ParseError::GenericError(format!("Unable to parse PeerId data, {}",e)))?;
158
159                match pk {
160                    PublicKey::Ed25519(pk) => Ok(pk.0),
161                    _ =>  Err(ParseError::NotFoundDalekPK),
162                }
163            },
164            _ => return Err(ParseError::NotFoundDalekPK),
165        }
166    }
167
168    pub fn as_onion_address(&self) -> Result<String, ParseError> {
169        let pk = self.as_dalek_pubkey()?;
170        Ok(Self::onion_v3_from_pubkey(&pk))
171    }
172
173    // Generate an onion address from an ed25519_dalek public key
174    pub fn onion_v3_from_pubkey(pub_key: &ed25519_dalek::PublicKey) -> String {
175        // calculate checksum
176        let mut hasher = Sha3_256::new();
177        hasher.input(b".onion checksum");
178        hasher.input(pub_key.as_bytes());
179        hasher.input([0x03u8]);
180        let checksum = hasher.result();
181
182        let mut address_bytes = pub_key.as_bytes().to_vec();
183        address_bytes.push(checksum[0]);
184        address_bytes.push(checksum[1]);
185        address_bytes.push(0x03u8);
186
187        let ret = BASE32.encode(&address_bytes);
188        ret.to_lowercase()
189    }
190}
191
192impl TryFrom<Vec<u8>> for PeerId {
193    type Error = Vec<u8>;
194
195    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
196        PeerId::from_bytes(&value).map_err(|_| value)
197    }
198}
199
200impl AsRef<Multihash> for PeerId {
201    fn as_ref(&self) -> &Multihash {
202        &self.multihash
203    }
204}
205
206impl From<PeerId> for Multihash {
207    fn from(peer_id: PeerId) -> Self {
208        peer_id.multihash
209    }
210}
211
212/* Automatic conversion is disabled because sometimes we need all bytes, sometimes just multihash data
213impl From<PeerId> for Vec<u8> {
214    fn from(peer_id: PeerId) -> Self {
215        peer_id.to_bytes()
216    }
217}*/
218
219impl FromStr for PeerId {
220    type Err = ParseError;
221
222    #[inline]
223    fn from_str(s: &str) -> Result<Self, Self::Err> {
224        let bytes = bs58::decode(s).into_vec()?;
225        PeerId::from_bytes(&bytes).map_err(|_| ParseError::MultiHash)
226    }
227}
228
229#[cfg(test)]
230mod tests {
231    use crate::{PeerId, identity};
232
233    #[test]
234    fn peer_id_is_public_key() {
235        let key = identity::Keypair::generate_ed25519().public();
236        let peer_id = key.clone().into_peer_id();
237        assert_eq!(peer_id.is_public_key(&key), Some(true));
238    }
239
240    #[test]
241    fn peer_id_into_bytes_then_from_bytes() {
242        let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
243        let second = PeerId::from_bytes(&peer_id.to_bytes()).unwrap();
244        assert_eq!(peer_id, second);
245    }
246
247    #[test]
248    fn peer_id_to_base58_then_back() {
249        let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
250        let second: PeerId = peer_id.to_base58().parse().unwrap();
251        assert_eq!(peer_id, second);
252    }
253
254    #[test]
255    fn random_peer_id_is_valid() {
256        for _ in 0 .. 5000 {
257            let peer_id = PeerId::random();
258            assert_eq!(peer_id, PeerId::from_bytes(&peer_id.to_bytes()).unwrap());
259        }
260    }
261}