use crate::{
ed25519::{self},
error::{Error, Result},
peer::PeerUtils,
};
use sn_messaging::{
node::{Peer, SrcAuthority},
SrcLocation,
};
use std::net::SocketAddr;
use xor_name::XorName;
pub trait SrcAuthorityUtils {
fn src_location(&self) -> SrcLocation;
fn is_section(&self) -> bool;
fn name(&self) -> XorName;
fn peer(&self, addr: SocketAddr) -> Result<Peer>;
}
impl SrcAuthorityUtils for SrcAuthority {
fn src_location(&self) -> SrcLocation {
match self {
SrcAuthority::Node { public_key, .. } => SrcLocation::Node(ed25519::name(public_key)),
SrcAuthority::BlsShare { src_name, .. } => SrcLocation::Section(*src_name),
SrcAuthority::Section { src_name, .. } => SrcLocation::Section(*src_name),
}
}
fn is_section(&self) -> bool {
matches!(self, SrcAuthority::Section { .. })
}
fn name(&self) -> XorName {
match self {
SrcAuthority::Node { public_key, .. } => ed25519::name(public_key),
SrcAuthority::BlsShare { src_name, .. } => *src_name,
SrcAuthority::Section { src_name, .. } => *src_name,
}
}
fn peer(&self, addr: SocketAddr) -> Result<Peer> {
match self {
SrcAuthority::Node { public_key, .. } => Ok(Peer::new(ed25519::name(public_key), addr)),
SrcAuthority::Section { .. } | SrcAuthority::BlsShare { .. } => {
Err(Error::InvalidSrcLocation)
}
}
}
}