|
| 1 | +# Using a Cashu Wallet (NIP-60) with NDK |
| 2 | + |
| 3 | +This snippet demonstrates how to create, configure, and use an NDKCashuWallet for managing Cashu tokens. |
| 4 | + |
| 5 | +```typescript |
| 6 | +import NDK, { NDKKind, NDKCashuMintList } from "@nostr-dev-kit/ndk"; |
| 7 | +import { NDKCashuWallet, NDKWalletStatus } from "@nostr-dev-kit/ndk-wallet"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Example function to set up an NDKCashuWallet |
| 11 | + */ |
| 12 | +async function setupCashuWallet(ndk: NDK, mints: string[], relays: string[]) { |
| 13 | + // Create the Cashu wallet instance |
| 14 | + const wallet = new NDKCashuWallet(ndk); |
| 15 | + |
| 16 | + // Add mints to the wallet |
| 17 | + wallet.mints = mints; |
| 18 | + wallet.relays = relays; |
| 19 | + |
| 20 | + // Generate or load a p2pk (Pay-to-Public-Key) token |
| 21 | + // This is used for receiving payments with NIP-61 (nutzaps) |
| 22 | + const p2pk = await wallet.getP2pk(); |
| 23 | + console.log(`Wallet p2pk: ${p2pk}`); |
| 24 | + |
| 25 | + await wallet.publish(); |
| 26 | + console.log('published the wallet event') |
| 27 | + |
| 28 | + // configure reception of NIP-61 nutzaps for the user |
| 29 | + // this publishes an event that tells others who want to zap |
| 30 | + // this user the information they need to publish a NIP-61 nutzap. |
| 31 | + const mintlistForNutzapReception = new NDKCashuMintList(ndk); |
| 32 | + mintlistForNutzapReception.relays = wallet.relays; |
| 33 | + mintlistForNutzapReception.mints = wallet.mints; |
| 34 | + mintlistForNutzapReception.p2pk = wallet.p2pk; |
| 35 | + await mintlistForNutzapReception.publish(); |
| 36 | + console.log('published he nutzap mintlist event to receive nutzaps', mintlistForNutzapReception.rawEvent()) |
| 37 | + |
| 38 | + return wallet; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Get balance for a specific mint |
| 43 | + */ |
| 44 | +function getMintBalance(wallet: NDKCashuWallet, mintUrl: string) { |
| 45 | + const balance = wallet.mintBalance(mintUrl); |
| 46 | + console.log(`Balance for mint ${mintUrl}: ${balance} sats`); |
| 47 | + return balance; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Check if the user already has a nutsack (NIP-60) wallet. |
| 52 | + **/ |
| 53 | +async function findExistingWallet(ndk: NDK): Promise<NDKCashuWallet | undefined> { |
| 54 | + const activeUser = ndk.activeUser; |
| 55 | + |
| 56 | + if (!activeUser) throw "we need a user first, set a signer in ndk"; |
| 57 | + |
| 58 | + const event = await ndk.fetchEvent([ |
| 59 | + { kinds: [ NDKKind.CashuWallet], authors: [activeUser.pubkey] } |
| 60 | + ]) |
| 61 | + |
| 62 | + // if we receive a CashuWallet event we load the wallet |
| 63 | + if (event) return await NDKCashuWallet.from(event); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Example usage |
| 68 | + */ |
| 69 | +async function main() { |
| 70 | + // we assume ndk is already connected and ready |
| 71 | + // ... |
| 72 | + |
| 73 | + let wallet: NDKCashuWallet | undefined; |
| 74 | + |
| 75 | + wallet = await findExistingWallet(ndk); |
| 76 | + |
| 77 | + // if we don't have a wallet, we create one |
| 78 | + if (!wallet) { |
| 79 | + // List of mints to use |
| 80 | + const mints = [ "https://8333.space:3338" ]; |
| 81 | + |
| 82 | + // Setup the wallet |
| 83 | + wallet = await setupCashuWallet(ndk, mints); |
| 84 | + } |
| 85 | + |
| 86 | + wallet.on("balance_updated", (balance) => { |
| 87 | + console.log(`Wallet balance updated: ${balance?.amount} sats`); |
| 88 | + // You might want to update your UI here |
| 89 | + }); |
| 90 | + |
| 91 | + wallet.start(); |
| 92 | + |
| 93 | + // Example: Check wallet balance |
| 94 | + const totalBalance = wallet.balance?.amount || 0; |
| 95 | + console.log(`Total wallet balance: ${totalBalance} sats`); |
| 96 | + |
| 97 | + // Example: Need to fund wallet? |
| 98 | + // See the Cashu Deposits snippet for funding your wallet with lightning |
| 99 | + |
| 100 | + // Example: Get balance for specific mint |
| 101 | + for (const mint of mints) { |
| 102 | + getMintBalance(wallet, mint); |
| 103 | + } |
| 104 | + |
| 105 | + // Note: For monitoring nutzaps, see the Nutzap Monitor snippet |
| 106 | + |
| 107 | + // Keep the connection open for monitoring |
| 108 | + // In a real app, you'd use proper lifecycle management |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Notes |
| 113 | + |
| 114 | +- The Cashu wallet implements the NIP-60 specification for Nostr eCash |
| 115 | +- The wallet needs to be assigned to the NDK instance with `ndk.wallet = wallet` for full integration |
| 116 | +- To receive tokens, you need to generate a p2pk (Pay-to-Public-Key) identifier |
| 117 | +- The wallet monitors and processes events continuously while active |
| 118 | +- Always handle wallet operations with proper error handling |
| 119 | +- For handling nutzaps (Cashu tokens sent via Nostr), see the Nutzap Monitor snippet |
| 120 | +- For depositing funds to your wallet, see the Cashu Deposits snippet |
0 commit comments