diff --git a/common/client-core/gateways-storage/Cargo.toml b/common/client-core/gateways-storage/Cargo.toml index 3968df079e..567744d1b6 100644 --- a/common/client-core/gateways-storage/Cargo.toml +++ b/common/client-core/gateways-storage/Cargo.toml @@ -21,7 +21,7 @@ nym-gateway-requests = { path = "../../../gateway/gateway-requests" } [target."cfg(not(target_arch = \"wasm32\"))".dependencies.sqlx] workspace = true -features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate"] +features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate", "time"] optional = true [build-dependencies] diff --git a/common/client-core/gateways-storage/src/backend/fs_backend/manager.rs b/common/client-core/gateways-storage/src/backend/fs_backend/manager.rs index 72d886bce6..965ace23f6 100644 --- a/common/client-core/gateways-storage/src/backend/fs_backend/manager.rs +++ b/common/client-core/gateways-storage/src/backend/fs_backend/manager.rs @@ -54,87 +54,171 @@ impl StorageManager { } pub(crate) async fn get_active_gateway(&self) -> Result { - todo!() + sqlx::query_as!( + RawActiveGateway, + "SELECT active_gateway_id_bs58 FROM active_gateway" + ) + .fetch_one(&self.connection_pool) + .await } pub(crate) async fn set_active_gateway( &self, gateway_id: Option<&str>, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + "UPDATE active_gateway SET active_gateway_id_bs58 = ?", + gateway_id + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn maybe_get_registered_gateway( &self, gateway_id: &str, ) -> Result, sqlx::Error> { - todo!() + sqlx::query_as("SELECT * FROM registered_gateway WHERE gateway_id_bs58 = ?") + .bind(gateway_id) + .fetch_optional(&self.connection_pool) + .await } pub(crate) async fn must_get_registered_gateway( &self, gateway_id: &str, ) -> Result { - todo!() + sqlx::query_as("SELECT * FROM registered_gateway WHERE gateway_id_bs58 = ?") + .bind(gateway_id) + .fetch_one(&self.connection_pool) + .await } pub(crate) async fn set_registered_gateway( &self, registered_gateway: &RawRegisteredGateway, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + r#" + INSERT INTO registered_gateway(gateway_id_bs58, registration_timestamp, gateway_type) + VALUES (?, ?, ?) + "#, + registered_gateway.gateway_id_bs58, + registered_gateway.registration_timestamp, + registered_gateway.gateway_type, + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn remove_registered_gateway( &self, gateway_id: &str, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + "DELETE FROM registered_gateway WHERE gateway_id_bs58 = ?", + gateway_id + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn get_remote_gateway_details( &self, gateway_id: &str, ) -> Result { - todo!() + sqlx::query_as!( + RawRemoteGatewayDetails, + "SELECT * FROM remote_gateway_details WHERE gateway_id_bs58 = ?", + gateway_id + ) + .fetch_one(&self.connection_pool) + .await } pub(crate) async fn set_remote_gateway_details( &self, remote: &RawRemoteGatewayDetails, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + r#" + INSERT INTO remote_gateway_details(gateway_id_bs58, derived_aes128_ctr_blake3_hmac_keys_bs58, gateway_owner_address, gateway_listener, wg_tun_address) + VALUES (?, ?, ?, ?, ?) + "#, + remote.gateway_id_bs58, + remote.derived_aes128_ctr_blake3_hmac_keys_bs58, + remote.gateway_owner_address, + remote.gateway_listener, + remote.wg_tun_address, + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn remove_remote_gateway_details( &self, gateway_id: &str, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + "DELETE FROM remote_gateway_details WHERE gateway_id_bs58 = ?", + gateway_id + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn get_custom_gateway_details( &self, gateway_id: &str, ) -> Result { - todo!() + sqlx::query_as!( + RawCustomGatewayDetails, + "SELECT * FROM custom_gateway_details WHERE gateway_id_bs58 = ?", + gateway_id + ) + .fetch_one(&self.connection_pool) + .await } pub(crate) async fn set_custom_gateway_details( &self, custom: &RawCustomGatewayDetails, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + r#" + INSERT INTO custom_gateway_details(gateway_id_bs58, data) + VALUES (?, ?) + "#, + custom.gateway_id_bs58, + custom.data, + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn remove_custom_gateway_details( &self, gateway_id: &str, ) -> Result<(), sqlx::Error> { - todo!() + sqlx::query!( + "DELETE FROM custom_gateway_details WHERE gateway_id_bs58 = ?", + gateway_id + ) + .execute(&self.connection_pool) + .await?; + Ok(()) } pub(crate) async fn registered_gateways(&self) -> Result, sqlx::Error> { - todo!() + sqlx::query!("SELECT gateway_id_bs58 FROM registered_gateway") + .fetch_all(&self.connection_pool) + .await + .map(|records| records.into_iter().map(|r| r.gateway_id_bs58).collect()) } } diff --git a/common/client-core/src/client/base_client/storage/migration_helpers.rs b/common/client-core/src/client/base_client/storage/migration_helpers.rs index 83869b5796..87738faef4 100644 --- a/common/client-core/src/client/base_client/storage/migration_helpers.rs +++ b/common/client-core/src/client/base_client/storage/migration_helpers.rs @@ -203,7 +203,7 @@ pub mod v1_1_33 { ) .await?; - remove_old_gateway_details(&old_storage_paths).map_err(|err| { + remove_old_gateway_details(old_storage_paths).map_err(|err| { ClientCoreError::UpgradeFailure { message: format!("failed to remove old data: {err}"), } diff --git a/nym-connect/desktop/Cargo.lock b/nym-connect/desktop/Cargo.lock index 86afcd16e4..afbc2f403f 100644 --- a/nym-connect/desktop/Cargo.lock +++ b/nym-connect/desktop/Cargo.lock @@ -6618,6 +6618,7 @@ dependencies = [ "sqlx-rt", "stringprep", "thiserror", + "time", "tokio-stream", "url", "webpki-roots",