Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Sep 12, 2024
1 parent 60a2fc5 commit fd8797c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 91 deletions.
81 changes: 81 additions & 0 deletions mm2src/kdf_walletconnect/src/inbound_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::sync::Arc;

use mm2_err_handle::prelude::{MmError, MmResult};
use relay_rpc::{domain::Topic,
rpc::{params::ResponseParamsSuccess, Params, Request, Response}};

use crate::{error::WalletConnectCtxError,
pairing::{process_pairing_delete_response, process_pairing_extend_response, process_pairing_ping_response},
WalletConnectCtx};

pub(crate) async fn process_inbound_request(
ctx: Arc<WalletConnectCtx>,
request: Request,
topic: &Topic,
) -> MmResult<(), WalletConnectCtxError> {
let response = match request.params {
Params::SessionPropose(proposal) => {
ctx.session
.process_proposal_request(&ctx, proposal, topic.clone())
.await
},
Params::SessionExtend(param) => ctx.session.process_session_extend_request(topic, param).await,
Params::SessionDelete(param) => ctx.session.process_session_delete_request(param),
Params::SessionPing(()) => ctx.session.process_session_ping_request(),
Params::SessionSettle(param) => ctx.session.process_session_settle_request(topic, param).await,
Params::SessionUpdate(param) => ctx.session.process_session_update_request(topic, param).await,
Params::SessionRequest(_) => todo!(),
Params::SessionEvent(_) => todo!(),

Params::PairingPing(_param) => process_pairing_ping_response().await,
Params::PairingDelete(param) => process_pairing_delete_response(&ctx, topic, param).await,
Params::PairingExtend(param) => process_pairing_extend_response(&ctx, topic, param).await,
_ => todo!(),
}?;

ctx.publish_response(topic, response.0, response.1, request.id).await?;

// ctx.session.session_delete_cleanup(ctx.clone(), topic).await?

Ok(())
}

pub(crate) async fn process_inbound_response(
ctx: Arc<WalletConnectCtx>,
response: Response,
topic: &Topic,
) -> MmResult<(), WalletConnectCtxError> {
match response {
Response::Success(value) => {
let params = serde_json::from_value::<ResponseParamsSuccess>(value.result)?;
match params {
ResponseParamsSuccess::SessionPropose(param) => {
ctx.session.handle_session_propose_response(topic, param).await;
Ok(())
},
ResponseParamsSuccess::SessionSettle(success)
| ResponseParamsSuccess::SessionUpdate(success)
| ResponseParamsSuccess::SessionExtend(success)
| ResponseParamsSuccess::SessionRequest(success)
| ResponseParamsSuccess::SessionEvent(success)
| ResponseParamsSuccess::SessionDelete(success)
| ResponseParamsSuccess::SessionPing(success)
| ResponseParamsSuccess::PairingExtend(success)
| ResponseParamsSuccess::PairingDelete(success)
| ResponseParamsSuccess::PairingPing(success) => {
if !success {
return MmError::err(WalletConnectCtxError::UnsuccessfulResponse(format!(
"Unsuccessful response={params:?}"
)));
}

Ok(())
},
}
},
Response::Error(err) => {
println!("Error: {err:?}");
todo!()
},
}
}
89 changes: 7 additions & 82 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
mod error;
mod handler;
mod inbound_message;
mod pairing;
mod session;
mod session_key;

