1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! Module implement Peer ID for libp2p network. _Refer [peer-id] spec
//! for details.
//!
//! [peer-id]: https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md

use bs58;
use multibase::Base;
use rand::Rng;

use std::{fmt, hash};

use crate::{
    identity::PublicKey,
    multibase::Multibase,
    multicodec::{self, Multicodec},
    multihash::Multihash,
    Error, Result,
};

/// Keys that serialize to more than 42 bytes must be hashed using
/// sha256 multihash, keys that serialize to at most 42 bytes must
/// be hashed using the "identity" multihash codec.
const MAX_INLINE_KEY_LENGTH: usize = 42;

/// Unique identifier of a peer in the network.
///
/// Peer IDs are derived by hashing the encoded public-key with multihash.
///
/// PublicKey to PeerId:
///
/// * Encode the public key as described in the [keys section].
/// * If the length of the serialized bytes is less than or equal to 42,
///   compute the "identity" multihash of the serialized bytes. In other
///   words, no hashing is performed, but the multihash format is still
///   followed. The idea here is that if the serialized byte array is
///   short enough, we can fit it in a multihash verbatim without having
///   to condense it using a hash function.
/// * If the length is greater than 42, then hash it using it using the
///   SHA256 multihash.
///
/// [keys section]: https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#keys
#[derive(Clone, Eq)]
pub struct PeerId {
    mh: Multihash,
}

impl fmt::Debug for PeerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("PeerId").field(&self.to_base58btc()).finish()
    }
}

impl fmt::Display for PeerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.to_base58btc() {
            Ok(val) => val.fmt(f),
            Err(_) => Err(fmt::Error),
        }
    }
}

impl hash::Hash for PeerId {
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher,
    {
        hash::Hash::hash(&self.mh.encode().unwrap(), state)
    }
}

impl PartialEq<PeerId> for PeerId {
    fn eq(&self, other: &PeerId) -> bool {
        self.mh == other.mh
    }
}

impl From<Multihash> for PeerId {
    fn from(mh: Multihash) -> Self {
        PeerId { mh }
    }
}

impl From<PeerId> for Multihash {
    fn from(peer_id: PeerId) -> Self {
        peer_id.mh
    }
}

impl PeerId {
    /// Builds a `PeerId` from a public key.
    pub fn from_public_key(key: PublicKey) -> Result<PeerId> {
        let enc_buf = key.into_protobuf_encoding()?;

        let codec: Multicodec = match enc_buf.len() <= MAX_INLINE_KEY_LENGTH {
            true => multicodec::IDENTITY.into(),
            false => multicodec::SHA2_256.into(),
        };

        let mh = Multihash::new(codec, &enc_buf)?;
        Ok(PeerId { mh })
    }

    /// 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 generate() -> Result<PeerId> {
        let (bytes, codec) = match rand::thread_rng().gen::<bool>() {
            true => {
                let codec: Multicodec = multicodec::IDENTITY.into();
                let bytes = rand::thread_rng().gen::<[u8; 32]>().to_vec();
                (bytes, codec)
            }
            false => {
                let codec: Multicodec = multicodec::SHA2_256.into();
                let bytes = {
                    let mut data = vec![];
                    data.extend(&rand::thread_rng().gen::<[u8; 32]>());
                    data.extend(&rand::thread_rng().gen::<[u8; 32]>());
                    data
                };
                (bytes, codec)
            }
        };

        let mh = Multihash::new(codec, &bytes)?;
        Ok(PeerId { mh })
    }

    /// Decode a base encoded PeerId, human readable text. Peerid format
    /// can either be in legacy format (base58btc) or multi-base encoded
    /// CID format.
    pub fn from_text(text: &str) -> Result<PeerId> {
        let mut chars = text.chars();
        let peer_id = match (chars.next(), chars.next()) {
            (Some('Q'), Some('m')) | (Some('1'), Some(_)) => {
                // legacy format base58btc.
                let bytes = {
                    let res = bs58::decode(text.as_bytes()).into_vec();
                    err_at!(BadInput, res)?
                };
                let (mh, _) = Multihash::decode(&bytes)?;
                PeerId { mh }
            }
            _ => {
                let bytes = {
                    let mb = Multibase::decode(text)?;
                    match mb.to_bytes() {
                        Some(bytes) => bytes,
                        None => err_at!(BadInput, msg: format!("{}", text))?,
                    }
                };
                // <multicodec-cidv1><libp2p-key-codec><multihash>
                let (codec, bytes) = Multicodec::decode(&bytes)?;
                match codec.to_code() {
                    multicodec::CID_V1 => (),
                    _ => err_at!(BadInput, msg: format!("CID {}", codec))?,
                }

                let (codec, bytes) = Multicodec::decode(bytes)?;
                match codec.to_code() {
                    multicodec::LIBP2P_KEY => (),
                    _ => err_at!(BadInput, msg: format!("codec {}", codec))?,
                }
                let (mh, _) = Multihash::decode(bytes)?;
                PeerId { mh }
            }
        };

        Ok(peer_id)
    }

    /// Encode peer-id to base58btc format.
    pub fn to_base58btc(&self) -> Result<String> {
        Ok(bs58::encode(self.mh.encode()?).into_string())
    }

    /// Encode peer-id to multi-base encoded CID format.
    pub fn to_base_text(&self, base: Base) -> Result<String> {
        let mut data = {
            let codec = Multicodec::from_code(multicodec::CID_V1)?;
            codec.encode()?
        };
        {
            let codec = Multicodec::from_code(multicodec::LIBP2P_KEY)?;
            data.extend(codec.encode()?);
        };
        data.extend(self.mh.encode()?);

        Ok(Multibase::from_base(base.clone(), &data)?.encode()?)
    }

    /// Encode PeerId into multihash-binary-format.
    ///
    /// **NOTE:** This byte representation is not necessarily consistent
    /// with equality of peer IDs. That is, two peer IDs may be considered
    /// equal while having a different byte representation.
    pub fn encode(&self) -> Result<Vec<u8>> {
        self.mh.encode()
    }

    /// Decode PeerId from multihash-binary-format.
    pub fn decode(buf: &[u8]) -> Result<(PeerId, &[u8])> {
        let (mh, rem) = Multihash::decode(buf)?;
        Ok((PeerId { mh }, rem))
    }

    /// 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.
    pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
        let other = PeerId::from_public_key(public_key.clone()).ok()?;
        Some(self.mh == other.mh)
    }

    /// Return the peer-id as condensed version of PeerID::to_string().
    pub fn to_short_string(&self) -> String {
        use std::iter::FromIterator;

        let s = self.to_string();
        let chars: Vec<char> = s.chars().collect();

        if chars.len() <= 10 {
            String::from_iter(chars.into_iter())
        } else {
            let mut short = chars[..2].to_vec();
            short.push('*');
            short.extend_from_slice(&chars[(chars.len() - 6)..]);
            String::from_iter(short.into_iter())
        }
    }

    /// Sometimes PeerID could be encoded using IDENTITY hash. Which means,
    /// unlike when it is encoded as public-key's hash, it is possible to
    /// extract the public-key from the peer-id.
    pub fn to_public_key(&self) -> Result<Option<PublicKey>> {
        let (codec, digest) = self.mh.clone().unwrap()?;
        let public_key = match codec.to_code() {
            multicodec::IDENTITY => {
                let public_key = PublicKey::from_protobuf_encoding(&digest)?;
                Some(public_key)
            }
            _ => None,
        };

        Ok(public_key)
    }
}

#[cfg(test)]
#[path = "peer_id_test.rs"]
mod peer_id_test;