aboutsummaryrefslogtreecommitdiff
path: root/p2p/src/peer/peer_id.rs
blob: c8aec7d34f3e473e7bf04e949bd6a7919f48b6d4 (plain)
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
use bincode::{Decode, Encode};
use rand::{rngs::OsRng, RngCore};
use sha2::{Digest, Sha256};

/// Represents a unique identifier for a peer.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Decode, Encode)]
pub struct PeerID(pub [u8; 32]);

impl std::fmt::Display for PeerID {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let id = self.0[0..8]
            .iter()
            .map(|b| format!("{:x}", b))
            .collect::<Vec<String>>()
            .join("");

        write!(f, "{}", id)
    }
}

impl PeerID {
    /// Creates a new PeerID.
    pub fn new(src: &[u8]) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(src);
        Self(hasher.finalize().into())
    }

    /// Generates a random PeerID.
    pub fn random() -> Self {
        let mut id: [u8; 32] = [0; 32];
        OsRng.fill_bytes(&mut id);
        Self(id)
    }
}

impl From<[u8; 32]> for PeerID {
    fn from(b: [u8; 32]) -> Self {
        PeerID(b)
    }
}