Refactor NIP05 verification logic: move verifyNIP05 function to nip05.ts and clean up NIP05Verification component

This commit is contained in:
2025-11-27 22:24:14 +01:00
parent c121cbd36f
commit 831fdc14f7
2 changed files with 12 additions and 33 deletions

View File

@@ -1,45 +1,13 @@
import { useQuery } from '@tanstack/react-query';
import { Badge } from '@/components/ui/badge';
import { CheckCircle2, XCircle, Loader2 } from 'lucide-react';
import { verifyNIP05 } from '@/lib/nip05';
interface NIP05VerificationProps {
nip05: string;
pubkey: string;
}
async function verifyNIP05(nip05: string, pubkey: string): Promise<boolean> {
try {
// Parse the NIP-05 identifier (name@domain.com)
const [name, domain] = nip05.split('@');
if (!name || !domain) {
return false;
}
// Fetch the .well-known/nostr.json file
const url = `https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(name)}`;
const response = await fetch(url, {
signal: AbortSignal.timeout(5000),
});
if (!response.ok) {
return false;
}
const data = await response.json();
// Check if the pubkey matches
const verifiedPubkey = data.names?.[name];
if (!verifiedPubkey) {
return false;
}
// Compare pubkeys
return verifiedPubkey.toLowerCase() === pubkey.toLowerCase();
} catch {
return false;
}
}
export function NIP05Verification({ nip05, pubkey }: NIP05VerificationProps) {
const { data: isVerified, isLoading } = useQuery({
queryKey: ['nip05-verification', nip05, pubkey],

View File

@@ -33,6 +33,17 @@ export async function verifyAndGetPubkey(nip05: string): Promise<string | null>
}
}
/**
* Verify that a NIP-05 identifier matches the given pubkey
*/
export async function verifyNIP05(nip05: string, pubkey: string): Promise<boolean> {
const verifiedPubkey = await verifyAndGetPubkey(nip05);
if (!verifiedPubkey) {
return false;
}
return verifiedPubkey.toLowerCase() === pubkey.toLowerCase();
}
/**
* Check if a string looks like a NIP-05 identifier
*/