From 75091d3998e7e2ba3058f443df2ed2743e2baef3 Mon Sep 17 00:00:00 2001 From: verbiricha Date: Thu, 18 Jan 2024 11:42:31 +0100 Subject: [PATCH] feat: allow logging in with nip05 --- apps/badges/app/layout.tsx | 2 +- apps/emojis/ui/layout.tsx | 6 ++- packages/core/src/components/Login.tsx | 2 +- packages/core/src/context.tsx | 70 ++++++++++++++++++-------- packages/core/src/lang.json | 2 +- packages/core/src/translations/en.json | 2 +- packages/core/src/types.ts | 1 - 7 files changed, 58 insertions(+), 27 deletions(-) diff --git a/apps/badges/app/layout.tsx b/apps/badges/app/layout.tsx index 6fb63aa..df7c484 100644 --- a/apps/badges/app/layout.tsx +++ b/apps/badges/app/layout.tsx @@ -1,5 +1,5 @@ export const metadata = { - metadataBase: new URL('https://badges.page'), + metadataBase: new URL("https://badges.page"), title: "Badges", description: "Create, collect and award badges", }; diff --git a/apps/emojis/ui/layout.tsx b/apps/emojis/ui/layout.tsx index ca81129..8a1d803 100644 --- a/apps/emojis/ui/layout.tsx +++ b/apps/emojis/ui/layout.tsx @@ -15,7 +15,11 @@ import { theme } from "./theme"; const cacheAdapter = new NDKCacheAdapterDexie({ dbName: "emojis" }); const ndk = new NDK({ - explicitRelayUrls: ["wss://nos.lol", "wss://relay.nostr.band", "wss://frens.nostr1.com"], + explicitRelayUrls: [ + "wss://nos.lol", + "wss://relay.nostr.band", + "wss://frens.nostr1.com", + ], outboxRelayUrls: ["wss://purplepag.es"], enableOutboxModel: true, cacheAdapter, diff --git a/packages/core/src/components/Login.tsx b/packages/core/src/components/Login.tsx index 845906d..0a8a583 100644 --- a/packages/core/src/components/Login.tsx +++ b/packages/core/src/components/Login.tsx @@ -160,7 +160,7 @@ export function LoginBunker({ onLogin }: LoginProps) { placeholder={formatMessage({ id: "ngine.bunker-url-placeholder", description: "Bunker URL placeholder", - defaultMessage: "bunker://", + defaultMessage: "bunker:// URL or nostr address", })} value={url} onChange={(ev) => setUrl(ev.target.value)} diff --git a/packages/core/src/context.tsx b/packages/core/src/context.tsx index b5aa7f7..1fa5cb4 100644 --- a/packages/core/src/context.tsx +++ b/packages/core/src/context.tsx @@ -166,7 +166,7 @@ export const NgineProvider = ({ const signer = new NDKPrivateKeySigner(session.privkey); ndk.signer = signer; } else if (session?.method === "nip46" && session.bunker) { - const { privkey, pubkey, relays } = session.bunker; + const { privkey, relays } = session.bunker; const localSigner = new NDKPrivateKeySigner(privkey); const bunkerNDK = new NDK({ explicitRelayUrls: relays }); bunkerNDK.connect().then(() => { @@ -175,6 +175,9 @@ export const NgineProvider = ({ session.pubkey, localSigner, ); + signer.on("authUrl", (url) => { + window.open(url, "auth", "width=600,height=600"); + }); ndk.signer = signer; }); } @@ -194,30 +197,55 @@ export const NgineProvider = ({ return user; } - async function nip46Login(url: string) { - const asURL = new URL(url); - const relays = asURL.searchParams.getAll("relay"); - const pubkey = asURL.pathname.replace(/^\/\//, ""); - const bunkerNDK = new NDK({ - explicitRelayUrls: relays, - }); - await bunkerNDK.connect(); - const localSigner = NDKPrivateKeySigner.generate(); - const signer = new NDKNip46Signer(bunkerNDK, pubkey, localSigner); - const user = await signer.blockUntilReady(); - if (user) { - ndk.signer = signer; - setSession({ - method: "nip46", - pubkey: user.pubkey, - bunker: { - privkey: localSigner.privateKey as string, + async function getNostrConnectSettings(url: string) { + if (url.includes("bunker://")) { + const asURL = new URL(url); + const relays = asURL.searchParams.getAll("relay"); + const pubkey = asURL.pathname.replace(/^\/\//, ""); + return { relays, pubkey }; + } else { + const user = await NDKUser.fromNip05(url, ndk); + if (user) { + const pubkey = user.hexpubkey; + const relays = + user.nip46Urls?.length > 0 + ? user.nip46Urls + : ["wss://relay.nsecbunker.com"]; + return { pubkey, relays, - }, + }; + } + } + } + + async function nip46Login(url: string) { + const settings = await getNostrConnectSettings(url); + if (settings) { + const { pubkey, relays } = settings; + const bunkerNDK = new NDK({ + explicitRelayUrls: relays, + }); + await bunkerNDK.connect(); + const localSigner = NDKPrivateKeySigner.generate(); + const signer = new NDKNip46Signer(bunkerNDK, pubkey, localSigner); + signer.on("authUrl", (url) => { + window.open(url, "auth", "width=600,height=600"); }); + const user = await signer.blockUntilReady(); + if (user) { + ndk.signer = signer; + setSession({ + method: "nip46", + pubkey, + bunker: { + privkey: localSigner.privateKey as string, + relays, + }, + }); + } + return user; } - return user; } async function npubLogin(pubkey: string) { diff --git a/packages/core/src/lang.json b/packages/core/src/lang.json index 85e9842..82298bf 100644 --- a/packages/core/src/lang.json +++ b/packages/core/src/lang.json @@ -8,7 +8,7 @@ "description": "A button for logging in with a nsecbunker" }, "ngine.bunker-url-placeholder": { - "defaultMessage": "bunker://", + "defaultMessage": "bunker:// URL or nostr address", "description": "Bunker URL placeholder" }, "ngine.comment-label": { diff --git a/packages/core/src/translations/en.json b/packages/core/src/translations/en.json index 177c0f5..9cac72f 100644 --- a/packages/core/src/translations/en.json +++ b/packages/core/src/translations/en.json @@ -1,7 +1,7 @@ { "ngine.bookmarks-count": "Bookmarks ({count})", "ngine.bunker-login": "Log in", - "ngine.bunker-url-placeholder": "bunker://", + "ngine.bunker-url-placeholder": "bunker:// URL or nostr address", "ngine.comment-label": "Comment", "ngine.comment-placeholder": "Type your reply here", "ngine.copy": "Copy", diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 91c3f6d..01ce4ac 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -48,7 +48,6 @@ export interface Session { privkey?: string; bunker?: { privkey: string; - pubkey: string; relays: string[]; }; }