From 5457d800415830dfddfb9b4b042607c6f4e62535 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Thu, 26 Sep 2024 12:40:38 +0100 Subject: [PATCH] save dev state - implement wc rpc commands --- mm2src/kdf_walletconnect/src/error.rs | 2 + .../kdf_walletconnect/src/inbound_message.rs | 36 ++++++------ mm2src/kdf_walletconnect/src/lib.rs | 13 ++--- mm2src/kdf_walletconnect/src/pairing.rs | 35 +----------- .../src/rpc_commands/delete_connection.rs | 27 +++++++++ .../src/rpc_commands/get_chain_id.rs | 20 +++++++ .../src/rpc_commands/get_session.rs | 19 +++++++ .../kdf_walletconnect/src/rpc_commands/mod.rs | 15 +++++ .../src/rpc_commands/new_connection.rs | 22 ++++++++ .../src/rpc_commands/ping.rs | 24 ++++++++ .../kdf_walletconnect/src/session/delete.rs | 24 ++++++-- mm2src/kdf_walletconnect/src/session/event.rs | 3 +- .../kdf_walletconnect/src/session/extend.rs | 2 +- mm2src/kdf_walletconnect/src/session/ping.rs | 14 ++++- .../kdf_walletconnect/src/session/propose.rs | 4 +- .../kdf_walletconnect/src/session/settle.rs | 2 +- .../kdf_walletconnect/src/session/update.rs | 2 +- .../mm2_main/src/rpc/dispatcher/dispatcher.rs | 12 +++- .../src/rpc/lp_commands/lp_commands.rs | 56 ------------------- 19 files changed, 202 insertions(+), 130 deletions(-) create mode 100644 mm2src/kdf_walletconnect/src/rpc_commands/delete_connection.rs create mode 100644 mm2src/kdf_walletconnect/src/rpc_commands/get_chain_id.rs create mode 100644 mm2src/kdf_walletconnect/src/rpc_commands/get_session.rs create mode 100644 mm2src/kdf_walletconnect/src/rpc_commands/mod.rs create mode 100644 mm2src/kdf_walletconnect/src/rpc_commands/new_connection.rs create mode 100644 mm2src/kdf_walletconnect/src/rpc_commands/ping.rs diff --git a/mm2src/kdf_walletconnect/src/error.rs b/mm2src/kdf_walletconnect/src/error.rs index aae333f737..1c3104bd47 100644 --- a/mm2src/kdf_walletconnect/src/error.rs +++ b/mm2src/kdf_walletconnect/src/error.rs @@ -38,6 +38,8 @@ pub(crate) const UNSUPPORTED_EVENTS: i32 = 5102; pub(crate) const UNSUPPORTED_ACCOUNTS: i32 = 5103; pub(crate) const UNSUPPORTED_NAMESPACE_KEY: i32 = 5104; +pub(crate) const USER_REQUESTED: i64 = 6000; + #[derive(Debug, Serialize, Deserialize, EnumFromStringify, thiserror::Error)] pub enum WalletConnectCtxError { #[error("Pairing Error: {0}")] diff --git a/mm2src/kdf_walletconnect/src/inbound_message.rs b/mm2src/kdf_walletconnect/src/inbound_message.rs index b83a297585..ae6e42be25 100644 --- a/mm2src/kdf_walletconnect/src/inbound_message.rs +++ b/mm2src/kdf_walletconnect/src/inbound_message.rs @@ -7,14 +7,14 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use crate::{error::WalletConnectCtxError, - pairing::{process_pairing_delete_response, process_pairing_extend_response, process_pairing_ping_response}, - session::{delete::process_session_delete_request, + pairing::{reply_pairing_delete_response, reply_pairing_extend_response, reply_pairing_ping_response}, + session::{delete::reply_session_delete_request, event::SessionEvents, - extend::process_session_extend_request, - ping::process_session_ping_request, - propose::{process_proposal_request, process_session_propose_response}, - settle::process_session_settle_request, - update::process_session_update_request}, + extend::reply_session_extend_request, + ping::reply_session_ping_request, + propose::{process_session_propose_response, reply_session_proposal_request}, + settle::reply_session_settle_request, + update::reply_session_update_request}, WalletConnectCtx}; #[derive(Debug, Serialize, Deserialize)] @@ -31,26 +31,25 @@ pub(crate) async fn process_inbound_request( ) -> MmResult<(), WalletConnectCtxError> { let message_id = request.id; match request.params { - Params::SessionPropose(proposal) => process_proposal_request(ctx, proposal, topic, &message_id).await?, - Params::SessionExtend(param) => process_session_extend_request(ctx, topic, &message_id, param).await?, - Params::SessionDelete(param) => process_session_delete_request(ctx, topic, &message_id, param).await?, - Params::SessionPing(()) => process_session_ping_request(ctx, topic, &message_id).await?, - Params::SessionSettle(param) => process_session_settle_request(ctx, topic, &message_id, param).await?, - Params::SessionUpdate(param) => process_session_update_request(ctx, topic, &message_id, param).await?, + Params::SessionPropose(proposal) => reply_session_proposal_request(ctx, proposal, topic, &message_id).await?, + Params::SessionExtend(param) => reply_session_extend_request(ctx, topic, &message_id, param).await?, + Params::SessionDelete(param) => reply_session_delete_request(ctx, topic, &message_id, param).await?, + Params::SessionPing(()) => reply_session_ping_request(ctx, topic, &message_id).await?, + Params::SessionSettle(param) => reply_session_settle_request(ctx, topic, &message_id, param).await?, + Params::SessionUpdate(param) => reply_session_update_request(ctx, topic, &message_id, param).await?, Params::SessionEvent(param) => { SessionEvents::from_events(param)? .handle_session_event(ctx, topic, &message_id) .await? }, Params::SessionRequest(_param) => { - // TODO: send back a success response. - info!("SessionRequest is not yet implemented."); + // TODO: Implement when integrating KDF as a wallet. return MmError::err(WalletConnectCtxError::NotImplemented); }, - Params::PairingPing(_param) => process_pairing_ping_response(ctx, topic, &message_id).await?, - Params::PairingDelete(param) => process_pairing_delete_response(ctx, topic, &message_id, param).await?, - Params::PairingExtend(param) => process_pairing_extend_response(ctx, topic, &message_id, param).await?, + Params::PairingPing(_param) => reply_pairing_ping_response(ctx, topic, &message_id).await?, + Params::PairingDelete(param) => reply_pairing_delete_response(ctx, topic, &message_id, param).await?, + Params::PairingExtend(param) => reply_pairing_extend_response(ctx, topic, &message_id, param).await?, _ => { info!("Unknown request params received."); return MmError::err(WalletConnectCtxError::InvalidRequest); @@ -72,7 +71,6 @@ pub(crate) async fn process_inbound_response( let success_response = serde_json::from_value::(value.result)?; match success_response { SuccessResponses::ResponseParamsSuccess(params) => match params { - // Handle known success responses match success_response { ResponseParamsSuccess::SessionPropose(param) => { process_session_propose_response(ctx, topic, param).await }, diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 6a788f40af..8f1d07700d 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -4,6 +4,8 @@ mod handler; mod inbound_message; mod metadata; #[allow(unused)] mod pairing; +pub mod rpc_commands; + mod session; mod storage; @@ -33,7 +35,7 @@ use relay_rpc::{auth::{ed25519_dalek::SigningKey, AuthToken}, ResponseParamsSuccess}, ErrorResponse, Payload, Request, Response, SuccessfulResponse}}; use serde_json::Value; -use session::{propose::send_proposal, Session, SymKeyPair}; +use session::{propose::send_proposal_request, Session, SymKeyPair}; use std::{sync::Arc, time::Duration}; use storage::{SessionStorageDb, WalletConnectStorageOps}; use wc_common::{decode_and_decrypt_type0, encrypt_and_encode, EnvelopeType}; @@ -124,7 +126,7 @@ impl WalletConnectCtx { } /// Create a WalletConnect pairing connection url. - pub async fn create_pairing( + pub async fn new_connection( &self, required_namespaces: Option, ) -> MmResult { @@ -136,7 +138,7 @@ impl WalletConnectCtx { info!("Subscribed to topic: {topic:?}"); - send_proposal(self, topic.clone(), required_namespaces).await?; + send_proposal_request(self, topic.clone(), required_namespaces).await?; { let mut subs = self.subscriptions.lock().await; @@ -173,10 +175,7 @@ impl WalletConnectCtx { /// Get current active chain id. pub async fn get_active_chain_id(&self) -> String { self.active_chain_id.lock().await.clone() } - pub async fn get_session(&self) -> Option { - let session = self.session.lock().await; - session.clone() - } + pub async fn get_session(&self) -> Option { self.session.lock().await.clone() } /// Get available accounts for a given chain_id. pub async fn get_account_for_chain_id(&self, chain_id: &str) -> MmResult { diff --git a/mm2src/kdf_walletconnect/src/pairing.rs b/mm2src/kdf_walletconnect/src/pairing.rs index 3f3b82da31..4838074e4f 100644 --- a/mm2src/kdf_walletconnect/src/pairing.rs +++ b/mm2src/kdf_walletconnect/src/pairing.rs @@ -10,7 +10,7 @@ use relay_rpc::{domain::Topic, rpc::params::{pairing_delete::PairingDeleteRequest, pairing_extend::PairingExtendRequest, ResponseParamsSuccess}}; -pub(crate) async fn process_pairing_ping_response( +pub(crate) async fn reply_pairing_ping_response( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, @@ -21,7 +21,7 @@ pub(crate) async fn process_pairing_ping_response( Ok(()) } -pub(crate) async fn process_pairing_extend_response( +pub(crate) async fn reply_pairing_extend_response( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, @@ -41,7 +41,7 @@ pub(crate) async fn process_pairing_extend_response( Ok(()) } -pub(crate) async fn process_pairing_delete_response( +pub(crate) async fn reply_pairing_delete_response( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, @@ -56,32 +56,3 @@ pub(crate) async fn process_pairing_delete_response( Ok(()) } - -pub(crate) async fn pairing_ping_request() -> WcRequestResponseResult { - let request = RequestParams::PairingPing(PairingPingRequest {}); - let irn_metadata = request.irn_metadata(); - let value = serde_json::to_value(request)?; - - Ok((value, irn_metadata)) -} - -pub(crate) async fn pairing_delete_request() -> WcRequestResponseResult { - let request = RequestParams::PairingDelete(PairingDeleteRequest { - code: 6000, - message: "Delete my pairing".to_string(), - }); - let irn_metadata = request.irn_metadata(); - let value = serde_json::to_value(request)?; - - Ok((value, irn_metadata)) -} - -pub(crate) async fn pairing_extend_request() -> WcRequestResponseResult { - let request = RequestParams::PairingExtend(PairingExtendRequest { - expiry: Utc::now().timestamp() as u64 + THIRTY_DAYS, - }); - let irn_metadata = request.irn_metadata(); - let value = serde_json::to_value(request)?; - - Ok((value, irn_metadata)) -} diff --git a/mm2src/kdf_walletconnect/src/rpc_commands/delete_connection.rs b/mm2src/kdf_walletconnect/src/rpc_commands/delete_connection.rs new file mode 100644 index 0000000000..543f47f21a --- /dev/null +++ b/mm2src/kdf_walletconnect/src/rpc_commands/delete_connection.rs @@ -0,0 +1,27 @@ +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::MmResult; +use relay_rpc::domain::Topic; +use serde::{Deserialize, Serialize}; + +use crate::{error::WalletConnectCtxError, session::delete::send_session_delete_request, WalletConnectCtx}; + +#[derive(Debug, PartialEq, Serialize)] +pub struct DeleteConnectionResponse { + pub successful: bool, +} + +#[derive(Deserialize)] +pub struct DeleteConnectionRequest { + topic: Topic, +} + +/// `delete connection` RPC command implementation. +pub async fn delete_connection( + ctx: MmArc, + req: DeleteConnectionRequest, +) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)?; + send_session_delete_request(&ctx, &req.topic).await?; + + Ok(DeleteConnectionResponse { successful: true }) +} diff --git a/mm2src/kdf_walletconnect/src/rpc_commands/get_chain_id.rs b/mm2src/kdf_walletconnect/src/rpc_commands/get_chain_id.rs new file mode 100644 index 0000000000..6c1e2e2f76 --- /dev/null +++ b/mm2src/kdf_walletconnect/src/rpc_commands/get_chain_id.rs @@ -0,0 +1,20 @@ +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::MmResult; +use serde::Serialize; + +use crate::{error::WalletConnectCtxError, WalletConnectCtx}; + +use super::EmptyRpcRequst; + +#[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)?; + let chain_id = ctx.get_active_chain_id().await; + + Ok(GetChainIdResponse { chain_id }) +} diff --git a/mm2src/kdf_walletconnect/src/rpc_commands/get_session.rs b/mm2src/kdf_walletconnect/src/rpc_commands/get_session.rs new file mode 100644 index 0000000000..8734f0d223 --- /dev/null +++ b/mm2src/kdf_walletconnect/src/rpc_commands/get_session.rs @@ -0,0 +1,19 @@ +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::MmResult; +use serde::Serialize; + +use super::EmptyRpcRequst; +use crate::{error::WalletConnectCtxError, session::Session, WalletConnectCtx}; + +#[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)?; + let session = ctx.get_session().await; + + Ok(GetSessionResponse { session }) +} diff --git a/mm2src/kdf_walletconnect/src/rpc_commands/mod.rs b/mm2src/kdf_walletconnect/src/rpc_commands/mod.rs new file mode 100644 index 0000000000..3ae7f66bfc --- /dev/null +++ b/mm2src/kdf_walletconnect/src/rpc_commands/mod.rs @@ -0,0 +1,15 @@ +mod delete_connection; +mod get_chain_id; +mod get_session; +mod new_connection; +mod ping; + +pub use delete_connection::delete_connection; +pub use get_chain_id::get_chain_id; +pub use get_session::get_session; +pub use new_connection::new_connection; +pub use ping::ping_session; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct EmptyRpcRequst {} diff --git a/mm2src/kdf_walletconnect/src/rpc_commands/new_connection.rs b/mm2src/kdf_walletconnect/src/rpc_commands/new_connection.rs new file mode 100644 index 0000000000..11b995381e --- /dev/null +++ b/mm2src/kdf_walletconnect/src/rpc_commands/new_connection.rs @@ -0,0 +1,22 @@ +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::MmResult; +use serde::Serialize; + +use super::EmptyRpcRequst; +use crate::{error::WalletConnectCtxError, WalletConnectCtx}; + +#[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)?; + let url = ctx.new_connection(None).await?; + + Ok(CreateConnectionResponse { url }) +} diff --git a/mm2src/kdf_walletconnect/src/rpc_commands/ping.rs b/mm2src/kdf_walletconnect/src/rpc_commands/ping.rs new file mode 100644 index 0000000000..363bceacf7 --- /dev/null +++ b/mm2src/kdf_walletconnect/src/rpc_commands/ping.rs @@ -0,0 +1,24 @@ +use mm2_core::mm_ctx::MmArc; +use mm2_err_handle::prelude::MmResult; +use relay_rpc::domain::Topic; +use serde::{Deserialize, Serialize}; + +use crate::{error::WalletConnectCtxError, session::ping::send_session_ping_request, WalletConnectCtx}; + +#[derive(Debug, PartialEq, Serialize)] +pub struct SessionPingResponse { + pub successful: bool, +} + +#[derive(Deserialize)] +pub struct SessionPingRequest { + topic: Topic, +} + +/// `ping session` RPC command implementation. +pub async fn ping_session(ctx: MmArc, req: SessionPingRequest) -> MmResult { + let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)?; + send_session_ping_request(&ctx, &req.topic).await?; + + Ok(SessionPingResponse { successful: true }) +} diff --git a/mm2src/kdf_walletconnect/src/session/delete.rs b/mm2src/kdf_walletconnect/src/session/delete.rs index d1702bb5a2..d4baf5f180 100644 --- a/mm2src/kdf_walletconnect/src/session/delete.rs +++ b/mm2src/kdf_walletconnect/src/session/delete.rs @@ -1,11 +1,12 @@ -use crate::{error::WalletConnectCtxError, WalletConnectCtx}; +use crate::{error::{WalletConnectCtxError, USER_REQUESTED}, + WalletConnectCtx}; use common::log::debug; use mm2_err_handle::prelude::MmResult; use relay_rpc::{domain::{MessageId, Topic}, - rpc::params::{session_delete::SessionDeleteRequest, ResponseParamsSuccess}}; + rpc::params::{session_delete::SessionDeleteRequest, RequestParams, ResponseParamsSuccess}}; -pub(crate) async fn process_session_delete_request( +pub(crate) async fn reply_session_delete_request( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, @@ -14,9 +15,22 @@ pub(crate) async fn process_session_delete_request( let param = ResponseParamsSuccess::SessionDelete(true); ctx.publish_response_ok(topic, param, message_id).await?; - session_delete_cleanup(ctx, topic).await?; + session_delete_cleanup(ctx, topic).await +} - Ok(()) +pub(crate) async fn send_session_delete_request( + ctx: &WalletConnectCtx, + session_topic: &Topic, +) -> MmResult<(), WalletConnectCtxError> { + let delete_request = SessionDeleteRequest { + code: USER_REQUESTED, + message: "User Disconnected".to_owned(), + }; + let param = RequestParams::SessionDelete(delete_request); + + ctx.publish_request(session_topic, param).await?; + + session_delete_cleanup(ctx, session_topic).await } async fn session_delete_cleanup(ctx: &WalletConnectCtx, topic: &Topic) -> MmResult<(), WalletConnectCtxError> { diff --git a/mm2src/kdf_walletconnect/src/session/event.rs b/mm2src/kdf_walletconnect/src/session/event.rs index eede3933dc..02a2db9b0f 100644 --- a/mm2src/kdf_walletconnect/src/session/event.rs +++ b/mm2src/kdf_walletconnect/src/session/event.rs @@ -65,7 +65,8 @@ impl SessionEvents { if namespace.chains.contains(&chain) { // TODO: Notify GUI about chain changed. // Update active chain_id - *ctx.active_chain_id.lock().await = chain_id.clone().to_owned(); + ctx.set_active_chain(chain_id.clone()).await; + let params = ResponseParamsSuccess::SessionEvent(true); ctx.publish_response_ok(topic, params, message_id).await?; diff --git a/mm2src/kdf_walletconnect/src/session/extend.rs b/mm2src/kdf_walletconnect/src/session/extend.rs index e637008264..5e34cb0e69 100644 --- a/mm2src/kdf_walletconnect/src/session/extend.rs +++ b/mm2src/kdf_walletconnect/src/session/extend.rs @@ -6,7 +6,7 @@ use relay_rpc::{domain::{MessageId, Topic}, rpc::params::{session_extend::SessionExtendRequest, ResponseParamsSuccess}}; /// Process session extend request. -pub(crate) async fn process_session_extend_request( +pub(crate) async fn reply_session_extend_request( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, diff --git a/mm2src/kdf_walletconnect/src/session/ping.rs b/mm2src/kdf_walletconnect/src/session/ping.rs index 3da8d26d03..87ebdba0c8 100644 --- a/mm2src/kdf_walletconnect/src/session/ping.rs +++ b/mm2src/kdf_walletconnect/src/session/ping.rs @@ -2,9 +2,9 @@ use crate::{error::WalletConnectCtxError, WalletConnectCtx}; use mm2_err_handle::prelude::MmResult; use relay_rpc::{domain::{MessageId, Topic}, - rpc::params::ResponseParamsSuccess}; + rpc::params::{RequestParams, ResponseParamsSuccess}}; -pub(crate) async fn process_session_ping_request( +pub(crate) async fn reply_session_ping_request( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, @@ -14,3 +14,13 @@ pub(crate) async fn process_session_ping_request( Ok(()) } + +pub(crate) async fn send_session_ping_request( + ctx: &WalletConnectCtx, + topic: &Topic, +) -> MmResult<(), WalletConnectCtxError> { + let param = RequestParams::SessionPing(()); + ctx.publish_request(topic, param).await?; + + Ok(()) +} diff --git a/mm2src/kdf_walletconnect/src/session/propose.rs b/mm2src/kdf_walletconnect/src/session/propose.rs index 80d40f7c18..bc233cc0e2 100644 --- a/mm2src/kdf_walletconnect/src/session/propose.rs +++ b/mm2src/kdf_walletconnect/src/session/propose.rs @@ -13,7 +13,7 @@ use relay_rpc::{domain::{MessageId, Topic}, Metadata, RequestParams, ResponseParamsSuccess}}; /// Creates a new session proposal form topic and metadata. -pub(crate) async fn send_proposal( +pub(crate) async fn send_proposal_request( ctx: &WalletConnectCtx, topic: Topic, required_namespaces: Option, @@ -35,7 +35,7 @@ pub(crate) async fn send_proposal( /// Process session proposal request /// https://specs.walletconnect.com/2.0/specs/clients/sign/session-proposal -pub async fn process_proposal_request( +pub async fn reply_session_proposal_request( ctx: &WalletConnectCtx, proposal: SessionProposeRequest, topic: &Topic, diff --git a/mm2src/kdf_walletconnect/src/session/settle.rs b/mm2src/kdf_walletconnect/src/session/settle.rs index b9a851f300..09534c0103 100644 --- a/mm2src/kdf_walletconnect/src/session/settle.rs +++ b/mm2src/kdf_walletconnect/src/session/settle.rs @@ -36,7 +36,7 @@ pub(crate) async fn send_session_settle_request( } /// Process session settle request. -pub(crate) async fn process_session_settle_request( +pub(crate) async fn reply_session_settle_request( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, diff --git a/mm2src/kdf_walletconnect/src/session/update.rs b/mm2src/kdf_walletconnect/src/session/update.rs index 842b23ce71..ba03417bc6 100644 --- a/mm2src/kdf_walletconnect/src/session/update.rs +++ b/mm2src/kdf_walletconnect/src/session/update.rs @@ -6,7 +6,7 @@ use relay_rpc::{domain::{MessageId, Topic}, // TODO: Handle properly when multi chain is supported. // Hanlding for only cosmos support. -pub(crate) async fn process_session_update_request( +pub(crate) async fn reply_session_update_request( ctx: &WalletConnectCtx, topic: &Topic, message_id: &MessageId, diff --git a/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs b/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs index ece20f9eda..c8abf7950b 100644 --- a/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs +++ b/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs @@ -1,4 +1,3 @@ -use super::lp_commands::{connect_to_peer, create_new_pairing}; use super::{DispatcherError, DispatcherResult, PUBLIC_METHODS}; use crate::lp_native_dex::init_hw::{cancel_init_trezor, init_trezor, init_trezor_status, init_trezor_user_action}; #[cfg(target_arch = "wasm32")] @@ -55,6 +54,7 @@ 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,6 +161,9 @@ 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, @@ -168,8 +171,6 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult handle_mmrpc(ctx, request, add_node_to_version_stat).await, "best_orders" => handle_mmrpc(ctx, request, best_orders_rpc_v2).await, "clear_nft_db" => handle_mmrpc(ctx, request, clear_nft_db).await, - "wc_connect_pairing" => handle_mmrpc(ctx, request, connect_to_peer).await, - "wc_create_pairing" => handle_mmrpc(ctx, request, create_new_pairing).await, "enable_bch_with_tokens" => handle_mmrpc(ctx, request, enable_platform_coin_with_tokens::).await, "enable_slp" => handle_mmrpc(ctx, request, enable_token::).await, "enable_eth_with_tokens" => handle_mmrpc(ctx, request, enable_platform_coin_with_tokens::).await, @@ -224,6 +225,11 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult handle_mmrpc(ctx, request, set_swap_transaction_fee_policy).await, "send_asked_data" => handle_mmrpc(ctx, request, send_asked_data_rpc).await, "z_coin_tx_history" => handle_mmrpc(ctx, request, coins::my_tx_history_v2::z_coin_tx_history_rpc).await, + "wc_new_connection" => handle_mmrpc(ctx, request, new_connection).await, + "wc_delete_connection" => handle_mmrpc(ctx, request, delete_connection).await, + "wc_get_chain_id" => handle_mmrpc(ctx, request, get_chain_id).await, + "wc_get_session" => handle_mmrpc(ctx, request, get_session).await, + "wc_ping_session" => handle_mmrpc(ctx, request, ping_session).await, #[cfg(not(target_arch = "wasm32"))] native_only_methods => match native_only_methods { #[cfg(all(feature = "enable-solana", not(target_os = "ios"), not(target_os = "android")))] diff --git a/mm2src/mm2_main/src/rpc/lp_commands/lp_commands.rs b/mm2src/mm2_main/src/rpc/lp_commands/lp_commands.rs index 39547af0c3..ae992c6d3e 100644 --- a/mm2src/mm2_main/src/rpc/lp_commands/lp_commands.rs +++ b/mm2src/mm2_main/src/rpc/lp_commands/lp_commands.rs @@ -2,7 +2,6 @@ use common::HttpStatusCode; use crypto::{CryptoCtx, CryptoCtxError, HwConnectionStatus, HwPubkey}; use derive_more::Display; use http::StatusCode; -use kdf_walletconnect::WalletConnectCtx; use mm2_core::mm_ctx::MmArc; use mm2_err_handle::prelude::*; use rpc::v1::types::H160 as H160Json; @@ -115,58 +114,3 @@ pub async fn trezor_connection_status( status: hw_ctx.trezor_connection_status().await, }) } - -//////////// TESTING PURPOSES ///////////// -use serde::Serialize; - -#[derive(Deserialize)] -pub struct ConnectPairingRequest { - url: String, -} - -#[derive(Debug, PartialEq, Serialize)] -pub struct ConnectPairingResponse { - pub topic: String, -} - -/// `connect_to_peer` RPC command implementation. -pub async fn connect_to_peer( - ctx: MmArc, - req: ConnectPairingRequest, -) -> MmResult { - let walletconnect_ctx = - WalletConnectCtx::try_from_ctx_or_initialize(&ctx).expect("WalletConnectCtx should be initialized by now!"); - - let topic = walletconnect_ctx - .connect_to_pairing(&req.url, true) - .await - .map_err(|err| TrezorConnectionError::Internal(err.to_string()))?; - - Ok(ConnectPairingResponse { - topic: topic.to_string(), - }) -} - -#[derive(Debug, PartialEq, Serialize)] -pub struct CreatePairingResponse { - pub url: String, -} - -#[derive(Deserialize)] -pub struct CreatePairingRequest {} - -/// `create_new_pairing` RPC command implementation. -pub async fn create_new_pairing( - ctx: MmArc, - _req: CreatePairingRequest, -) -> MmResult { - let walletconnect_ctx = - WalletConnectCtx::try_from_ctx_or_initialize(&ctx).expect("WalletConnectCtx should be initialized by now!"); - - let url = walletconnect_ctx - .create_pairing(None) - .await - .map_err(|err| TrezorConnectionError::Internal(err.to_string()))?; - - Ok(CreatePairingResponse { url }) -}