@@ -482,6 +482,44 @@ fn pairing_relay_from_nip11(json: &serde_json::Value) -> PairingRelay {
482482 }
483483}
484484
485+ fn parse_auth_challenge ( text : & str ) -> Option < String > {
486+ let arr: serde_json:: Value = serde_json:: from_str ( text) . ok ( ) ?;
487+ let arr = arr. as_array ( ) ?;
488+ if arr. len ( ) >= 2 && arr[ 0 ] . as_str ( ) ? == "AUTH" {
489+ return arr[ 1 ] . as_str ( ) . map ( |s| s. to_string ( ) ) ;
490+ }
491+ None
492+ }
493+
494+ async fn wait_for_eose < S > ( read : & mut S , sub_id : & str , dur : Duration ) -> Result < ( ) , String >
495+ where
496+ S : StreamExt < Item = Result < Message , tokio_tungstenite:: tungstenite:: Error > > + Unpin ,
497+ {
498+ tokio:: time:: timeout ( dur, async {
499+ loop {
500+ let msg = read
501+ . next ( )
502+ . await
503+ . ok_or_else ( || "relay closed waiting for EOSE" . to_string ( ) ) ?
504+ . map_err ( |e| format ! ( "WS error waiting for EOSE: {e}" ) ) ?;
505+ if let Message :: Text ( text) = msg {
506+ if let Ok ( arr) = serde_json:: from_str :: < serde_json:: Value > ( text. as_str ( ) ) {
507+ if let Some ( arr) = arr. as_array ( ) {
508+ if arr. len ( ) >= 2
509+ && arr[ 0 ] . as_str ( ) == Some ( "EOSE" )
510+ && arr[ 1 ] . as_str ( ) == Some ( sub_id)
511+ {
512+ return Ok ( ( ) ) ;
513+ }
514+ }
515+ }
516+ }
517+ }
518+ } )
519+ . await
520+ . map_err ( |_| "timeout waiting for EOSE" . to_string ( ) ) ?
521+ }
522+
485523#[ cfg( test) ]
486524mod pairing_relay_tests {
487525 use super :: { pairing_relay_from_nip11, probe_pairing_relay, PairingRelay } ;
@@ -554,41 +592,3 @@ mod pairing_relay_tests {
554592 assert_eq ! ( pairing_relay_from_nip11( & document) , PairingRelay :: MainRelay ) ;
555593 }
556594}
557-
558- fn parse_auth_challenge ( text : & str ) -> Option < String > {
559- let arr: serde_json:: Value = serde_json:: from_str ( text) . ok ( ) ?;
560- let arr = arr. as_array ( ) ?;
561- if arr. len ( ) >= 2 && arr[ 0 ] . as_str ( ) ? == "AUTH" {
562- return arr[ 1 ] . as_str ( ) . map ( |s| s. to_string ( ) ) ;
563- }
564- None
565- }
566-
567- async fn wait_for_eose < S > ( read : & mut S , sub_id : & str , dur : Duration ) -> Result < ( ) , String >
568- where
569- S : StreamExt < Item = Result < Message , tokio_tungstenite:: tungstenite:: Error > > + Unpin ,
570- {
571- tokio:: time:: timeout ( dur, async {
572- loop {
573- let msg = read
574- . next ( )
575- . await
576- . ok_or_else ( || "relay closed waiting for EOSE" . to_string ( ) ) ?
577- . map_err ( |e| format ! ( "WS error waiting for EOSE: {e}" ) ) ?;
578- if let Message :: Text ( text) = msg {
579- if let Ok ( arr) = serde_json:: from_str :: < serde_json:: Value > ( text. as_str ( ) ) {
580- if let Some ( arr) = arr. as_array ( ) {
581- if arr. len ( ) >= 2
582- && arr[ 0 ] . as_str ( ) == Some ( "EOSE" )
583- && arr[ 1 ] . as_str ( ) == Some ( sub_id)
584- {
585- return Ok ( ( ) ) ;
586- }
587- }
588- }
589- }
590- }
591- } )
592- . await
593- . map_err ( |_| "timeout waiting for EOSE" . to_string ( ) ) ?
594- }
0 commit comments