2022-09-25 22:04:53 +00:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:04:53 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct RouteHop {
|
2022-10-10 02:07:15 +00:00
|
|
|
pub node: RouteNode,
|
2022-09-25 22:04:53 +00:00
|
|
|
pub next_hop: Option<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>,
|
2022-09-25 22:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-25 22:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 22:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-09-25 22:04:53 +00:00
|
|
|
} else {
|
|
|
|
"".to_owned()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum SafetyRouteHops {
|
|
|
|
Data(RouteHopData),
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:04:53 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|