diff --git a/common/crypto/src/identity/mod.rs b/common/crypto/src/identity/mod.rs index cf39000e5d..3c920f84dc 100644 --- a/common/crypto/src/identity/mod.rs +++ b/common/crypto/src/identity/mod.rs @@ -6,14 +6,13 @@ use bs58; use curve25519_dalek::scalar::Scalar; use sphinx::route::DestinationAddressBytes; -pub trait MixnetIdentityKeyPair: Clone -where - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, -{ +pub trait MixnetIdentityKeyPair: Clone { + type PublicKeyMaterial: MixnetIdentityPublicKey; + type PrivateKeyMaterial: MixnetIdentityPrivateKey; + fn new() -> Self; - fn private_key(&self) -> &Priv; - fn public_key(&self) -> &Pub; + fn private_key(&self) -> &Self::PrivateKeyMaterial; + fn public_key(&self) -> &Self::PublicKeyMaterial; fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self; // TODO: signing related methods @@ -56,9 +55,10 @@ pub struct DummyMixIdentityKeyPair { pub public_key: DummyMixIdentityPublicKey, } -impl MixnetIdentityKeyPair - for DummyMixIdentityKeyPair -{ +impl MixnetIdentityKeyPair for DummyMixIdentityKeyPair { + type PublicKeyMaterial = DummyMixIdentityPublicKey; + type PrivateKeyMaterial = DummyMixIdentityPrivateKey; + fn new() -> Self { let keypair = encryption::x25519::KeyPair::new(); DummyMixIdentityKeyPair { diff --git a/common/healthcheck/src/lib.rs b/common/healthcheck/src/lib.rs index 7cd97692db..5554b2bb97 100644 --- a/common/healthcheck/src/lib.rs +++ b/common/healthcheck/src/lib.rs @@ -1,10 +1,7 @@ use crate::result::HealthCheckResult; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; -use directory_client::requests::presence_topology_get::PresenceTopologyGetRequester; -use directory_client::DirectoryClient; -use log::{debug, error, info, trace}; +use crypto::identity::MixnetIdentityKeyPair; +use log::trace; use std::fmt::{Error, Formatter}; -use std::marker::PhantomData; use std::time::Duration; use topology::{NymTopology, NymTopologyError}; @@ -36,26 +33,13 @@ impl From for HealthCheckerError { } } -pub struct HealthChecker -where - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, -{ +pub struct HealthChecker { num_test_packets: usize, resolution_timeout: Duration, identity_keypair: IDPair, - - _phantom_private: PhantomData, - _phantom_public: PhantomData, } -impl HealthChecker -where - IDPair: crypto::identity::MixnetIdentityKeyPair, - Priv: crypto::identity::MixnetIdentityPrivateKey, - Pub: crypto::identity::MixnetIdentityPublicKey, -{ +impl HealthChecker { pub fn new( resolution_timeout_f64: f64, num_test_packets: usize, @@ -65,9 +49,6 @@ where resolution_timeout: Duration::from_secs_f64(resolution_timeout_f64), num_test_packets, identity_keypair, - - _phantom_private: PhantomData, - _phantom_public: PhantomData, } } diff --git a/common/healthcheck/src/path_check.rs b/common/healthcheck/src/path_check.rs index b1d2617ad8..a345e82d99 100644 --- a/common/healthcheck/src/path_check.rs +++ b/common/healthcheck/src/path_check.rs @@ -1,4 +1,4 @@ -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; use itertools::Itertools; use log::{debug, error, info, trace, warn}; use mix_client::MixClient; @@ -27,16 +27,11 @@ pub(crate) struct PathChecker { } impl PathChecker { - pub(crate) async fn new( + pub(crate) async fn new( providers: Vec, identity_keys: &IDPair, check_id: [u8; 16], - ) -> Self - where - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, - { + ) -> Self { let mut provider_clients = HashMap::new(); let address = identity_keys.public_key().derive_address(); diff --git a/common/healthcheck/src/result.rs b/common/healthcheck/src/result.rs index 9eda0885f5..2dd199ffde 100644 --- a/common/healthcheck/src/result.rs +++ b/common/healthcheck/src/result.rs @@ -1,6 +1,6 @@ use crate::path_check::{PathChecker, PathStatus}; use crate::score::NodeScore; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::MixnetIdentityKeyPair; use log::{debug, error, info, warn}; use rand_os::rand_core::RngCore; use sphinx::route::NodeAddressBytes; @@ -102,7 +102,7 @@ impl HealthCheckResult { id } - pub async fn calculate( + pub async fn calculate( topology: &T, iterations: usize, resolution_timeout: Duration, @@ -110,9 +110,7 @@ impl HealthCheckResult { ) -> Self where T: NymTopology, - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, + IDPair: MixnetIdentityKeyPair, { // currently healthchecker supports only up to 255 iterations - if we somehow // find we need more, it's relatively easy change diff --git a/common/pemstore/src/pemstore.rs b/common/pemstore/src/pemstore.rs index 9c7a914d02..84bc8cf60a 100644 --- a/common/pemstore/src/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -1,4 +1,6 @@ use crate::pathfinder::PathFinder; +use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::PemStorable; use pem::{encode, parse, Pem}; use std::fs::File; use std::io; @@ -25,12 +27,7 @@ impl PemStore { } } - pub fn read_identity(&self) -> io::Result - where - IDPair: crypto::identity::MixnetIdentityKeyPair, - Priv: crypto::identity::MixnetIdentityPrivateKey, - Pub: crypto::identity::MixnetIdentityPublicKey, - { + pub fn read_identity(&self) -> io::Result { let private_pem = self.read_pem_file(self.private_mix_key.clone())?; let public_pem = self.read_pem_file(self.public_mix_key.clone())?; @@ -62,12 +59,10 @@ impl PemStore { // This should be refactored and made more generic for when we have other kinds of // KeyPairs that we want to persist (e.g. validator keypairs, or keys for // signing vs encryption). However, for the moment, it does the job. - pub fn write_identity(&self, key_pair: IDPair) -> io::Result<()> - where - IDPair: crypto::identity::MixnetIdentityKeyPair, - Priv: crypto::identity::MixnetIdentityPrivateKey, - Pub: crypto::identity::MixnetIdentityPublicKey, - { + pub fn write_identity( + &self, + key_pair: IDPair, + ) -> io::Result<()> { std::fs::create_dir_all(self.config_dir.clone())?; let private_key = key_pair.private_key(); diff --git a/nym-client/src/client/mod.rs b/nym-client/src/client/mod.rs index 4d4e0da408..8bb090b95f 100644 --- a/nym-client/src/client/mod.rs +++ b/nym-client/src/client/mod.rs @@ -3,14 +3,13 @@ use crate::client::received_buffer::ReceivedMessagesBuffer; use crate::client::topology_control::TopologyInnerRef; use crate::sockets::tcp; use crate::sockets::ws; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; use directory_client::presence::Topology; use futures::channel::mpsc; use futures::join; use log::*; use sfw_provider_requests::AuthToken; use sphinx::route::Destination; -use std::marker::PhantomData; use std::net::SocketAddr; use tokio::runtime::Runtime; use topology::NymTopology; @@ -37,11 +36,9 @@ pub enum SocketType { None, } -pub struct NymClient +pub struct NymClient where - IDPair: MixnetIdentityKeyPair + Send + Sync, - Priv: MixnetIdentityPrivateKey + Send + Sync, - Pub: MixnetIdentityPublicKey + Send + Sync, + IDPair: MixnetIdentityKeyPair + Send + Sync, { keypair: IDPair, @@ -53,19 +50,14 @@ where directory: String, auth_token: Option, socket_type: SocketType, - - _phantom_private: PhantomData, - _phantom_public: PhantomData, } #[derive(Debug)] pub struct InputMessage(pub Destination, pub Vec); -impl NymClient +impl NymClient where - IDPair: MixnetIdentityKeyPair + Send + Sync, - Priv: MixnetIdentityPrivateKey + Send + Sync, - Pub: MixnetIdentityPublicKey + Send + Sync, + IDPair: 'static + MixnetIdentityKeyPair + Send + Sync, { pub fn new( keypair: IDPair, @@ -84,8 +76,6 @@ where directory, auth_token, socket_type, - _phantom_private: PhantomData, - _phantom_public: PhantomData, } } @@ -129,7 +119,7 @@ where // TODO: when we switch to our graph topology, we need to remember to change 'Topology' type let topology_controller = - rt.block_on(topology_control::TopologyControl::::new( + rt.block_on(topology_control::TopologyControl::::new( self.directory.clone(), TOPOLOGY_REFRESH_RATE, healthcheck_keys, diff --git a/nym-client/src/client/topology_control.rs b/nym-client/src/client/topology_control.rs index 9833f81166..5ec9422782 100644 --- a/nym-client/src/client/topology_control.rs +++ b/nym-client/src/client/topology_control.rs @@ -1,5 +1,5 @@ use crate::built_info; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::MixnetIdentityKeyPair; use healthcheck::HealthChecker; use log::{error, info, trace, warn}; use std::sync::Arc; @@ -12,16 +12,14 @@ const NODE_HEALTH_THRESHOLD: f64 = 0.0; // auxiliary type for ease of use pub type TopologyInnerRef = Arc>>; -pub(crate) struct TopologyControl +pub(crate) struct TopologyControl where T: NymTopology, - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, + IDPair: MixnetIdentityKeyPair, { directory_server: String, inner: Arc>>, - health_checker: HealthChecker, + health_checker: HealthChecker, refresh_rate: f64, } @@ -31,12 +29,10 @@ enum TopologyError { NoValidPathsError, } -impl TopologyControl +impl TopologyControl where T: NymTopology, - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, + IDPair: MixnetIdentityKeyPair, { pub(crate) async fn new( directory_server: String,