applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
48 lines (47 loc) • 2.27 kB
JavaScript
import { getHiddenContent, isHiddenContentUnlocked, notifyEventUpdate, setHiddenContentEncryptionMethod, unlockHiddenContent, } from "applesauce-core/helpers";
export const WALLET_NOTIFICATION_KIND = 23197;
export const WALLET_LEGACY_NOTIFICATION_KIND = 23196;
/** A symbol used to cache the wallet notification on the event */
export const WalletNotificationSymbol = Symbol("wallet-notification");
// Setup the encryption method to use for notification kinds
setHiddenContentEncryptionMethod(WALLET_NOTIFICATION_KIND, "nip44");
setHiddenContentEncryptionMethod(WALLET_LEGACY_NOTIFICATION_KIND, "nip04");
/** Checks if a kind 23196 or 23197 event is locked */
export function isWalletNotificationUnlocked(notification) {
// Wrap in try catch to avoid throwing validation errors
try {
return (WalletNotificationSymbol in notification ||
(isHiddenContentUnlocked(notification) && getWalletNotification(notification) !== undefined));
}
catch { }
return false;
}
/** Unlocks a kind 23196 or 23197 event */
export async function unlockWalletNotification(notification, signer) {
if (WalletNotificationSymbol in notification)
return notification[WalletNotificationSymbol];
await unlockHiddenContent(notification, signer);
const parsed = getWalletNotification(notification);
if (!parsed)
throw new Error("Failed to unlock wallet notification");
return parsed;
}
/** Gets the wallet notification from a kind 23196 or 23197 event */
export function getWalletNotification(notification) {
if (WalletNotificationSymbol in notification)
return notification[WalletNotificationSymbol];
// Get the encrypted content
const content = getHiddenContent(notification);
if (!content)
return undefined;
// Parse the content as a wallet notification
const parsed = JSON.parse(content);
// Save the parsed content
Reflect.set(notification, WalletNotificationSymbol, parsed);
notifyEventUpdate(notification);
return parsed;
}
/** Checks if an event is a valid wallet notification event */
export function isValidWalletNotification(notification) {
return notification.kind === WALLET_NOTIFICATION_KIND || notification.kind === WALLET_LEGACY_NOTIFICATION_KIND;
}