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:
Pierre Krieger
2018-08-10 17:47:02 +02:00
committed by Benjamin Kampmann
parent d4b98e8646
commit d94fe1b831
20 changed files with 70 additions and 666 deletions

View File

@ -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 {