Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Sep 18, 2024
1 parent 591fbdb commit f6e7864
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 23 additions & 7 deletions mm2src/kdf_walletconnect/src/session/event.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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}};

pub enum SessionEvents {
ChainChanged(String),
AccountsChanged(String, Vec<String>),
Unknown,
Unknown(String),
}

impl SessionEvents {
Expand All @@ -21,7 +21,7 @@ impl SessionEvents {
let data = serde_json::from_value::<Vec<String>>(event.event.data)?;
Ok(SessionEvents::AccountsChanged(event.chain_id, data))
},
_ => Ok(SessionEvents::Unknown),
_ => Ok(SessionEvents::Unknown(event.event.name)),
}
}

Expand All @@ -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!(),
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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],
Expand Down
28 changes: 10 additions & 18 deletions mm2src/kdf_walletconnect/src/session/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProposeNamespaces>,
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion mm2src/kdf_walletconnect/src/session/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit f6e7864

Please sign in to comment.