Pairing

Struct Pairing 

Source
pub struct Pairing { /* private fields */ }

Implementations§

Source§

impl Pairing

Source

pub fn new(uri: &str, connection: Connection) -> Result<Self>

Source

pub async fn init_pairing(&mut self) -> Result<WcMessage>

Initialise the pairing process

  1. Subscribe to the relay with the topic in the URI
  2. Fetch messages from the relay sent by the dapp
  3. Decrypt the messages
  4. Check if the first message is a session_propose and the second is a session_authenticate

To continue the process further, use the approve method

Source

pub async fn approve_with_session_settle( &mut self, account_address: Address, ) -> Result<Vec<WcMessage>>

Approve the pairing by using wc_sessionSettle

  1. Create a SessionProposeResponse message on the SessionProposal message id, mention our public key in it.
  2. Create a SessionSettle message with the session properties encrypted using the derived symmetric key
  3. Also subscribe to the topic derived from the symmetric key to receive messages from the dappcxd
Examples found in repository?
examples/connect-settle.rs (lines 41-45)
8async fn main() {
9    env_logger::init();
10
11    // ProjectId is required to prevent DOS on the relay. In case following
12    // cause rate limits, you can create your own from https://cloud.reown.com
13    let project_id = "35d44d49c2dee217a3eb24bb4410acc7";
14
15    // Used to sign JWTs. Must be generated and stored by client. Same seed
16    // should be reused for all connections.
17    let client_seed = [123u8; 32];
18
19    let conn = Connection::new(
20        "https://relay.walletconnect.org/rpc",
21        "https://relay.walletconnect.org",
22        project_id,
23        client_seed,  Metadata {
24            name: "WalletConnect Rust SDK".to_string(),
25            description: "WalletConnect Rust SDK enables to connect to relay and interact with dapp".to_string(),
26            url: "https://github.com/zemse/walletconnect-sdk".to_string(),
27            icons: vec![],
28        },
29    );
30
31    // WalletConnect URI - you can get it by visiting any dApp and clicking on
32    // "Connect Wallet" and select WalletConnect
33    let uri_from_dapp = "wc:e4b9eb7a1372bf88abc46c37acac3687301afdfd0d2a4c2355945d66a1164464@2?relay-protocol=irn&symKey=d7430284e1b70853829a010518a088cde0e163bcad5f24425e3b17578b2b402d&expiryTimestamp=1749783095&methods=wc_sessionAuthenticate";
34
35    let (mut pairing, _) = conn
36        .init_pairing(uri_from_dapp)
37        .await
38        .expect("pairing failed");
39
40    pairing
41        .approve_with_session_settle(
42            "0x0000000000000000000000000000000000000123"
43                .parse()
44                .unwrap(),
45        )
46        .await
47        .expect("approve failed");
48
49    loop {
50        let result =
51            pairing.watch_messages(Topic::Derived, None).await.unwrap();
52
53        println!("result: {result:?}");
54    }
55}
Source

pub async fn approve_with_cacao(&self, cacao: Cacao) -> Result<()>

Approve the pairing by responding to wc_sessionAuthenticate

  1. Create a SessionAuthenticateResponse message on the SessionAuthenticate message id. Encrypt the message using derived symetric key and mention our public key by using the type 1 envelope.
Examples found in repository?
examples/connect-auth.rs (line 56)
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}
Source

pub async fn watch_messages( &self, topic: Topic, dur: Option<Duration>, ) -> Result<Vec<WcMessage>>

Examples found in repository?
examples/connect-settle.rs (line 51)
8async fn main() {
9    env_logger::init();
10
11    // ProjectId is required to prevent DOS on the relay. In case following
12    // cause rate limits, you can create your own from https://cloud.reown.com
13    let project_id = "35d44d49c2dee217a3eb24bb4410acc7";
14
15    // Used to sign JWTs. Must be generated and stored by client. Same seed
16    // should be reused for all connections.
17    let client_seed = [123u8; 32];
18
19    let conn = Connection::new(
20        "https://relay.walletconnect.org/rpc",
21        "https://relay.walletconnect.org",
22        project_id,
23        client_seed,  Metadata {
24            name: "WalletConnect Rust SDK".to_string(),
25            description: "WalletConnect Rust SDK enables to connect to relay and interact with dapp".to_string(),
26            url: "https://github.com/zemse/walletconnect-sdk".to_string(),
27            icons: vec![],
28        },
29    );
30
31    // WalletConnect URI - you can get it by visiting any dApp and clicking on
32    // "Connect Wallet" and select WalletConnect
33    let uri_from_dapp = "wc:e4b9eb7a1372bf88abc46c37acac3687301afdfd0d2a4c2355945d66a1164464@2?relay-protocol=irn&symKey=d7430284e1b70853829a010518a088cde0e163bcad5f24425e3b17578b2b402d&expiryTimestamp=1749783095&methods=wc_sessionAuthenticate";
34
35    let (mut pairing, _) = conn
36        .init_pairing(uri_from_dapp)
37        .await
38        .expect("pairing failed");
39
40    pairing
41        .approve_with_session_settle(
42            "0x0000000000000000000000000000000000000123"
43                .parse()
44                .unwrap(),
45        )
46        .await
47        .expect("approve failed");
48
49    loop {
50        let result =
51            pairing.watch_messages(Topic::Derived, None).await.unwrap();
52
53        println!("result: {result:?}");
54    }
55}
Source

pub async fn send_message<T>( &self, topic: Topic, message: &Message<String, T>, type_byte: Option<u8>, tag: IrnTag, ttl: u64, ) -> Result<Value>

Source

pub fn get_proposal(&self) -> Result<&WcMessage>

Source

pub fn get_proposal_old( &self, account_address: Address, chain_id: u64, ) -> Result<(Cacao, WcMessage, WcMessage)>

Examples found in repository?
examples/connect-auth.rs (line 47)
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}

Trait Implementations§

Source§

impl Clone for Pairing

Source§

fn clone(&self) -> Pairing

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Pairing

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Pairing

Source§

fn eq(&self, other: &Pairing) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Pairing

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryClone for T
where T: Clone,

Source§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,