BOLT12 Implementation for Anya-core¶
Overview¶
This document describes the BOLT12 (Basis of Lightning Technology 12) implementation in the Anya-core project. BOLT12 is a protocol specification for the Lightning Network that introduces offers, a flexible way for receivers to request payments from senders without creating an invoice in advance.
Components¶
The BOLT12 implementation includes the following key components:
- Bolt12Offer: Represents a payment offer that can be shared with potential payers.
- Bolt12InvoiceRequest: Created by a payer in response to an offer to request a specific invoice.
- Bolt12Invoice: Created by the payee in response to an invoice request.
- Bolt12Payment: Created by the payer to make payment to the payee.
- Bolt12Refund: Allows for refunds of payments when needed.
Usage Flow¶
The typical BOLT12 payment flow is:
- Merchant creates an Offer with details like amount, description, and expiry
- Merchant shares the offer encoding with the customer
- Customer decodes the offer and creates an InvoiceRequest
- Merchant receives the request and creates an Invoice
- Customer receives the invoice and makes a Payment
- If needed, Merchant can issue a Refund
Technical Implementation¶
The implementation is based on the Lightning Network Rust libraries and provides a clean, safe interface for working with BOLT12 components.
Offer Creation¶
let offer = Bolt12Offer::new(
1_000_000, // 1000 sats
"Test Payment".into(),
3600, // 1 hour expiry
"Test Merchant".into()
)?;
// Convert to bytes for sharing
let encoded = offer.serialize()?;
Invoice Request¶
let payer_id = [0u8; 32]; // Customer identifier
let invoice_request = Bolt12InvoiceRequest::new(
&offer,
payer_id,
Some("Payment for goods".into())
)?;
Invoice Generation¶
let payment_hash = [0u8; 32]; // Generated payment hash
let node_id = [0u8; 33]; // Merchant node ID
let invoice = Bolt12Invoice::from_request(
&invoice_request,
payment_hash,
node_id
)?;
Payment Processing¶
let payment_preimage = [0u8; 32]; // Payment preimage
let payment = Bolt12Payment::new(&invoice, payment_preimage)?;
Refund Processing¶
let refund_amount = 500_000; // Partial refund
let refund = Bolt12Refund::new(&payment, refund_amount)?;
Layer 2 Interoperability¶
The BOLT12 implementation is crucial for Layer 2 interoperability as it enables:
- Cross-platform compatibility with other Lightning Network implementations
- Flexible payments without requiring pre-generated invoices
- Enhanced metadata for improved payment context
- Offer reusability allowing multiple payments from a single offer
- Refund capability supporting complete payment lifecycle
Security Considerations¶
- Encryption: All offer data should be encrypted in transit
- Key Management: Secure management of node keys is essential
- Payment Hash Generation: Use secure random number generation for payment hashes
- Timeouts: Enforce proper timeout handling for expired offers
- Validation: Validate all inputs, especially from untrusted sources
Future Enhancements¶
- Payment Streaming: Support for streaming micropayments
- Multi-path Payments: Support for splitting payments across multiple routes
- Metadata Extensions: Support for additional merchant and product metadata
- Invoice Features: Support for additional BOLT12 features as they become standardized
- Subscription Support: Support for recurring payment features
Testing¶
A comprehensive test suite is included in /tests/lightning/bolt12_test.rs
that validates:
- Offer creation and serialization
- Invoice request flow
- Invoice generation
- Payment creation
- Refund processing
- Complete end-to-end flows
Status and Compliance¶
This implementation is fully compliant with the BOLT12 specification and has been tested for interoperability with other Lightning Network implementations including:
- LND
- c-lightning
- LDK
The implementation is intended for production use in the Anya-core project for Layer 2 payment processing.