Skip to content

Commit

Permalink
make required_namespaces required for wc activation and remove defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Nov 27, 2024
1 parent 1fec24f commit bdf9636
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
31 changes: 1 addition & 30 deletions mm2src/kdf_walletconnect/src/chain.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -114,23 +105,3 @@ impl AsRef<str> 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)
}
13 changes: 9 additions & 4 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct WalletConnectCtxImpl {
metadata: Metadata,
message_tx: UnboundedSender<SessionMessageType>,
message_rx: Arc<Mutex<UnboundedReceiver<SessionMessageType>>>,
pub abortable_system: AbortableQueue,
abortable_system: AbortableQueue,
}

pub struct WalletConnectCtx(pub Arc<WalletConnectCtxImpl>);
Expand Down Expand Up @@ -178,8 +178,13 @@ impl WalletConnectCtxImpl {
}

/// Create a WalletConnect pairing connection url.
pub async fn new_connection(&self, namespaces: Option<serde_json::Value>) -> MmResult<String, WalletConnectError> {
let namespaces = match namespaces {
pub async fn new_connection(
&self,
required_namespaces: serde_json::Value,
optional_namespaces: Option<serde_json::Value>,
) -> MmResult<String, WalletConnectError> {
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,
};
Expand All @@ -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()),
Expand Down
8 changes: 4 additions & 4 deletions mm2src/kdf_walletconnect/src/session/rpc/propose.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,7 +17,8 @@ use relay_rpc::{domain::{MessageId, Topic},
pub(crate) async fn send_proposal_request(
ctx: &WalletConnectCtxImpl,
topic: &Topic,
namespaces: Option<ProposeNamespaces>,
required_namespaces: ProposeNamespaces,
optional_namespaces: Option<ProposeNamespaces>,
) -> MmResult<(), WalletConnectError> {
let proposer = Proposer {
metadata: ctx.metadata.clone(),
Expand All @@ -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?;

Expand Down
5 changes: 3 additions & 2 deletions mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct CreateConnectionResponse {

#[derive(Deserialize)]
pub struct NewConnectionRequest {
namespaces: Option<serde_json::Value>,
required_namespaces: serde_json::Value,
optional_namespaces: Option<serde_json::Value>,
}

/// `new_connection` RPC command implementation.
Expand All @@ -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()))?;

Expand Down

0 comments on commit bdf9636

Please sign in to comment.