-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: watch rpc types; http client (#22)
- Loading branch information
Showing
24 changed files
with
1,667 additions
and
578 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
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,72 @@ | ||
use { | ||
relay_client::{http::Client, ConnectionOptions}, | ||
relay_rpc::{ | ||
auth::{ed25519_dalek::Keypair, rand, 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: &Keypair, 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 = Keypair::generate(&mut rand::thread_rng()); | ||
let client1 = Client::new(&create_conn_opts(&key1, &args.address, &args.project_id))?; | ||
|
||
let key2 = Keypair::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(), | ||
1100, | ||
Duration::from_secs(30), | ||
) | ||
.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 | ||
.get(0) | ||
.ok_or(anyhow::anyhow!("fetch did not return any messages"))?; | ||
|
||
println!("[client2] received message: {}", message.message); | ||
|
||
Ok(()) | ||
} |
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
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
Oops, something went wrong.