forked from reown-com/reown-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from KomodoPlatform/pairing-api
feat: implement Pairing API
- Loading branch information
Showing
34 changed files
with
2,918 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ Cargo.lock | |
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Oops, something went wrong.