@@ -344,9 +344,9 @@ impl WalletConnectCtxImpl {
344
344
& self ,
345
345
topic : & Topic ,
346
346
param : RequestParams ,
347
+ message_id : MessageId ,
347
348
) -> MmResult < ( ) , WalletConnectError > {
348
349
let irn_metadata = param. irn_metadata ( ) ;
349
- let message_id = MessageIdGenerator :: new ( ) . next ( ) ;
350
350
let request = Request :: new ( message_id, param. into ( ) ) ;
351
351
352
352
self . publish_payload ( topic, irn_metadata, Payload :: Request ( request) )
@@ -463,6 +463,7 @@ impl WalletConnectCtxImpl {
463
463
session : & Session ,
464
464
chain_id : & WcChainId ,
465
465
) -> MmResult < ( ) , WalletConnectError > {
466
+ println ! ( "{:?}" , session. namespaces. get( chain_id. chain. as_ref( ) ) ) ;
466
467
if let Some ( Namespace {
467
468
chains : Some ( chains) , ..
468
469
} ) = session. namespaces . get ( chain_id. chain . as_ref ( ) )
@@ -581,21 +582,41 @@ impl WalletConnectCtxImpl {
581
582
} ;
582
583
let request = RequestParams :: SessionRequest ( request) ;
583
584
let ttl = request. irn_metadata ( ) . ttl ;
584
- self . publish_request ( & active_topic, request) . await ?;
585
-
586
- if let Ok ( Some ( resp) ) = timeout ( Duration :: from_secs ( ttl) , async {
587
- self . message_rx . lock ( ) . await . next ( ) . await
585
+ let message_id = MessageIdGenerator :: new ( ) . next ( ) ;
586
+ self . publish_request ( & active_topic, request, message_id) . await ?;
587
+
588
+ match timeout ( Duration :: from_secs ( ttl) , async {
589
+ // Check if the message exists and matches the expected message ID and
590
+ // wait till we get a message with expected id or timeout.
591
+ loop {
592
+ let next_message = {
593
+ let mut lock = self . message_rx . lock ( ) . await ;
594
+ lock. next ( ) . await
595
+ } ;
596
+
597
+ if let Some ( Ok ( message) ) = & next_message {
598
+ if message. message_id == message_id {
599
+ return next_message;
600
+ }
601
+ }
602
+ }
588
603
} )
589
604
. await
590
605
{
591
- let result = resp. mm_err ( WalletConnectError :: InternalError ) ?;
592
- if let ResponseParamsSuccess :: Arbitrary ( data) = result. data {
593
- let data = serde_json:: from_value :: < T > ( data) ?;
594
- return callback ( data) ;
595
- }
606
+ Ok ( Some ( result) ) => {
607
+ let result = result. mm_err ( WalletConnectError :: InternalError ) ;
608
+ match result?. data {
609
+ ResponseParamsSuccess :: Arbitrary ( data) => {
610
+ let data = serde_json:: from_value :: < T > ( data)
611
+ . map_err ( |e| WalletConnectError :: SerdeError ( e. to_string ( ) ) ) ?;
612
+ callback ( data)
613
+ } ,
614
+ _ => MmError :: err ( WalletConnectError :: PayloadError ( "Unexpected response type" . to_string ( ) ) ) ,
615
+ }
616
+ } ,
617
+ Ok ( None ) => MmError :: err ( WalletConnectError :: NoWalletFeedback ) ,
618
+ Err ( _) => MmError :: err ( WalletConnectError :: TimeoutError ) ,
596
619
}
597
-
598
- MmError :: err ( WalletConnectError :: NoWalletFeedback )
599
620
}
600
621
601
622
pub async fn drop_session ( & self , topic : & Topic ) -> MmResult < ( ) , WalletConnectError > {
0 commit comments