Skip to content

Commit 91be66a

Browse files
tlongwell-blocknpub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf
andauthored
Use selected relay for desktop pairing socket (#1934)
Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
1 parent 202201f commit 91be66a

1 file changed

Lines changed: 56 additions & 14 deletions

File tree

‎desktop/src-tauri/src/commands/pairing.rs‎

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,9 @@ pub async fn start_pairing(
9292
// own NIP-11 declaration of NIP-43 support rather than `auth_required`,
9393
// which is also true for plain NIP-42 / NIP-OA relays where the main
9494
// relay is reachable.
95-
let qr_relay_url = match probe_pairing_relay(&ws_url).await {
96-
PairingRelay::Configured(url) => url,
97-
PairingRelay::LegacyPath => {
98-
let mut url =
99-
url::Url::parse(&ws_url).map_err(|e| format!("invalid relay URL: {e}"))?;
100-
let path = url.path().trim_end_matches('/').to_string();
101-
url.set_path(&format!("{path}/pair"));
102-
url.to_string()
103-
}
104-
PairingRelay::MainRelay => ws_url.clone(),
105-
};
95+
let pairing_relay_url = resolve_pairing_relay_url(&ws_url, probe_pairing_relay(&ws_url).await)?;
10696

107-
let (session, qr_payload) = PairingSession::new_source(qr_relay_url);
97+
let (session, qr_payload) = PairingSession::new_source(pairing_relay_url.clone());
10898
let qr_uri = encode_qr(&qr_payload);
10999

110100
let payload_json = serde_json::json!({
@@ -128,7 +118,7 @@ pub async fn start_pairing(
128118

129119
let session_arc = Arc::clone(&pairing.session);
130120
tauri::async_runtime::spawn(pairing_ws_task(
131-
ws_url,
121+
pairing_relay_url,
132122
session_arc,
133123
cancel,
134124
outbound_rx,
@@ -459,6 +449,23 @@ async fn probe_pairing_relay(relay_url: &str) -> PairingRelay {
459449
pairing_relay_from_nip11(&json)
460450
}
461451

452+
fn resolve_pairing_relay_url(
453+
main_relay_url: &str,
454+
pairing_relay: PairingRelay,
455+
) -> Result<String, String> {
456+
match pairing_relay {
457+
PairingRelay::Configured(url) => Ok(url),
458+
PairingRelay::LegacyPath => {
459+
let mut url =
460+
url::Url::parse(main_relay_url).map_err(|e| format!("invalid relay URL: {e}"))?;
461+
let path = url.path().trim_end_matches('/').to_string();
462+
url.set_path(&format!("{path}/pair"));
463+
Ok(url.to_string())
464+
}
465+
PairingRelay::MainRelay => Ok(main_relay_url.to_string()),
466+
}
467+
}
468+
462469
fn pairing_relay_from_nip11(json: &serde_json::Value) -> PairingRelay {
463470
if let Some(value) = json
464471
.get("pairing_relay_url")
@@ -522,7 +529,9 @@ where
522529

523530
#[cfg(test)]
524531
mod pairing_relay_tests {
525-
use super::{pairing_relay_from_nip11, probe_pairing_relay, PairingRelay};
532+
use super::{
533+
pairing_relay_from_nip11, probe_pairing_relay, resolve_pairing_relay_url, PairingRelay,
534+
};
526535
use tokio::io::{AsyncReadExt, AsyncWriteExt};
527536

528537
#[tokio::test]
@@ -591,4 +600,37 @@ mod pairing_relay_tests {
591600

592601
assert_eq!(pairing_relay_from_nip11(&document), PairingRelay::MainRelay);
593602
}
603+
604+
#[test]
605+
fn configured_pairing_relay_resolves_to_configured_url() {
606+
let resolved = resolve_pairing_relay_url(
607+
"wss://flint.communities.buzz.xyz",
608+
PairingRelay::Configured("wss://pairing.buzz.xyz".to_string()),
609+
)
610+
.expect("resolve configured pairing relay");
611+
612+
assert_eq!(resolved, "wss://pairing.buzz.xyz");
613+
}
614+
615+
#[test]
616+
fn legacy_pairing_relay_appends_pair_path() {
617+
let resolved = resolve_pairing_relay_url(
618+
"wss://flint.communities.buzz.xyz/community",
619+
PairingRelay::LegacyPath,
620+
)
621+
.expect("resolve legacy pairing relay");
622+
623+
assert_eq!(resolved, "wss://flint.communities.buzz.xyz/community/pair");
624+
}
625+
626+
#[test]
627+
fn main_relay_pairing_uses_main_relay_url() {
628+
let resolved = resolve_pairing_relay_url(
629+
"wss://sprout-oss.stage.blox.sqprod.co",
630+
PairingRelay::MainRelay,
631+
)
632+
.expect("resolve main pairing relay");
633+
634+
assert_eq!(resolved, "wss://sprout-oss.stage.blox.sqprod.co");
635+
}
594636
}

0 commit comments

Comments
 (0)