Skip to content

Commit

Permalink
Merge pull request #3 from KomodoPlatform/pairing-api
Browse files Browse the repository at this point in the history
feat: implement Pairing API
  • Loading branch information
borngraced authored Dec 3, 2024
2 parents 2ed6840 + 0bd862f commit 0a5a4f7
Show file tree
Hide file tree
Showing 34 changed files with 2,918 additions and 188 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.idea
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ authors = ["WalletConnect Team"]
license = "Apache-2.0"

[workspace]
members = ["blockchain_api", "relay_client", "relay_rpc"]
members = [
"blockchain_api",
"wc_common",
"pairing_api",
"relay_client",
"relay_rpc",
]

[features]
default = ["full"]
full = ["client", "rpc", "http"]
client = ["dep:relay_client"]
http = ["relay_client/http"]
rpc = ["dep:relay_rpc"]
pairing_api = ["dep:pairing_api"]

[dependencies]
relay_client = { path = "./relay_client", optional = true }
relay_rpc = { path = "./relay_rpc", optional = true }
pairing_api = { path = "./pairing_api", optional = true }

[dev-dependencies]
anyhow = "1"
Expand All @@ -40,7 +48,7 @@ name = "http_client"
required-features = ["client", "rpc", "http"]

[[example]]
name = "ws_unmanaged"
name = "websocket_client_with_callback"
required-features = ["client", "rpc"]

[[example]]
Expand Down
130 changes: 130 additions & 0 deletions examples/websocket_client_with_callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use {
relay_client::{
error::ClientError,
websocket::{
connection_event_loop,
Client,
CloseFrame,
ConnectionHandler,
PublishedMessage,
},
ConnectionOptions,
},
relay_rpc::{
auth::{ed25519_dalek::SigningKey, AuthToken},
domain::Topic,
},
std::{sync::Arc, time::Duration},
structopt::StructOpt,
tokio::spawn,
};

#[derive(StructOpt)]
struct Args {
/// Specify WebSocket address.
#[structopt(short, long, default_value = "wss://relay.walletconnect.org")]
address: String,

/// Specify WalletConnect project ID.
#[structopt(short, long, default_value = "86e916bcbacee7f98225dde86b697f5b")]
project_id: String,
}

struct Handler {
name: &'static str,
}

impl Handler {
fn new(name: &'static str) -> Self {
Self { name }
}
}

impl ConnectionHandler for Handler {
fn connected(&mut self) {
println!("[{}] connection open", self.name);
}

fn disconnected(&mut self, frame: Option<CloseFrame<'static>>) {
println!("[{}] connection closed: frame={frame:?}", self.name);
}

fn message_received(&mut self, message: PublishedMessage) {
println!(
"[{}] inbound message: topic={} message={}",
self.name, message.topic, message.message
);
}

fn inbound_error(&mut self, error: ClientError) {
println!("[{}] inbound error: {error}", self.name);
}

fn outbound_error(&mut self, error: ClientError) {
println!("[{}] outbound error: {error}", self.name);
}
}

fn create_conn_opts(address: &str, project_id: &str) -> ConnectionOptions {
let key = SigningKey::generate(&mut rand::thread_rng());

let auth = AuthToken::new("http://example.com")
.aud(address)
.ttl(Duration::from_secs(60 * 60))
.as_jwt(&key)
.unwrap();

ConnectionOptions::new(project_id, auth).with_address(address)
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::from_args();

let handler = Handler::new("ws_test");
let (client1, _) = Client::new_with_callback(handler, |rx, handler| {
spawn(connection_event_loop(rx, handler))
});

client1
.connect(&create_conn_opts(&args.address, &args.project_id))
.await?;

let handler = Handler::new("ws_test2");
let (client2, _) = Client::new_with_callback(handler, |rx, handler| {
spawn(connection_event_loop(rx, handler))
});

client2
.connect(&create_conn_opts(&args.address, &args.project_id))
.await?;

let topic = Topic::generate();

let subscription_id = client1.subscribe(topic.clone()).await?;
println!("[client1] subscribed: topic={topic} subscription_id={subscription_id}");

client2
.publish(
topic.clone(),
Arc::from("Hello WalletConnect!"),
None,
0,
Duration::from_secs(60),
false,
)
.await?;

println!("[client2] published message with topic: {topic}",);

tokio::time::sleep(Duration::from_millis(500)).await;

drop(client1);
drop(client2);

tokio::time::sleep(Duration::from_millis(100)).await;

println!("clients disconnected");

Ok(())
}
140 changes: 0 additions & 140 deletions examples/ws_unmanaged.rs

This file was deleted.

45 changes: 45 additions & 0 deletions pairing_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "pairing_api"
version = "0.1.0"
edition = "2021"

[features]
example = ["dep:structopt", "dep:tokio", "dep:getrandom"]

[dependencies]
chrono = { version = "0.4", default-features = false, features = [
"std",
"clock",
] }
anyhow = "1.0.86"
hex = "0.4.2"
lazy_static = "1.4"
paste = "1.0.15"
rand = "0.8.5"
regex = "1.7"
relay_client = { path = "../relay_client" }
relay_rpc = { path = "../relay_rpc" }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive", "rc"] }
structopt = { version = "0.3", default-features = false, optional = true }
thiserror = "1.0"
url = "2.3"
wc_common = { path = "../wc_common" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.22", features = [
"rt",
"rt-multi-thread",
"sync",
"macros",
"time",
"signal",
], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"], optional = true }
tokio = { version = "1.22", features = ["sync", "macros"], optional = true }

[[example]]
name = "pairing"
required-features = ["example"]
Loading

0 comments on commit 0a5a4f7

Please sign in to comment.