forked from reown-com/reown-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.rs
74 lines (59 loc) · 2.02 KB
/
http_client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use {
relay_client::{http::Client, ConnectionOptions},
relay_rpc::{
auth::{ed25519_dalek::SigningKey, AuthToken},
domain::Topic,
},
std::{sync::Arc, time::Duration},
structopt::StructOpt,
url::Url,
};
#[derive(StructOpt)]
struct Args {
/// Specify HTTP address.
#[structopt(short, long, default_value = "https://relay.walletconnect.com/rpc")]
address: String,
/// Specify WalletConnect project ID.
#[structopt(short, long, default_value = "3cbaa32f8fbf3cdcc87d27ca1fa68069")]
project_id: String,
}
fn create_conn_opts(key: &SigningKey, address: &str, project_id: &str) -> ConnectionOptions {
let aud = Url::parse(address)
.unwrap()
.origin()
.unicode_serialization();
let auth = AuthToken::new("http://example.com")
.aud(aud)
.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 key1 = SigningKey::generate(&mut rand::thread_rng());
let client1 = Client::new(&create_conn_opts(&key1, &args.address, &args.project_id))?;
let key2 = SigningKey::generate(&mut rand::thread_rng());
let client2 = Client::new(&create_conn_opts(&key2, &args.address, &args.project_id))?;
let topic = Topic::generate();
let message: Arc<str> = Arc::from("Hello WalletConnect!");
client1
.publish(
topic.clone(),
message.clone(),
None,
1100,
Duration::from_secs(30),
false,
)
.await?;
println!("[client1] published message with topic: {topic}",);
tokio::time::sleep(Duration::from_secs(1)).await;
let messages = client2.fetch(topic).await?.messages;
let message = messages
.first()
.ok_or(anyhow::anyhow!("fetch did not return any messages"))?;
println!("[client2] received message: {}", message.message);
Ok(())
}