From 3bbeb113a78b36bfd76ab5fd7e534d45400edd98 Mon Sep 17 00:00:00 2001 From: Danny Morabito Date: Tue, 26 Nov 2024 21:21:32 +0100 Subject: [PATCH] add utilities to convert nostr npubs to and from hex strings --- src/nostr.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/nostr.ts b/src/nostr.ts index 4b4cd80..af68454 100644 --- a/src/nostr.ts +++ b/src/nostr.ts @@ -1,6 +1,7 @@ import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk"; import {generateSecretKey} from "nostr-tools"; import {randomTimeUpTo2DaysInThePast} from "./general.ts"; +import {decode as nip19Decode, npubEncode} from "nostr-tools/nip19"; export async function encryptEventForRecipient( ndk: NDK, @@ -25,3 +26,14 @@ export async function encryptEventForRecipient( giftWrap.ndk = ndk; return giftWrap; } + +export function npubToPubKeyString(npub: string) { + const decoded = nip19Decode(npub); + if (decoded.type !== 'npub') + throw new Error('Invalid npub'); + return decoded.data; +} + +export function pubKeyStringToNpub(pubKey: string) { + return npubEncode(pubKey); +}