fix: normalize supported NIPs to strings

This commit is contained in:
Alejandro Gómez
2026-03-06 15:09:02 +01:00
parent a7c70fa0a6
commit 80bd6c4e72
7 changed files with 24 additions and 18 deletions

View File

@@ -64,7 +64,7 @@ async function checkNip45Support(url: string): Promise<boolean | null> {
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
}

View File

@@ -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) => (
<NIPBadge
key={nip}
nipNumber={nip.toString().padStart(2, "0")}
nipNumber={nip.padStart(2, "0")}
showName={true}
/>
))}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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", () => {

View File

@@ -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));
}
/**

View File

@@ -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;