diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 00139e3e82..b81d7f27c2 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -1,6 +1,7 @@ mod error; mod handler; mod inbound_message; +mod metadata; mod pairing; mod session; mod session_key; @@ -12,6 +13,7 @@ use futures::{channel::mpsc::{unbounded, UnboundedReceiver}, StreamExt}; use handler::Handler; use inbound_message::{process_inbound_request, process_inbound_response}; +use metadata::{generate_metadata, AUTH_TOKEN_SUB, PROJECT_ID, RELAY_ADDRESS}; use mm2_err_handle::prelude::MmResult; use mm2_err_handle::prelude::*; use pairing_api::{Methods, PairingClient}; @@ -21,17 +23,13 @@ use relay_client::{websocket::{Client, PublishedMessage}, 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}, + rpc::{params::{session_propose::SessionProposeRequest, IrnMetadata, RequestParams}, Params, Payload, Request, Response, SuccessfulResponse, JSON_RPC_VERSION_STR}}; -use session::{Session, SessionInfo, SessionUserType, APP_DESCRIPTION, APP_NAME}; +use session::{Session, SessionInfo, SessionType}; use session_key::SessionKey; use std::{sync::Arc, time::Duration}; use wc_common::{decode_and_decrypt_type0, encrypt_and_encode, EnvelopeType}; -const RELAY_ADDRESS: &str = "wss://relay.walletconnect.com"; -const PROJECT_ID: &str = "86e916bcbacee7f98225dde86b697f5b"; -const AUTH_TOKEN_SUB: &str = "http://127.0.0.1:8000"; - const SUPPORTED_PROTOCOL: &str = "irn"; const SUPPORTED_METHODS: &[&str] = &[ "eth_sendTransaction", @@ -114,12 +112,7 @@ impl WalletConnectCtx { } pub async fn create_pairing(&self) -> MmResult { - let metadata = Metadata { - description: APP_DESCRIPTION.to_owned(), - url: "127.0.0.1:3000".to_owned(), - icons: vec![], - name: APP_NAME.to_owned(), - }; + let metadata = generate_metadata(); let methods = Methods(vec![SUPPORTED_METHODS .iter() .map(|m| m.to_string()) @@ -145,7 +138,7 @@ impl WalletConnectCtx { session_key, topic.clone(), metadata, - SessionUserType::Proposer, + SessionType::Proposer, ); let session_proposal = RequestParams::SessionPropose(SessionProposeRequest { diff --git a/mm2src/kdf_walletconnect/src/metadata.rs b/mm2src/kdf_walletconnect/src/metadata.rs new file mode 100644 index 0000000000..777bbd6148 --- /dev/null +++ b/mm2src/kdf_walletconnect/src/metadata.rs @@ -0,0 +1,16 @@ +use relay_rpc::rpc::params::Metadata; + +pub(crate) const RELAY_ADDRESS: &str = "wss://relay.walletconnect.com"; +pub(crate) const PROJECT_ID: &str = "86e916bcbacee7f98225dde86b697f5b"; +pub(crate) const AUTH_TOKEN_SUB: &str = "http://127.0.0.1:8000"; +pub(crate) const APP_NAME: &str = "Komodefi Framework"; +pub(crate) const APP_DESCRIPTION: &str = "WallectConnect Komodefi Framework Playground"; + +pub(crate) fn generate_metadata() -> Metadata { + Metadata { + description: APP_DESCRIPTION.to_owned(), + url: AUTH_TOKEN_SUB.to_owned(), + icons: vec![], + name: APP_NAME.to_owned(), + } +} diff --git a/mm2src/kdf_walletconnect/src/session.rs b/mm2src/kdf_walletconnect/src/session.rs index b6bb696da5..cc30867946 100644 --- a/mm2src/kdf_walletconnect/src/session.rs +++ b/mm2src/kdf_walletconnect/src/session.rs @@ -19,15 +19,13 @@ use std::collections::HashMap; use std::ops::Deref; 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>; #[derive(Debug, Clone)] -pub enum SessionUserType { +pub enum SessionType { Controller, Proposer, } @@ -45,7 +43,7 @@ pub struct SessionInfo { pub settled_namespaces: SettleNamespaces, pub expiry: u64, pub pairing_topic: Topic, - pub session_type: SessionUserType, + pub session_type: SessionType, } impl SessionInfo { @@ -54,7 +52,7 @@ impl SessionInfo { session_key: SessionKey, pairing_topic: Topic, metadata: Metadata, - session_type: SessionUserType, + session_type: SessionType, ) -> Self { // Initialize the namespaces for both proposer and controller let mut namespaces = BTreeMap::::new(); @@ -78,14 +76,14 @@ impl SessionInfo { // handle proposer or controller let (proposer, controller) = match session_type { - SessionUserType::Proposer => ( + SessionType::Proposer => ( Proposer { public_key: hex::encode(session_key.diffie_public_key()), metadata, }, Controller::default(), ), - SessionUserType::Controller => (Proposer::default(), Controller { + SessionType::Controller => (Proposer::default(), Controller { public_key: hex::encode(session_key.diffie_public_key()), metadata, }), @@ -106,7 +104,9 @@ impl SessionInfo { } fn supported_propose_namespaces(&self) -> &ProposeNamespaces { &self.namespaces } + fn supported_settle_namespaces(&self) -> &SettleNamespaces { &self.settled_namespaces } + fn create_settle_request(&self) -> RequestParams { RequestParams::SessionSettle(SessionSettleRequest { relay: self.relay.clone(), @@ -115,6 +115,7 @@ impl SessionInfo { expiry: Utc::now().timestamp() as u64 + FIVE_MINUTES, }) } + fn create_proposal_response(&self) -> Result<(Value, IrnMetadata), WalletConnectCtxError> { let response = ResponseParamsSuccess::SessionPropose(SessionProposeResponse { relay: self.relay.clone(), @@ -210,18 +211,12 @@ impl Session { .await .map_to_mm(|err| WalletConnectCtxError::SubscriptionError(err.to_string()))?; - let metadata = Metadata { - description: APP_DESCRIPTION.to_owned(), - url: "127.0.0.1:3000".to_owned(), - icons: vec![], - name: APP_NAME.to_owned(), - }; let session = SessionInfo::new( subscription_id, session_key, pairing_topic, - metadata, - SessionUserType::Controller, + proposal.proposer.metadata, + SessionType::Controller, ); session .supported_propose_namespaces()