connect_auth/
connect-auth.rs

1use alloy::signers::{
2    SignerSync, k256::ecdsa::SigningKey, local::PrivateKeySigner,
3};
4use rand::rngs::OsRng;
5use walletconnect_sdk::{connection::Connection, types::Metadata};
6
7/// This example shows how to use connect to a dApp using our wallet and use the
8/// wc_sessionAuthenticate method. This requires private key to sign the CACAO.
9#[tokio::main]
10async fn main() {
11    env_logger::init();
12
13    // ProjectId is required to prevent DOS on the relay. In case following
14    // cause rate limits, you can create your own from https://cloud.reown.com
15    let project_id = "35d44d49c2dee217a3eb24bb4410acc7";
16
17    // Used to sign JWTs. Must be generated and stored by client. Same seed
18    // should be reused for all connections.
19    let client_seed = [123u8; 32];
20
21    let conn = Connection::new(
22        "https://relay.walletconnect.org/rpc",
23        "https://relay.walletconnect.org",
24        project_id,
25        client_seed,  Metadata {
26            name: "WalletConnect Rust SDK".to_string(),
27            description: "WalletConnect Rust SDK enables to connect to relay and interact with dapp".to_string(),
28            url: "https://github.com/zemse/walletconnect-sdk".to_string(),
29            icons: vec![],
30        },
31    );
32
33    // WalletConnect URI - you can get it by visiting any dApp and clicking on
34    // "Connect Wallet" and select WalletConnect
35    let uri_from_dapp = "wc:60b429580a4c390b05661a1921a806abe0fa9891c6f38b303a519367d3aafba0@2?relay-protocol=irn&symKey=d08415aff3fb5b387b4a607ad20d5431e81e54dad759f9a658d99353a6815775&expiryTimestamp=1744387698&methods=wc_sessionAuthenticate";
36
37    let (pairing, _) = conn
38        .init_pairing(uri_from_dapp)
39        .await
40        .expect("pairing failed");
41
42    let private_key = SigningKey::random(&mut OsRng);
43    let signer = PrivateKeySigner::from(private_key);
44
45    // inspect pairing requests if it looks good
46    let (mut cacao, proposal, auth) =
47        pairing.get_proposal_old(signer.address(), 1).unwrap();
48    println!("cacao: {cacao:?}");
49    println!("proposal: {proposal:?}");
50    println!("auth: {auth:?}");
51
52    let message = cacao.caip122_message().unwrap();
53    let signature = signer.sign_message_sync(message.as_bytes()).unwrap();
54    cacao.insert_signature(signature).unwrap();
55
56    pairing.approve_with_cacao(cacao).await.unwrap();
57    // TODO there's error from dApp side "Signature verification failed"
58}