From 23c13a409af4ac4e97a4faa0d40007b0350ab710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Thu, 5 Jan 2023 16:56:09 +0100 Subject: [PATCH] rust-sdk: rename key_paths to paths --- sdk/rust/nym-sdk/examples/complex_config.rs | 2 +- sdk/rust/nym-sdk/src/mixnet.rs | 4 +- sdk/rust/nym-sdk/src/mixnet/client.rs | 28 +++++------ .../src/mixnet/{key_paths.rs => paths.rs} | 50 +++++-------------- 4 files changed, 30 insertions(+), 54 deletions(-) rename sdk/rust/nym-sdk/src/mixnet/{key_paths.rs => paths.rs} (81%) diff --git a/sdk/rust/nym-sdk/examples/complex_config.rs b/sdk/rust/nym-sdk/examples/complex_config.rs index 2251174e6d..4883ead34f 100644 --- a/sdk/rust/nym-sdk/examples/complex_config.rs +++ b/sdk/rust/nym-sdk/examples/complex_config.rs @@ -11,7 +11,7 @@ async fn main() { // Setting `KeyMode::Keep` will use existing keys, and existing config, if there is one. // Regardles of `user_chosen_gateway`. - let keys = mixnet::KeyPaths::new_from_dir(mixnet::KeyMode::Keep, &config_dir); + let keys = mixnet::StoragePaths::new_from_dir(mixnet::KeyMode::Keep, &config_dir); // Provide key paths for the client to read/write keys to. let mut client = mixnet::Client::new(None, Some(keys)).unwrap(); diff --git a/sdk/rust/nym-sdk/src/mixnet.rs b/sdk/rust/nym-sdk/src/mixnet.rs index fc190053c9..50972de756 100644 --- a/sdk/rust/nym-sdk/src/mixnet.rs +++ b/sdk/rust/nym-sdk/src/mixnet.rs @@ -1,7 +1,7 @@ mod client; mod config; mod connection_state; -mod key_paths; +mod paths; pub use client_core::config::GatewayEndpointConfig; pub use nymsphinx::{ @@ -9,7 +9,7 @@ pub use nymsphinx::{ receiver::ReconstructedMessage, }; -pub use key_paths::{GatewayKeyMode, KeyMode, KeyPaths, Keys}; +pub use paths::{GatewayKeyMode, KeyMode, Keys, StoragePaths}; pub use client::Client; pub use config::Config; diff --git a/sdk/rust/nym-sdk/src/mixnet/client.rs b/sdk/rust/nym-sdk/src/mixnet/client.rs index 5565c6df88..2d0ed126e7 100644 --- a/sdk/rust/nym-sdk/src/mixnet/client.rs +++ b/sdk/rust/nym-sdk/src/mixnet/client.rs @@ -19,7 +19,7 @@ use client_core::{ use crate::error::{Error, Result}; -use super::{connection_state::ConnectionState, Config, GatewayKeyMode, KeyPaths, Keys}; +use super::{connection_state::ConnectionState, Config, GatewayKeyMode, StoragePaths, Keys}; pub struct Client { /// Keys handled by the client @@ -29,7 +29,7 @@ pub struct Client { config: Config, /// Paths for client keys, including identity, encryption, ack and shared gateway keys. - key_paths: Option, + storage_paths: Option, /// The client can be in one of multiple states, depending on how it is created and if it's /// connected to the mixnet. @@ -42,20 +42,20 @@ impl Client { /// /// Callers have the option of supplying futher parameters to store persistent identities at a /// location on-disk, if desired. - pub fn new(config_option: Option, key_paths: Option) -> Result { + pub fn new(config_option: Option, paths: Option) -> Result { let config = config_option.unwrap_or_default(); // If we are provided paths to keys, use them if they are available. And if they are // not, write the generated keys back to storage. - let key_manager = if let Some(ref key_paths) = key_paths { - let path_finder = ClientKeyPathfinder::from(key_paths.clone()); + let key_manager = if let Some(ref paths) = paths { + let path_finder = ClientKeyPathfinder::from(paths.clone()); // Try load keys match KeyManager::load_keys_maybe_gateway(&path_finder) { Ok(key_manager) => key_manager, Err(err) => { log::debug!("Not loading keys: {err}"); - if path_finder.any_file_exists() && key_paths.operating_mode.is_keep() { + if path_finder.any_file_exists() && paths.operating_mode.is_keep() { return Err(Error::DontOverwrite); } @@ -74,7 +74,7 @@ impl Client { Ok(Client { key_manager, config, - key_paths, + storage_paths: paths, connection_state: ConnectionState::New, }) } @@ -135,8 +135,8 @@ impl Client { Ok(()) } - fn write_gateway_key(&self, key_paths: KeyPaths, key_mode: &GatewayKeyMode) -> Result<()> { - let path_finder = ClientKeyPathfinder::from(key_paths); + fn write_gateway_key(&self, paths: StoragePaths, key_mode: &GatewayKeyMode) -> Result<()> { + let path_finder = ClientKeyPathfinder::from(paths); if path_finder.gateway_key_file_exists() && key_mode.is_keep() { return Err(Error::DontOverwriteGatewayKey); }; @@ -175,20 +175,20 @@ impl Client { // For some simple cases we can figure how to setup gateway without it having to have been // called in advance. if matches!(self.connection_state, ConnectionState::New) { - if let Some(key_paths) = &self.key_paths { - let key_paths = key_paths.clone(); + if let Some(paths) = &self.storage_paths { + let paths = paths.clone(); if self.has_gateway_key() { // If we have a gateway key from client, then we can just read the corresponding // config println!("Has gateway key: loading"); - self.read_gateway_endpoint_config(&key_paths.gateway_endpoint_config)?; + self.read_gateway_endpoint_config(&paths.gateway_endpoint_config)?; } else { // If we didn't find any shared gateway key during creation, that means we first // need to register a gateway println!("NO gateway key: registering new"); self.register_with_gateway().await?; - self.write_gateway_key(key_paths.clone(), &GatewayKeyMode::Overwrite)?; - Self::write_gateway_endpoint_config(&key_paths.gateway_endpoint_config)?; + self.write_gateway_key(paths.clone(), &GatewayKeyMode::Overwrite)?; + Self::write_gateway_endpoint_config(&paths.gateway_endpoint_config)?; } } else { // If we don't have any key paths, just use ephemeral keys diff --git a/sdk/rust/nym-sdk/src/mixnet/key_paths.rs b/sdk/rust/nym-sdk/src/mixnet/paths.rs similarity index 81% rename from sdk/rust/nym-sdk/src/mixnet/key_paths.rs rename to sdk/rust/nym-sdk/src/mixnet/paths.rs index 374f807c65..8f18c57b2f 100644 --- a/sdk/rust/nym-sdk/src/mixnet/key_paths.rs +++ b/sdk/rust/nym-sdk/src/mixnet/paths.rs @@ -35,7 +35,7 @@ impl GatewayKeyMode { } #[derive(Clone, Debug)] -pub struct KeyPaths { +pub struct StoragePaths { // Determines how to handle existing key files found. pub operating_mode: KeyMode, @@ -56,32 +56,14 @@ pub struct KeyPaths { // The key isn't much use without knowing which entity it refers to. pub gateway_endpoint_config: PathBuf, + // The database containing credentials pub credential_database_path: PathBuf, + // The database storing reply surbs in-between sessions pub reply_surb_database_path: PathBuf, } -pub struct Keys { - pub identity_keypair: identity::KeyPair, - pub encryption_keypair: encryption::KeyPair, - pub ack_key: AckKey, - pub gateway_shared_key: SharedKeys, -} - -//#[derive(Clone, Debug)] -//pub struct GatewaySetup { -// pub gateway_shared_key: PathBuf, -// pub gateway_endpoint_config: GatewayEndpointConfig, -// //pub gateway_endpoint_config: GatewayConfig, -//} - -//#[derive(Clone, Debug)] -//pub enum GatewayConfig { -// File(PathBuf), -// Struct(GatewayEndpointConfig), -//} - -impl KeyPaths { +impl StoragePaths { pub fn new_from_dir(operating_mode: KeyMode, dir: &Path) -> Self { assert!(!dir.is_file(), "WIP"); Self { @@ -101,8 +83,8 @@ impl KeyPaths { } } -impl From for ClientKeyPathfinder { - fn from(paths: KeyPaths) -> Self { +impl From for ClientKeyPathfinder { + fn from(paths: StoragePaths) -> Self { Self { identity_private_key: paths.private_identity, identity_public_key: paths.public_identity, @@ -114,6 +96,13 @@ impl From for ClientKeyPathfinder { } } +pub struct Keys { + pub identity_keypair: identity::KeyPair, + pub encryption_keypair: encryption::KeyPair, + pub ack_key: AckKey, + pub gateway_shared_key: SharedKeys, +} + impl From for KeyManager { fn from(keys: Keys) -> Self { KeyManager::new_from_keys( @@ -124,16 +113,3 @@ impl From for KeyManager { ) } } - -//pub struct GatewayConfigPath { -// pub path: PathBuf, -//} -// -//impl GatewayConfigPath { -// pub fn new_from_dir(dir: PathBuf) -> Self { -// assert!(!dir.is_file(), "WIP"); -// Self { -// path: dir.join("gateway_endpoint_config.toml"), -// } -// } -//}