|
| 1 | +use { |
| 2 | + futures_util::TryStreamExt as _, |
| 3 | + relay_client::{ |
| 4 | + error::ClientError, |
| 5 | + websocket::{Client, CloseFrame, ConnectionHandler, PublishedMessage}, |
| 6 | + ConnectionOptions, |
| 7 | + }, |
| 8 | + relay_rpc::{ |
| 9 | + auth::{ed25519_dalek::SigningKey, AuthToken}, |
| 10 | + domain::Topic, |
| 11 | + }, |
| 12 | + std::time::Duration, |
| 13 | + structopt::StructOpt, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(StructOpt)] |
| 17 | +struct Args { |
| 18 | + /// Specify WebSocket address. |
| 19 | + #[structopt(short, long, default_value = "wss://relay.walletconnect.com")] |
| 20 | + address: String, |
| 21 | + |
| 22 | + /// Specify WalletConnect project ID. |
| 23 | + #[structopt(short, long, default_value = "3cbaa32f8fbf3cdcc87d27ca1fa68069")] |
| 24 | + project_id: String, |
| 25 | +} |
| 26 | + |
| 27 | +struct Handler { |
| 28 | + name: &'static str, |
| 29 | +} |
| 30 | + |
| 31 | +impl Handler { |
| 32 | + fn new(name: &'static str) -> Self { |
| 33 | + Self { name } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl ConnectionHandler for Handler { |
| 38 | + fn connected(&mut self) { |
| 39 | + println!("[{}] connection open", self.name); |
| 40 | + } |
| 41 | + |
| 42 | + fn disconnected(&mut self, frame: Option<CloseFrame<'static>>) { |
| 43 | + println!("[{}] connection closed: frame={frame:?}", self.name); |
| 44 | + } |
| 45 | + |
| 46 | + fn message_received(&mut self, message: PublishedMessage) { |
| 47 | + println!( |
| 48 | + "[{}] inbound message: topic={} message={}", |
| 49 | + self.name, message.topic, message.message |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + fn inbound_error(&mut self, error: ClientError) { |
| 54 | + println!("[{}] inbound error: {error}", self.name); |
| 55 | + } |
| 56 | + |
| 57 | + fn outbound_error(&mut self, error: ClientError) { |
| 58 | + println!("[{}] outbound error: {error}", self.name); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn create_conn_opts(address: &str, project_id: &str) -> ConnectionOptions { |
| 63 | + let key = SigningKey::generate(&mut rand::thread_rng()); |
| 64 | + |
| 65 | + let auth = AuthToken::new("http://example.com") |
| 66 | + .aud(address) |
| 67 | + .ttl(Duration::from_secs(60 * 60)) |
| 68 | + .as_jwt(&key) |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + ConnectionOptions::new(project_id, auth).with_address(address) |
| 72 | +} |
| 73 | + |
| 74 | +#[tokio::main] |
| 75 | +async fn main() -> anyhow::Result<()> { |
| 76 | + let args = Args::from_args(); |
| 77 | + |
| 78 | + let app_client = Client::new(Handler::new("client1")); |
| 79 | + app_client |
| 80 | + .connect(&create_conn_opts(&args.address, &args.project_id)) |
| 81 | + .await?; |
| 82 | + |
| 83 | + let wallet_client = Client::new(Handler::new("client2")); |
| 84 | + wallet_client |
| 85 | + .connect(&create_conn_opts(&args.address, &args.project_id)) |
| 86 | + .await?; |
| 87 | + |
| 88 | + // Pre-generate topics, while the actual clients would derive them from the keys |
| 89 | + // exchanged during pairing: |
| 90 | + let pairing_topic = Topic::generate(); |
| 91 | + let session_topic = Topic::generate(); |
| 92 | + |
| 93 | + // App proposes session: |
| 94 | + app_client |
| 95 | + .propose_session( |
| 96 | + pairing_topic.clone(), |
| 97 | + "wc_sessionPropose_req", |
| 98 | + Some("attestation".into()), |
| 99 | + ) |
| 100 | + .await?; |
| 101 | + println!("[client1] proposed session: pairing_topic={pairing_topic}"); |
| 102 | + |
| 103 | + // Wallet scans the QR code and receives the `wc_sessionPropose` request: |
| 104 | + let msg = wallet_client |
| 105 | + .fetch_stream([pairing_topic.clone()]) |
| 106 | + .try_collect::<Vec<_>>() |
| 107 | + .await? |
| 108 | + .pop() |
| 109 | + .unwrap(); |
| 110 | + println!("[client2] received session proposal: {msg:?}"); |
| 111 | + |
| 112 | + // After user confirmation, the wallet approves this session: |
| 113 | + wallet_client |
| 114 | + .approve_session( |
| 115 | + pairing_topic.clone(), |
| 116 | + session_topic.clone(), |
| 117 | + "wc_sessionPropose_res", |
| 118 | + "wc_sessionSettle_req", |
| 119 | + ) |
| 120 | + .await?; |
| 121 | + println!( |
| 122 | + "[client2] approved session: pairing_topic={pairing_topic} session_topic={session_topic}" |
| 123 | + ); |
| 124 | + |
| 125 | + // App receives `wc_sessionPropose` response, derives `session_topic` and |
| 126 | + // subscribes to it: |
| 127 | + app_client.subscribe(session_topic.clone()).await?; |
| 128 | + |
| 129 | + tokio::time::sleep(Duration::from_millis(500)).await; |
| 130 | + |
| 131 | + // App responds to the `wc_sessionSettle`: |
| 132 | + app_client |
| 133 | + .publish( |
| 134 | + session_topic.clone(), |
| 135 | + "wc_sessionSettle_res", |
| 136 | + None, |
| 137 | + 1103, |
| 138 | + Duration::from_secs(300), |
| 139 | + false, |
| 140 | + ) |
| 141 | + .await?; |
| 142 | + println!("[client1] published `wc_sessionSettle` response: session_topic={session_topic}"); |
| 143 | + |
| 144 | + tokio::time::sleep(Duration::from_millis(1000)).await; |
| 145 | + |
| 146 | + drop(app_client); |
| 147 | + drop(wallet_client); |
| 148 | + |
| 149 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 150 | + |
| 151 | + println!("clients disconnected"); |
| 152 | + |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments