applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
98 lines (97 loc) • 4.11 kB
JavaScript
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) => [
"pay_invoice",
"multi_pay_invoice",
"pay_keysend",
"multi_pay_keysend",
"make_invoice",
"lookup_invoice",
"list_transactions",
"get_balance",
"get_info",
"notifications",
].includes(part))
.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 ?? [];
}