From 14b974d2e1e42f05a30346ce65bc9752e8ff0bf1 Mon Sep 17 00:00:00 2001 From: Simon Wicky Date: Mon, 6 Nov 2023 10:47:15 +0100 Subject: [PATCH] add ecash key to api --- nym-api/src/network_monitor/mod.rs | 19 ++++++++++++++++++- nym-api/src/support/config/helpers.rs | 3 +++ .../src/support/config/old_config_v1_1_21.rs | 2 ++ .../src/support/config/old_config_v1_1_27.rs | 4 ++++ nym-api/src/support/config/persistence.rs | 7 +++++++ nym-api/src/support/config/template.rs | 6 ++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/nym-api/src/network_monitor/mod.rs b/nym-api/src/network_monitor/mod.rs index 59797ab2ed..9817c6823b 100644 --- a/nym-api/src/network_monitor/mod.rs +++ b/nym-api/src/network_monitor/mod.rs @@ -15,6 +15,7 @@ use crate::network_monitor::monitor::Monitor; use crate::nym_contract_cache::cache::NymContractCache; use crate::storage::NymApiStorage; use crate::support::{config, nyxd}; +use anyhow::Result; use futures::channel::mpsc; use nym_bandwidth_controller::BandwidthController; use nym_compact_ecash::generate_keypair_user; @@ -34,6 +35,18 @@ pub(crate) mod test_route; pub(crate) const ROUTE_TESTING_TEST_NONCE: u64 = 0; +pub(crate) fn init_ecash_keypair(config: &config::NetworkMonitor) -> Result<()> { + let kp = generate_keypair_user(&GroupParameters::new().unwrap()); + nym_pemstore::store_keypair( + &kp, + &nym_pemstore::KeyPairPath::new( + &config.storage_paths.ecash_private_key_path, + &config.storage_paths.ecash_public_key_path, + ), + )?; + Ok(()) +} + pub(crate) fn setup<'a>( config: &'a config::NetworkMonitor, nym_contract_cache_state: &NymContractCache, @@ -81,6 +94,10 @@ impl<'a> NetworkMonitorBuilder<'a> { let identity_keypair = Arc::new(identity::KeyPair::new(&mut rng)); let encryption_keypair = Arc::new(encryption::KeyPair::new(&mut rng)); let ecash_keypair = generate_keypair_user(&GroupParameters::new().unwrap()); + // let ecash_keypair = nym_pemstore::load_keypair(&KeyPairPath::new( + // self.config.storage_paths.ecash_private_key_path, + // self.config.storage_paths.ecash_public_key_path, + // )); //SW todo : load ecash key let ack_key = Arc::new(AckKey::new(&mut rng)); let ecash_parameters = EcashParameters::new().ecash_params().clone(); @@ -106,7 +123,7 @@ impl<'a> NetworkMonitorBuilder<'a> { self.nyxd_client.clone(), ecash_keypair, ecash_parameters, - ) //SW fill in those todo + ) }; let packet_sender = new_packet_sender( diff --git a/nym-api/src/support/config/helpers.rs b/nym-api/src/support/config/helpers.rs index e04c152c20..b79c40b466 100644 --- a/nym-api/src/support/config/helpers.rs +++ b/nym-api/src/support/config/helpers.rs @@ -20,6 +20,7 @@ fn try_upgrade_v1_1_21_config(id: &str) -> Result<()> { info!("It is going to get updated to the current specification."); let updated: Config = old_config.into(); + crate::network_monitor::init_ecash_keypair(&updated.network_monitor)?; Ok(updated.save_to_default_location()?) } @@ -36,6 +37,7 @@ fn try_upgrade_v1_1_27_config(id: &str) -> Result<()> { info!("It is going to get updated to the current specification."); let updated: Config = old_config.into(); + crate::network_monitor::init_ecash_keypair(&updated.network_monitor)?; Ok(updated.save_to_default_location()?) } @@ -48,6 +50,7 @@ pub(crate) fn initialise_new(id: &str) -> Result { let config = Config::new(id); init_paths(id)?; crate::coconut::dkg::controller::init_keypair(&config.coconut_signer)?; + crate::network_monitor::init_ecash_keypair(&config.network_monitor)?; Ok(config) } diff --git a/nym-api/src/support/config/old_config_v1_1_21.rs b/nym-api/src/support/config/old_config_v1_1_21.rs index 3eee079a53..7ccb903b55 100644 --- a/nym-api/src/support/config/old_config_v1_1_21.rs +++ b/nym-api/src/support/config/old_config_v1_1_21.rs @@ -95,6 +95,8 @@ impl From for Config { enabled: value.network_monitor.enabled, storage_paths: NetworkMonitorPaths { credentials_database_path: value.network_monitor.credentials_database_path, + ecash_public_key_path: Default::default(), + ecash_private_key_path: Default::default(), }, debug: NetworkMonitorDebug { min_mixnode_reliability: value.network_monitor.min_mixnode_reliability, diff --git a/nym-api/src/support/config/old_config_v1_1_27.rs b/nym-api/src/support/config/old_config_v1_1_27.rs index 3243147bc4..f92ec600cd 100644 --- a/nym-api/src/support/config/old_config_v1_1_27.rs +++ b/nym-api/src/support/config/old_config_v1_1_27.rs @@ -68,6 +68,8 @@ impl From for Config { .network_monitor .storage_paths .credentials_database_path, + ecash_public_key_path: Default::default(), + ecash_private_key_path: Default::default(), }, debug: NetworkMonitorDebug { min_mixnode_reliability: value.network_monitor.debug.min_mixnode_reliability, @@ -204,6 +206,8 @@ impl Default for NetworkMonitorV1_1_27 { enabled: false, storage_paths: NetworkMonitorPaths { credentials_database_path: Default::default(), + ecash_public_key_path: Default::default(), + ecash_private_key_path: Default::default(), }, debug: Default::default(), } diff --git a/nym-api/src/support/config/persistence.rs b/nym-api/src/support/config/persistence.rs index 7d4e58190a..ef7178438c 100644 --- a/nym-api/src/support/config/persistence.rs +++ b/nym-api/src/support/config/persistence.rs @@ -6,6 +6,8 @@ use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf}; pub const DEFAULT_NETWORK_MONITOR_CREDENTIALS_DATABASE_FILENAME: &str = "credentials_database.db"; +pub const DEFAULT_NETWORK_MONITOR_ECASH_PRIVATE_KEY_FILENAME: &str = "private_ecash.pem"; +pub const DEFAULT_NETWORK_MONITOR_ECASH_PUBLIC_KEY_FILENAME: &str = "public_ecash.pem"; pub const DEFAULT_NODE_STATUS_API_DATABASE_FILENAME: &str = "db.sqlite"; @@ -39,6 +41,8 @@ pub struct NetworkMonitorPaths { // TODO: this should contain the path to the database holding the results, but changing it would break backwards compatibility /// Path to the database containing bandwidth credentials of this client. pub credentials_database_path: PathBuf, + pub ecash_public_key_path: PathBuf, + pub ecash_private_key_path: PathBuf, } impl NetworkMonitorPaths { @@ -48,6 +52,9 @@ impl NetworkMonitorPaths { NetworkMonitorPaths { credentials_database_path: data_dir .join(DEFAULT_NETWORK_MONITOR_CREDENTIALS_DATABASE_FILENAME), + ecash_public_key_path: data_dir.join(DEFAULT_NETWORK_MONITOR_ECASH_PUBLIC_KEY_FILENAME), + ecash_private_key_path: data_dir + .join(DEFAULT_NETWORK_MONITOR_ECASH_PRIVATE_KEY_FILENAME), } } } diff --git a/nym-api/src/support/config/template.rs b/nym-api/src/support/config/template.rs index 88b0fb85d7..778cdce7f5 100644 --- a/nym-api/src/support/config/template.rs +++ b/nym-api/src/support/config/template.rs @@ -36,6 +36,12 @@ enabled = {{ network_monitor.enabled }} # Path to the database containing bandwidth credentials of this client. credentials_database_path = '{{ network_monitor.storage_paths.credentials_database_path }}' +# Path to the ecash public key +ecash_public_key_path = '{{ network_monitor.storage_paths.ecash_public_key_path }}' + +# Path to the ecash private key +ecash_private_key_path = '{{ network_monitor.storage_paths.ecash_private_key_path }}' + [network_monitor.debug] # Indicates whether this validator api is running in a disabled credentials mode, thus attempting