Open Regent for nostrconnect:// links

Register the scheme (x-scheme-handler/nostrconnect) in the desktop file, with
`Exec=regent %u`, so a clicked link is offered to Regent; `regent-deploy` sets
it as the default with `xdg-mime`, asking first if another app already handles
it.

The GUI claims G_APPLICATION_HANDLES_OPEN and pairs straight from a link's URI
over the main window, reusing the "Connect an app" flow (begin_pairing now takes
the widget to parent the approval dialog to). One trap: `run_with_args` passes
the slice as argv verbatim, so a lone URI became argv[0] — the program name —
and GApplication saw no file; it now prepends "regent".

The acceptance test checks that GIO resolves the scheme to Regent. The click
itself is desktop behaviour and is left to a real session.
This commit is contained in:
raucao committed 2026-11-26 20:25:57 +04:00
1 parent 8224d09f20
commit 592837145b
7 files changed
+133 -22

No files matched your search

+19
View File
@@ -139,6 +139,21 @@ PY
check $? "a desktop file with an unresolvable Exec is dropped"
rm -f "$APPS_DIR/$DATA_DIR.broken.desktop"
# The `nostrconnect://` scheme handler: the desktop file declares the scheme,
# so a clicked link must be offered to Regent (the default when no rival owns
# it).
update-desktop-database "$APPS_DIR" >/dev/null 2>&1 || true
xdg-mime default "$DATA_DIR.desktop" x-scheme-handler/nostrconnect >/dev/null 2>&1 || true
python3 - "$DATA_DIR.desktop" <<'PY'
import sys
from gi.repository import Gio
wanted = sys.argv[1]
app = Gio.AppInfo.get_default_for_uri_scheme("nostrconnect")
assert app is not None and app.get_id() == wanted, \
"the nostrconnect:// handler is {}".format(app.get_id() if app else None)
PY
check $? "GIO opens nostrconnect:// links with Regent"
# An already-approved client, so a request can reach a prompt without pairing:
# the pairing secret is deliberately never logged, and the in-app pairing flow
# would need a click. The client is bound to the seeded identity and has no
@@ -281,6 +296,10 @@ check $? "activating Connect and Preferences twice and Identities leaves the app
! grep -q 'panicked' "$TMP_HOME/app.log"
check $? "and nothing panicked"
# Opening a `nostrconnect://` link is desktop behaviour (the scheme handler
# launches `regent %u`, and GApplication's file/URI handling picks it up) — it is
# not exercised here; the GIO-handler check above covers the registration.
# The GUI owns the D-Bus name; headless must not run alongside it, and we need
# it gone so the two do not race over apps.json.
kill "$APP" 2>/dev/null
+36
View File
@@ -116,6 +116,41 @@ PY
fi
}
# Make Regent the desktop's handler for `nostrconnect://` links.
#
# `update-desktop-database` only records that Regent *can* handle the scheme;
# `xdg-mime default` is what makes a clicked link open it. Ask before taking the
# scheme from another app, so a link that opens somewhere else is not silently
# re-pointed at Regent.
set_scheme_handler() {
command -v xdg-mime >/dev/null 2>&1 || return 0
local current
current=$(xdg-mime query default x-scheme-handler/nostrconnect 2>/dev/null || true)
if [ "$current" = "$APP_ID.desktop" ]; then
say "nostrconnect:// links open with Regent"
return 0
fi
if [ -n "$current" ]; then
if [ ! -t 0 ]; then
say "note: nostrconnect:// links open with $current; not changing it (non-interactive)"
return 0
fi
local answer
read -r -p "regent-deploy: nostrconnect:// links open with $current. Open them with Regent? [y/N] " answer || answer=
case $answer in
[yY]*) ;;
*) say "left $current as the nostrconnect:// handler"; return 0 ;;
esac
fi
if xdg-mime default "$APP_ID.desktop" x-scheme-handler/nostrconnect 2>/dev/null; then
say "nostrconnect:// links now open with Regent"
else
say "note: could not set the nostrconnect:// handler; set it with:" >&2
say " xdg-mime default $APP_ID.desktop x-scheme-handler/nostrconnect" >&2
fi
}
command -v ssh >/dev/null || die "ssh not found"
command -v pkg-config >/dev/null || die "pkg-config not found"
command -v tar >/dev/null || die "tar not found"
@@ -151,6 +186,7 @@ else
update-desktop-database "$HOME/.local/share/applications" >/dev/null 2>&1 || true
fix_desktop_file
check_desktop_file
set_scheme_handler
fi
if [ -x "$HOME/.local/bin/regent" ]; then
+2 -1
View File
@@ -3,9 +3,10 @@ Type=Application
Name=Regent
GenericName=Nostr Remote Signer
Comment=Sign Nostr events without sharing your private key
Exec=regent
Exec=regent %u
Icon=org.kosmos.raucao.Regent
Terminal=false
Categories=Utility;Security;
Keywords=nostr;signer;keys;nip46;nip-46;
StartupNotify=true
MimeType=x-scheme-handler/nostrconnect;
+37 -10
View File
@@ -11,8 +11,15 @@ use crate::ui::approval;
use crate::window::Windows;
use crate::{APP_ID, RESOURCE_PATH};
pub fn run() -> glib::ExitCode {
let app = adw::Application::builder().application_id(APP_ID).build();
pub fn run(args: &[String]) -> glib::ExitCode {
// `HANDLES_OPEN` is what makes a `nostrconnect://` argument (the desktop's
// scheme handler runs `regent %u`) arrive in the `open` signal instead of
// being refused — and it is what lets a link reach an already-running
// instance rather than starting a second one.
let app = adw::Application::builder()
.application_id(APP_ID)
.flags(gio::ApplicationFlags::HANDLES_OPEN)
.build();
// One shared state per process, not per window: every window must see the
// same registry and signer. A vault that cannot be read is fatal — starting
@@ -119,17 +126,37 @@ pub fn run() -> glib::ExitCode {
app.add_action(&focus);
app.connect_startup(|_| load_css());
app.connect_activate(move |_| {
windows.build();
// First run has nothing to sign with and nothing to listen for, so the
// identities window is the whole app until a key exists. Activation can
// arrive more than once, which is why this presents rather than creates.
windows.present_primary();
});
{
let windows = Rc::clone(&windows);
app.connect_activate(move |_| {
windows.build();
// First run has nothing to sign with and nothing to listen for, so the
// identities window is the whole app until a key exists. Activation can
// arrive more than once, which is why this presents rather than creates.
windows.present_primary();
});
}
// A `nostrconnect://` link (the desktop opens it with us) pairs straight
// away — the same approval dialog as pasting a code, over the main window.
{
let windows = Rc::clone(&windows);
app.connect_open(move |_, files, _hint| {
windows.build();
windows.present_primary();
for file in files {
windows.pair_uri(file.uri().as_str());
}
});
}
// `run_with_args` passes the slice as `argv` as-is, and GApplication reads
// everything after `argv[0]` as the arguments — so the program name has to
// be there, or a lone `nostrconnect://` URI would be taken for it.
let mut argv = vec!["regent".to_string()];
argv.extend(args.iter().cloned());
// The resource file is passed through the environment, not argv, so that
// GApplication does not try to interpret it.
app.run_with_args::<&str>(&[])
app.run_with_args(&argv)
}
/// Warn once if the desktop cannot find this installation's desktop file.
+8 -1
View File
@@ -70,7 +70,10 @@ fn main() -> glib::ExitCode {
}
register_resources();
application::run()
// A `nostrconnect://` link (if any) is left in `args`: GApplication's `open`
// signal delivers it, so it works for a cold launch and for a link that
// arrives while Regent is already running.
application::run(&args)
}
/// The (few) arguments we accept.
@@ -107,6 +110,10 @@ fn parse_args(args: &[String]) -> Result<Args, String> {
parsed.key = Some(value.to_string());
} else if let Some(value) = arg.strip_prefix("--passphrase-file=") {
parsed.passphrase_file = Some(value.to_string());
} else if arg.starts_with("nostrconnect://") {
// A pairing link the desktop opened with us. GApplication's
// `open` signal handles it, so it stays in `args` and is not
// parsed here.
} else {
return Err(format!("unknown argument: {arg}"));
}
+7 -3
View File
@@ -338,10 +338,13 @@ impl ConnectDialog {
if text.trim().is_empty() {
return;
}
self.begin_pairing(text.trim());
self.begin_pairing(text.trim(), &self.dialog.clone().upcast::<gtk::Widget>());
}
fn begin_pairing(self: &Rc<Self>, uri: &str) {
/// Pair from a `nostrconnect://` URI, asked over `parent`: the "Connect an
/// app" dialog passes itself, an opened link passes the main window (that
/// dialog is not open then).
pub(crate) fn begin_pairing(self: &Rc<Self>, uri: &str, parent: &gtk::Widget) {
let pairing = match parse_client_uri(uri) {
Ok(pairing) => pairing,
Err(error) => {
@@ -387,12 +390,13 @@ impl ConnectDialog {
rebind_from,
};
let parent = parent.clone();
let connect = Rc::clone(self);
glib::MainContext::default().spawn_local(async move {
// The same approval dialog as a responder question: the app is
// registered *before* the response goes out, and the user is the
// one who decides.
let decision = approval::present_question(&connect.dialog, &question, None).await;
let decision = approval::present_question(&parent, &question, None).await;
if decision == Decision::Deny {
return;
}
+24 -7
View File
@@ -134,22 +134,39 @@ impl Windows {
/// Built once and re-presented, like Preferences: its listeners stay
/// registered and the dialog re-presents after being closed.
pub fn present_connect(self: &Rc<Self>) {
if let Some(connect) = self.connect_dialog() {
connect.present(self.primary().as_ref());
}
}
/// The "Connect an app" dialog, created once and re-used: an `AdwDialog`
/// re-presents after being closed and its listeners must stay registered.
fn connect_dialog(self: &Rc<Self>) -> Option<Rc<ConnectDialog>> {
// Bind the clone to a local first: a `match` scrutinee is a temporary
// scope that lasts to the end of the match, so an inline `borrow()`
// would still hold the `Ref` when the `None` arm takes `borrow_mut()`.
let existing = self.connect.borrow().clone();
let connect = match existing {
Some(connect) => connect,
match existing {
Some(connect) => Some(connect),
None => {
let Some(runner) = self.runner.borrow().clone() else {
return;
};
let runner = self.runner.borrow().clone()?;
let connect = ConnectDialog::new(Rc::clone(&self.state), runner, self);
*self.connect.borrow_mut() = Some(Rc::clone(&connect));
connect
Some(connect)
}
}
}
/// Pair from a `nostrconnect://` URI — what opening a link does. Goes
/// straight to the approval dialog, over the window the app is.
pub fn pair_uri(self: &Rc<Self>, uri: &str) {
let Some(connect) = self.connect_dialog() else {
return;
};
connect.present(self.primary().as_ref());
let Some(parent) = self.primary().map(|window| window.upcast::<gtk::Widget>()) else {
return;
};
connect.begin_pairing(uri, &parent);
}
/// Bring the Apps page to the front. A pairing closes the "Connect an app"