diff --git a/libp2p-peer/src/lib.rs b/libp2p-peer/src/lib.rs index a798abe6..ee11532b 100644 --- a/libp2p-peer/src/lib.rs +++ b/libp2p-peer/src/lib.rs @@ -1,19 +1,27 @@ extern crate base58; +use std::fmt; use base58::ToBase58; /// A PeerId is a reference to a multihash /// Ideally we would want to store the Multihash object directly here but because /// the multihash package is lacking some things right now, lets store a reference to /// some bytes that represent the full bytes of the multihash -pub struct PeerId<'a> { +#[derive(PartialEq, Eq, Hash, Debug, Clone)] +pub struct PeerId { /// Rereference to multihash bytes - multihash: &'a [u8] + multihash: Vec } -impl<'a> PeerId<'a> { +impl fmt::Display for PeerId { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_base58()) + } +} + +impl PeerId { /// Create a new PeerId from a multihash - pub fn new(mh: &'a [u8]) -> PeerId<'a> { + pub fn new(mh: Vec) -> PeerId { PeerId { multihash: mh } } @@ -34,7 +42,14 @@ mod tests { #[test] fn peer_id_produces_correct_b58() { let multihash_bytes = encode(Hash::SHA2256, b"hello world").unwrap(); - let peer_id = PeerId::new(&multihash_bytes); + let peer_id = PeerId::new(multihash_bytes); assert_eq!(peer_id.to_base58(), "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"); } + + #[test] + fn string_key_concatenates_correctly() { + let multihash_bytes = encode(Hash::SHA2256, b"hello world").unwrap(); + let peer_id = PeerId::new(multihash_bytes); + assert_eq!(peer_id.string_key("hello"), "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4/hello"); + } }