import React, { useEffect, useMemo, useRef } from 'react'; import { type NostrSigner, NostrEvent, NostrFilter, NPool, NRelay1 } from '@nostrify/nostrify'; import { NostrContext } from '@nostrify/react'; import { NUser, useNostrLogin } from '@nostrify/react/login'; import { useQueryClient } from '@tanstack/react-query'; import { useAppContext } from '@/hooks/useAppContext'; interface NostrProviderProps { children: React.ReactNode; } const NostrProvider: React.FC = (props) => { const { children } = props; const { config } = useAppContext(); const { logins } = useNostrLogin(); const queryClient = useQueryClient(); // Create NPool instance only once const pool = useRef(undefined); // Use refs so the pool always has the latest data const relayMetadata = useRef(config.relayMetadata); // Stable ref to the current user's signer for NIP-42 AUTH. // The `open()` callback reads from this ref when a relay sends an AUTH // challenge, so it always uses the latest signer without recreating the pool. const signerRef = useRef(undefined); // Derive the current signer from the active login. This mirrors the // logic in useCurrentUser but avoids a circular dependency (useCurrentUser // depends on NostrContext which we are providing here). const currentLogin = logins[0]; const currentSigner = useMemo(() => { if (!currentLogin) return undefined; try { switch (currentLogin.type) { case 'nsec': return NUser.fromNsecLogin(currentLogin).signer; case 'bunker': // pool.current is guaranteed to exist here: the pool is created // synchronously during the first render (below), and useMemo runs // after the render body has executed. return NUser.fromBunkerLogin(currentLogin, pool.current!).signer; case 'extension': return NUser.fromExtensionLogin(currentLogin).signer; default: return undefined; } } catch { return undefined; } }, [currentLogin]); // Keep the ref in sync so the AUTH callback always sees the latest signer. signerRef.current = currentSigner; // Invalidate Nostr queries when relay metadata changes useEffect(() => { relayMetadata.current = config.relayMetadata; queryClient.invalidateQueries({ queryKey: ['nostr'] }); }, [config.relayMetadata, queryClient]); // Initialize NPool only once if (!pool.current) { pool.current = new NPool({ open(url: string) { return new NRelay1(url, { // NIP-42: Respond to relay AUTH challenges by signing a kind // 22242 ephemeral event with the current user's signer. auth: async (challenge: string) => { const signer = signerRef.current; if (!signer) { throw new Error('AUTH failed: no signer available (user not logged in)'); } return signer.signEvent({ kind: 22242, content: '', tags: [ ['relay', url], ['challenge', challenge], ], created_at: Math.floor(Date.now() / 1000), }); }, }); }, reqRouter(filters: NostrFilter[]) { const routes = new Map(); // Route to all read relays const readRelays = relayMetadata.current.relays .filter(r => r.read) .map(r => r.url); for (const url of readRelays) { routes.set(url, filters); } return routes; }, eventRouter(_event: NostrEvent) { // Get write relays from metadata const writeRelays = relayMetadata.current.relays .filter(r => r.write) .map(r => r.url); const allRelays = new Set(writeRelays); return [...allRelays]; }, eoseTimeout: 200, }); } return ( {children} ); }; export default NostrProvider;