Support Aegis URL scheme for NIP-46 login

This commit is contained in:
2026-04-06 12:51:12 +05:30
committed by hodlbod
parent f1f2083c88
commit f3a49e1ebf
3 changed files with 71 additions and 1 deletions
+16
View File
@@ -1,4 +1,6 @@
<script lang="ts">
import {Capacitor} from "@capacitor/core"
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import QRCode from "@app/components/QRCode.svelte"
import type {Nip46Controller} from "@app/util/nip46"
@@ -9,6 +11,14 @@
const {controller}: Props = $props()
const {url, loading} = controller
const openAegis = () => {
controller.launchSigner("aegis")
}
const openNostrSigner = () => {
controller.launchSigner("nostrsigner")
}
</script>
{#if $url}
@@ -20,6 +30,12 @@
<div class="flex flex-col items-center gap-2">
<QRCode code={$url} />
<p class="text-sm opacity-75">Scan with your signer to log in, or click to copy.</p>
{#if Capacitor.isNativePlatform()}
<div class="flex w-full flex-col gap-2">
<Button class="btn btn-primary" onclick={openAegis}>Open in Aegis</Button>
<Button class="btn" onclick={openNostrSigner}>Open in Nostr Signer</Button>
</div>
{/if}
</div>
{/if}
{/if}
+40 -1
View File
@@ -1,4 +1,4 @@
import {writable} from "svelte/store"
import {get, writable} from "svelte/store"
import type {Nip46ResponseWithResult} from "@welshman/signer"
import {Nip46Broker} from "@welshman/signer"
import {makeSecret} from "@welshman/util"
@@ -11,8 +11,28 @@ import {
} from "@app/core/state"
import {pushToast} from "@app/util/toast"
const APP_SCHEME = "social.flotilla"
type NostrSignerScheme = "aegis" | "nostrsigner"
const makeSignerCallbackUrl = (path: string, sourceAppScheme: string) =>
`${sourceAppScheme}://x-callback-url/${path}`
const makeSignerLaunchUrl = (scheme: NostrSignerScheme, nostrconnectUrl: string) => {
const params = new URLSearchParams({
method: "connect",
nostrconnect: nostrconnectUrl,
"x-source": APP_SCHEME,
"x-success": makeSignerCallbackUrl("authSuccess", APP_SCHEME),
"x-error": makeSignerCallbackUrl("authError", APP_SCHEME),
})
return `${scheme}://x-callback-url/auth/nip46?${params.toString()}`
}
export class Nip46Controller {
url = writable("")
signerUrls = writable<{aegis: string; nostrsigner: string} | undefined>(undefined)
bunker = writable("")
loading = writable(false)
clientSecret = makeSecret()
@@ -33,6 +53,10 @@ export class Nip46Controller {
})
this.url.set(url)
this.signerUrls.set({
aegis: makeSignerLaunchUrl("aegis", url),
nostrsigner: makeSignerLaunchUrl("nostrsigner", url),
})
let response
try {
@@ -54,6 +78,21 @@ export class Nip46Controller {
}
}
launchSigner(scheme: NostrSignerScheme) {
const signerUrl = get(this.signerUrls)?.[scheme]
if (!signerUrl) {
pushToast({
theme: "error",
message: "Unable to open signer app right now. Please try again.",
})
return
}
window.location.href = signerUrl
}
stop() {
this.broker.cleanup()
this.abortController.abort()
+15
View File
@@ -78,6 +78,21 @@
return
}
if (url.host === "x-callback-url") {
if (url.pathname === "/authError") {
const errorMessage = url.searchParams.get("errorMessage")
pushToast({
theme: "error",
message: errorMessage || "Signer authorization failed.",
})
}
if (["/authSuccess", "/authError"].includes(url.pathname)) {
return
}
}
const target = `${url.pathname}${url.search}${url.hash}`
goto(target, {replaceState: false, noScroll: false})
})