diff --git a/package-lock.json b/package-lock.json index cd1474a..37c3d78 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@hookform/resolvers": "^5.2.2", "@nostrify/nostrify": "^0.51.0", "@nostrify/react": "^0.3.1", + "@nostrify/types": "^0.36.9", "@radix-ui/react-accordion": "^1.2.0", "@radix-ui/react-alert-dialog": "^1.1.1", "@radix-ui/react-aspect-ratio": "^1.1.0", diff --git a/package.json b/package.json index 4133a1f..80793ca 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@hookform/resolvers": "^5.2.2", "@nostrify/nostrify": "^0.51.0", "@nostrify/react": "^0.3.1", + "@nostrify/types": "^0.36.9", "@radix-ui/react-accordion": "^1.2.0", "@radix-ui/react-alert-dialog": "^1.1.1", "@radix-ui/react-aspect-ratio": "^1.1.0", diff --git a/src/components/NostrProvider.tsx b/src/components/NostrProvider.tsx index d38324b..1064ee7 100644 --- a/src/components/NostrProvider.tsx +++ b/src/components/NostrProvider.tsx @@ -1,6 +1,7 @@ -import React, { useEffect, useRef } from 'react'; -import { NostrEvent, NostrFilter, NPool, NRelay1 } from '@nostrify/nostrify'; +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'; @@ -11,6 +12,7 @@ interface NostrProviderProps { const NostrProvider: React.FC = (props) => { const { children } = props; const { config } = useAppContext(); + const { logins } = useNostrLogin(); const queryClient = useQueryClient(); @@ -20,6 +22,39 @@ const NostrProvider: React.FC = (props) => { // 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; @@ -30,7 +65,25 @@ const NostrProvider: React.FC = (props) => { if (!pool.current) { pool.current = new NPool({ open(url: string) { - return new NRelay1(url); + 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();