diff --git a/common/client-core/src/init/helpers.rs b/common/client-core/src/init/helpers.rs index 3f6a390bd0..30e73a5142 100644 --- a/common/client-core/src/init/helpers.rs +++ b/common/client-core/src/init/helpers.rs @@ -7,7 +7,7 @@ use futures::{SinkExt, StreamExt}; use log::{debug, info, trace, warn}; use nym_crypto::asymmetric::identity; use nym_gateway_client::GatewayClient; -use nym_topology::{gateway, mix}; +use nym_topology::gateway; use nym_validator_client::client::IdentityKeyRef; use nym_validator_client::UserAgent; use rand::{seq::SliceRandom, Rng}; @@ -94,7 +94,9 @@ pub async fn current_gateways( log::debug!("Fetching list of gateways from: {nym_api}"); - let gateways = client.get_all_basic_entry_assigned_nodes(None).await?; + // let gateways = client.get_all_basic_entry_assigned_nodes(None).await?; + #[allow(deprecated)] + let gateways = client.get_cached_described_gateways().await?; log::debug!("Found {} gateways", gateways.len()); log::trace!("Gateways: {:#?}", gateways); @@ -110,27 +112,28 @@ pub async fn current_gateways( Ok(valid_gateways) } -pub async fn current_mixnodes( - rng: &mut R, - nym_apis: &[Url], -) -> Result, ClientCoreError> { - let nym_api = nym_apis - .choose(rng) - .ok_or(ClientCoreError::ListOfNymApisIsEmpty)?; - let client = nym_validator_client::client::NymApiClient::new(nym_api.clone()); - - log::trace!("Fetching list of mixnodes from: {nym_api}"); - - let mixnodes = client - .get_all_basic_active_mixing_assigned_nodes(None) - .await?; - let valid_mixnodes = mixnodes - .iter() - .filter_map(|mixnode| mixnode.try_into().ok()) - .collect::>(); - - Ok(valid_mixnodes) -} +//pub async fn current_mixnodes( +// rng: &mut R, +// nym_apis: &[Url], +//) -> Result, ClientCoreError> { +// let nym_api = nym_apis +// .choose(rng) +// .ok_or(ClientCoreError::ListOfNymApisIsEmpty)?; +// let client = nym_validator_client::client::NymApiClient::new(nym_api.clone()); +// +// log::trace!("Fetching list of mixnodes from: {nym_api}"); +// +// //let mixnodes = client +// // .get_all_basic_active_mixing_assigned_nodes(None) +// // .await?; +// let mixnodes = client.get_cached_mixnodes().await?; +// let valid_mixnodes = mixnodes +// .iter() +// .filter_map(|mixnode| (&mixnode.bond_information).try_into().ok()) +// .collect::>(); +// +// Ok(valid_mixnodes) +//} #[cfg(not(target_arch = "wasm32"))] async fn connect(endpoint: &str) -> Result { diff --git a/common/topology/src/gateway.rs b/common/topology/src/gateway.rs index e6f4981560..8b501f330a 100644 --- a/common/topology/src/gateway.rs +++ b/common/topology/src/gateway.rs @@ -2,8 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 use crate::{filter, NetworkAddress, NodeVersion}; +use nym_api_requests::models::LegacyDescribedGateway; use nym_api_requests::nym_nodes::SkimmedNode; use nym_crypto::asymmetric::{encryption, identity}; +use nym_mixnet_contract_common::GatewayBond; use nym_mixnet_contract_common::NodeId; use nym_sphinx_addressing::nodes::{NodeIdentity, NymNodeRoutingAddress}; use nym_sphinx_types::Node as SphinxNode; @@ -179,3 +181,83 @@ impl<'a> TryFrom<&'a SkimmedNode> for LegacyNode { }) } } + +impl<'a> TryFrom<&'a GatewayBond> for LegacyNode { + type Error = GatewayConversionError; + + fn try_from(bond: &'a GatewayBond) -> Result { + let host = Self::parse_host(&bond.gateway.host)?; + + // try to completely resolve the host in the mix situation to avoid doing it every + // single time we want to construct a path + let mix_host = Self::extract_mix_host(&host, bond.gateway.mix_port)?; + + Ok(LegacyNode { + // owner: Some(bond.owner.as_str().to_owned()), + node_id: 0, + host, + mix_host, + clients_ws_port: bond.gateway.clients_port, + clients_wss_port: None, + identity_key: identity::PublicKey::from_base58_string(&bond.gateway.identity_key)?, + sphinx_key: encryption::PublicKey::from_base58_string(&bond.gateway.sphinx_key)?, + version: bond.gateway.version.as_str().into(), + }) + } +} + +impl TryFrom for LegacyNode { + type Error = GatewayConversionError; + + fn try_from(bond: GatewayBond) -> Result { + LegacyNode::try_from(&bond) + } +} + +impl<'a> TryFrom<&'a LegacyDescribedGateway> for LegacyNode { + type Error = GatewayConversionError; + + fn try_from(value: &'a LegacyDescribedGateway) -> Result { + let Some(ref self_described) = value.self_described else { + return (&value.bond).try_into(); + }; + + let ips = &self_described.host_information.ip_address; + if ips.is_empty() { + return Err(GatewayConversionError::NoIpAddressesProvided { + gateway: value.bond.gateway.identity_key.clone(), + }); + } + + let host = match &self_described.host_information.hostname { + None => NetworkAddress::IpAddr(ips[0]), + Some(hostname) => NetworkAddress::Hostname(hostname.clone()), + }; + + // get ip from the self-reported values so we wouldn't need to do any hostname resolution + // (which doesn't really work in wasm) + let mix_host = SocketAddr::new(ips[0], value.bond.gateway.mix_port); + + Ok(LegacyNode { + // owner: Some(value.bond.owner.as_str().to_owned()), + node_id: 0, + host, + mix_host, + clients_ws_port: self_described.mixnet_websockets.ws_port, + clients_wss_port: self_described.mixnet_websockets.wss_port, + //identity_key: identity::PublicKey::from_base58_string( + // &self_described.host_information.keys.ed25519, + //)?, + identity_key: self_described.host_information.keys.ed25519, + //sphinx_key: encryption::PublicKey::from_base58_string( + // &self_described.host_information.keys.x25519, + //)?, + sphinx_key: self_described.host_information.keys.x25519, + version: self_described + .build_information + .build_version + .as_str() + .into(), + }) + } +}