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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// 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 crate::PublicKey;
use bs58;
use thiserror::Error;
use multihash;
use std::{convert::TryFrom, borrow::Borrow, fmt, hash, str::FromStr};

/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
/// automatically used as the peer id using an identity multihash.
const _MAX_INLINE_KEY_LENGTH: usize = 42;

/// 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?
#[derive(Clone, Eq)]
pub struct PeerId {
    multihash: multihash::Multihash,
    /// A (temporary) "canonical" multihash if `multihash` is of type
    /// multihash::Hash::Identity, so that `Borrow<[u8]>` semantics
    /// can be given, i.e. a view of a byte representation whose
    /// equality is consistent with `PartialEq`.
    canonical: Option<multihash::Multihash>,
}

impl fmt::Debug for PeerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        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)
    }
}

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

        // 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
        // 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, canonical_algorithm) = /*if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
            (multihash::Hash::Identity, Some(multihash::Hash::SHA2256))
        } else {*/
            (multihash::Hash::SHA2256, None);
        //};

        let canonical = canonical_algorithm.map(|alg|
            multihash::encode(alg, &key_enc).expect("SHA2256 is always supported"));

        let multihash = multihash::encode(hash_algorithm, &key_enc)
            .expect("identity and sha2-256 are always supported by known public key types");

        PeerId { multihash, canonical }
    }

    /// 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>> {
        match multihash::Multihash::from_bytes(data) {
            Ok(multihash) => {
                if multihash.algorithm() == multihash::Hash::SHA2256 {
                    Ok(PeerId { multihash, canonical: None })
                }
                else if multihash.algorithm() == multihash::Hash::Identity {
                    let canonical = multihash::encode(multihash::Hash::SHA2256, multihash.digest())
                        .expect("SHA2256 is always supported");
                    Ok(PeerId { multihash, canonical: Some(canonical) })
                } else {
                    Err(multihash.into_bytes())
                }
            }
            Err(err) => Err(err.data),
        }
    }

    /// 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> {
        if data.algorithm() == multihash::Hash::SHA2256 {
            Ok(PeerId { multihash: data, canonical: None })
        } else if data.algorithm() == multihash::Hash::Identity {
            let canonical = multihash::encode(multihash::Hash::SHA2256, data.digest())
                .expect("SHA2256 is always supported");
            Ok(PeerId { multihash: data, canonical: Some(canonical) })
        } else {
            Err(data)
        }
    }

    /// 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),
            canonical: None,
        }
    }

    /// Returns a raw bytes representation of this `PeerId`.
    ///
    /// **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 as per `into_bytes`.
    pub fn into_bytes(self) -> Vec<u8> {
        self.multihash.into_bytes()
    }

    /// Returns a raw bytes representation of this `PeerId`.
    ///
    /// **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 as per `as_bytes`.
    pub fn as_bytes(&self) -> &[u8] {
        self.multihash.as_bytes()
    }

    /// Returns a base-58 encoded string of this `PeerId`.
    pub fn to_base58(&self) -> String {
        bs58::encode(self.borrow() as &[u8]).into_string()
    }

    /// 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 alg = self.multihash.algorithm();
        let enc = public_key.clone().into_protobuf_encoding();
        match multihash::encode(alg, &enc) {
            Ok(h) => Some(h == self.multihash),
            Err(multihash::EncodeError::UnsupportedType) => None,
            Err(multihash::EncodeError::UnsupportedInputLength) => None,
        }
    }
}

impl hash::Hash for PeerId {
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher
    {
        let digest = self.borrow() as &[u8];
        hash::Hash::hash(digest, state)
    }
}

impl From<PublicKey> for PeerId {
    #[inline]
    fn from(key: PublicKey) -> PeerId {
        PeerId::from_public_key(key)
    }
}

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)
    }
}

impl PartialEq<PeerId> for PeerId {
    fn eq(&self, other: &PeerId) -> bool {
        let self_digest = self.borrow() as &[u8];
        let other_digest = other.borrow() as &[u8];
        self_digest == other_digest
    }
}

impl Borrow<[u8]> for PeerId {
    fn borrow(&self) -> &[u8] {
        self.canonical.as_ref().map_or(self.multihash.as_bytes(), |c| c.as_bytes())
    }
}

/// **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 as per `AsRef<[u8]>`.
impl AsRef<[u8]> for PeerId {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

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

#[derive(Debug, Error)]
pub enum ParseError {
    #[error("base-58 decode error: {0}")]
    B58(#[from] bs58::decode::Error),
    #[error("decoding multihash failed")]
    MultiHash,
}

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)
    }
}

#[cfg(test)]
mod tests {
    use crate::{PeerId, identity};
    use std::{convert::TryFrom as _, hash::{self, Hasher as _}};

    #[test]
    fn peer_id_is_public_key() {
        let key = identity::Keypair::generate_ed25519().public();
        let peer_id = key.clone().into_peer_id();
        assert_eq!(peer_id.is_public_key(&key), Some(true));
    }

    #[test]
    fn peer_id_into_bytes_then_from_bytes() {
        let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
        let second = PeerId::from_bytes(peer_id.clone().into_bytes()).unwrap();
        assert_eq!(peer_id, second);
    }

    #[test]
    fn peer_id_to_base58_then_back() {
        let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
        let second: PeerId = peer_id.to_base58().parse().unwrap();
        assert_eq!(peer_id, second);
    }

    #[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());
        }
    }

    #[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)
    }
}