Learn how to receive Bitcoin deposits through the Lightning Network. This guide covers Lightning invoice creation, receiving payments, and integrating Lightning deposits into your Spark wallet.
To send and receive Lightning payments, you can generate and pay Lightning invoices. A Lightning invoice (also called a payment request) is a specially formatted string that contains all the information needed to make a Lightning Network payment:
Amount: How many satoshis to send (can be omitted for zero-amount invoices)
Destination: The recipient’s node public key
Payment Hash: A unique identifier for the payment
Description: Optional memo describing the payment
Expiry: How long the invoice is valid for (default 24 hours)
Lightning invoices start with “ln” followed by the network identifier (bc for mainnet) and typically look like this: lnbc1...Mainnet invoice Example:
Generate Lightning invoices to receive Bitcoin payments that will be deposited into your Spark wallet.createLightningInvoice(params)Creates a Lightning invoice for receiving Bitcoin payments.
By passing in true for includeSparkAddress, a 36-byte string consisting of a recognizable header and a receiver’s compressed identity public key SPK:identitypubkey will get embedded in the fallback address (f) field of a BOLT11 invoice:
Copy
Ask AI
const invoice = await wallet.createLightningInvoice({ amountSats: 100, memo: "Invoice with Spark address", includeSparkAddress: true,});console.log("Invoice with Spark address:", invoice);
To generate an invoice for another Spark user, pass in the 33-byte compressed identity pubkey as a string to receiverIdentityPubkey:
Copy
Ask AI
const invoice = await wallet.createLightningInvoice({ amountSats: 100, memo: "Invoice for another user", receiverIdentityPubkey: "033b4f8cf891e45e2e3995e29b3c8b3d4d4e67f8a9b2c1d3e4f567890abcdef12",});console.log("Invoice for another user:", invoice);
If a wallet is generating an invoice for itself and wants to embed its own Spark identity in the invoice, it will not need to pass in a receiverIdentityPubkey to embed a Spark address. That will get taken care of on the backend. Passing it in shouldn’t change anything though.
Spark supports creating zero-amount Lightning invoices. Zero-amount invoices don’t have a fixed amount and allow the sender to specify the amount when making the payment.
Zero-amount invoices are not widely supported across the Lightning Network. Some exchanges, such as Binance, currently do not support them.
Track incoming Lightning payments and their status using receive request monitoring.getLightningReceiveRequest(id)Gets the status of a Lightning receive request by ID.
Copy
Ask AI
// Monitor a specific invoiceconst receiveRequest = await wallet.getLightningReceiveRequest(invoiceId);console.log("Payment status:", receiveRequest.status);console.log("Amount received:", receiveRequest.amountReceived);// Check if payment is completeif (receiveRequest.status === "completed") { console.log("Payment received successfully!");}
Implement proper error handling for Lightning invoice operations.
Copy
Ask AI
async function createInvoiceSafely(params) { try { // Validate amount if (params.amountSats <= 0) { throw new Error("Amount must be greater than 0"); } // Validate expiry time if (params.expiryTime && params.expiryTime < 60) { throw new Error("Expiry time must be at least 60 seconds"); } // Create invoice const invoice = await wallet.createLightningInvoice(params); console.log("Invoice created successfully:", invoice.id); return invoice; } catch (error) { console.error("Failed to create invoice:", error.message); // Handle specific error types if (error.message.includes("Amount")) { console.log("Please check the amount value"); } else if (error.message.includes("Expiry")) { console.log("Please check the expiry time"); } else if (error.message.includes("Network")) { console.log("Network error. Please try again."); } throw error; }}
You can use getBalance() to check a wallet balance after receiving payments.The getBalance() method returns a Promise resolving to an object containing:
balance: A bigint representing the total amount in satoshis
tokenBalances: A Map of token balances, where each entry contains:
balance: A bigint representing the token amount
tokenInfo: Information about the specific token the wallet is holding