Overview
The invoice module provides comprehensive Lightning Network invoice creation, parsing, and management. It handles invoice secret generation, persistence, metadata encryption, and builder-pattern invoice construction.Invoice Structure
Represents a parsed Lightning Network invoice.The original invoice string (bech32 encoded)
Optional on-chain fallback address
Payment amount in millisatoshis
Recipient node public key (compressed)
Payment hash (32 bytes)
Unix timestamp when invoice expires
Invoice description/memo
Payment amount in satoshis (MilliSat / 1000)
ParseInvoice
Parses a Lightning Network invoice string.Lightning invoice string (with or without “lightning:” prefix)
Network configuration (mainnet, testnet, regtest)
*Invoice - Parsed invoice structureErrors:
ErrInvalidInvoice
Example
Invoice Generation
InvoiceSecrets
Bundle of cryptographic secrets required for invoice generation.SHA256 hash of the preimage
Derived identity key for signing invoices
User’s HTLC key for receiving payments
Muun’s HTLC key for receiving payments
Synthetic short channel ID
GenerateInvoiceSecrets
Generates new invoice secrets to register with the server.User’s HD public key
Muun’s HD public key
*InvoiceSecretsList - List of secrets to registerGenerates: Up to
MaxUnusedSecrets (5) new secrets
Example
PersistInvoiceSecrets
Stores registered secrets in the local database.List of secrets returned from server registration
error if database operation fails
Example
InvoiceBuilder
Builder pattern for creating Lightning invoices.Builder Methods
Network
Network configuration
UserKey
User’s private key for signing
Description
Invoice description/memo
AmountSat
Amount in satoshis
AmountMSat
Amount in millisatoshis
AddRouteHints
Routing hints for reaching the node
Metadata
Additional encrypted metadata
Build
Creates and returns the signed invoice string.string - Bech32 encoded Lightning invoiceErrors: Database errors, signing errors, no unused secrets available
Complete Example
RouteHints
Routing information for reaching the invoice recipient.Public key of the routing node (hex)
Base routing fee in millisatoshis
Proportional fee in millionths (ppm)
CLTV expiry delta in blocks
OperationMetadata
Encrypted metadata stored with invoices.The generated invoice string
LNURL sender identifier
GetInvoiceMetadata
Retrieves encrypted metadata for a payment hash.Payment hash (32 bytes)
string - Encrypted metadataErrors: Database errors, invoice not found
Invoice Features
Generated invoices include the following features:Standard Parameters
- CLTV Expiry: 72 blocks (~12 hours)
- Expiry Time: 24 hours
- Payment Secret: Required for all invoices
- TLV Onion: Required for payment secrets
Payment secrets are required in all Muun invoices to prevent amount theft attacks. This has been standard since LND v0.12.0 (January 2021).
Key Derivation Paths
Invoice keys are derived using the following structure:Constants
Security Features
Secret Management
- Preimages generated with cryptographically secure randomness
- Payment secrets unique per invoice
- Automatic secret rotation (max 5 unused)
- Secure key derivation with random indexes
Metadata Encryption
- Metadata encrypted with derived encryption key
- Unique encryption key per invoice
- JSON encoding before encryption
Invoice Signing
- Signed with derived identity key
- Compact signature format
- BIP340-compatible for modern wallets