Understanding Lightning Invoices
The Spark Service Provider enables trustless Lightning interoperability. 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)
lnbc1...
Mainnet invoice Example:
lnbcrt2500n1pj0ytfcpp5qqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqdx40shp5jqp3qymd6qgpy99ppk0jqjzylqg5t7fhqhpl6s4kxmqgmrn59w5k0z0cqqqqqqzqqqqq9qsqqqqqq9qqqqqqgq9qsqxl9l55y5cwa9s2h8nvdh4h7h43tcwjdcysf7v0fprz5uh6vshs4n0tvhgzz2xgcqpg8yqv7
Receiving Lightning Payments
To receive a payment, generate a lightning invoice then share it with the sender.Bolt11 Support Updates
createLightningInvoice
now accepts two new optional parameters: includeSparkAddress
and receiverIdentityPubkey
.
Embedding Spark Addresses
By passing intrue
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:
Creating Invoices for Other Spark Users
To generate an invoice for another Spark user, simply pass in the 33-byte compressed identity pubkey as a string toreceiverIdentityPubkey
:
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.Fallback Address Format
It will look something like this:Zero-Amount Invoices
Spark supports creating and paying 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.Creating Zero-Amount Invoices
To create a zero-amount invoice, pass0
for the amountSats
parameter:
Paying Zero-Amount Invoices
When paying a zero-amount invoice, you need to specify the amount using theamountSatsToSend
parameter:
The
amountSatsToSend
parameter is only used for zero-amount invoices. For regular invoices with a fixed amount, this parameter is ignored.Get payment status
To check the status of a Lightning payment, you can use thegetLightningReceiveRequest
Sending Lightning Payments
To send a payment, you’ll need a Lightning invoice from the recipient and the maximum amount of fees to pay. We recommend setting the maximum routing fee to whichever is greater 5 sats or 17 bps * transaction amount.Spark Transfer Preference
payLightningInvoice
now accepts a preferSpark
boolean that defaults to false
:
preferSpark
is set to true
, Spark wallets will initiate a Spark transfer instead of a Lightning transfer if a valid Spark address is found in the invoice. If not, a regular Lightning payment will occur.
Checking Balance
You can usegetBalance()
to check a Wallet balance after sending or receiving payments.
The getBalance()
method returns a Promise resolving to an object containing:
balance
: Abigint
representing the total amount in satoshistokenBalances
: A Map of token balances, where each entry contains:balance
: Abigint
representing the token amounttokenInfo
: information about the specific token the wallet is holding
Best Practices
- Always verify invoice amounts before paying
- In order to analyze invoice data, you can use the bolt11 library
- Keep track of invoice ids to match balance changes to created invoices
- Set appropriate expiry times for invoices if necesary
- Monitor payment status for confirmation
Need Help?
- Check our FAQ
- Review the API Reference
- Follow our Testing Guide
- Check our Code Samples