In this guide, we'll cover common operations when using LDK after you've set up your lightning node.
- Connecting Peers shows how get on the Lightning Network.
- Managing Channels covers the ins and outs of channels management.
- Sending Payments demonstrates making payments using your channels.
- Receiving Payments walks through how to get paid via lightning.
Time to roll up your sleeves!
First things first, let's join the Lightning Network! Connections to other peers
are established with
PeerManager. You'll need to know the pubkey and address
of another node that you want as a peer. Once the connection is established and
the handshake is complete,
PeerManager will show the peer's pubkey in its list
Channels are the basic building blocks of the Lightning Network. With channels, you can transact not only with your immediate peers but with others on the network. Let's explore how to open and close channels with LDK.
Now that you have a peer, you can open a channel with them using
ChannelManager. You'll need the peer's pubkey as before along with:
- the amount in sats to use when funding the channel,
- any msats to push to your peer,
- an id which is given back in the
- an optional
Channels can be announced to the network or can remain private, which is
At this point, an outbound channel has been initiated with your peer and it will
ChannelManager::list_channels. However, the channel is not yet
funded. Once your peer accepts the channel, you will be notified with a
FundingGenerationReady event. It's then your responsibility to construct the
funding transaction and pass it to
ChannelManager, which will broadcast it
once it receives your channel counterparty's signature.
ChannelManager has broadcast the funding transaction, the channel will
become usable once the transaction has enough confirmations and will appear in
ChannelManager::list_usable_channels. See the guide on
Blockchain Data for details on confirmations.
With a fully funded channel, you can now make Lightning payments! No more hefty on-chain fees and long confirmation times when you're transacting on layer 2.
While a channel can remain open indefinitely, there may come a time when you need to close it. There are two ways to close a channel: either cooperatively or unilaterally. The cooperative case makes for lower fees and immediate access to funds while the unilateral case does not. The latter may be necessary if your peer isn't behaving properly or has gone offline.
Now that we know how to manage channels, let's put our new channel to use!
Lightning payments are used to pay invoices, which are typically encoded as a
string in accordance with BOLT 11. After parsing the invoice, you'll need to
find a route from your node to the recipient and then make the payment using
An event is generated once a payment has completed. Successful payments result
PaymentSent event with the preimage of the payment hash. Be sure to look
out for a
PaymentFailed event, if the payment fails for some reason, and act
To receive a payment, you'll need to create an invoice of your own with an
amount and description.
ChannelManager contains the remaining information
needed for the invoice. Use the provided utility to generate an invoice and
register a pending payment in
While it is possible to create an invoice without using the utility,
ChannelManager will reject any incoming HTLCs for unregistered payments to
protect your privacy. In this case, use either
create_inbound_payment_for_hash to register a payment with
before creating the invoice with the returned payment hash and/or secret.
As with sending a payment, LDK will generate an event once a payment is
received. It is your responsibility to handle the
PaymentReceived event by
ChannelManager to release the preimage and claim the funds.
So there you have it! Those are the basics of using LDK. As you can see, LDK offers a ton of flexibility for building Lightning-enabled wallets and apps.