subspace_networking/utils/
multihash.rsuse std::error::Error;
use subspace_core_primitives::pieces::PieceIndex;
pub type Multihash = libp2p::multihash::Multihash<64>;
const SUBSPACE_MULTICODEC_NAMESPACE_START: u64 = 0xb39910;
#[derive(Debug, Clone, PartialEq)]
#[repr(u64)]
pub enum MultihashCode {
PieceIndex = SUBSPACE_MULTICODEC_NAMESPACE_START,
}
impl From<MultihashCode> for u64 {
#[inline]
fn from(code: MultihashCode) -> Self {
code as u64
}
}
impl TryFrom<u64> for MultihashCode {
type Error = Box<dyn Error>;
#[inline]
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
x if x == MultihashCode::PieceIndex as u64 => Ok(MultihashCode::PieceIndex),
_ => Err("Unexpected multihash code".into()),
}
}
}
pub trait ToMultihash {
fn to_multihash(&self) -> Multihash;
fn to_multihash_by_code(&self, code: MultihashCode) -> Multihash;
}
impl ToMultihash for PieceIndex {
fn to_multihash(&self) -> Multihash {
self.to_multihash_by_code(MultihashCode::PieceIndex)
}
fn to_multihash_by_code(&self, code: MultihashCode) -> Multihash {
Multihash::wrap(u64::from(code), &self.to_bytes())
.expect("Input never exceeds allocated size; qed")
}
}