Overview of lightning-invoice
mainlightning-invoice crate provides data structures for BOLT 11 lightning invoices. It includes functions to parse and serialize these invoices to and from the bech32 format.repository·main·Indexed 23 days ago
https://github.com/lightningdevkit/rust-lightningA performant, flexible, and runtime-agnostic implementation of the Lightning Network protocol. Designed as a library, it provides core protocol logic and channel state machines while allowing developers to implement their own networking, storage, and key management. The project includes specialized crates such as lightning-invoice for BOLT 11, lightning-rapid-gossip-sync for optimized network graph synchronization, and lightning-liquidity for liquidity management.
lightning-invoice crate provides data structures for BOLT 11 lightning invoices. It includes functions to parse and serialize these invoices to and from the bech32 format.The RGS protocol uses a compressed, compact serialization format designed to omit signatures and use incremental updates. The structure is as follows:
76, 68, 75 (ASCII for LDK).1 and 2.u32.Default Values for Updates:
default_cltv_expiry_deltadefault_htlc_minimum_msatdefault_fee_base_msatdefault_fee_proportional_millionthsdefault_htlc_maximum_msat (u64, or u64::MAX if no maximum)Note: NodeAnnouncement messages are omitted because node IDs are implicitly extracted from channel data.
The monitoring (ChainMonitor) subsystem can be deployed using the following configurations:
Warning: Monitor Replicas require a correctly functioning Hardware Security Module (HSM) because they manage sensitive keys that, if compromised, could lead to the loss of all funds in the channel.
Rust-Lightning is a runtime-agnostic implementation of the Lightning Network protocol. The core lightning crate handles the Lightning protocol, channel state machines, and on-chain logic, but it does not manage networking, data persistence, or blockchain interactions itself.
Instead, it provides a clean API that allows you to plug in your own implementations for:
block_connected/block_disconnected API.In the context of Lightning channel management, roles are defined by who is driving the operation and whose interests the implementation serves:
These terms are used throughout the Channel data structure in the channel-management subsystem.
Because Lightning states are symmetric but punishment is asymmetric, parties must maintain different commitment transactions. The roles in transaction construction are:
At any given time, there should be two 'latest' commitment transactions processed by the implementation:
These roles are used across the channel-utils library (chan_utils.rs).
To integrate lightning-liquidity with an LDK-based node, you must set up a LiquidityManager and configure it as the CustomMessageHandler of your LDK node.
Once configured, you can access specific protocol handlers depending on whether you are building a client or a service:
LiquidityManager::lsps1_client_handler and LiquidityManager::lsps2_client_handler.LiquidityManager::lsps2_service_handler and LiquidityManager::lsps5_service_handler.LiquidityManager::lsps5_client_handler.LiquidityManager uses an eventing system to notify you about protocol updates. You must handle these events by calling the provided event handling methods, such as LiquidityManager::next_event.
Depending on your integration needs, you should choose one of the following paths:
rust-lightning (the lightning crate): Use this if you want maximum control and want to implement your own custom logic for storage, networking, and key management.rust-lightning core plus its sample modules (like lightning-persister), language bindings, and sample node implementations. Use this for a more complete set of building blocks.LDK-sample: Use this if you want an out-of-the-box Lightning node implementation.LDK-node: Use this if you want to easily integrate Lightning into an existing application without handling all the boilerplate code.To perform a rapid gossip sync, use the RapidGossipSync instance to apply snapshots retrieved from an RGS (Rapid Gossip Sync) server.
To start from the beginning, request a snapshot from the RGS server using an initial timestamp of 0.
When applying a snapshot, the sync methods return a Result<u32, GraphSyncError>. The successful u32 value is the timestamp that must be used for the subsequent server request to ensure continuous synchronization.
You do not need to implement additional caching for the timestamp. The RapidGossipSync methods automatically update the timestamp stored within the NetworkGraph object. You can retrieve the current sync timestamp by calling get_last_rapid_gossip_sync_timestamp on the NetworkGraph.
Because the crate makes assumptions regarding SystemTime bounds, you must verify compatibility with your specific platform. You can do this in two ways:
check_platform function during application startup or within your own test suite.The BackgroundProcessor is a utility designed to handle tasks that must run periodically to maintain the proper operation of Rust-Lightning. These tasks are suitable for background execution to avoid blocking the main application logic.
Key Responsibilities:
Event]s using a user-provided [EventHandler].ChannelManager] needs re-persisting to disk and performs the write operation in the background.ChannelManager::timer_tick_occurred]ChainMonitor::rebroadcast_pending_claims]PeerManager::timer_tick_occurred]GossipSync] with a [NetworkGraph] is provided during startup, it calls [NetworkGraph::remove_stale_channels_and_tracking].PeerManager::process_events] (note: this may result in higher latency).Important Safety Note:
If [ChannelManager] persistence fails and the persisted state becomes outdated, there is a risk of channels being force-closed on startup. However, as long as [ChannelMonitor] backups are valid, funds are generally safe except for those used for unilateral chain closure fees.
Lifecycle Note:
BackgroundProcessor will immediately stop when it is dropped. It should be stored in a long-lived location until application shutdown.
In BOLT 12 selective disclosure, markers are used to identify omitted TLVs. A marker is typically one greater than the previous value (either the previous included TLV or the previous marker).
Crucially, there is a gap between the standard invoice TLV range and the experimental/signature range. The next_marker function ensures that if a marker would land in this signature range (e.g., landing on 240), it instead jumps to the start of the experimental range (e.g., 1_000_000_000). This ensures producers and consumers stay in agreement regarding the marker sequence.