Skip to content

Commit

Permalink
minor changes - create metadata mod
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Sep 13, 2024
1 parent 437767b commit 6374da3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
19 changes: 6 additions & 13 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod error;
mod handler;
mod inbound_message;
mod metadata;
mod pairing;
mod session;
mod session_key;
Expand All @@ -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};
Expand All @@ -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",
Expand Down Expand Up @@ -114,12 +112,7 @@ impl WalletConnectCtx {
}

pub async fn create_pairing(&self) -> MmResult<String, WalletConnectCtxError> {
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())
Expand All @@ -145,7 +138,7 @@ impl WalletConnectCtx {
session_key,
topic.clone(),
metadata,
SessionUserType::Proposer,
SessionType::Proposer,
);

let session_proposal = RequestParams::SessionPropose(SessionProposeRequest {
Expand Down
16 changes: 16 additions & 0 deletions mm2src/kdf_walletconnect/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}
25 changes: 10 additions & 15 deletions mm2src/kdf_walletconnect/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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 {
Expand All @@ -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::<String, ProposeNamespace>::new();
Expand All @@ -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,
}),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6374da3

Please sign in to comment.