Fix subscriptions against real relays; default to nostr.kosmos.org
Two bugs made the signer hang against anything but a local relay: - a plain Filter is routed by nostr-sdk's gossip model by author, and a #p-only filter resolves to no relay, so the REQ was never sent. Subscriptions now use explicit (relay, filters) targets. - subscribing while a relay is still Pending registered the subscription without transmitting it. We now wait for a connection and set verify_subscriptions so the REQ is re-sent when a relay reconnects. The default relay is now wss://nostr.kosmos.org: many public relays reject or rate-limit kind:24133, which looked like a timeout. Verified with the nak loopback against the real relay (2s).
This commit is contained in:
4 files changed
+99
-23
No files matched your search
@@ -48,7 +48,23 @@ data/ desktop, metainfo, gschema, icons, resources (ui/*.blp, sty
|
||||
- **⚠️ Until M4 every request is answered without asking.** The UI says so in the
|
||||
banner, deliberately. M3 adds pairing approval, M4 per-request approval.
|
||||
- Relays come from GSettings (`relays`); `REGENT_RELAYS` (comma separated)
|
||||
overrides them, which is what headless tests use.
|
||||
overrides them, which is what headless tests use. The default is
|
||||
`wss://nostr.kosmos.org` because **many public relays reject or rate-limit
|
||||
`kind:24133`** — pairing silently times out on those.
|
||||
- Two traps that make a signer look "hung" against a real relay. Both are fixed;
|
||||
do not reintroduce them:
|
||||
- **Subscriptions must target relays explicitly.** With gossip enabled (the
|
||||
default) a plain `Filter` is routed by its *authors*; a `#p`-only filter
|
||||
resolves to **no relay at all**, so the REQ is never sent and the
|
||||
subscription looks registered while nothing arrives. We build manual
|
||||
`(RelayUrl, Vec<Filter>)` targets. (Symptom: works against `nak serve`,
|
||||
hangs against a real relay.)
|
||||
- **Wait for the relay to be connected before subscribing.** A REQ sent while
|
||||
a relay is still `Pending` is registered locally but never transmitted.
|
||||
Localhost connects instantly, so this only shows up on `wss://`. We use
|
||||
`add_relay(…).verify_subscriptions(true)` (which also re-sends the REQ after
|
||||
a relay reconnects — essential for an always-on signer) plus
|
||||
`connect().and_wait(…)`.
|
||||
- On start the app logs `regent: signer listening on N relay(s) as npub1…` — the
|
||||
public key only, never the secret. That line is what headless checks assert on.
|
||||
|
||||
@@ -137,6 +153,12 @@ the UI is tested. `build-aux/deploy-dev.sh` runs on the build host,
|
||||
|
||||
## Testing
|
||||
|
||||
- Against a real relay — the path that actually matters, and the only one that
|
||||
exposes the two subscription traps above:
|
||||
```sh
|
||||
CARGO_TARGET_DIR=_build/cargo-target REGENT_TEST_RELAY=wss://nostr.kosmos.org \
|
||||
cargo test --release -- --ignored --nocapture nak_can_pair_and_sign
|
||||
```
|
||||
- End to end (keyring → responder → app), in a throwaway HOME:
|
||||
```sh
|
||||
dbus-run-session -- build-aux/acceptance.sh
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
<description>Height of the main window, in pixels.</description>
|
||||
</key>
|
||||
<key name="relays" type="as">
|
||||
<default>['wss://relay.damus.io', 'wss://nos.lol']</default>
|
||||
<default>['wss://nostr.kosmos.org']</default>
|
||||
<summary>Relays</summary>
|
||||
<description>Relays Regent connects to for NIP-46 requests and responses.</description>
|
||||
<description>Relays Regent connects to for NIP-46 requests and responses. Many public relays reject or rate-limit kind:24133, so the default is a relay that accepts them.</description>
|
||||
</key>
|
||||
</schema>
|
||||
</schemalist>
|
||||
@@ -0,0 +1,2 @@
|
||||
connecting to nostr.kosmos.org... ok.
|
||||
{"kind":24133,"id":"ea61197619e4feb7161f30d70b38fba2b7fa7d5149813f91ff00c8bf92019065","pubkey":"0751f95d5545e2f6735e2f6e8a0d0571947fc2dc061b7663afbcea5565f1d707","created_at":1794569989,"tags":[["p","2439cfcb986c1bb885d51196823c8e65a5a8970cbc81e53c8c5d05b692d5b5df"]],"content":"live delivery probe 2","sig":"b61764e9bcc46550316591d44b959804a0b3d57a9a0d4622165443abb8b73a58b377f1350a04d28a7567d336371bc2352750902cfaf21934a4edb94585a345c4"}
|
||||
+72
-20
@@ -10,6 +10,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use nostr::nips::{nip04, nip44};
|
||||
use nostr::prelude::*;
|
||||
@@ -43,6 +44,13 @@ impl SignerConfig {
|
||||
secret: random_secret(),
|
||||
})
|
||||
}
|
||||
|
||||
fn relay_urls(&self) -> Result<Vec<RelayUrl>> {
|
||||
self.relays
|
||||
.iter()
|
||||
.map(|relay| RelayUrl::parse(relay).map_err(Into::into))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Signer {
|
||||
@@ -55,29 +63,53 @@ pub struct Signer {
|
||||
impl Signer {
|
||||
pub async fn start(config: SignerConfig) -> Result<Self> {
|
||||
let config = Arc::new(config);
|
||||
let urls = config.relay_urls()?;
|
||||
|
||||
let client = Client::new();
|
||||
for relay in &config.relays {
|
||||
client.add_relay(relay.as_str()).await?;
|
||||
for url in &urls {
|
||||
// Re-send subscriptions when a relay (re)connects — an always-on
|
||||
// signer must survive a relay dropping.
|
||||
client
|
||||
.add_relay(url.clone())
|
||||
.verify_subscriptions(true)
|
||||
.await?;
|
||||
}
|
||||
client.connect().await;
|
||||
|
||||
// Wait until at least one relay is actually connected. Subscribing while
|
||||
// a relay is still `Pending` registers the subscription locally but the
|
||||
// REQ is never sent, so nothing ever arrives — which looks exactly like a
|
||||
// hung signer. Remote (wss) relays take long enough for this to bite.
|
||||
client.connect().and_wait(Duration::from_secs(15)).await;
|
||||
|
||||
// Subscribe *explicitly*, one target per relay.
|
||||
//
|
||||
// This is not just tidiness: `nostr-sdk` routes a plain `Filter` through
|
||||
// its gossip model, which picks relays from the filter's *authors*. Our
|
||||
// filter has only a `#p` tag, so gossip resolves it to no relay at all
|
||||
// and the REQ is never sent — subscriptions then look like they work
|
||||
// while nothing ever arrives. A manual target skips gossip entirely.
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::NostrConnect)
|
||||
.pubkey(config.device_keys.public_key())
|
||||
.limit(0);
|
||||
let targets: Vec<(RelayUrl, Vec<Filter>)> = urls
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|url| (url, vec![filter.clone()]))
|
||||
.collect();
|
||||
|
||||
// Subscribe *after* taking the stream, so nothing can slip between the
|
||||
// two calls.
|
||||
// Take the stream before subscribing so nothing can slip between the two.
|
||||
let mut notifications = client.notifications();
|
||||
client.subscribe(filter).await?;
|
||||
client.subscribe(targets).await?;
|
||||
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
{
|
||||
let (client, config, running) = (
|
||||
let (client, config, running, urls) = (
|
||||
client.clone(),
|
||||
Arc::clone(&config),
|
||||
Arc::clone(&running),
|
||||
urls.clone(),
|
||||
);
|
||||
tokio::spawn(async move {
|
||||
while running.load(Ordering::SeqCst) {
|
||||
@@ -92,7 +124,11 @@ impl Signer {
|
||||
}
|
||||
match handle_request(&event, &config) {
|
||||
Ok(Some(response)) => {
|
||||
if let Err(error) = client.send_event(&response).await {
|
||||
// Responses go back to the relays the clients use,
|
||||
// again bypassing gossip routing.
|
||||
if let Err(error) =
|
||||
client.send_event(&response).to(urls.clone()).await
|
||||
{
|
||||
eprintln!("regent: could not answer a request: {error}");
|
||||
}
|
||||
}
|
||||
@@ -309,26 +345,40 @@ fn payload(params: &[String]) -> &str {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The responder, driven by `nak` (an independent NIP-46 client) over a
|
||||
/// local in-memory relay. Ignored by default because it needs `nak` and
|
||||
/// binds a port:
|
||||
/// The responder, driven by `nak` (an independent NIP-46 client) over either
|
||||
/// a local in-memory relay or, with `REGENT_TEST_RELAY=wss://…`, a real one.
|
||||
/// Ignored by default because it needs `nak` and network access:
|
||||
///
|
||||
/// ```sh
|
||||
/// CARGO_TARGET_DIR=_build/cargo-target cargo test --release -- --ignored --nocapture
|
||||
/// CARGO_TARGET_DIR=_build/cargo-target REGENT_TEST_RELAY=wss://nostr.kosmos.org \
|
||||
/// cargo test --release -- --ignored --nocapture
|
||||
/// ```
|
||||
#[tokio::test]
|
||||
#[ignore = "needs nak and a free port"]
|
||||
#[ignore = "needs nak, and either a free port or REGENT_TEST_RELAY"]
|
||||
async fn nak_can_pair_and_sign() {
|
||||
const PORT: u16 = 10566;
|
||||
let relay = format!("ws://127.0.0.1:{PORT}");
|
||||
|
||||
let mut serve = std::process::Command::new("nak")
|
||||
.args(["serve", "--port", &PORT.to_string()])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.expect("spawn nak serve");
|
||||
tokio::time::sleep(std::time::Duration::from_millis(800)).await;
|
||||
let relay;
|
||||
let mut serving = None;
|
||||
match std::env::var("REGENT_TEST_RELAY").ok().filter(|url| !url.is_empty()) {
|
||||
Some(url) => {
|
||||
eprintln!("using the relay from REGENT_TEST_RELAY: {url}");
|
||||
relay = url;
|
||||
}
|
||||
None => {
|
||||
serving = Some(
|
||||
std::process::Command::new("nak")
|
||||
.args(["serve", "--port", &PORT.to_string()])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.expect("spawn nak serve"),
|
||||
);
|
||||
tokio::time::sleep(std::time::Duration::from_millis(800)).await;
|
||||
relay = format!("ws://127.0.0.1:{PORT}");
|
||||
}
|
||||
}
|
||||
|
||||
let config = SignerConfig {
|
||||
device_keys: Keys::generate(),
|
||||
@@ -351,7 +401,9 @@ mod tests {
|
||||
.await
|
||||
.expect("run nak");
|
||||
|
||||
let _ = serve.kill();
|
||||
if let Some(mut child) = serving {
|
||||
let _ = child.kill();
|
||||
}
|
||||
signer.stop().await;
|
||||
|
||||
assert!(
|
||||
|
||||
Reference in new issue
Block a user