UNPKG

applesauce-wallet-connect

Version:

NIP-47 Nostr Wallet Connect implementation for both clients and services.

85 lines (84 loc) 3.75 kB
import { getOrComputeCachedValue, getTagValue, isEvent } from "applesauce-core/helpers"; export const WALLET_INFO_KIND = 13194; /** A symbol used to cache the wallet info on the event */ export const WalletInfoSymbol = Symbol("wallet-info"); /** Gets the wallet info from a kind 13194 event */ export function getWalletSupport(info) { if (info.kind !== WALLET_INFO_KIND) return null; return getOrComputeCachedValue(info, WalletInfoSymbol, () => { const content = info.content.trim(); if (!content) return null; // Parse methods from content (space-separated) const contentParts = content.split(/\s+/); const methods = contentParts.filter((part) => part !== "notifications"); // Parse encryption methods from encryption tag const encryptionTag = getTagValue(info, "encryption"); const encryption = encryptionTag ? encryptionTag .split(/\s+/) .filter((method) => method === "nip44_v2" || method === "nip04") : ["nip04"]; // Default to nip04 if no encryption tag is present // Parse notifications from notifications tag const notificationsTag = getTagValue(info, "notifications"); const notifications = notificationsTag ? notificationsTag .split(/\s+/) .filter((notif) => notif === "payment_received" || notif === "payment_sent") : undefined; return { methods, encryption, notifications, }; }); } /** Gets the encryption methods from a wallet info event */ export function getEncryptionMethods(info) { const walletInfo = isEvent(info) ? getWalletSupport(info) : info; return walletInfo?.encryption ?? []; } /** Checks if the wallet service supports a specific encryption method */ export function supportsEncryption(info, encryption) { const encryptionMethods = getEncryptionMethods(info); return encryptionMethods.includes(encryption); } /** Gets the preferred encryption method (nip44_v2 preferred over nip04) */ export function getPreferredEncryption(info) { const encryptionMethods = getEncryptionMethods(info); if (encryptionMethods.length === 0) return "nip04"; // Prefer nip44_v2 over nip04 if (encryptionMethods.includes("nip44_v2")) return "nip44_v2"; if (encryptionMethods.includes("nip04")) return "nip04"; // Absence of this tag implies that the wallet only supports nip04. return "nip04"; } /** Checks if the wallet service supports a specific method */ export function supportsMethod(info, method) { const walletInfo = isEvent(info) ? getWalletSupport(info) : info; return walletInfo?.methods.includes(method) ?? false; } /** Checks if the wallet service supports notifications */ export function supportsNotifications(info) { const walletInfo = isEvent(info) ? getWalletSupport(info) : info; return (walletInfo?.notifications?.length ?? 0) > 0; } /** Checks if the wallet service supports a specific notification type */ export function supportsNotificationType(info, notificationType) { const walletInfo = isEvent(info) ? getWalletSupport(info) : info; return walletInfo?.notifications?.includes(notificationType) ?? false; } /** Gets all supported methods from the wallet info */ export function getSupportedMethods(info) { const walletInfo = isEvent(info) ? getWalletSupport(info) : info; return walletInfo?.methods ?? []; } /** Gets all supported notifications from the wallet info */ export function getSupportedNotifications(info) { const walletInfo = isEvent(info) ? getWalletSupport(info) : info; return walletInfo?.notifications ?? []; }