UNPKG

applesauce-wallet-connect

Version:

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

36 lines (35 loc) 1.65 kB
import { getHiddenContent, getOrComputeCachedValue, getTagValue, isHiddenContentLocked, isNIP04Encrypted, setHiddenContentEncryptionMethod, unlockHiddenContent, } from "applesauce-core/helpers"; export const WALLET_RESPONSE_KIND = 23195; // Set the encryption method to use for response kind setHiddenContentEncryptionMethod(WALLET_RESPONSE_KIND, "nip04"); /** A symbol used to cache the wallet response on the event */ export const WalletResponseSymbol = Symbol("wallet-response"); /** Checks if a kind 23195 event is locked */ export function isWalletResponseLocked(response) { return isHiddenContentLocked(response); } /** Unlocks a kind 23195 event */ export async function unlockWalletResponse(response, signer, override) { const encryption = override ?? (!isNIP04Encrypted(response.content) ? "nip44" : "nip04"); await unlockHiddenContent(response, signer, encryption); return getWalletResponse(response); } /** Gets the wallet response from a kind 23195 event */ export function getWalletResponse(response) { if (isWalletResponseLocked(response)) return undefined; return getOrComputeCachedValue(response, WalletResponseSymbol, () => { const content = getHiddenContent(response); if (!content) return null; return JSON.parse(content); }); } /** Returns the client pubkey of client this response is for */ export function getWalletResponseClientPubkey(response) { return getTagValue(response, "p"); } /** Returns the request id of the request this response is for */ export function getWalletResponseRequestId(response) { return getTagValue(response, "e"); }