walletconnect-sdk 0.2.3

Implementation of WalletConnect Specifications in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/// Pairing
///
/// Implementation of walletconnect specs to pair with a dApp and fetch messages.
///
use std::time::Duration;
use std::{str, thread};

use alloy::hex;
use alloy::primitives::Address;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;

use crate::cacao::Cacao;
use crate::connection::Connection;
use crate::error::{Error, Result};
use crate::message::Message;
use crate::types::{
    EncryptedMessage, IrnTag, Namespace, Participant, Relay,
    SessionAuthenticateResponse, SessionProposeResponse, SessionSettleParams,
};
use crate::utils::{
    DAYS, UriParameters, derive_sym_key, random_bytes32, sha256, unix_timestamp,
};
use crate::wc_message::{WcData, WcMessage, WcMethod};

#[derive(Debug, Clone, Copy)]
pub enum Topic {
    // We get this topic from the URI scanned from QR code
    Initial,
    // This is hash of the dapp's public key, used during handshake
    Response,
    // This is using derived symmetric key from both public keys
    Derived,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Pairing {
    private_key: [u8; 32],
    params: UriParameters,
    connection: Connection,
    proposal_request: Option<WcMessage>,
    authenticate_request: Option<WcMessage>,
    approve_done: bool,
}

impl Pairing {
    pub fn new(uri: &str, connection: Connection) -> crate::Result<Self> {
        let params = UriParameters::try_from(uri.to_string())?;
        Ok(Self {
            // Generate a fresh private key for the pairing
            private_key: random_bytes32(),
            params,
            connection,
            proposal_request: None,
            authenticate_request: None,
            approve_done: false,
        })
    }

    /// 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
    pub async fn init_pairing(&mut self) -> Result<WcMessage> {
        self.subscribe(Topic::Initial).await?;

        let messages = self.fetch_messages(Topic::Initial).await?;

        if messages.is_empty() {
            return Err(
                "Please generate a fresh WalletConnect URI from the dApp"
                    .into(),
            );
        }

        if messages.len() == 1 {
            // Sometimes dApps send us just the SessionPropose message
            if messages[0].method() != Some(WcMethod::SessionPropose) {
                return Err("Message is not SessionPropose".into());
            }

            self.proposal_request = Some(messages[0].clone());
            Ok(messages[0].clone())
        } else if messages.len() == 2 {
            // Sometimes dApps send us both SessionPropose and SessionAuthenticate messages
            let proposal_request = messages
                .iter()
                .find(|m| m.method() == Some(WcMethod::SessionPropose))
                .ok_or("SessionPropose message not found")?;

            let authenticate_request = messages
                .iter()
                .find(|m| m.method() == Some(WcMethod::SessionAuthenticate))
                .ok_or("SessionAuthenticate message not found")?;

            self.proposal_request = Some(proposal_request.clone());
            self.authenticate_request = Some(authenticate_request.clone());

            {
                let proposal_request =
                    proposal_request.data.as_session_propose().ok_or(
                        crate::Error::InternalError2("not session propose"),
                    )?;
                let authenticate_request = authenticate_request
                    .data
                    .as_session_authenticate()
                    .ok_or(crate::Error::InternalError2(
                        "not session authenticate",
                    ))?;
                assert_eq!(
                    proposal_request.proposer.public_key,
                    authenticate_request.requester.public_key,
                    "proposer and requester public keys are not equal - {messages:?}"
                );
            }
            Ok(proposal_request.clone())
        } else {
            Err(format!(
                "Expected 1 or 2 messages, got {}: {messages:?}",
                messages.len()
            )
            .into())
        }
    }

    /// 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
    pub async fn approve_with_session_settle(
        &mut self,
        account_address: Address,
    ) -> Result<Vec<WcMessage>> {
        let proposal = self.get_proposal()?;
        let response = proposal.create_response(
            WcData::SessionProposeResponse(SessionProposeResponse {
                relay: Relay {
                    protocol: "irn".to_string(),
                },
                responder_public_key: self.public_key(),
            }),
            None,
        );

        self.send_message(
            Topic::Initial,
            &response.into_raw()?,
            Some(0),
            IrnTag::SessionProposeApproveResponse,
            3600,
        )
        .await?;

        self.subscribe(Topic::Derived).await?;

        let proposal =
            proposal.data.as_session_propose().ok_or("not proposal")?;

        let session_settle =
            self.new_message(WcData::SessionSettle(SessionSettleParams {
                controller: self.participant(),
                expiry: unix_timestamp()? + 10 * DAYS,
                namespaces: proposal
                    .required_namespaces
                    .clone()
                    .into_iter()
                    .chain(proposal.optional_namespaces.clone())
                    .map(|(name, n)| {
                        (
                            name,
                            Namespace {
                                accounts: Some(
                                    n.chains
                                        .iter()
                                        .map(|c| {
                                            format!("{c}:{account_address}")
                                        })
                                        .collect(),
                                ),
                                chains: n.chains,
                                events: n.events,
                                methods: vec![
                                    "personal_sign".to_string(),
                                    "eth_sendTransaction".to_string(),
                                    "eth_signTypedData_v4".to_string(),
                                ],
                            },
                        )
                    })
                    .collect(),
                relay: Relay {
                    protocol: "irn".to_string(),
                },
                session_properties: None,
            }))?;

        self.send_message(
            Topic::Derived,
            &session_settle,
            Some(0),
            IrnTag::SessionSettle,
            3600,
        )
        .await?;

        let mut excess_messages = vec![];
        let mut success = false;

        loop {
            let mut messages = self.fetch_messages(Topic::Derived).await?;
            let mut rm_idx = None;
            for (i, msg) in messages.iter().enumerate() {
                if msg.id == session_settle.id {
                    let result = msg.data.as_result::<bool>();

                    if let Some(result) = result {
                        success = result;
                    }

                    rm_idx = Some(i);
                }
            }
            if let Some(i) = rm_idx {
                messages.remove(i);
            }
            excess_messages.extend(messages);
            if rm_idx.is_some() {
                break;
            }
        }

        if success {
            self.approve_done = true;
            Ok(excess_messages)
        } else {
            Err(crate::Error::PairingNotApproved)
        }
    }

