From f6e7864f0a6c7281dac72bd7e4da00563ecb5225 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Wed, 18 Sep 2024 13:54:14 +0100 Subject: [PATCH] minor changes --- mm2src/kdf_walletconnect/src/lib.rs | 4 +-- mm2src/kdf_walletconnect/src/session/event.rs | 30 ++++++++++++++----- .../kdf_walletconnect/src/session/propose.rs | 28 +++++++---------- .../kdf_walletconnect/src/session/settle.rs | 1 - 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 8bb3925236..f761cc9f87 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -25,7 +25,7 @@ use relay_rpc::{auth::{ed25519_dalek::SigningKey, AuthToken}, rpc::{params::{session::{ProposeNamespace, ProposeNamespaces}, IrnMetadata, Metadata, Relay, ResponseParamsError, ResponseParamsSuccess}, ErrorResponse, Payload, Request, Response, SuccessfulResponse}}; -use session::{propose::create_proposal_session, Session}; +use session::{propose::new_proposal, Session}; use std::{collections::BTreeMap, sync::Arc, time::Duration}; use wc_common::{decode_and_decrypt_type0, encrypt_and_encode, EnvelopeType}; @@ -102,7 +102,7 @@ impl WalletConnectCtx { self.client.subscribe(topic.clone()).await?; info!("Subscribed to topic: {topic:?}"); - create_proposal_session(self, topic, required_namespaces).await?; + new_proposal(self, topic, required_namespaces).await?; Ok(url) } diff --git a/mm2src/kdf_walletconnect/src/session/event.rs b/mm2src/kdf_walletconnect/src/session/event.rs index f6cdf9b52a..1b851f6cc2 100644 --- a/mm2src/kdf_walletconnect/src/session/event.rs +++ b/mm2src/kdf_walletconnect/src/session/event.rs @@ -1,8 +1,8 @@ -use crate::{error::{WalletConnectCtxError, UNSUPPORTED_CHAINS}, +use crate::{error::{WalletConnectCtxError, INVALID_EVENT, UNSUPPORTED_CHAINS}, WalletConnectCtx}; use chrono::Utc; -use mm2_err_handle::prelude::MmResult; +use mm2_err_handle::prelude::{MmError, MmResult}; use relay_rpc::{domain::{MessageId, Topic}, rpc::{params::{session_event::SessionEventRequest, ResponseParamsError, ResponseParamsSuccess}, ErrorData}}; @@ -10,7 +10,7 @@ use relay_rpc::{domain::{MessageId, Topic}, pub enum SessionEvents { ChainChanged(String), AccountsChanged(String, Vec), - Unknown, + Unknown(String), } impl SessionEvents { @@ -21,7 +21,7 @@ impl SessionEvents { let data = serde_json::from_value::>(event.event.data)?; Ok(SessionEvents::AccountsChanged(event.chain_id, data)) }, - _ => Ok(SessionEvents::Unknown), + _ => Ok(SessionEvents::Unknown(event.event.name)), } } @@ -36,9 +36,21 @@ impl SessionEvents { Self::handle_chain_changed_event(ctx, chain_id, topic, message_id).await }, SessionEvents::AccountsChanged(chain_id, data) => { - Self::handle_account_changed_event(ctx, chain_id, data, topic, message_id).await + Self::handle_accounts_changed_event(ctx, chain_id, data, topic, message_id).await + }, + SessionEvents::Unknown(name) => { + let error_data = ErrorData { + code: INVALID_EVENT, + message: format!("Received an invalid/unsupported session event: {name}"), + data: None, + }; + ctx.publish_response_err(topic, ResponseParamsError::SessionEvent(error_data), message_id) + .await?; + + return MmError::err(WalletConnectCtxError::SessionError(format!( + "Unsupported session event" + ))); }, - SessionEvents::Unknown => todo!(), } } @@ -67,6 +79,10 @@ impl SessionEvents { ctx.publish_response_err(topic, ResponseParamsError::SessionEvent(error_data), message_id) .await?; + + return MmError::err(WalletConnectCtxError::SessionError(format!( + "Unsupported chainChanged chain_id from session request: {chain_id}", + ))); } let mut sessions = ctx.sessions.lock().await; @@ -88,7 +104,7 @@ impl SessionEvents { Ok(()) } - async fn handle_account_changed_event( + async fn handle_accounts_changed_event( ctx: &WalletConnectCtx, _chain_id: &str, _data: &[String], diff --git a/mm2src/kdf_walletconnect/src/session/propose.rs b/mm2src/kdf_walletconnect/src/session/propose.rs index 8d551e6211..512e3f7cd6 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}, use std::ops::Deref; /// Creates a new session proposal form topic and metadata. -pub(crate) async fn create_proposal_session( +pub(crate) async fn new_proposal( ctx: &WalletConnectCtx, topic: Topic, required_namespaces: Option, @@ -33,22 +33,6 @@ pub(crate) async fn create_proposal_session( Ok(()) } -async fn send_proposal_request_response( - ctx: &WalletConnectCtx, - topic: &Topic, - message_id: &MessageId, - responder_public_key: String, -) -> MmResult<(), WalletConnectCtxError> { - let param = ResponseParamsSuccess::SessionPropose(SessionProposeResponse { - relay: ctx.relay.clone(), - responder_public_key, - }); - - ctx.publish_response_ok(topic, param, message_id).await?; - - Ok(()) -} - /// Process session proposal request /// https://specs.walletconnect.com/2.0/specs/clients/sign/session-proposal pub async fn process_proposal_request( @@ -92,7 +76,15 @@ pub async fn process_proposal_request( send_session_settle_request(ctx, session, session_topic).await?; }; - send_proposal_request_response(ctx, topic, message_id, proposal.proposer.public_key).await + // Respond to incoming session propose. + let param = ResponseParamsSuccess::SessionPropose(SessionProposeResponse { + relay: ctx.relay.clone(), + responder_public_key: proposal.proposer.public_key, + }); + + ctx.publish_response_ok(topic, param, message_id).await?; + + Ok(()) } /// Process session propose reponse. diff --git a/mm2src/kdf_walletconnect/src/session/settle.rs b/mm2src/kdf_walletconnect/src/session/settle.rs index cc5c2e8f86..7a06c487db 100644 --- a/mm2src/kdf_walletconnect/src/session/settle.rs +++ b/mm2src/kdf_walletconnect/src/session/settle.rs @@ -52,7 +52,6 @@ pub(crate) async fn process_session_settle_request( session.expiry = settle.expiry; info!("Session successfully settled for topic: {:?}", topic); - info!("Updated session info: {:?}", session); } }