mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-21 22:01:34 +00:00
Use a Multihash in AddrComponent::P2P, drop cid (#407)
* Use a Multihash in AddrComponent::P2P * Remove the cid crate from the repo
This commit is contained in:
committed by
Benjamin Kampmann
parent
d4b98e8646
commit
d94fe1b831
@ -53,11 +53,28 @@ impl PeerId {
|
||||
#[inline]
|
||||
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
|
||||
match multihash::Multihash::from_bytes(data) {
|
||||
Ok(multihash) => Ok(PeerId { multihash }),
|
||||
Ok(multihash) => {
|
||||
if multihash.algorithm() == multihash::Hash::SHA2256 {
|
||||
Ok(PeerId { multihash })
|
||||
} 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.
|
||||
#[inline]
|
||||
pub fn from_multihash(data: multihash::Multihash) -> Result<PeerId, multihash::Multihash> {
|
||||
if data.algorithm() == multihash::Hash::SHA2256 {
|
||||
Ok(PeerId { multihash: data })
|
||||
} else {
|
||||
Err(data)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a raw bytes representation of this `PeerId`.
|
||||
///
|
||||
/// Note that this is not the same as the public key of the peer.
|
||||
@ -106,6 +123,13 @@ impl From<PublicKey> for PeerId {
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<multihash::Multihash> for PeerId {
|
||||
#[inline]
|
||||
fn into(self) -> multihash::Multihash {
|
||||
self.multihash
|
||||
}
|
||||
}
|
||||
|
||||
quick_error! {
|
||||
#[derive(Debug)]
|
||||
pub enum ParseError {
|
||||
|
Reference in New Issue
Block a user