Add NIP-42 relay authentication support

Sign kind 22242 AUTH events automatically when relays send challenges.
Uses a signer ref pattern so the pool doesn't need to be recreated
when the user logs in/out or switches accounts.
This commit is contained in:
Alex Gleason committed 2026-03-25 02:14:41 -05:00
1 parent 699a3acb85
commit cb52a18d09
3 files changed
+58 -3

No files matched your search

+1
View File
@@ -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",
+1
View File
@@ -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",
+56 -3
View File
@@ -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<NostrProviderProps> = (props) => {
const { children } = props;
const { config } = useAppContext();
const { logins } = useNostrLogin();
const queryClient = useQueryClient();
@@ -20,6 +22,39 @@ const NostrProvider: React.FC<NostrProviderProps> = (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<NostrSigner | undefined>(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<NostrProviderProps> = (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<string, NostrFilter[]>();