From 3a85a174e87117a3a78c6c0624dd92fbdb47f05b Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Mon, 23 Sep 2024 12:36:50 +0100 Subject: [PATCH] start persistent session storage implementation --- Cargo.lock | 1 + mm2src/kdf_walletconnect/Cargo.toml | 1 + mm2src/kdf_walletconnect/src/lib.rs | 1 + mm2src/kdf_walletconnect/src/storage/mod.rs | 26 +++++++++++++++++++ .../kdf_walletconnect/src/storage/sqlite.rs | 10 +++++++ mm2src/kdf_walletconnect/src/storage/wasm.rs | 0 6 files changed, 39 insertions(+) create mode 100644 mm2src/kdf_walletconnect/src/storage/mod.rs create mode 100644 mm2src/kdf_walletconnect/src/storage/sqlite.rs create mode 100644 mm2src/kdf_walletconnect/src/storage/wasm.rs diff --git a/Cargo.lock b/Cargo.lock index 4736c261e7..70b45b19cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3555,6 +3555,7 @@ dependencies = [ "base64 0.21.7", "chrono", "common", + "db_common", "derive_more", "enum_derives", "futures 0.3.28", diff --git a/mm2src/kdf_walletconnect/Cargo.toml b/mm2src/kdf_walletconnect/Cargo.toml index b8f5bd9a29..3c15f941e0 100644 --- a/mm2src/kdf_walletconnect/Cargo.toml +++ b/mm2src/kdf_walletconnect/Cargo.toml @@ -8,6 +8,7 @@ async-trait = "0.1.52" base64 = "0.21.2" chrono = { version = "0.4.23", "features" = ["serde"] } common = { path = "../common" } +db_common = { path = "../db_common" } derive_more = "0.99" enum_derives = { path = "../derives/enum_derives" } futures = { version = "0.3", package = "futures", features = [ diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 0ddb8900ea..5984bee4b0 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -5,6 +5,7 @@ mod inbound_message; mod metadata; #[allow(unused)] mod pairing; mod session; +mod storage; use chain::{build_required_namespaces, cosmos::{cosmos_get_accounts_impl, CosmosAccount}, diff --git a/mm2src/kdf_walletconnect/src/storage/mod.rs b/mm2src/kdf_walletconnect/src/storage/mod.rs new file mode 100644 index 0000000000..bf4c603af2 --- /dev/null +++ b/mm2src/kdf_walletconnect/src/storage/mod.rs @@ -0,0 +1,26 @@ +use async_trait::async_trait; +use mm2_err_handle::prelude::MmResult; +use relay_rpc::{domain::Topic, rpc::params::session::SettleNamespaces}; + +use crate::session::Session; + +pub(crate) mod sqlite; +pub(crate) mod wasm; + +pub(crate) const SESSION_STORAGE_TABLE_NAME: &str = "kdf_wc_session_storage"; + +#[derive(Debug, thiserror::Error)] +pub(crate) enum WalletConnectStorageError { + #[error("Table Error: {0}")] + TableError(String), +} + +#[async_trait] +pub(crate) trait WalletConnectStorageOps { + fn init(&self) -> MmResult<(), WalletConnectStorageError>; + fn save_session(&self, session: &Session) -> MmResult<(), WalletConnectStorageError>; + fn get_all_sessions(&self) -> MmResult, WalletConnectStorageError>; + fn delete_session(&self, topic: &Topic) -> MmResult<(), WalletConnectStorageError>; + fn update_namespace(&self, topic: &Topic, namespace: SettleNamespaces) -> MmResult<(), WalletConnectStorageError>; + fn update_expiry(&self, expiry: u64) -> MmResult<(), WalletConnectStorageError>; +} diff --git a/mm2src/kdf_walletconnect/src/storage/sqlite.rs b/mm2src/kdf_walletconnect/src/storage/sqlite.rs new file mode 100644 index 0000000000..fd0951fd69 --- /dev/null +++ b/mm2src/kdf_walletconnect/src/storage/sqlite.rs @@ -0,0 +1,10 @@ +use db_common::sqlite::validate_table_name; +use mm2_err_handle::prelude::MmResult; + +use super::{WalletConnectStorageError, SESSION_STORAGE_TABLE_NAME}; + +fn validate_sql_table_name() -> MmResult { + validate_table_name(SESSION_STORAGE_TABLE_NAME) + .map_err(|err| WalletConnectStorageError::TableError(err.to_string()))?; + Ok(SESSION_STORAGE_TABLE_NAME.to_owned()) +} diff --git a/mm2src/kdf_walletconnect/src/storage/wasm.rs b/mm2src/kdf_walletconnect/src/storage/wasm.rs new file mode 100644 index 0000000000..e69de29bb2