From 9d12079eb64ac803a323cba589f2a4db94804a1f Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Thu, 26 Sep 2024 13:36:13 +0100 Subject: [PATCH] move walletconnect rpc to mm2_main rpc --- mm2src/kdf_walletconnect/src/lib.rs | 5 +-- mm2src/kdf_walletconnect/src/session.rs | 4 +- .../kdf_walletconnect/src/session/delete.rs | 2 +- mm2src/kdf_walletconnect/src/session/ping.rs | 5 +-- mm2src/mm2_main/src/rpc.rs | 1 + .../mm2_main/src/rpc/dispatcher/dispatcher.rs | 5 +-- .../src/rpc/wc_commands/delete_connection.rs | 30 +++++++++++++++ .../src/rpc/wc_commands/get_chain_id.rs | 20 ++++++++++ .../src/rpc/wc_commands/get_session.rs | 20 ++++++++++ .../src/rpc/wc_commands/new_connection.rs | 26 +++++++++++++ mm2src/mm2_main/src/rpc/wc_commands/ping.rs | 27 ++++++++++++++ .../src/rpc/wc_commands/wc_commands.rs | 37 +++++++++++++++++++ 12 files changed, 168 insertions(+), 14 deletions(-) create mode 100644 mm2src/mm2_main/src/rpc/wc_commands/delete_connection.rs create mode 100644 mm2src/mm2_main/src/rpc/wc_commands/get_chain_id.rs create mode 100644 mm2src/mm2_main/src/rpc/wc_commands/get_session.rs create mode 100644 mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs create mode 100644 mm2src/mm2_main/src/rpc/wc_commands/ping.rs create mode 100644 mm2src/mm2_main/src/rpc/wc_commands/wc_commands.rs diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 8f1d07700d..f13d053cdf 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -1,12 +1,11 @@ pub mod chain; -#[allow(unused)] mod error; +#[allow(unused)] pub mod error; mod handler; mod inbound_message; mod metadata; #[allow(unused)] mod pairing; pub mod rpc_commands; - -mod session; +pub mod session; mod storage; use chain::{build_required_namespaces, diff --git a/mm2src/kdf_walletconnect/src/session.rs b/mm2src/kdf_walletconnect/src/session.rs index beeb14053f..f9683fe33b 100644 --- a/mm2src/kdf_walletconnect/src/session.rs +++ b/mm2src/kdf_walletconnect/src/session.rs @@ -1,7 +1,7 @@ -pub(crate) mod delete; +pub mod delete; pub(crate) mod event; pub(crate) mod extend; -pub(crate) mod ping; +pub mod ping; pub(crate) mod propose; pub(crate) mod settle; pub(crate) mod update; diff --git a/mm2src/kdf_walletconnect/src/session/delete.rs b/mm2src/kdf_walletconnect/src/session/delete.rs index d4baf5f180..7fcd944833 100644 --- a/mm2src/kdf_walletconnect/src/session/delete.rs +++ b/mm2src/kdf_walletconnect/src/session/delete.rs @@ -18,7 +18,7 @@ pub(crate) async fn reply_session_delete_request( session_delete_cleanup(ctx, topic).await } -pub(crate) async fn send_session_delete_request( +pub async fn send_session_delete_request( ctx: &WalletConnectCtx, session_topic: &Topic, ) -> MmResult<(), WalletConnectCtxError> { diff --git a/mm2src/kdf_walletconnect/src/session/ping.rs b/mm2src/kdf_walletconnect/src/session/ping.rs index 87ebdba0c8..55ff2af13b 100644 --- a/mm2src/kdf_walletconnect/src/session/ping.rs +++ b/mm2src/kdf_walletconnect/src/session/ping.rs @@ -15,10 +15,7 @@ pub(crate) async fn reply_session_ping_request( Ok(()) } -pub(crate) async fn send_session_ping_request( - ctx: &WalletConnectCtx, - topic: &Topic, -) -> MmResult<(), WalletConnectCtxError> { +pub async fn send_session_ping_request(ctx: &WalletConnectCtx, topic: &Topic) -> MmResult<(), WalletConnectCtxError> { let param = RequestParams::SessionPing(()); ctx.publish_request(topic, param).await?; diff --git a/mm2src/mm2_main/src/rpc.rs b/mm2src/mm2_main/src/rpc.rs index 389f81a8b8..97e7a0d07a 100644 --- a/mm2src/mm2_main/src/rpc.rs +++ b/mm2src/mm2_main/src/rpc.rs @@ -50,6 +50,7 @@ mod dispatcher_legacy; #[path = "rpc/lp_commands/lp_commands_legacy.rs"] pub mod lp_commands_legacy; mod rate_limiter; +#[path = "rpc/wc_commands/wc_commands.rs"] pub mod wc_commands; /// Lists the RPC method not requiring the "userpass" authentication. /// None is also public to skip auth and display proper error in case of method is missing diff --git a/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs b/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs index c8abf7950b..a9f5036086 100644 --- a/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs +++ b/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs @@ -7,6 +7,7 @@ use crate::lp_ordermatch::{best_orders_rpc_v2, orderbook_rpc_v2, start_simple_ma use crate::lp_swap::swap_v2_rpcs::{active_swaps_rpc, my_recent_swaps_rpc, my_swap_status_rpc}; use crate::lp_wallet::get_mnemonic_rpc; use crate::rpc::rate_limiter::{process_rate_limit, RateLimitContext}; +use crate::rpc::wc_commands::{delete_connection, get_chain_id, get_session, new_connection, ping_session}; use crate::{lp_stats::{add_node_to_version_stat, remove_node_from_version_stat, start_version_stat_collection, stop_version_stat_collection, update_version_stat_collection}, lp_swap::{get_locked_amount_rpc, max_maker_vol, recreate_swap_data, trade_preimage_rpc}, @@ -54,7 +55,6 @@ use common::log::{error, warn}; use common::HttpStatusCode; use futures::Future as Future03; use http::Response; -use kdf_walletconnect::rpc_commands::{delete_connection, get_chain_id, get_session, new_connection, ping_session}; use mm2_core::data_asker::send_asked_data_rpc; use mm2_core::mm_ctx::MmArc; use mm2_err_handle::prelude::*; @@ -161,9 +161,6 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult handle_mmrpc(ctx, request, account_balance).await, "active_swaps" => handle_mmrpc(ctx, request, active_swaps_rpc).await, diff --git a/mm2src/mm2_main/src/rpc/wc_commands/delete_connection.rs b/mm2src/mm2_main/src/rpc/wc_commands/delete_connection.rs new file mode 100644 index 0000000000..07118b6c4c --- /dev/null +++ b/mm2src/mm2_main/src/rpc/wc_commands/delete_connection.rs @@ -0,0 +1,30 @@ +use kdf_walletconnect::{session::delete::send_session_delete_request, WalletConnectCtx}; +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::*; +use serde::{Deserialize, Serialize}; + +use super::WalletConnectRpcError; + +#[derive(Debug, PartialEq, Serialize)] +pub struct DeleteConnectionResponse { + pub successful: bool, +} + +#[derive(Deserialize)] +pub struct DeleteConnectionRequest { + topic: String, +} + +/// `delete connection` RPC command implementation. +pub async fn delete_connection( + ctx: MmArc, + req: DeleteConnectionRequest, +) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) + .mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; + send_session_delete_request(&ctx, &req.topic.into()) + .await + .mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?; + + Ok(DeleteConnectionResponse { successful: true }) +} diff --git a/mm2src/mm2_main/src/rpc/wc_commands/get_chain_id.rs b/mm2src/mm2_main/src/rpc/wc_commands/get_chain_id.rs new file mode 100644 index 0000000000..4243167278 --- /dev/null +++ b/mm2src/mm2_main/src/rpc/wc_commands/get_chain_id.rs @@ -0,0 +1,20 @@ +use kdf_walletconnect::WalletConnectCtx; +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::*; +use serde::Serialize; + +use super::{EmptyRpcRequst, WalletConnectRpcError}; + +#[derive(Debug, PartialEq, Serialize)] +pub struct GetChainIdResponse { + pub chain_id: String, +} + +/// `delete connection` RPC command implementation. +pub async fn get_chain_id(ctx: MmArc, _req: EmptyRpcRequst) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) + .mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; + let chain_id = ctx.get_active_chain_id().await; + + Ok(GetChainIdResponse { chain_id }) +} diff --git a/mm2src/mm2_main/src/rpc/wc_commands/get_session.rs b/mm2src/mm2_main/src/rpc/wc_commands/get_session.rs new file mode 100644 index 0000000000..d5a34644ff --- /dev/null +++ b/mm2src/mm2_main/src/rpc/wc_commands/get_session.rs @@ -0,0 +1,20 @@ +use kdf_walletconnect::{session::Session, WalletConnectCtx}; +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::*; +use serde::Serialize; + +use super::{EmptyRpcRequst, WalletConnectRpcError}; + +#[derive(Debug, PartialEq, Serialize)] +pub struct GetSessionResponse { + pub session: Option, +} + +/// `delete connection` RPC command implementation. +pub async fn get_session(ctx: MmArc, _req: EmptyRpcRequst) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) + .mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; + let session = ctx.get_session().await; + + Ok(GetSessionResponse { session }) +} diff --git a/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs b/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs new file mode 100644 index 0000000000..46b58d5988 --- /dev/null +++ b/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs @@ -0,0 +1,26 @@ +use kdf_walletconnect::WalletConnectCtx; +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::*; +use serde::Serialize; + +use super::{EmptyRpcRequst, WalletConnectRpcError}; + +#[derive(Debug, PartialEq, Serialize)] +pub struct CreateConnectionResponse { + pub url: String, +} + +/// `new_connection` RPC command implementation. +pub async fn new_connection( + ctx: MmArc, + _req: EmptyRpcRequst, +) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) + .mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; + let url = ctx + .new_connection(None) + .await + .mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?; + + Ok(CreateConnectionResponse { url }) +} diff --git a/mm2src/mm2_main/src/rpc/wc_commands/ping.rs b/mm2src/mm2_main/src/rpc/wc_commands/ping.rs new file mode 100644 index 0000000000..a49e5d7d48 --- /dev/null +++ b/mm2src/mm2_main/src/rpc/wc_commands/ping.rs @@ -0,0 +1,27 @@ +use kdf_walletconnect::{session::ping::send_session_ping_request, WalletConnectCtx}; +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::*; +use serde::{Deserialize, Serialize}; + +use super::WalletConnectRpcError; + +#[derive(Debug, PartialEq, Serialize)] +pub struct SessionPingResponse { + pub successful: bool, +} + +#[derive(Deserialize)] +pub struct SessionPingRequest { + topic: String, +} + +/// `ping session` RPC command implementation. +pub async fn ping_session(ctx: MmArc, req: SessionPingRequest) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) + .mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; + send_session_ping_request(&ctx, &req.topic.into()) + .await + .mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?; + + Ok(SessionPingResponse { successful: true }) +} diff --git a/mm2src/mm2_main/src/rpc/wc_commands/wc_commands.rs b/mm2src/mm2_main/src/rpc/wc_commands/wc_commands.rs new file mode 100644 index 0000000000..ec63b4a60b --- /dev/null +++ b/mm2src/mm2_main/src/rpc/wc_commands/wc_commands.rs @@ -0,0 +1,37 @@ +mod delete_connection; +mod get_chain_id; +mod get_session; +mod new_connection; +mod ping; + +use common::HttpStatusCode; +pub use delete_connection::delete_connection; +use derive_more::Display; +pub use get_chain_id::get_chain_id; +pub use get_session::get_session; +use http::StatusCode; +pub use new_connection::new_connection; +pub use ping::ping_session; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct EmptyRpcRequst {} + +#[derive(Serialize, Display, SerializeErrorType)] +#[serde(tag = "error_type", content = "error_data")] +pub enum WalletConnectRpcError { + InternalError(String), + InitializationError(String), + SessionRequestError(String), +} + +impl HttpStatusCode for WalletConnectRpcError { + fn status_code(&self) -> StatusCode { + match self { + WalletConnectRpcError::InitializationError(_) => StatusCode::BAD_REQUEST, + WalletConnectRpcError::SessionRequestError(_) | WalletConnectRpcError::InternalError(_) => { + StatusCode::INTERNAL_SERVER_ERROR + }, + } + } +}