From c7e3cbf993614f2cb12ffaa2f53fcb61a57c4dd8 Mon Sep 17 00:00:00 2001 From: silberengel Date: Mon, 11 Aug 2025 07:27:05 +0200 Subject: [PATCH] switched to npub --- .../publications/PublicationFeed.svelte | 30 ++++--------------- src/lib/utils/nostrUtils.ts | 17 +++++++++-- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/lib/components/publications/PublicationFeed.svelte b/src/lib/components/publications/PublicationFeed.svelte index e95ddf5..0a2593b 100644 --- a/src/lib/components/publications/PublicationFeed.svelte +++ b/src/lib/components/publications/PublicationFeed.svelte @@ -7,6 +7,7 @@ import { onMount, onDestroy } from "svelte"; import { getMatchingTags, + toNpub, } from "$lib/utils/nostrUtils"; import { WebSocketPool } from "$lib/data_structures/websocket_pool"; import { NDKEvent } from "@nostr-dev-kit/ndk"; @@ -292,32 +293,13 @@ loading = false; } - // Function to convert various Nostr identifiers to npub + // Function to convert various Nostr identifiers to npub using the utility function const convertToNpub = (input: string): string | null => { - try { - // If it's already an npub, return it - if (input.startsWith('npub')) { - return input; - } - - // If it's a hex pubkey, convert to npub - if (input.length === 64 && /^[0-9a-fA-F]+$/.test(input)) { - return nip19.npubEncode(input); - } - - // If it's an nprofile, decode and extract npub - if (input.startsWith('nprofile')) { - const decoded = nip19.decode(input); - if (decoded.type === 'nprofile') { - return decoded.data.pubkey ? nip19.npubEncode(decoded.data.pubkey) : null; - } - } - - return null; - } catch (error) { - console.debug("[PublicationFeed] Failed to convert to npub:", input, error); - return null; + const result = toNpub(input); + if (!result) { + console.debug("[PublicationFeed] Failed to convert to npub:", input); } + return result; }; // Function to filter events by npub (author or p tags) diff --git a/src/lib/utils/nostrUtils.ts b/src/lib/utils/nostrUtils.ts index d3be24d..06ae2bf 100644 --- a/src/lib/utils/nostrUtils.ts +++ b/src/lib/utils/nostrUtils.ts @@ -519,15 +519,28 @@ export async function fetchEventWithFallback( } /** - * Converts a hex pubkey to npub, or returns npub if already encoded. + * Converts various Nostr identifiers to npub format. + * Handles hex pubkeys, npub strings, and nprofile strings. */ export function toNpub(pubkey: string | undefined): string | null { if (!pubkey) return null; try { + // If it's already an npub, return it + if (pubkey.startsWith("npub")) return pubkey; + + // If it's a hex pubkey, convert to npub if (new RegExp(`^[a-f0-9]{${VALIDATION.HEX_LENGTH}}$`, "i").test(pubkey)) { return nip19.npubEncode(pubkey); } - if (pubkey.startsWith("npub1")) return pubkey; + + // If it's an nprofile, decode and extract npub + if (pubkey.startsWith("nprofile")) { + const decoded = nip19.decode(pubkey); + if (decoded.type === 'nprofile') { + return decoded.data.pubkey ? nip19.npubEncode(decoded.data.pubkey) : null; + } + } + return null; } catch { return null;