mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-09 06:57:07 +02:00
fix: normalize supported NIPs to strings
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
/>
|
||||
))}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user