veilid/veilid-core/src/veilid_api/privacy.rs

121 lines
2.8 KiB
Rust
Raw Normal View History

use super::*;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compiled Privacy Objects
#[derive(Clone, Debug)]
pub struct RouteHopData {
pub nonce: Nonce,
pub blob: Vec<u8>,
}
2022-10-10 02:07:15 +00:00
#[derive(Clone, Debug)]
pub enum RouteNode {
2022-10-19 01:53:45 +00:00
NodeId(NodeId),
2022-10-10 02:07:15 +00:00
PeerInfo(PeerInfo),
}
impl fmt::Display for RouteNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
2022-10-19 01:53:45 +00:00
RouteNode::NodeId(x) => x.key.encode(),
2022-10-10 02:07:15 +00:00
RouteNode::PeerInfo(pi) => pi.node_id.key.encode(),
}
)
}
}
#[derive(Clone, Debug)]
pub struct RouteHop {
2022-10-10 02:07:15 +00:00
pub node: RouteNode,
2022-10-29 02:26:21 +00:00
pub next_hop: RouteHopData,
}
#[derive(Clone, Debug)]
pub struct PrivateRoute {
pub public_key: DHTKey,
pub hop_count: u8,
2022-10-10 02:07:15 +00:00
pub first_hop: Option<RouteHop>,
}
impl PrivateRoute {
pub fn new_stub(public_key: DHTKey) -> Self {
Self {
public_key,
hop_count: 0,
2022-10-10 02:07:15 +00:00
first_hop: None,
}
}
2022-10-20 19:09:04 +00:00
pub fn simplify(self) -> Self {
Self {
public_key: self.public_key,
hop_count: self.hop_count,
first_hop: self.first_hop.map(|h| RouteHop {
node: match h.node {
RouteNode::NodeId(ni) => RouteNode::NodeId(ni),
RouteNode::PeerInfo(pi) => RouteNode::NodeId(pi.node_id),
},
next_hop: h.next_hop,
}),
}
}
}
impl fmt::Display for PrivateRoute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"PR({:?}+{}{})",
self.public_key,
self.hop_count,
2022-10-10 02:07:15 +00:00
if let Some(first_hop) = &self.first_hop {
format!("->{}", first_hop.node)
} else {
"".to_owned()
}
)
}
}
#[derive(Clone, Debug)]
pub enum SafetyRouteHops {
2022-10-29 02:26:21 +00:00
/// Has >= 1 safety route hops
Data(RouteHopData),
2022-10-29 02:26:21 +00:00
/// Has 0 safety route hops
Private(PrivateRoute),
}
#[derive(Clone, Debug)]
pub struct SafetyRoute {
pub public_key: DHTKey,
pub hop_count: u8,
pub hops: SafetyRouteHops,
}
2022-10-20 19:09:04 +00:00
impl SafetyRoute {
pub fn new_stub(public_key: DHTKey, private_route: PrivateRoute) -> Self {
Self {
public_key,
hop_count: 0,
hops: SafetyRouteHops::Private(private_route),
}
}
}
impl fmt::Display for SafetyRoute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"SR({:?}+{}{})",
self.public_key,
self.hop_count,
match &self.hops {
SafetyRouteHops::Data(_) => "".to_owned(),
SafetyRouteHops::Private(p) => format!("->{}", p),
}
)
}
}