From 80bd6c4e72f1a9ede5dfffe7358219ae5585f641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20G=C3=B3mez?= Date: Fri, 6 Mar 2026 15:09:02 +0100 Subject: [PATCH] fix: normalize supported NIPs to strings --- src/components/CountViewer.tsx | 2 +- src/components/nostr/RelaySupportedNips.tsx | 4 ++-- src/hooks/useRelayInfo.ts | 2 +- src/lib/nip11.ts | 11 +++++++++-- src/lib/nip66-helpers.test.ts | 6 +++--- src/lib/nip66-helpers.ts | 13 ++++++------- src/types/nip11.ts | 4 ++-- 7 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/components/CountViewer.tsx b/src/components/CountViewer.tsx index 4a98dd9..aedf2bc 100644 --- a/src/components/CountViewer.tsx +++ b/src/components/CountViewer.tsx @@ -64,7 +64,7 @@ async function checkNip45Support(url: string): Promise { const info = await getRelayInfo(url); if (!info) return null; // Couldn't fetch relay info if (!info.supported_nips) return null; // No NIP support info available - return info.supported_nips.includes(45); + return info.supported_nips.includes("45"); } catch { return null; // Error fetching info } diff --git a/src/components/nostr/RelaySupportedNips.tsx b/src/components/nostr/RelaySupportedNips.tsx index c0cfe8d..15a2366 100644 --- a/src/components/nostr/RelaySupportedNips.tsx +++ b/src/components/nostr/RelaySupportedNips.tsx @@ -1,7 +1,7 @@ import { NIPBadge } from "@/components/NIPBadge"; interface RelaySupportedNipsProps { - nips: number[]; + nips: string[]; title?: string; showTitle?: boolean; } @@ -28,7 +28,7 @@ export function RelaySupportedNips({ {nips.map((nip) => ( ))} diff --git a/src/hooks/useRelayInfo.ts b/src/hooks/useRelayInfo.ts index 3cd2e58..2886561 100644 --- a/src/hooks/useRelayInfo.ts +++ b/src/hooks/useRelayInfo.ts @@ -61,5 +61,5 @@ export function useRelaySupportsNip( const info = useRelayInfo(wsUrl); if (!info) return undefined; - return info.supported_nips?.includes(nipNumber) ?? false; + return info.supported_nips?.includes(String(nipNumber)) ?? false; } diff --git a/src/lib/nip11.ts b/src/lib/nip11.ts index 9550db5..7bc1186 100644 --- a/src/lib/nip11.ts +++ b/src/lib/nip11.ts @@ -29,7 +29,14 @@ export async function fetchRelayInfo( if (!response.ok) return null; - return (await response.json()) as RelayInformation; + const info = (await response.json()) as RelayInformation; + + // Normalize supported_nips to strings (relays may return numbers, strings, or mixed) + if (info.supported_nips) { + info.supported_nips = info.supported_nips.map(String); + } + + return info; } catch (error) { console.warn(`NIP-11: Failed to fetch ${wsUrl}:`, error); return null; @@ -140,7 +147,7 @@ export async function relaySupportsNip( try { const normalizedUrl = normalizeRelayURL(wsUrl); const info = await getRelayInfo(normalizedUrl); - return info?.supported_nips?.includes(nipNumber) ?? false; + return info?.supported_nips?.includes(String(nipNumber)) ?? false; } catch (error) { console.warn(`NIP-11: Failed to check NIP support for ${wsUrl}:`, error); return false; diff --git a/src/lib/nip66-helpers.test.ts b/src/lib/nip66-helpers.test.ts index 88beecd..d3ec2aa 100644 --- a/src/lib/nip66-helpers.test.ts +++ b/src/lib/nip66-helpers.test.ts @@ -185,7 +185,7 @@ describe("Kind 30166 (Relay Discovery) Helpers", () => { ["N", "42"], ], }); - expect(getSupportedNips(event)).toEqual([1, 11, 42, 65]); + expect(getSupportedNips(event)).toEqual(["1", "11", "42", "65"]); }); it("should deduplicate NIPs", () => { @@ -197,7 +197,7 @@ describe("Kind 30166 (Relay Discovery) Helpers", () => { ["N", "1"], ], }); - expect(getSupportedNips(event)).toEqual([1, 11]); + expect(getSupportedNips(event)).toEqual(["1", "11"]); }); it("should filter out invalid NIP numbers", () => { @@ -209,7 +209,7 @@ describe("Kind 30166 (Relay Discovery) Helpers", () => { ["N", "11"], ], }); - expect(getSupportedNips(event)).toEqual([1, 11]); + expect(getSupportedNips(event)).toEqual(["1", "11"]); }); it("should return empty array if no NIP tags", () => { diff --git a/src/lib/nip66-helpers.ts b/src/lib/nip66-helpers.ts index fc3231e..b886003 100644 --- a/src/lib/nip66-helpers.ts +++ b/src/lib/nip66-helpers.ts @@ -59,18 +59,17 @@ export function getRelayType(event: NostrEvent): string | undefined { } /** - * Get array of supported NIP numbers + * Get array of supported NIP numbers as strings * @param event Relay discovery event (kind 30166) - * @returns Array of NIP numbers + * @returns Array of NIP number strings, sorted numerically */ -export function getSupportedNips(event: NostrEvent): number[] { +export function getSupportedNips(event: NostrEvent): string[] { const nips = event.tags - .filter((t) => t[0] === "N") - .map((t) => parseInt(t[1], 10)) - .filter((n) => !isNaN(n)); + .filter((t) => t[0] === "N" && t[1] && !isNaN(Number(t[1]))) + .map((t) => t[1]); // Return unique sorted NIPs - return Array.from(new Set(nips)).sort((a, b) => a - b); + return Array.from(new Set(nips)).sort((a, b) => Number(a) - Number(b)); } /** diff --git a/src/types/nip11.ts b/src/types/nip11.ts index 2a84a21..0de319b 100644 --- a/src/types/nip11.ts +++ b/src/types/nip11.ts @@ -16,8 +16,8 @@ export interface RelayInformation { /** Administrative contact for the relay */ contact?: string; - /** List of NIPs supported by this relay */ - supported_nips?: number[]; + /** List of NIPs supported by this relay (normalized to strings) */ + supported_nips?: string[]; /** Software version running the relay */ software?: string;