import { useNostr } from '@nostrify/react'; import { NLogin, type NLoginType, type NostrConnectParams, type NostrConnectStatus, useNostrLogin, } from '@nostrify/react/login'; import { useAppContext } from '@/hooks/useAppContext'; import { APP_RELAYS } from '@/lib/appRelays'; import { getNip07Provider, hasConnect } from '@/lib/nip07'; // NOTE: This file should not be edited except for adding new login methods. export type { NostrConnectParams, NostrConnectStatus }; export { generateNostrConnectParams, generateNostrConnectURI } from '@nostrify/react/login'; export function useLoginActions() { const { nostr } = useNostr(); const { logins, addLogin, setLogin, removeLogin } = useNostrLogin(); const { config } = useAppContext(); // Add a login and promote it to be the current user. Without the // setLogin call the new login is appended to the end of the array, // leaving the prior account as logins[0] — which is what // useCurrentUser / useLoggedInAccounts treat as the active user. // Promoting here makes "Add another account" actually switch. const addAndActivate = (login: NLoginType) => { addLogin(login); setLogin(login.id); }; return { // Login with a Nostr secret key nsec(nsec: string): void { const login = NLogin.fromNsec(nsec); addAndActivate(login); }, // Login with a NIP-46 "bunker://" URI async bunker(uri: string): Promise { const login = await NLogin.fromBunker(uri, nostr); addAndActivate(login); }, // Login with a NIP-07 browser extension. One that can give the site an // account of the user's choosing (connect) is asked for one; the // extension shows the account itself. async extension(): Promise { const provider = getNip07Provider(); if (hasConnect(provider)) { const signer = await provider.connect(); const answer: unknown = await signer.getPublicKey(); const pubkey = typeof answer === 'string' ? answer.toLowerCase() : ''; if (!/^[0-9a-f]{64}$/.test(pubkey)) { throw new Error('The extension returned an invalid public key'); } addAndActivate(new NLogin('extension', pubkey, null)); return; } const login = await NLogin.fromExtension(); addAndActivate(login); }, // Login via nostrconnect:// (client-initiated NIP-46) // The client displays a QR code and waits for the remote signer to connect. // // `onStatus` is forwarded from @nostrify/react so the UI can render // live progress through the handshake phases — see NostrConnectStatus. async nostrconnect( params: NostrConnectParams, signal?: AbortSignal, onStatus?: (status: NostrConnectStatus) => void, ): Promise { const login = await NLogin.fromNostrConnect(params, nostr, { signal, onStatus }); addAndActivate(login); }, // Get the relay URLs for NIP-46 nostrconnect communication getRelayUrls(): string[] { const relays = config.relayMetadata.relays .filter((r) => r.write) .map((r) => r.url); // Fall back to the app default relays if the user has none configured, // so the remote signer has multiple connection options during handshake. return relays.length > 0 ? relays : APP_RELAYS.relays.filter((r) => r.write).map((r) => r.url); }, // Log out the current user async logout(): Promise { const login = logins[0]; if (login) { removeLogin(login.id); } } }; }