    /// 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.
    pub async fn approve_with_cacao(&self, cacao: Cacao) -> Result<()> {
        if cacao.signature.is_none() {
            return Err("Cacao signature is None".into());
        }

        cacao.verify()?;

        let message =
            self.authenticate_request.as_ref().unwrap().create_response(
                WcData::SessionAuthenticateResponse(
                    SessionAuthenticateResponse {
                        cacaos: vec![cacao],
                        responder: self.participant(),
                    },
                ),
                None,
            );

        self.send_message(
            Topic::Derived,
            &message.into_raw()?,
            Some(1),
            IrnTag::SessionAuthenticateApproveResponse,
            3600,
        )
        .await?;

        Ok(())
    }

    pub async fn watch_messages(
        &self,
        topic: Topic,
        dur: Option<Duration>,
    ) -> Result<Vec<WcMessage>> {
        loop {
            let result = self.fetch_messages(topic).await?;
            if !result.is_empty() {
                return Ok(result);
            }
            thread::sleep(dur.unwrap_or(Duration::from_secs(1)));
        }
    }

    fn new_message(&self, data: WcData) -> crate::Result<Message> {
        if data.result()?.is_some() {
            return Err("Cannot create new message with result".into());
        }
        Ok(Message {
            jsonrpc: "2.0".to_string(),
            method: data.method().map(|s| s.to_string()),
            params: data.params()?,
            result: None,
            error: None,
            id: self.connection.get_id(),
        })
    }

    /// Subscribe to the topic so we can fetch messages
    ///
    /// This returns subscription id - not sure how it is useful
    async fn subscribe(&self, topic: Topic) -> Result<String> {
        self.connection.irn_subscribe(&self.topic(topic)?).await
    }

    async fn fetch_messages(&self, topic: Topic) -> Result<Vec<WcMessage>> {
        self.connection
            .irn_fetch_messages(&self.topic(topic)?)
            .await?
            .iter()
            .map(|m| {
                Message::decrypt(&m.message, self.sym_key(topic)?, None)
                    .and_then(|m| m.decode())
            })
            .collect()
    }

    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,
    {
        let cipher_text = message.encrypt(
            self.sym_key(topic)?,
            type_byte,
            Some(self.public_key()),
            None,
        )?;

        let result = self
            .connection
            .irn_publish(EncryptedMessage::new(
                self.topic(topic)?,
                cipher_text,
                tag,
                ttl,
            ))
            .await?;

        Ok(result)
    }

    fn public_key(&self) -> String {
        let secret = x25519_dalek::StaticSecret::from(self.private_key);
        let public_key = x25519_dalek::PublicKey::from(&secret);
        hex::encode(public_key.to_bytes())
    }

    fn other_public_key(&self) -> Result<[u8; 32]> {
        let proposer_public_key = self
            .proposal_request
            .as_ref()
            .and_then(|p| p.data.as_session_propose())
            .map(|p| &p.proposer.public_key);
        let auth_public_key = self
            .authenticate_request
            .as_ref()
            .and_then(|p| p.data.as_session_authenticate())
            .map(|p| &p.requester.public_key);
        proposer_public_key
            .or(auth_public_key)
            .map(|k| {
                hex::decode_to_array::<String, 32>(k.clone())
                    .map_err(Error::from)
            })
            .ok_or::<Error>(
                "other_public_key not found because pairing is not init".into(),
            )?
    }

    pub(crate) fn participant(&self) -> Participant {
        Participant {
            public_key: self.public_key(),
            metadata: self.connection.metadata().clone(),
        }
    }

    fn sym_key(&self, topic: Topic) -> Result<[u8; 32]> {
        Ok(match topic {
            Topic::Initial => self.params.sym_key,
            Topic::Response | Topic::Derived => {
                derive_sym_key(self.private_key, self.other_public_key()?)
            }
        })
    }

    fn topic(&self, topic: Topic) -> Result<String> {
        Ok(match topic {
            // Usually topic is hash of sym key
            Topic::Initial | Topic::Derived => {
                hex::encode(sha256(self.sym_key(topic)?))
            }
            // In this specific case, the topic is not hash of sym key
            Topic::Response => hex::encode(sha256(self.other_public_key()?)),
        })
    }

    pub fn get_proposal(&self) -> Result<&WcMessage> {
        self.proposal_request
            .as_ref()
            .ok_or("error: proposal_request is None".into())
    }

    #[allow(dead_code)]
    pub fn get_proposal_old(
        &self,
        account_address: Address,
        chain_id: u64,
    ) -> Result<(Cacao, WcMessage, WcMessage)> {
        if self.proposal_request.is_none()
            || self.authenticate_request.is_none()
        {
            return Err("Pairing not initialised".into());
        }

        let cacao = Cacao::from_auth_request(
            &self
                .authenticate_request
                .as_ref()
                .unwrap()
                .data
                .as_session_authenticate()
                .unwrap()
                .auth_payload,
            account_address,
            chain_id,
        )?;

        Ok((
            cacao,
            // TODO is this necessary?
            self.proposal_request.clone().unwrap(),
            self.authenticate_request.clone().unwrap(),
        ))
    }
}