applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
73 lines (72 loc) • 3.18 kB
JavaScript
import { isNIP04Encrypted } from "applesauce-core/helpers/encryption";
import { getTagValue, notifyEventUpdate } from "applesauce-core/helpers/event";
import { getHiddenContent, isHiddenContentUnlocked, setHiddenContentEncryptionMethod, unlockHiddenContent, } from "applesauce-core/helpers/hidden-content";
import { unixNow } from "applesauce-core/helpers/time";
import { nip47EncryptionMethodToNip07EncryptionMethod } from "./encryption.js";
export const WALLET_REQUEST_KIND = 23194;
// Set the encryption method to use for request kind
setHiddenContentEncryptionMethod(WALLET_REQUEST_KIND, "nip44");
/** A symbol used to cache the wallet request on the event */
export const WalletRequestSymbol = Symbol("wallet-request");
/** Checks if a kind 23194 event is locked */
export function isWalletRequestUnlocked(request) {
// Wrap in try catch to avoid throwing validation errors
try {
return (WalletRequestSymbol in request || (isHiddenContentUnlocked(request) && getWalletRequest(request) !== undefined));
}
catch { }
return false;
}
/** Unlocks a kind 23194 event */
export async function unlockWalletRequest(request, signer, override) {
if (WalletRequestSymbol in request)
return request[WalletRequestSymbol];
// Get the encryption method from the tag or default to nip04 (pre NIP-47)
const encryption = override ?? nip47EncryptionMethodToNip07EncryptionMethod(getWalletRequestEncryption(request));
await unlockHiddenContent(request, signer, encryption);
const parsed = getWalletRequest(request);
if (!parsed)
throw new Error("Failed to unlock wallet request");
return parsed;
}
/** Gets the wallet request from a kind 23194 event */
export function getWalletRequest(request) {
if (WalletRequestSymbol in request)
return request[WalletRequestSymbol];
const content = getHiddenContent(request);
if (!content)
return undefined;
const parsed = JSON.parse(content);
// Save the parsed content
Reflect.set(request, WalletRequestSymbol, parsed);
notifyEventUpdate(request);
return parsed;
}
export function getWalletRequestServicePubkey(request) {
return getTagValue(request, "p");
}
/** Returns the expiration timestamp from a request */
export function getWalletRequestExpiration(request) {
const expiration = getTagValue(request, "expiration");
return expiration ? parseInt(expiration, 10) : undefined;
}
/** Checks if a request has expired */
export function isWalletRequestExpired(request) {
const expiration = getWalletRequestExpiration(request);
if (!expiration)
return false;
return unixNow() > expiration;
}
/** Gets the encryption method used for a request */
export function getWalletRequestEncryption(request) {
const encryption = getTagValue(request, "encryption");
return encryption
? encryption
: isNIP04Encrypted(request.content)
? "nip04"
: "nip44_v2";
}
/** Checks if an event is a valid wallet request event */
export function isValidWalletRequest(request) {
return request.kind === WALLET_REQUEST_KIND && getWalletRequestServicePubkey(request) !== undefined;
}