add utilities to convert nostr npubs to and from hex strings

This commit is contained in:
Danny Morabito 2024-11-26 21:21:32 +01:00
parent 4a177547fb
commit 3bbeb113a7
Signed by: dannym
GPG key ID: 7CC8056A5A04557E

View file

@ -1,6 +1,7 @@
import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk"; import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk";
import {generateSecretKey} from "nostr-tools"; import {generateSecretKey} from "nostr-tools";
import {randomTimeUpTo2DaysInThePast} from "./general.ts"; import {randomTimeUpTo2DaysInThePast} from "./general.ts";
import {decode as nip19Decode, npubEncode} from "nostr-tools/nip19";
export async function encryptEventForRecipient( export async function encryptEventForRecipient(
ndk: NDK, ndk: NDK,
@ -25,3 +26,14 @@ export async function encryptEventForRecipient(
giftWrap.ndk = ndk; giftWrap.ndk = ndk;
return giftWrap; 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);
}