applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
52 lines (51 loc) • 2.11 kB
JavaScript
import { getHiddenContent, getOrComputeCachedValue, getTagValue, isHiddenContentLocked, isNIP04Encrypted, setHiddenContentEncryptionMethod, unixNow, unlockHiddenContent, } from "applesauce-core/helpers";
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 isWalletRequestLocked(request) {
return isHiddenContentLocked(request);
}
/** Unlocks a kind 23194 event */
export async function unlockWalletRequest(request, signer) {
await unlockHiddenContent(request, signer);
return getWalletRequest(request);
}
/** Gets the wallet request from a kind 23194 event */
export function getWalletRequest(request) {
if (isWalletRequestLocked(request))
return undefined;
return getOrComputeCachedValue(request, WalletRequestSymbol, () => {
const content = getHiddenContent(request);
if (!content)
return null;
return JSON.parse(content);
});
}
/** Returns the wallet service pubkey from a request */
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";
}