Skip to content

Commit

Permalink
Add NymAddress type
Browse files Browse the repository at this point in the history
  • Loading branch information
pronebird committed Jan 21, 2025
1 parent c549c95 commit 4fdb9fe
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
29 changes: 27 additions & 2 deletions nym-vpn-core/crates/nym-vpn-lib-types/src/connection_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,35 @@ pub enum TunnelConnectionData {
Wireguard(WireguardConnectionData),
}

// Represents a nym-address of the form id.enc@gateway
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MixnetConnectionData {
pub struct NymAddress {
pub nym_address: String,
pub exit_ipr: String,
}

impl NymAddress {
pub fn new(nym_address: String) -> Self {
Self { nym_address }
}
}

impl fmt::Display for NymAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.nym_address.fmt(f)
}
}

#[cfg(feature = "nym-type-conversions")]
impl From<nym_gateway_directory::Recipient> for NymAddress {
fn from(value: nym_gateway_directory::Recipient) -> Self {
Self::new(value.gateway().to_base58_string())
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MixnetConnectionData {
pub nym_address: NymAddress,
pub exit_ipr: NymAddress,
pub ipv4: Ipv4Addr,
pub ipv6: Ipv6Addr,
}
Expand Down
4 changes: 2 additions & 2 deletions nym-vpn-core/crates/nym-vpn-lib-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod tunnel_event;
mod tunnel_state;

pub use connection_data::{
ConnectionData, Gateway, MixnetConnectionData, TunnelConnectionData, WireguardConnectionData,
WireguardNode,
ConnectionData, Gateway, MixnetConnectionData, NymAddress, TunnelConnectionData,
WireguardConnectionData, WireguardNode,
};
pub use tunnel_event::{
BandwidthEvent, ConnectionEvent, ConnectionStatisticsEvent, MixnetEvent, SphinxPacketRates,
Expand Down
28 changes: 21 additions & 7 deletions nym-vpn-core/crates/nym-vpn-lib/src/platform/uniffi_lib_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use nym_vpn_lib_types::{
ConnectionStatisticsEvent as CoreConnectionStatisticsEvent,
ErrorStateReason as CoreErrorStateReason, Gateway as CoreGateway,
MixnetConnectionData as CoreMixnetConnectionData, MixnetEvent as CoreMixnetEvent,
SphinxPacketRates as CoreSphinxPacketRates, TunnelConnectionData as CoreTunnelConnectionData,
TunnelEvent as CoreTunnelEvent, TunnelState as CoreTunnelState,
WireguardConnectionData as CoreWireguardConnectionData, WireguardNode as CoreWireguardNode,
NymAddress as CoreNymAddress, SphinxPacketRates as CoreSphinxPacketRates,
TunnelConnectionData as CoreTunnelConnectionData, TunnelEvent as CoreTunnelEvent,
TunnelState as CoreTunnelState, WireguardConnectionData as CoreWireguardConnectionData,
WireguardNode as CoreWireguardNode,
};
use time::OffsetDateTime;

Expand Down Expand Up @@ -257,6 +258,19 @@ impl From<CoreGateway> for Gateway {
}
}

#[derive(uniffi::Record)]
pub struct NymAddress {
pub nym_address: String,
}

impl From<CoreNymAddress> for NymAddress {
fn from(value: CoreNymAddress) -> Self {
Self {
nym_address: value.nym_address,
}
}
}

#[derive(uniffi::Record)]
pub struct ConnectionData {
pub entry_gateway: Gateway,
Expand Down Expand Up @@ -292,8 +306,8 @@ impl From<CoreTunnelConnectionData> for TunnelConnectionData {
impl From<CoreMixnetConnectionData> for MixnetConnectionData {
fn from(value: CoreMixnetConnectionData) -> Self {
Self {
nym_address: value.nym_address,
exit_ipr: value.exit_ipr,
nym_address: NymAddress::from(value.nym_address),
exit_ipr: NymAddress::from(value.exit_ipr),
ipv4: value.ipv4,
ipv6: value.ipv6,
}
Expand All @@ -317,8 +331,8 @@ pub enum TunnelConnectionData {

#[derive(uniffi::Record)]
pub struct MixnetConnectionData {
pub nym_address: String,
pub exit_ipr: String,
pub nym_address: NymAddress,
pub exit_ipr: NymAddress,
pub ipv4: Ipv4Addr,
pub ipv6: Ipv6Addr,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use super::{
Error, NymConfig, Result, TunnelSettings,
};
use nym_vpn_lib_types::{
ConnectionData, ErrorStateReason, Gateway, MixnetConnectionData, MixnetEvent,
ConnectionData, ErrorStateReason, Gateway, MixnetConnectionData, MixnetEvent, NymAddress,
TunnelConnectionData, TunnelType, WireguardConnectionData, WireguardNode,
};

Expand Down Expand Up @@ -444,15 +444,8 @@ impl TunnelMonitor {
}

let tunnel_conn_data = TunnelConnectionData::Mixnet(MixnetConnectionData {
nym_address: assigned_addresses
.mixnet_client_address
.gateway()
.to_base58_string(),
exit_ipr: assigned_addresses
.exit_mix_addresses
.0
.gateway()
.to_base58_string(),
nym_address: NymAddress::from(assigned_addresses.mixnet_client_address),
exit_ipr: NymAddress::from(assigned_addresses.exit_mix_addresses.0),
ipv4: assigned_addresses.interface_addresses.ipv4,
ipv6: assigned_addresses.interface_addresses.ipv6,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use nym_vpn_lib_types::{
ActionAfterDisconnect, ConnectionData, ErrorStateReason, Gateway, MixnetConnectionData,
TunnelConnectionData, TunnelState, WireguardConnectionData, WireguardNode,
NymAddress, TunnelConnectionData, TunnelState, WireguardConnectionData, WireguardNode,
};

use crate::{
Expand All @@ -23,7 +23,7 @@ use crate::{
Disconnecting as ProtoDisconnecting, Error as ProtoError,
ErrorStateReason as ProtoErrorStateReason, Offline as ProtoOffline, State as ProtoState,
},
ConnectionData as ProtoConnectionData, Gateway as ProtoGateway,
Address as ProtoAddress, ConnectionData as ProtoConnectionData, Gateway as ProtoGateway,
MixnetConnectionData as ProtoMixnetConnectionData,
TunnelConnectionData as ProtoTunnelConnectionData, TunnelState as ProtoTunnelState,
WireguardConnectionData as ProtoWireguardConnectionData, WireguardNode as ProtoWireguardNode,
Expand Down Expand Up @@ -158,16 +158,13 @@ impl TryFrom<ProtoMixnetConnectionData> for MixnetConnectionData {

fn try_from(value: ProtoMixnetConnectionData) -> Result<Self> {
Ok(Self {
nym_address: value
.nym_address
.ok_or(ConversionError::NoValueSet(
"MixnetConnectionData.nym_address",
))?
.nym_address,
nym_address: value.nym_address.map(NymAddress::from).ok_or(
ConversionError::NoValueSet("MixnetConnectionData.nym_address"),
)?,
exit_ipr: value
.exit_ipr
.ok_or(ConversionError::NoValueSet("MixnetConnectionData.exit_ipr"))?
.nym_address,
.map(NymAddress::from)
.ok_or(ConversionError::NoValueSet("MixnetConnectionData.exit_ipr"))?,
ipv4: Ipv4Addr::from_str(&value.ipv4)
.map_err(|e| ConversionError::ParseAddr("MixnetConnectionData.ipv4", e))?,
ipv6: Ipv6Addr::from_str(&value.ipv6)
Expand Down Expand Up @@ -217,4 +214,10 @@ impl From<ProtoGateway> for Gateway {
}
}

impl From<ProtoAddress> for NymAddress {
fn from(value: ProtoAddress) -> Self {
Self::new(value.nym_address)
}
}

pub type Result<T, E = ConversionError> = std::result::Result<T, E>;

0 comments on commit 4fdb9fe

Please sign in to comment.