mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-01 20:21:21 +00:00
Make PeerId own its data
This commit is contained in:
parent
0e8216bf38
commit
68f573d8ae
@ -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<u8>
|
||||
}
|
||||
|
||||
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<u8>) -> 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");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user