Merge branch 'feature/nostr-remote_signing' into live

This commit is contained in:
raucao committed 2026-11-28 14:54:25 +04:00
commit 8cd7a561ae
10 files changed
+238 -75

No files matched your search

@@ -33,7 +33,8 @@
<div data-controller="nostr-signer-connect"
data-nostr-signer-connect-status-url-value="<%= @status_url %>"
data-nostr-signer-connect-replace-url-value="<%= @replace_url %>"
data-nostr-signer-connect-destroy-url-value="<%= @destroy_url %>">
data-nostr-signer-connect-destroy-url-value="<%= @destroy_url %>"
data-nostr-signer-connect-section-url-value="<%= @section_url %>">
<% unless @pending_session.bunker? %>
<div data-nostr-signer-connect-target="details">
<div class="<%= @title.present? ? "mt-4" : "" %> hidden justify-center sm:flex"
@@ -131,7 +132,7 @@
<details class="mt-4">
<summary class="cursor-pointer text-gray-600">Prefer a bunker:// URI?</summary>
<%= form_with url: @create_url, method: :post, local: true, class: "mt-3" do %>
<%= form_with url: @create_url, method: :post, class: "mt-3" do %>
<p>
<%= text_field_tag :bunker_uri, nil,
placeholder: "bunker://…",
@@ -3,7 +3,8 @@ require "rqrcode"
class NostrSignerConnectComponent < ViewComponent::Base
def initialize(pending_session: nil, active_session: nil, create_url:, destroy_url: nil,
status_url: nil, replace_url: nil, title: "Nostr Connect", description: nil,
show_disconnect: true, modal: false, collapsible: false, flush: false)
show_disconnect: true, modal: false, collapsible: false, flush: false,
section_url: nil)
@pending_session = pending_session
@active_session = active_session
@create_url = create_url
@@ -16,6 +17,7 @@ class NostrSignerConnectComponent < ViewComponent::Base
@modal = modal
@collapsible = collapsible
@flush = flush
@section_url = section_url
end
def section_class
@@ -37,6 +37,10 @@ module Settings
respond_to do |format|
format.html { redirect_to setting_path(:nostr) }
format.turbo_stream do
@pending_signer_session = signer_session
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
end
format.json do
render json: {
token: signer_session.token,
@@ -75,6 +79,15 @@ module Settings
end
end
def section
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
end
format.html { redirect_to setting_path(:nostr) }
end
end
def replace
signer_session = NostrSignerSession.find_by_token(params[:token])
return render_error("Unknown connection", :not_found) if signer_session.nil? ||
@@ -97,8 +110,15 @@ module Settings
session.delete(:nostr_signer_token)
respond_to do |format|
format.html { redirect_to setting_path(:nostr), flash: { success: "Remote signer disconnected" } }
format.html { redirect_to setting_path(:nostr) }
format.json { render json: { ok: true } }
format.turbo_stream do
if current_user.nostr_pubkey.present?
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
else
redirect_to setting_path(:nostr)
end
end
end
end
@@ -6,7 +6,7 @@ const MAX_ATTEMPTS = 160
export default class extends Controller {
static targets = [ "status", "error", "authWrapper", "authLink", "details", "retry", "signerHint", "qr", "qrToggle", "replace", "replaceExisting", "replaceSigner" ]
static values = { statusUrl: String, replaceUrl: String, destroyUrl: String }
static values = { statusUrl: String, replaceUrl: String, destroyUrl: String, sectionUrl: String }
connect () {
this.attempts = 0
@@ -36,8 +36,7 @@ export default class extends Controller {
}
if (data.status === "connected") {
this.statusTarget.textContent = "Connected. Redirecting…"
window.location.href = data.redirect_url || window.location.href
this.finishConnection(data.redirect_url)
return
}
@@ -157,8 +156,8 @@ export default class extends Controller {
}
if (data.status === "connected") {
this.statusTarget.classList.remove("hidden")
this.statusTarget.textContent = "Connected. Redirecting…"
// Replacing links a different key, so the pubkey field, profile and
// media server lists elsewhere on the page are stale — full reload.
window.location.reload()
return
}
@@ -178,10 +177,51 @@ export default class extends Controller {
})
}
} catch (error) {
// Ignore; reload below reflects whatever state the server is in.
// Ignore; the section update below reflects whatever state the server is in.
}
window.location.reload()
this.finishConnection()
}
// After the connection is established (or cancelled), update just the signer
// section so the page's Nostr metadata isn't re-fetched from relays. Falls
// back to a full reload when there is no section to update (login flow) or
// when the account had no key yet (the pubkey field and metadata need a fresh
// page).
finishConnection (redirectUrl) {
if (!this.hasSectionUrlValue || !this.sectionUrlValue) {
if (this.hasStatusTarget) this.statusTarget.textContent = "Connected. Redirecting…"
window.location.href = redirectUrl || window.location.href
return
}
if (!this.hasAccountKey()) {
window.location.reload()
return
}
if (this.hasStatusTarget) this.statusTarget.textContent = "Connected. Updating…"
this.refreshSection()
}
hasAccountKey () {
return Boolean(document.querySelector('[data-controller~="nostr-metadata"]'))
}
async refreshSection () {
try {
const response = await fetch(this.sectionUrlValue, {
credentials: "include",
headers: { "Accept": "text/vnd.turbo-stream.html" }
})
if (!response.ok || response.redirected) {
window.location.reload()
return
}
window.Turbo.renderStreamMessage(await response.text())
} catch (error) {
window.location.reload()
}
}
showAuthUrl (url) {
@@ -0,0 +1,12 @@
import { Controller } from "@hotwired/stimulus"
// Mirrors the signer section's connection state onto the document, so the
// `data-remote-signer` flag stays correct when the section is replaced in place
// (turbo stream) instead of via a full page load.
export default class extends Controller {
static values = { connected: Boolean }
connect () {
document.documentElement.dataset.remoteSigner = this.connectedValue ? "true" : "false"
}
}
+4 -63
View File
@@ -13,7 +13,7 @@
<input type="text" value="<%= current_user.nostr_pubkey_bech32 %>" disabled
name="nostr_public_key" class="w-full" />
<%= link_to nostr_pubkey_settings_path,
class: 'btn-md btn-outline relative grow-0 shrink-0 text-red-700',
class: 'btn-md btn-outline relative flex items-center justify-center grow-0 shrink-0 text-red-700',
data: { turbo_method: :delete,
turbo_confirm: 'Remove your Nostr public key? Any connected remote signer will be disconnected.' } do %>
Remove
@@ -47,66 +47,7 @@
</p>
<% end %>
<% if nostr_remote_signer_available? && @pending_signer_session.present? %>
<div data-controller="modal"
data-modal-open-value="true"
data-action="turbo:load->modal#open">
<%= render ModalComponent.new(
show_close_button: false,
dismissible: false,
card_class: "w-96 max-w-[85vw]"
) do %>
<%= render NostrSignerConnectComponent.new(
pending_session: @pending_signer_session,
create_url: nostr_signer_settings_path,
destroy_url: nostr_signer_settings_path,
status_url: nostr_signer_status_settings_path(token: @pending_signer_session.token),
replace_url: nostr_signer_replace_settings_path(token: @pending_signer_session.token),
title: "Nostr Connect",
modal: true
) %>
<% end %>
</div>
<% elsif current_user.nostr_pubkey.present? %>
<% if nostr_remote_signer_available? %>
<%= render NostrSignerConnectComponent.new(
active_session: active_nostr_signer_session,
create_url: nostr_signer_settings_path,
destroy_url: nostr_signer_settings_path,
collapsible: true
) %>
<% end %>
<% else %>
<div class="mt-8 flex flex-col gap-8 sm:flex-row">
<div class="flex-1">
<h4 class="mb-0">Browser extension</h4>
<p class="mt-3 hidden" data-settings--nostr-pubkey-target="extensionDescription">
Use a Nostr-signing browser extension, like Alby or nos2x
</p>
<p class="mt-3" data-settings--nostr-pubkey-target="extensionUnavailable">
No browser extension detected. Install a Nostr signer like Alby to use this option.
</p>
<div class="mt-4">
<button class="btn-md btn-gray w-full sm:w-auto" disabled
data-settings--nostr-pubkey-target="setPubkey"
data-action="settings--nostr-pubkey#setPubkey">
Connect browser extension
</button>
</div>
</div>
<% if nostr_remote_signer_available? %>
<div class="flex-1">
<%= render NostrSignerConnectComponent.new(
active_session: active_nostr_signer_session,
create_url: nostr_signer_settings_path,
destroy_url: nostr_signer_settings_path,
flush: true
) %>
</div>
<% end %>
</div>
<% end %>
<%= render "nostr_signer" %>
</section>
<% if current_user.nostr_pubkey.present? %>
@@ -114,8 +55,8 @@
data-nostr-metadata-url-value="<%= nostr_metadata_settings_path %>"
data-action="nostr:publish->nostr-metadata#refresh">
<div data-nostr-metadata-target="loading"
class="flex flex-col items-center justify-center py-16">
<div class="mb-4 h-12 w-12"></div>
class="flex flex-col items-center justify-center pt-0 pb-0">
<%= image_tag "/img/illustrations/relay_constellation.svg", class: "mb-6 h-48 w-auto", alt: "" %>
<h3>Just a moment...</h3>
<p data-nostr-metadata-target="status"
class="text-gray-500 animate-pulse">Loading</p>
+65
View File
@@ -0,0 +1,65 @@
<div id="nostr-signer-section"
data-controller="nostr-signer-status"
data-nostr-signer-status-connected-value="<%= active_nostr_signer_session.present? %>">
<% if nostr_remote_signer_available? && @pending_signer_session.present? %>
<div data-controller="modal"
data-modal-open-value="true"
data-action="turbo:load->modal#open">
<%= render ModalComponent.new(
show_close_button: false,
dismissible: false,
card_class: "w-96 max-w-[85vw]"
) do %>
<%= render NostrSignerConnectComponent.new(
pending_session: @pending_signer_session,
create_url: nostr_signer_settings_path,
destroy_url: nostr_signer_settings_path,
status_url: nostr_signer_status_settings_path(token: @pending_signer_session.token),
replace_url: nostr_signer_replace_settings_path(token: @pending_signer_session.token),
title: "Nostr Connect",
modal: true,
section_url: nostr_signer_section_settings_path
) %>
<% end %>
</div>
<% elsif current_user.nostr_pubkey.present? %>
<% if nostr_remote_signer_available? %>
<%= render NostrSignerConnectComponent.new(
active_session: active_nostr_signer_session,
create_url: nostr_signer_settings_path,
destroy_url: nostr_signer_settings_path,
collapsible: true
) %>
<% end %>
<% else %>
<div class="mt-8 flex flex-col gap-8 sm:flex-row">
<div class="flex-1">
<h4 class="mb-0">Browser extension</h4>
<p class="mt-3 hidden" data-settings--nostr-pubkey-target="extensionDescription">
Use a Nostr-signing browser extension, like Alby or nos2x
</p>
<p class="mt-3" data-settings--nostr-pubkey-target="extensionUnavailable">
No browser extension detected. Install a Nostr signer like Alby to use this option.
</p>
<div class="mt-4">
<button class="btn-md btn-gray w-full sm:w-auto" disabled
data-settings--nostr-pubkey-target="setPubkey"
data-action="settings--nostr-pubkey#setPubkey">
Connect browser extension
</button>
</div>
</div>
<% if nostr_remote_signer_available? %>
<div class="flex-1">
<%= render NostrSignerConnectComponent.new(
active_session: active_nostr_signer_session,
create_url: nostr_signer_settings_path,
destroy_url: nostr_signer_settings_path,
flush: true
) %>
</div>
<% end %>
</div>
<% end %>
</div>
+1
View File
@@ -79,6 +79,7 @@ Rails.application.routes.draw do
post 'nostr_publish', to: 'settings/nostr_events#create', as: :nostr_publish
post 'nostr_signer', to: 'settings/nostr_signers#create', as: :nostr_signer
get 'nostr_signer/status', to: 'settings/nostr_signers#status', as: :nostr_signer_status
get 'nostr_signer/section', to: 'settings/nostr_signers#section', as: :nostr_signer_section
post 'nostr_signer/replace', to: 'settings/nostr_signers#replace', as: :nostr_signer_replace
delete 'nostr_signer', to: 'settings/nostr_signers#destroy'
post 'nostr_sign', to: 'settings/nostr_signatures#create', as: :nostr_sign
@@ -0,0 +1,46 @@
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="118" viewBox="0 10 200 118">
<style>
.query { stroke-dasharray: 13 100; stroke-dashoffset: 16; opacity: 0; animation: constellation-query 3.6s linear infinite; }
.reply { stroke-dasharray: 18 100; stroke-dashoffset: 21; opacity: 0; animation: constellation-reply 3.6s linear infinite; }
.relay-halo { transform-box: fill-box; transform-origin: center; animation: constellation-relay 3.6s ease-out infinite; }
@keyframes constellation-query {
0% { opacity: 0; stroke-dashoffset: 16; }
6% { opacity: 1; }
31% { opacity: 1; stroke-dashoffset: -100; }
33%, 100% { opacity: 0; stroke-dashoffset: -116; }
}
@keyframes constellation-reply {
0%, 34% { opacity: 0; stroke-dashoffset: 21; }
41% { opacity: 1; }
71% { opacity: 1; stroke-dashoffset: -100; }
73%, 100% { opacity: 0; stroke-dashoffset: -121; }
}
@keyframes constellation-relay {
0%, 24%, 55%, 100% { opacity: 0; transform: scale(.6); }
33% { opacity: .9; transform: scale(1); }
50% { opacity: 0; transform: scale(1.55); }
}
@media (prefers-reduced-motion: reduce) {
.query, .reply, .relay-halo { animation: none; }
}
</style>
<g fill="none" stroke-linecap="round" stroke-linejoin="round">
<ellipse cx="100" cy="68" rx="84" ry="47" transform="rotate(-15 100 68)" stroke="#eee9f8" stroke-dasharray="2 5"/>
<path d="M28 86 51 37 132 21 171 53 153 106 74 117Z M51 37 74 117 M132 21 153 106 M28 86 153 106" stroke="#e0d6f2" stroke-width="1.15"/>
<path d="M100 68 51 37 M100 68 171 53 M100 68 74 117" stroke="#c9b6e9" stroke-width="1.5"/>
<path class="query" d="M100 68 51 37" pathLength="100" stroke="#8750ce" stroke-width="2.6"/>
<path class="query" d="M100 68 171 53" pathLength="100" stroke="#8750ce" stroke-width="2.6" style="animation-delay:.16s"/>
<path class="query" d="M100 68 74 117" pathLength="100" stroke="#8750ce" stroke-width="2.6" style="animation-delay:.32s"/>
<path class="reply" d="M171 53 100 68" pathLength="100" stroke="#159a8c" stroke-width="3.2"/>
<circle class="relay-halo" cx="171" cy="53" r="9" stroke="#159a8c" opacity="0"/>
</g>
<g fill="#fff" stroke="#ae96d3" stroke-width="1.65">
<circle cx="28" cy="86" r="4.5"/><circle cx="51" cy="37" r="5.5"/>
<circle cx="132" cy="21" r="4.5"/><circle cx="153" cy="106" r="5"/>
<circle cx="74" cy="117" r="5"/>
</g>
<circle cx="171" cy="53" r="5.5" fill="#e0f8f3" stroke="#159a8c" stroke-width="1.65"/>
<circle cx="100" cy="68" r="22" fill="#f1e9fc"/>
<circle cx="100" cy="68" r="17.5" fill="#7844ba"/>
<path d="M99 67 a4.4 4.4 0 1 0 -6.2 0 a4.4 4.4 0 0 0 6.2 0L106 74m-1.9-2 2.2-2.2m-4.3.1 2.1-2.1" fill="none" stroke="#fff" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 2.8 KiB

+36 -1
View File
@@ -44,6 +44,15 @@ RSpec.describe "Settings::NostrSigners", type: :request do
expect(data["uri"]).to start_with("nostrconnect://")
end
it "replaces the signer section with the pending modal for turbo streams" do
post nostr_signer_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
expect(response.media_type).to eq("text/vnd.turbo-stream.html")
expect(response.body).to include('target="nostr-signer-section"')
expect(response.body).to include("nostrconnect://")
expect(response.body).to include('data-modal-open-value="true"')
end
it "renders the QR code and status poll inside an open modal" do
post nostr_signer_settings_path, as: :json
@@ -83,6 +92,15 @@ RSpec.describe "Settings::NostrSigners", type: :request do
end
end
describe "GET /settings/nostr_signer/section" do
it "replaces the signer section for turbo streams" do
get nostr_signer_section_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
expect(response.media_type).to eq("text/vnd.turbo-stream.html")
expect(response.body).to include('target="nostr-signer-section"')
end
end
describe "GET /settings/nostr_signer/status" do
it "stores the verified pubkey in LDAP once" do
session = create_verified_session
@@ -247,5 +265,22 @@ RSpec.describe "Settings::NostrSigners", type: :request do
expect(signer_session.reload.status).to eq("disconnecting")
end
it "replaces the signer section for turbo streams when a key is linked" do
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(user_pubkey)
create_connected_session
delete nostr_signer_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
expect(response.media_type).to eq("text/vnd.turbo-stream.html")
expect(response.body).to include('target="nostr-signer-section"')
expect(response.body).to include("Connect a signer app")
end
it "redirects for turbo streams when no key is linked" do
delete nostr_signer_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
expect(response).to redirect_to(setting_path(:nostr))
end
end
end
end