use common::log::info;
use common::{executor::Timer, log::info};
use error::WalletConnectCtxError;
use futures::{channel::mpsc::{unbounded, UnboundedReceiver},
lock::Mutex,
StreamExt};
use handler::Handler;
use inbound_message::{process_inbound_request, process_inbound_response};
use mm2_err_handle::prelude::MmResult;
use mm2_err_handle::prelude::*;
use pairing::{process_pairing_delete_response, process_pairing_extend_response, process_pairing_ping_response};
use pairing_api::{Methods, PairingClient};
use rand::rngs::OsRng;
use relay_client::{websocket::{Client, PublishedMessage},
ConnectionOptions, MessageIdGenerator};
use relay_rpc::rpc::params::RelayProtocolMetadata;
use relay_rpc::{auth::{ed25519_dalek::SigningKey, AuthToken},
domain::{MessageId, Topic},
rpc::{params::{session_propose::SessionProposeRequest, IrnMetadata, Metadata, RequestParams,
ResponseParamsSuccess},
rpc::{params::{session_propose::SessionProposeRequest, IrnMetadata, Metadata, RequestParams},
Params, Payload, Request, Response, SuccessfulResponse, JSON_RPC_VERSION_STR}};
use session::{Session, SessionInfo, SessionUserType, APP_DESCRIPTION, APP_NAME};
use session_key::SessionKey;
Expand Down Expand Up @@ -86,13 +86,11 @@ impl WalletConnectCtx {
let opts = ConnectionOptions::new(PROJECT_ID, auth).with_address(RELAY_ADDRESS);
self.client.connect(&opts).await?;

// let is_connected = self.client.;
info!("WC connected");

Ok(())
}

// todo: return slice
async fn sym_key(&self, topic: &Topic) -> MmResult<Vec<u8>, WalletConnectCtxError> {
{
let sessions = self.session.lock().await;
Expand Down Expand Up @@ -126,6 +124,7 @@ impl WalletConnectCtx {
.iter()
.map(|m| m.to_string())
.collect::<Vec<_>>()]);

let (topic, url) = self
.pairing
.create(metadata.clone(), Some(methods), &self.client)
Expand Down Expand Up @@ -155,7 +154,6 @@ impl WalletConnectCtx {
required_namespaces: session.namespaces.clone(),
});

// Store the session information in your session manager (self.sessions or similar)
{
let mut sessions = self.session.lock().await;
sessions.insert(session_topic.clone(), session);
Expand Down Expand Up @@ -249,7 +247,7 @@ impl WalletConnectCtx {
decode_and_decrypt_type0(msg.message.as_bytes(), &key).unwrap()
};

println!("\nInbound message payload={message}");
info!("\nInbound message payload={message}");

let response = serde_json::from_str::<Payload>(&message).unwrap();
let result = match response {
Expand All @@ -270,83 +268,10 @@ impl WalletConnectCtx {
info!("connection disconnected, reconnecting");
if let Err(err) = self.connect_client().await {
common::log::error!("{err:?}");
Timer::sleep(5.).await;
continue;
};
info!("reconnecting success!");
}
}
}

async fn process_inbound_request(
ctx: Arc<WalletConnectCtx>,
request: Request,
topic: &Topic,
) -> MmResult<(), WalletConnectCtxError> {
let response = match request.params {
Params::SessionPropose(proposal) => {
ctx.session
.process_proposal_request(&ctx, proposal, topic.clone())
.await
},
Params::SessionExtend(param) => ctx.session.process_session_extend_request(topic, param).await,
Params::SessionDelete(param) => ctx.session.process_session_delete_request(param),
Params::SessionPing(()) => ctx.session.process_session_ping_request(),
Params::SessionSettle(param) => ctx.session.process_session_settle_request(topic, param).await,
Params::SessionUpdate(param) => ctx.session.process_session_update_request(topic, param).await,
Params::SessionRequest(_) => todo!(),
Params::SessionEvent(_) => todo!(),

Params::PairingPing(_param) => process_pairing_ping_response().await,
Params::PairingDelete(param) => process_pairing_delete_response(&ctx, topic, param).await,
Params::PairingExtend(param) => process_pairing_extend_response(&ctx, topic, param).await,
_ => todo!(),
}?;

info!("Publishing reponse");
ctx.publish_response(topic, response.0, response.1, request.id).await?;

// todo
// ctx.session.session_delete_cleanup(ctx.clone(), topic).await?

Ok(())
}

async fn process_inbound_response(
ctx: Arc<WalletConnectCtx>,
response: Response,
topic: &Topic,
) -> MmResult<(), WalletConnectCtxError> {
match response {
Response::Success(value) => {
let params = serde_json::from_value::<ResponseParamsSuccess>(value.result)?;
match params {
ResponseParamsSuccess::SessionPropose(param) => {
ctx.session.handle_session_propose_response(topic, param).await;
Ok(())
},
ResponseParamsSuccess::SessionSettle(success)
| ResponseParamsSuccess::SessionUpdate(success)
| ResponseParamsSuccess::SessionExtend(success)
| ResponseParamsSuccess::SessionRequest(success)
| ResponseParamsSuccess::SessionEvent(success)
| ResponseParamsSuccess::SessionDelete(success)
| ResponseParamsSuccess::SessionPing(success)
| ResponseParamsSuccess::PairingExtend(success)
| ResponseParamsSuccess::PairingDelete(success)
| ResponseParamsSuccess::PairingPing(success) => {
if !success {
return MmError::err(WalletConnectCtxError::UnsuccessfulResponse(format!(
"Unsuccessful response={params:?}"
)));
}

Ok(())
},
}
},
Response::Error(err) => {
println!("Error: {err:?}");
todo!()
},
}
}
10 changes: 6 additions & 4 deletions mm2src/kdf_walletconnect/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::{collections::BTreeMap, sync::Arc};

pub(crate) const APP_NAME: &str = "Komodefi Framework";
pub(crate) const APP_DESCRIPTION: &str = "WallectConnect Komodefi Framework Playground";
const FIVE_MINUTES: u64 = 300;
const THIRTY_DAYS: u64 = 60 * 60 * 30;

pub(crate) type WcRequestResult = MmResult<(Value, IrnMetadata), WalletConnectCtxError>;

Expand Down Expand Up @@ -69,13 +71,12 @@ impl SessionInfo {
events: SUPPORTED_EVENTS.iter().map(|e| e.to_string()).collect(),
});

