diff --git a/mm2src/coins/eth.rs b/mm2src/coins/eth.rs index d43f4dc616..ebe66b5070 100644 --- a/mm2src/coins/eth.rs +++ b/mm2src/coins/eth.rs @@ -2694,6 +2694,8 @@ async fn sign_raw_eth_tx(coin: &EthCoin, args: &SignEthTransactionParams) -> Raw .map_to_mm(|err| RawTransactionError::TransactionError(err.get_plain_text_format())) }, EthPrivKeyPolicy::WalletConnect { .. } => { + //NOTE: doesn't work with wallets that doesn't support `eth_signTransaction` e.g + //Metamask let wc = { let ctx = MmArc::from_weak(&coin.ctx).expect("No context"); WalletConnectCtx::from_ctx(&ctx).expect("WalletConnectCtx should be initialized by now!") diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 7427a70eb7..8f7cf9bfda 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -77,7 +77,6 @@ pub struct WalletConnectCtx { relay: Relay, metadata: Metadata, - subscriptions: Arc>>, inbound_message_rx: Arc>>, connection_live_rx: Arc>>, message_tx: UnboundedSender, @@ -112,7 +111,6 @@ impl WalletConnectCtx { metadata: generate_metadata(), key_pair: SymKeyPair::new(), session: SessionManager::new(), - subscriptions: Default::default(), inbound_message_rx: Arc::new(inbound_message_rx.into()), connection_live_rx: Arc::new(conn_live_receiver.into()), @@ -166,11 +164,6 @@ impl WalletConnectCtx { send_proposal_request(self, &topic, namespaces).await?; - { - let mut subs = self.subscriptions.lock().await; - subs.push(topic); - }; - Ok(url) } diff --git a/mm2src/kdf_walletconnect/src/session/rpc/delete.rs b/mm2src/kdf_walletconnect/src/session/rpc/delete.rs index 270ff078fc..75843e3dcc 100644 --- a/mm2src/kdf_walletconnect/src/session/rpc/delete.rs +++ b/mm2src/kdf_walletconnect/src/session/rpc/delete.rs @@ -48,10 +48,6 @@ async fn session_delete_cleanup(ctx: &WalletConnectCtx, topic: &Topic) -> MmResu ctx.client.unsubscribe(session.pairing_topic.clone()).await?; // Attempt to disconnect the pairing ctx.pairing.delete(session.pairing_topic.as_ref()).await; - // Remove subscriptions - let mut subs = ctx.subscriptions.lock().await; - subs.retain(|s| s != &session.topic); - subs.retain(|s| s != &session.pairing_topic); // delete session from storage as well. ctx.storage diff --git a/mm2src/kdf_walletconnect/src/session/rpc/propose.rs b/mm2src/kdf_walletconnect/src/session/rpc/propose.rs index 917a2f9198..eee8c90b8a 100644 --- a/mm2src/kdf_walletconnect/src/session/rpc/propose.rs +++ b/mm2src/kdf_walletconnect/src/session/rpc/propose.rs @@ -14,7 +14,7 @@ use relay_rpc::{domain::{MessageId, Topic}, rpc::params::{session_propose::{Proposer, SessionProposeRequest, SessionProposeResponse}, RequestParams, ResponseParamsSuccess}}; -/// Creates a new session proposal form topic and metadata. +/// Creates a new session proposal from topic and metadata. pub(crate) async fn send_proposal_request( ctx: &WalletConnectCtx, topic: &Topic, @@ -43,28 +43,29 @@ pub async fn reply_session_proposal_request( topic: &Topic, message_id: &MessageId, ) -> MmResult<(), WalletConnectError> { - let sender_public_key = const_hex::decode(&proposal.proposer.public_key)? - .as_slice() - .try_into() - .map_to_mm(|_| WalletConnectError::InternalError("Invalid sender_public_key".to_owned()))?; - - let session_key = SessionKey::from_osrng(&sender_public_key)?; - let session_topic: Topic = session_key.generate_topic().into(); - let subscription_id = ctx - .client - .subscribe(session_topic.clone()) - .await - .map_to_mm(|err| WalletConnectError::SubscriptionError(err.to_string()))?; - - let session = Session::new( - ctx, - session_topic.clone(), - subscription_id, - session_key, - topic.clone(), - proposal.proposer.metadata, - SessionType::Controller, - ); + let session = { + let sender_public_key = const_hex::decode(&proposal.proposer.public_key)? + .as_slice() + .try_into() + .map_to_mm(|_| WalletConnectError::InternalError("Invalid sender_public_key".to_owned()))?; + let session_key = SessionKey::from_osrng(&sender_public_key)?; + let session_topic: Topic = session_key.generate_topic().into(); + let subscription_id = ctx + .client + .subscribe(session_topic.clone()) + .await + .map_to_mm(|err| WalletConnectError::SubscriptionError(err.to_string()))?; + + Session::new( + ctx, + session_topic.clone(), + subscription_id, + session_key, + topic.clone(), + proposal.proposer.metadata, + SessionType::Controller, + ) + }; session .propose_namespaces .supported(&proposal.required_namespaces) @@ -79,14 +80,9 @@ pub async fn reply_session_proposal_request( // Add session to session lists ctx.session.add_session(session.clone()).await; - // Add topic to subscription list - let mut subs = ctx.subscriptions.lock().await; - subs.push(session_topic); } - { - send_session_settle_request(ctx, &session).await?; - }; + send_session_settle_request(ctx, &session).await?; // Respond to incoming session propose. let param = ResponseParamsSuccess::SessionPropose(SessionProposeResponse { @@ -105,34 +101,38 @@ pub(crate) async fn process_session_propose_response( pairing_topic: &Topic, response: &SessionProposeResponse, ) -> MmResult<(), WalletConnectError> { - let other_public_key = const_hex::decode(&response.responder_public_key)? - .as_slice() - .try_into() - .unwrap(); - - let mut session_key = SessionKey::new(ctx.key_pair.public_key); - session_key.generate_symmetric_key(&ctx.key_pair.secret, &other_public_key)?; - - let session_topic: Topic = session_key.generate_topic().into(); - let subscription_id = ctx - .client - .subscribe(session_topic.clone()) - .await - .map_to_mm(|err| WalletConnectError::SubscriptionError(err.to_string()))?; - - let mut session = Session::new( - ctx, - session_topic.clone(), - subscription_id, - session_key, - pairing_topic.clone(), - generate_metadata(), - SessionType::Proposer, - ); - session.relay = response.relay.clone(); - session.expiry = Utc::now().timestamp() as u64 + THIRTY_DAYS; - session.controller.public_key = response.responder_public_key.clone(); + let session_key = { + let other_public_key = const_hex::decode(&response.responder_public_key)? + .as_slice() + .try_into() + .unwrap(); + let mut session_key = SessionKey::new(ctx.key_pair.public_key); + session_key.generate_symmetric_key(&ctx.key_pair.secret, &other_public_key)?; + session_key + }; + let session = { + let session_topic: Topic = session_key.generate_topic().into(); + let subscription_id = ctx + .client + .subscribe(session_topic.clone()) + .await + .map_to_mm(|err| WalletConnectError::SubscriptionError(err.to_string()))?; + + let mut session = Session::new( + ctx, + session_topic.clone(), + subscription_id, + session_key, + pairing_topic.clone(), + generate_metadata(), + SessionType::Proposer, + ); + session.relay = response.relay.clone(); + session.expiry = Utc::now().timestamp() as u64 + THIRTY_DAYS; + session.controller.public_key = response.responder_public_key.clone(); + session + }; { // save session to storage ctx.storage @@ -142,9 +142,6 @@ pub(crate) async fn process_session_propose_response( // Add session to session lists ctx.session.add_session(session.clone()).await; - // Add topic to subscription list - let mut subs = ctx.subscriptions.lock().await; - subs.push(session_topic.clone()); }; // Activate pairing_topic diff --git a/mm2src/kdf_walletconnect/src/session/rpc/settle.rs b/mm2src/kdf_walletconnect/src/session/rpc/settle.rs index 4cdf9ef407..3205872fc3 100644 --- a/mm2src/kdf_walletconnect/src/session/rpc/settle.rs +++ b/mm2src/kdf_walletconnect/src/session/rpc/settle.rs @@ -43,7 +43,7 @@ pub(crate) async fn reply_session_settle_request( { let session = ctx.session.get_session_mut(topic); if let Some(mut session) = session { - session.namespaces = settle.namespaces.0.clone(); + session.namespaces = settle.namespaces.0; session.controller = settle.controller.clone(); session.relay = settle.relay; session.expiry = settle.expiry; @@ -66,6 +66,7 @@ pub(crate) async fn reply_session_settle_request( ctx.publish_response_ok(topic, param, message_id).await?; // Delete other sessions with same controller + // TODO: we might not want to do this! let all_sessions = ctx.session.get_sessions_full(); for session in all_sessions { if session.controller == settle.controller && session.topic.as_ref() != topic.as_ref() {