pub struct Pairing { /* private fields */ }
Implementations§
Source§impl Pairing
impl Pairing
pub fn new(uri: &str, connection: Connection) -> Result<Self>
Sourcepub async fn init_pairing(&mut self) -> Result<WcMessage>
pub async fn init_pairing(&mut self) -> Result<WcMessage>
Initialise the pairing process
- Subscribe to the relay with the topic in the URI
- Fetch messages from the relay sent by the dapp
- Decrypt the messages
- 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
Sourcepub async fn approve_with_session_settle(
&mut self,
account_address: Address,
) -> Result<Vec<WcMessage>>
pub async fn approve_with_session_settle( &mut self, account_address: Address, ) -> Result<Vec<WcMessage>>
Approve the pairing by using wc_sessionSettle
- Create a SessionProposeResponse message on the SessionProposal message id, mention our public key in it.
- Create a SessionSettle message with the session properties encrypted using the derived symmetric key
- 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}
Sourcepub async fn approve_with_cacao(&self, cacao: Cacao) -> Result<()>
pub async fn approve_with_cacao(&self, cacao: Cacao) -> Result<()>
Approve the pairing by responding to wc_sessionAuthenticate
- 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}
Sourcepub async fn watch_messages(
&self,
topic: Topic,
dur: Option<Duration>,
) -> Result<Vec<WcMessage>>
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}
pub async fn send_message<T>(
&self,
topic: Topic,
message: &Message<String, T>,
type_byte: Option<u8>,
tag: IrnTag,
ttl: u64,
) -> Result<Value>where
T: Serialize + DeserializeOwned,
pub fn get_proposal(&self) -> Result<&WcMessage>
Sourcepub fn get_proposal_old(
&self,
account_address: Address,
chain_id: u64,
) -> Result<(Cacao, WcMessage, WcMessage)>
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§
impl StructuralPartialEq for Pairing
Auto Trait Implementations§
impl !Freeze for Pairing
impl RefUnwindSafe for Pairing
impl Send for Pairing
impl Sync for Pairing
impl Unpin for Pairing
impl UnwindSafe for Pairing
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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