Browse Source

switched to npub

master
silberengel 7 months ago
parent
commit
c7e3cbf993
  1. 30
      src/lib/components/publications/PublicationFeed.svelte
  2. 17
      src/lib/utils/nostrUtils.ts

30
src/lib/components/publications/PublicationFeed.svelte

@ -7,6 +7,7 @@
import { onMount, onDestroy } from "svelte"; import { onMount, onDestroy } from "svelte";
import { import {
getMatchingTags, getMatchingTags,
toNpub,
} from "$lib/utils/nostrUtils"; } from "$lib/utils/nostrUtils";
import { WebSocketPool } from "$lib/data_structures/websocket_pool"; import { WebSocketPool } from "$lib/data_structures/websocket_pool";
import { NDKEvent } from "@nostr-dev-kit/ndk"; import { NDKEvent } from "@nostr-dev-kit/ndk";
@ -292,32 +293,13 @@
loading = false; 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 => { const convertToNpub = (input: string): string | null => {
try { const result = toNpub(input);
// If it's already an npub, return it if (!result) {
if (input.startsWith('npub')) { console.debug("[PublicationFeed] Failed to convert to npub:", input);
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;
} }
return result;
}; };
// Function to filter events by npub (author or p tags) // Function to filter events by npub (author or p tags)

17
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 { export function toNpub(pubkey: string | undefined): string | null {
if (!pubkey) return null; if (!pubkey) return null;
try { 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)) { if (new RegExp(`^[a-f0-9]{${VALIDATION.HEX_LENGTH}}$`, "i").test(pubkey)) {
return nip19.npubEncode(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; return null;
} catch { } catch {
return null; return null;

Loading…
Cancel
Save