diff --git a/mm2src/kdf_walletconnect/src/chain.rs b/mm2src/kdf_walletconnect/src/chain.rs index 78775e6f42..20e1acd6a8 100644 --- a/mm2src/kdf_walletconnect/src/chain.rs +++ b/mm2src/kdf_walletconnect/src/chain.rs @@ -1,20 +1,11 @@ use mm2_err_handle::prelude::{MmError, MmResult}; -use relay_rpc::rpc::params::session::{ProposeNamespace, ProposeNamespaces}; use serde::{Deserialize, Serialize}; -use std::{collections::{BTreeMap, BTreeSet}, - str::FromStr}; +use std::str::FromStr; use crate::error::WalletConnectError; pub(crate) const SUPPORTED_PROTOCOL: &str = "irn"; -pub(crate) const COSMOS_SUPPORTED_METHODS: &[&str] = &["cosmos_getAccounts", "cosmos_signDirect", "cosmos_signAmino"]; -pub(crate) const COSMOS_SUPPORTED_CHAINS: &[&str] = &["cosmos:cosmoshub-4"]; - -pub(crate) const ETH_SUPPORTED_METHODS: &[&str] = &["eth_signTransaction", "personal_sign", "eth_sendTransaction"]; -pub(crate) const ETH_SUPPORTED_CHAINS: &[&str] = &["eip155:1", "eip155:137"]; -pub(crate) const ETH_SUPPORTED_EVENTS: &[&str] = &["accountsChanged", "chainChanged"]; - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum WcChain { Eip155, @@ -114,23 +105,3 @@ impl AsRef for WcRequestMethods { } } } - -pub(crate) fn build_default_required_namespaces() -> ProposeNamespaces { - let required = BTreeMap::from([(WcChain::Eip155.as_ref().to_string(), ProposeNamespace { - events: ETH_SUPPORTED_EVENTS.iter().map(|m| m.to_string()).collect(), - chains: ETH_SUPPORTED_CHAINS.iter().map(|c| c.to_string()).collect(), - methods: ETH_SUPPORTED_METHODS.iter().map(|m| m.to_string()).collect(), - })]); - - ProposeNamespaces(required) -} - -pub(crate) fn build_optional_namespaces() -> ProposeNamespaces { - let optional = BTreeMap::from([(WcChain::Cosmos.as_ref().to_string(), ProposeNamespace { - methods: COSMOS_SUPPORTED_METHODS.iter().map(|m| m.to_string()).collect(), - chains: COSMOS_SUPPORTED_CHAINS.iter().map(|c| c.to_string()).collect(), - events: BTreeSet::default(), - })]); - - ProposeNamespaces(optional) -} diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 85e71cb947..355b329081 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -80,7 +80,7 @@ pub struct WalletConnectCtxImpl { metadata: Metadata, message_tx: UnboundedSender, message_rx: Arc>>, - pub abortable_system: AbortableQueue, + abortable_system: AbortableQueue, } pub struct WalletConnectCtx(pub Arc); @@ -178,8 +178,13 @@ impl WalletConnectCtxImpl { } /// Create a WalletConnect pairing connection url. - pub async fn new_connection(&self, namespaces: Option) -> MmResult { - let namespaces = match namespaces { + pub async fn new_connection( + &self, + required_namespaces: serde_json::Value, + optional_namespaces: Option, + ) -> MmResult { + let required_namespaces = serde_json::from_value(required_namespaces)?; + let optional_namespaces = match optional_namespaces { Some(value) => Some(serde_json::from_value(value)?), None => None, }; @@ -196,7 +201,7 @@ impl WalletConnectCtxImpl { { Ok(Ok(_)) => { info!("[topic] Subscribed to topic"); - send_proposal_request(self, &topic, namespaces).await?; + send_proposal_request(self, &topic, required_namespaces, optional_namespaces).await?; return Ok(url); }, Ok(Err(err)) => return MmError::err(err.into()), diff --git a/mm2src/kdf_walletconnect/src/session/rpc/propose.rs b/mm2src/kdf_walletconnect/src/session/rpc/propose.rs index 2d3c30a44f..f2b3f7c03c 100644 --- a/mm2src/kdf_walletconnect/src/session/rpc/propose.rs +++ b/mm2src/kdf_walletconnect/src/session/rpc/propose.rs @@ -1,5 +1,4 @@ use super::settle::send_session_settle_request; -use crate::chain::{build_default_required_namespaces, build_optional_namespaces}; use crate::storage::WalletConnectStorageOps; use crate::{error::WalletConnectError, metadata::generate_metadata, @@ -18,7 +17,8 @@ use relay_rpc::{domain::{MessageId, Topic}, pub(crate) async fn send_proposal_request( ctx: &WalletConnectCtxImpl, topic: &Topic, - namespaces: Option, + required_namespaces: ProposeNamespaces, + optional_namespaces: Option, ) -> MmResult<(), WalletConnectError> { let proposer = Proposer { metadata: ctx.metadata.clone(), @@ -27,8 +27,8 @@ pub(crate) async fn send_proposal_request( let session_proposal = RequestParams::SessionPropose(SessionProposeRequest { relays: vec![ctx.relay.clone()], proposer, - required_namespaces: namespaces.unwrap_or_else(build_default_required_namespaces), - optional_namespaces: Some(build_optional_namespaces()), + required_namespaces, + optional_namespaces, }); ctx.publish_request(topic, session_proposal).await?; diff --git a/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs b/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs index 2ba1d60469..410deae5d7 100644 --- a/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs +++ b/mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs @@ -12,7 +12,8 @@ pub struct CreateConnectionResponse { #[derive(Deserialize)] pub struct NewConnectionRequest { - namespaces: Option, + required_namespaces: serde_json::Value, + optional_namespaces: Option, } /// `new_connection` RPC command implementation. @@ -23,7 +24,7 @@ pub async fn new_connection( let ctx = WalletConnectCtx::from_ctx(&ctx).mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; let url = ctx - .new_connection(req.namespaces) + .new_connection(req.required_namespaces, req.optional_namespaces) .await .mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?;