// Initialize relay
let relay = Relay {
protocol: SUPPORTED_PROTOCOL.to_string(),
data: None,
};

// Conditional logic to handle proposer or controller
// handle proposer or controller
let (proposer, controller) = match session_type {
SessionUserType::Proposer => (
Proposer {
Expand All @@ -98,7 +99,7 @@ impl SessionInfo {
namespaces: ProposeNamespaces(namespaces),
settled_namespaces: SettleNamespaces(settled_namespaces),
relay,
expiry: Utc::now().timestamp() as u64 + 300,
expiry: Utc::now().timestamp() as u64 + FIVE_MINUTES,
pairing_topic,
session_type,
}
Expand All @@ -111,7 +112,7 @@ impl SessionInfo {
relay: self.relay.clone(),
controller: self.controller.clone(),
namespaces: self.supported_settle_namespaces().clone(),
expiry: Utc::now().timestamp() as u64 + 300, // 5 min TTL
expiry: Utc::now().timestamp() as u64 + FIVE_MINUTES,
})
}
fn create_proposal_response(&self) -> Result<(Value, IrnMetadata), WalletConnectCtxError> {
Expand Down Expand Up @@ -164,6 +165,7 @@ impl Session {
info!("session found!");
session.proposer.public_key = response.responder_public_key;
session.relay = response.relay;
session.expiry = Utc::now().timestamp() + THIRTY_DAYS;
};
}

Expand Down
14 changes: 9 additions & 5 deletions mm2src/mm2_test_helpers/src/for_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,7 @@ pub fn nft_dev_conf() -> Json {
})
}

fn set_chain_id(conf: &mut Json, chain_id: u64) {
conf["chain_id"] = json!(chain_id);
}
fn set_chain_id(conf: &mut Json, chain_id: u64) { conf["chain_id"] = json!(chain_id); }

pub fn eth_sepolia_conf() -> Json {
json!({
Expand Down Expand Up @@ -2903,7 +2901,10 @@ pub async fn enable_tendermint(
tx_history: bool,
) -> Json {
let ibc_requests: Vec<_> = ibc_assets.iter().map(|ticker| json!({ "ticker": ticker })).collect();
let nodes: Vec<Json> = rpc_urls.iter().map(|u| json!({"url": u, "komodo_proxy": false })).collect();
let nodes: Vec<Json> = rpc_urls
.iter()
.map(|u| json!({"url": u, "komodo_proxy": false }))
.collect();

let request = json!({
"userpass": mm.userpass,
Expand Down Expand Up @@ -2940,7 +2941,10 @@ pub async fn enable_tendermint_without_balance(
tx_history: bool,
) -> Json {
let ibc_requests: Vec<_> = ibc_assets.iter().map(|ticker| json!({ "ticker": ticker })).collect();
let nodes: Vec<Json> = rpc_urls.iter().map(|u| json!({"url": u, "komodo_proxy": false })).collect();
let nodes: Vec<Json> = rpc_urls
.iter()
.map(|u| json!({"url": u, "komodo_proxy": false }))
.collect();

let request = json!({
"userpass": mm.userpass,
Expand Down

0 comments on commit fd8797c

Please sign in to comment.