name: Theoretical Introduction to the Lightning Network goal: Discover the Lightning Network from a technical perspective objectives:


A Journey to Bitcoin's Second Layer

Dive into the heart of the Lightning Network, an essential system for the future of Bitcoin transactions. LNP201 is a theoretical course on the technical workings of Lightning. It unveils the foundations and mechanisms of this second-layer network, designed to make Bitcoin payments fast, economical, and scalable.

Thanks to its network of payment channels, Lightning enables rapid and secure transactions without recording each exchange on the Bitcoin blockchain. Throughout the chapters, you will learn how the opening, management, and closing of channels work, how payments are routed through intermediary nodes securely while minimizing the need for trust, and how to manage liquidity. You will discover what commitment transactions, HTLCs, revocation keys, punishment mechanisms, onion routing, and invoices are.

Whether you are a Bitcoin beginner or more experienced user, this course will provide valuable information to understand and use the Lightning Network. Although we will cover some fundamentals of Bitcoin's operation in the first parts, it is essential to master the basics of Satoshi's invention before diving into LNP201.

Enjoy your discovery!

Introduction

Course Overview

Welcome to the LNP201 course!

This training aims to provide you with an in-depth technical understanding of the Lightning Network, an overlay network designed to facilitate fast and often low-cost Bitcoin transactions. You will gradually discover the fundamental concepts that govern this system, from opening payment channels to routing techniques and liquidity management.

Section 1: Fundamentals
We will start with a general introduction to the Lightning Network, establishing essential foundations about Bitcoin, its addresses, UTXOs, and how transactions work. This fundamental review is necessary to understand how the Lightning Network relies on the underlying blockchain mechanisms to operate securely.

Section 2: Opening and Closing Channels
In this section, we will explore the channel opening process, which is the cornerstone of the Lightning Network. You will learn how commitment transactions are created, the role of revocation keys for security, and how channels can be closed either collaboratively or unilaterally. Each step will be explained precisely and technically to help you grasp all the subtleties.

Section 3: A Liquidity Network
The Lightning Network is not limited to individual channels; it is a real payment network. We will see how transactions can be routed through intermediary nodes using HTLCs. This section will also introduce you to the challenges of inbound and outbound liquidity.

Section 4: Lightning Network Tools
This section presents practical tools of the Lightning Network, such as Invoices, LNURL, and Keysend. You will also learn how to manage your channels' liquidity, an essential aspect to ensure smooth payments and maximize the efficiency of your transactions on Lightning.

Section 5: Going Further
Finally, we will conclude the training by recapping the concepts covered and paving the way for more advanced topics for those who wish to deepen their knowledge of the Lightning Network.

Ready to uncover the technical mechanisms of the Lightning Network? Let’s dive in!

The Fundamentals

Understanding the Lightning Network

The Lightning Network is a network of payment channels built on top of the Bitcoin protocol, aiming to enable fast and low-cost transactions. It allows the creation of payment channels between participants, within which transactions can be made almost instantly and with minimal fees, without having to record each transaction individually on the blockchain. Thus, the Lightning Network seeks to improve Bitcoin's scalability and make it usable for low-value payments.

Before exploring the "network" aspect, it is important to understand the concept of a payment channel on Lightning, how it works, and its specifics. This is the subject of this first chapter.

The Concept of Payment Channel

A payment channel allows two parties, here Alice and Bob, to exchange funds over the Lightning Network. Each protagonist has a node, symbolized by a circle, and the channel between them is represented by a line segment.

LNP201

In our example, Alice has 100,000 satoshis on her side of the channel, and Bob has 30,000, for a total of 130,000 satoshis, which constitutes the channel capacity.

But what is a satoshi?

The satoshi (or "sat") is a unit of account on Bitcoin. Similar to a cent for the euro, a satoshi is simply a fraction of Bitcoin. One satoshi is equal to 0.00000001 Bitcoin, or one hundred millionth of a Bitcoin. Using the satoshi becomes increasingly practical as the value of Bitcoin rises.

The Allocation of Funds in the Channel

Let's return to the payment channel. The key concept here is the "side of the channel". Each participant has funds on their side of the channel: Alice 100,000 satoshis and Bob 30,000. As we've seen, the sum of these funds represents the total capacity of the channel, a figure set when it is opened.

LNP201

Let's take an example of a Lightning transaction. If Alice wants to send 40,000 satoshis to Bob, this is possible because she has enough funds (100,000 satoshis). After this transaction, Alice will have 60,000 satoshis on her side and Bob 70,000.

LNP201

The channel capacity, at 130,000 satoshis, remains constant. What changes is the allocation of funds. This system does not allow sending more funds than one possesses. For example, if Bob wanted to send back 80,000 satoshis to Alice, he could not, because he only has 70,000.

Another way to imagine the allocation of funds is to think of a slider that indicates where the funds are in the channel. Initially, with 100,000 satoshis for Alice and 30,000 for Bob, the slider is logically on Alice's side. After the transaction of 40,000 satoshis, the slider will move slightly towards Bob's side, who now has 70,000 satoshis.

LNP201

This representation can be useful for imagining the balance of funds in a channel.

The Fundamental Rules of a Payment Channel

The first point to remember is that the channel capacity is fixed. It's somewhat like the diameter of a pipe: it determines the maximum amount of funds that can be sent through the channel at once. Let's take an example: if Alice has 130,000 satoshis on her side, she can only send a maximum of 130,000 satoshis to Bob in a single transaction. However, Bob can then send these funds back to Alice, either partially or in full.

What's important to understand is that the fixed capacity of the channel limits the maximum amount of a single transaction, but not the total number of possible transactions, nor the overall volume of funds exchanged within the channel.

What should you take away from this chapter?

This is the end of this first chapter, where we have laid the groundwork for the Lightning Network. In the coming chapters, we will see how to open a channel and delve deeper into the concepts discussed here.

Bitcoin, Addresses, UTXO, and Transactions

This chapter is a bit special since it will not be directly dedicated to Lightning, but to Bitcoin. Indeed, the Lightning Network is a layer on top of Bitcoin. It is therefore essential to understand certain fundamental concepts of Bitcoin to properly grasp the functioning of Lightning in the subsequent chapters. In this chapter, we will review the basics of Bitcoin receiving addresses, UTXOs, as well as the functioning of Bitcoin transactions.

Bitcoin Addresses, Private Keys, and Public Keys

A Bitcoin address is a series of characters derived from a public key, which is itself calculated from a private key. As you surely know, it is used to lock bitcoins, which is equivalent to receiving them in our wallet.

The private key is a secret element that should never be shared, while the public key and the address can be shared without security risk (their disclosure only represents a risk to your privacy). Here is a common representation that we will adopt throughout this training:

Bitcoin Transactions: Sending Funds and Scripts

On Bitcoin, a transaction involves sending funds from one address to another. Let's take the example of Alice sending 0.002 Bitcoin to Bob. Alice uses the private key associated with her address to sign the transaction, thereby proving that she is indeed able to spend these funds. But what exactly happens behind this transaction? The funds on a Bitcoin address are locked by a script, a kind of mini-program that imposes certain conditions to spend the funds.

The most common script requires a signature with the private key associated with the address. When Alice signs a transaction with her private key, she unlocks the script that blocks the funds, and they can then be transferred. The transfer of funds involves adding a new script to these funds, stipulating that to spend them this time, Bob's private key signature will be required.

LNP201

UTXOs: Unspent Transaction Outputs

On Bitcoin, what we actually exchange are not directly bitcoins, but UTXOs (Unspent Transaction Outputs), meaning "unspent transaction outputs".

A UTXO is a piece of bitcoin that can be of any value, for example, 2,000 bitcoins, 8 bitcoins, or even 8,000 sats. Each UTXO is locked by a script, and to spend it, one must satisfy the script's conditions, often a signature with the private key corresponding to a given receiving address.

UTXOs cannot be divided. Each time they are used to spend the amount in bitcoins they represent, it must be done in its entirety. It's a bit like a banknote: if you have a €10 bill and you owe the baker €5, you can't just cut the bill in half. You have to give him the €10 bill, and he will give you €5 in change. This is exactly the same principle for UTXOs on Bitcoin! For example, when Alice unlocks a script with her private key, she unlocks the entire UTXO. If she wishes to send only a part of the funds represented by this UTXO to Bob, she can "fragment" it into several smaller ones. She will then send 0.0015 BTC to Bob and send the remainder, 0.0005 BTC, to a change address.

Here is an example of a transaction with 2 outputs:

LNP201

Multi-signature Addresses

In addition to simple addresses generated from a single public key, it is possible to create multi-signature addresses from multiple public keys. A particularly interesting case for the Lightning Network is the 2/2 multi-signature address, generated from two public keys:

LNP201

To spend the funds locked with this 2/2 multi-signature address, it is necessary to sign with the two private keys associated with the public keys.

LNP201

This type of address is precisely the representation on the Bitcoin blockchain of payment channels on the Lightning Network.

What should you take away from this chapter?

This chapter on Bitcoin has allowed us to review some essential notions for what follows. In the next chapter, we will specifically discover how the opening of channels on the Lightning Network works.

Opening and Closing Channels

Channel Opening

In this chapter, we will see more precisely how to open a payment channel on the Lightning Network and understand the link between this operation and the underlying Bitcoin system.

Lightning Channels

As we saw in the first chapter, a payment channel on Lightning can be compared to a "pipe" for exchanging funds between two participants (Alice and Bob in our examples). The capacity of this channel corresponds to the sum of the available funds on each side. In our example, Alice has 100,000 satoshis and Bob has 30,000 satoshis, giving a total capacity of 130,000 satoshis.

LNP201

Levels of Information Exchange

It's crucial to clearly distinguish the different levels of exchange on the Lightning Network:

LNP201

It's worth noting that a Lightning node can communicate via the P2P protocol without opening a channel, but to exchange funds, a channel is necessary.

Steps to Open a Lightning Channel

LNP201 LNP201 LNP201 LNP201 LNP201 LNP201

When is the channel open?

The channel is considered open once the deposit transaction is included in a Bitcoin block and it has reached a certain depth of confirmations (number of following blocks).

What should you remember from this chapter?

In the next chapter, we will explore the technical workings of a Lightning transaction within a channel.

Commitment Transaction

In this chapter, we will discover the technical functioning of a transaction within a channel on the Lightning Network, that is, when funds are moved from one side of the channel to the other.

Reminder of the channel lifecycle

As seen previously, a Lightning channel begins with an opening via a Bitcoin transaction. The channel can be closed at any time, also via a Bitcoin transaction. Between these two moments, an almost infinite number of transactions can be performed within the channel, without going through the Bitcoin blockchain. Let's see what happens during a transaction in the channel.

LNP201

The initial state of the channel

At the time of opening the channel, Alice deposited 130,000 satoshis on the multisignature address of the channel. Thus, in the initial state, all the funds are on Alice's side. Before opening the channel, Alice also had Bob sign a withdrawal transaction, which would allow her to recover her funds if she wished to close the channel.

LNP201

Unpublished Transactions: The Commitment Transactions

When Alice makes a transaction in the channel to send funds to Bob, a new Bitcoin transaction is created to reflect this change in the distribution of funds. This transaction, called a commitment transaction, is not published on the blockchain but represents the new state of the channel following the Lightning transaction.

Let's take an example with Alice sending 30,000 satoshis to Bob:

Transfer Process: The Invoice

When Bob wants to receive funds, he sends Alice an invoice for 30,000 satoshis. Alice then proceeds to pay this invoice by starting the transfer within the channel. As we have seen, this process relies on the creation and signing of a new commitment transaction.

Each commitment transaction represents the new distribution of funds in the channel after the transfer. In this example, after the transaction, Bob has 30,000 satoshis and Alice has 100,000 satoshis. If either of the two participants decided to publish this commitment transaction on the blockchain, it would result in the closing of the channel and the funds would be distributed according to this last distribution.

LNP201

New State After a Second Transaction

Let's take another example: after the first transaction where Alice sent 30,000 satoshis to Bob, Bob decides to send 10,000 satoshis back to Alice. This creates a new state of the channel. The new commitment transaction will represent this updated distribution:

LNP201

Again, this transaction is not published on the blockchain but can be at any time in case the channel is closed.

In summary, when funds are transferred within a Lightning channel:

However, this system has a potential flaw, which we will address in the next chapter. We will see how each participant can protect themselves against an attempt to cheat by the other party.

Revocation Key

f2f61e5b-badb-5947-9a81-7aa530b44e59 :::video id=1d850f23-eff1-4725-b284-ce12456a2c26::: In this chapter, we will delve deeper into how transactions work on the Lightning Network by discussing the mechanisms in place to protect against cheating, ensuring that each party adheres to the rules within a channel.

Reminder: Commitment Transactions

As previously seen, transactions on Lightning rely on unpublished commitment transactions. These transactions reflect the current distribution of funds in the channel. When a new Lightning transaction is made, a new commitment transaction is created and signed by both parties to reflect the new state of the channel.

Let's take a simple example:

LNP201

At any time, both parties can publish the latest commitment transaction signed to close the channel and recover their funds.

The Flaw: Cheating by Publishing an Old Transaction

A potential problem arises if one of the parties decides to cheat by publishing an old commitment transaction. For example, Alice could publish an older commitment transaction where she had 100,000 satoshis, even though she now only has 60,000 in reality. This would allow her to steal 40,000 satoshis from Bob.

LNP201

Even worse, Alice could publish the very first withdrawal transaction, the one before the channel was opened, where she had 130,000 satoshis, and thus steal the entire channel's funds.

LNP201

Solution: Revocation Key and Timelock

To prevent this kind of cheating by Alice, on the Lightning Network, security mechanisms are added to the commitment transactions:

LNP201

Let's detail the functioning of this mechanism together.

Transaction Update Process

When Alice and Bob update the state of the channel with a new Lightning transaction, they exchange in advance their respective secrets for the previous commitment transaction (the one that will become obsolete and could allow one of them to cheat). This means that, in the new state of the channel:

Let's take an example to understand this process well:

LNP201 LNP201 LNP201

Even if, in this case, Bob has no economic interest in trying to cheat, if he does so anyway, Alice also benefits from symmetric protection offering her the same guarantees.

What should you take away from this chapter?

The commitment transactions on the Lightning Network include security mechanisms that reduce both the risk of cheating and the incentives to do so. Before signing a new commitment transaction, Alice and Bob exchange their respective secrets for the previous commitment transactions. If Alice tries to publish an old commitment transaction, Bob can use the revocation key to recover all the funds before Alice can (because she is blocked by the timelock), which punishes her for attempting to cheat.

This security system ensures that participants adhere to the rules of the Lightning Network, and they cannot profit from publishing old commitment transactions.

At this point in the training, you now know how Lightning channels are opened and how transactions within these channels work. In the next chapter, we will discover the different ways to close a channel and recover your bitcoins on the main blockchain.

Channel Closure

In this chapter, we will discuss closing a channel on the Lightning Network, which is done through a Bitcoin transaction, just like opening a channel. After seeing how transactions within a channel work, it is now time to see how to close a channel and recover the funds on the Bitcoin blockchain.

Reminder of the channel lifecycle

The lifecycle of a channel begins with its opening, via a Bitcoin transaction, then Lightning transactions are made within it, and finally, when the parties wish to recover their funds, the channel is closed through a second Bitcoin transaction. The intermediate transactions made on Lightning are represented by unpublished commitment transactions.

LNP201

The three types of channel closure

There are three main ways to close this channel, which can be called the good, the brute, and the truant (inspired by Andreas Antonopoulos in Mastering the Lightning Network):

Let's take an example:

LNP201

The Good: the cooperative closure

In a cooperative closure, Alice and Bob agree to close the channel. Here's how it goes:

LNP201

For example, if Alice owns 100,000 satoshis and Bob 30,000 satoshis, the closing transaction will send 100,000 satoshis to Alice's address and 30,000 satoshis to Bob's address, without timelock constraints. Once this transaction is signed by both parties, it is published by Alice. Once the transaction is confirmed on the Bitcoin blockchain, the Lightning channel will be officially closed.

LNP201

The cooperative closure is the preferred method of closing because it is fast (no timelock) and the transaction fees are adjusted according to the current Bitcoin market conditions. This avoids paying too little, which could risk blocking the transaction in the mempools, or overpaying unnecessarily, which leads to unnecessary financial loss for the participants.

The Bad: the forced closure

When Alice's node sends a message to Bob's asking for a cooperative closure, if he does not respond (for example, due to an internet outage or a technical problem), Alice can proceed with a forced closure by publishing the last signed commitment transaction. In this case, Alice will simply publish the last commitment transaction, which reflects the state of the channel at the time the last Lightning transaction took place with the correct distribution of funds.

LNP201

This transaction includes a timelock for Alice's funds, making the closure slower.

LNP201

Also, the fees of the commitment transaction may be unsuitable at the time of closure, as they were set when the transaction was created, sometimes several months before. Generally, Lightning clients overestimate fees to avoid future problems, but this can lead to excessive fees, or conversely too low.

In summary, forced closure is a last resort option when the peer no longer responds. It is slower and less economical than a cooperative closure. Therefore, it should be avoided as much as possible.

The cheat: cheating

Finally, a closure with cheating occurs when one of the parties tries to publish an old commitment transaction, often where they held more funds than they should. For example, Alice might publish an old transaction where she owned 120,000 satoshis, while she actually owns only 100,000 now.

LNP201

Bob, to prevent this cheating, monitors the Bitcoin blockchain and its mempool to ensure Alice does not publish an old transaction. If Bob detects a cheating attempt, he can use the revocation key to recover Alice's funds and punish her by taking the entire funds of the channel. Since Alice is blocked by the timelock on her output, Bob has time to spend it without a timelock on his side to recover the whole sum on an address he owns.

LNP201

Obviously, cheating can potentially succeed if Bob does not act within the time imposed by the timelock on Alice's output. In this case, Alice's output is unlocked, allowing her to consume it to create a new output to an address she controls.

What should you take away from this chapter?

There are three ways to close a channel:

In the upcoming chapters, we will explore the Lightning Network from a broader perspective, focusing on how its network operates.

A Liquidity Network

Lightning Network

In this chapter, we will explore how payments on the Lightning Network can reach a recipient even if they are not directly connected by a payment channel. Lightning is, indeed, a network of payment channels, which allows funds to be sent to a distant node through the channels of other participants. We will discover how payments are routed across the network, how liquidity moves between channels, and how transaction fees are calculated.

The Network of Payment Channels

On the Lightning Network, a transaction corresponds to a transfer of funds between two nodes. As seen in previous chapters, it is necessary to open a channel with someone to perform Lightning transactions. This channel allows for an almost infinite number of off-chain transactions before closing it to reclaim the on-chain balance. However, this method has the disadvantage of requiring a direct channel with the other person to receive or send funds, which implies an opening transaction and a closing transaction for each channel. If I plan to make a large number of payments with this person, opening and closing a channel becomes cost-effective. Conversely, if I only need to perform a few Lightning transactions, opening a direct channel is not advantageous, as it would cost me 2 on-chain transactions for a limited number of off-chain transactions. This case might occur, for example, when wanting to pay with Lightning at a merchant without planning to return.

To solve this problem, the Lightning Network allows for routing a payment through several channels and intermediary nodes, thus enabling a transaction without a direct channel with the other person.

For example, imagine that:

LNP201

If Alice wants to send funds to Bob without opening a direct channel with him, she will have to go through Suzie, and each channel will need to adjust the liquidity on each side. The sent satoshis remain within their respective channels; they don't actually "cross" the channels, but the transfer is made via an adjustment of the internal liquidity in each channel.

Suppose Alice wants to send 50,000 satoshis to Bob:

LNP201

Thus, the payment is routed to Bob via a movement of liquidity in each channel. At the end of the operation, Alice ends up with 50,000 sats. She has indeed transferred 50,000 sats since initially, she had 100,000. Bob, on his side, ends up with an additional 50,000 sats. For Suzie (the intermediate node), this operation is neutral: initially, she had 30,000 sats in her channel with Alice and 250,000 sats in her channel with Bob, a total of 280,000 sats. After the operation, she holds 80,000 sats in her channel with Alice and 200,000 sats in her channel with Bob, which is the same sum as at the start.

This transfer is thus limited by the available liquidity in the direction of the transfer.

Calculation of the Route and Liquidity Limits

Let's take a theoretical example of another network with:

LNP201

The maximum Alice can send to Bob in this configuration is 90,000 satoshis, as she is limited by the smallest liquidity available in the channel from Suzie to Carol. In the opposite direction (from Bob to Alice), no payment is possible because Suzie's side in the channel with Alice contains no satoshis. Therefore, there is no route usable for a transfer in this direction. Alice sends 40,000 satoshis to Bob through the channels:

LNP201

The satoshis sent in each channel remain in the channel, so the satoshis sent by Carol to Bob are not the same as those sent by Alice to Suzie. The transfer is made only by adjusting the liquidity inside each channel. Moreover, the total capacity of the channels remains unchanged.

LNP201

As in the previous example, after the transaction, the source node (Alice) has 40,000 satoshis less. The intermediate nodes (Suzie and Carol) retain the same total amount, making the operation neutral for them. Finally, the destination node (Bob) receives an additional 40,000 satoshis.

The role of the intermediate nodes is therefore very important in the functioning of the Lightning Network. They facilitate transfers by offering multiple paths for payments. To encourage these nodes to provide their liquidity and participate in routing payments, routing fees are paid to them.

Routing Fees

The intermediate nodes apply fees to allow payments to pass through their channels. These fees are set by each node for each channel. The fees consist of 2 elements:

The fees also differ depending on the direction of the transfer. For example, for a transfer from Alice to Suzie, Alice's fees apply. Conversely, from Suzie to Alice, Suzie's fees are used.

For example, for a channel between Alice and Suzie, we could have:

LNP201

To better understand how fees work, let's study the same Lightning Network as before, but now with the following routing fees:

For the same payment of 40,000 satoshis to Bob, Alice will have to send a little more, as each intermediary node will deduct its fees:

The total fees for this payment on this path are therefore 9.04 satoshis. Thus, Alice must send 40,009.04 satoshis for Bob to receive exactly 40,000 satoshis.

LNP201

The liquidity is therefore updated:

LNP201

Onion Routing

To route a payment from the sender to the recipient, the Lightning Network uses a method called "onion routing". Unlike the routing of classical data, where each router decides the direction of the data based on their destination, onion routing works differently:

In this chapter, we explored payment routing on the Lightning Network. But a question arises: what prevents intermediary nodes from accepting an incoming payment without forwarding it to the next destination, with the aim of intercepting the transaction? This is precisely the role of HTLCs that we will study in the following chapter.

HTLC – Hashed Time Locked Contract

In this chapter, we will discover how Lightning allows payments to transit through intermediary nodes without needing to trust them, thanks to HTLC (Hashed Time-Locked Contracts). These smart contracts ensure that each intermediary node will only receive the funds from its channel if it forwards the payment to the final recipient, otherwise, the payment will not be validated.

The issue that arises for payment routing is therefore the necessary trust in intermediary nodes, and among the intermediary nodes themselves. To illustrate this, let's revisit our simplified Lightning network example with 3 nodes and 2 channels:

Alice wants to send 40,000 sats to Bob but she does not have a direct channel with him and does not wish to open one. She looks for a route and decides to go through Suzie's node.

LNP201

If Alice naively sends 40,000 satoshis to Suzie hoping that Suzie will transfer this sum to Bob, Suzie could keep the funds for herself and not transmit anything to Bob.

LNP201 To avoid this situation, on Lightning, we use HTLCs (Hashed Time-Locked Contracts), which make the payment to the intermediary node conditional, meaning Suzie must meet certain conditions to access Alice's funds and transfer them to Bob.

How HTLCs Work

An HTLC is a special contract based on two principles:

Here's how this process works in our example with Alice, Suzie, and Bob:

LNP201

Creating the secret: Bob generates a random secret noted as s (the preimage), and calculates its hash noted as r with the hash function noted as h. We have:

r = h(s)

Using a hash function makes it impossible to find s with only h(s), but if s is provided, it's easy to verify that it corresponds to h(s).

LNP201

Sending the payment request: Bob sends an invoice to Alice asking for a payment. This invoice notably includes the hash r.

LNP201

Sending the conditional payment: Alice sends an HTLC of 40,000 satoshis to Suzie. The condition for Suzie to receive these funds is that she provides Alice with a secret s' that satisfies the following equation:

h(s') = r
LNP201

Transferring the HTLC to the final recipient: Suzie, to obtain the 40,000 satoshis from Alice, must transfer a similar HTLC of 40,000 satoshis to Bob, who has the same condition, namely that he must provide Suzie with a secret s' that satisfies the equation:

h(s') = r
LNP201

Validation by the secret s: Bob provides s to Suzie to receive the 40,000 satoshis promised in the HTLC. With this secret, Suzie can then unlock Alice's HTLC and obtain the 40,000 satoshis from Alice. The payment is then correctly routed to Bob.

LNP201 This process prevents Suzie from keeping Alice's funds without completing the transfer to Bob, as she must send the payment to Bob to obtain the secret s and thus unlock Alice's HTLC. The operation remains the same even if the route includes several intermediary nodes: it is simply a matter of repeating Suzie's steps for each intermediary node. Each node is protected by the conditions of the HTLCs, because unlocking the last HTLC by the recipient automatically triggers the unlocking of all other HTLCs in a cascade.

Expiration and management of HTLCs in case of problems

If during the payment process, one of the intermediary nodes, or the recipient node, stops responding, especially in case of an internet or power outage, then the payment cannot be completed, because the secret needed to unlock the HTLCs is not transmitted. Taking our example with Alice, Suzie, and Bob, this problem occurs, for example, if Bob does not transmit the secret s to Suzie. In this case, all the HTLCs upstream of the path are blocked, and the funds they secure as well.

LNP201

To avoid this, HTLCs on Lightning have an expiration that allows for the removal of the HTLC if it is not completed after a certain time. The expiration follows a specific order since it starts first with the HTLC closest to the recipient, and then progressively moves up to the transaction's issuer. In our example, if Bob never gives the secret s to Suzie, this would first cause Suzie's HTLC towards Bob to expire.

LNP201

Then the HTLC from Alice to Suzie.

LNP201

If the order of expiration was reversed, Alice could recover her payment before Suzie could protect herself from potential cheating. Indeed, if Bob comes back to claim his HTLC while Alice has already removed hers, Suzie would be at a disadvantage. This cascading order of HTLC expiration thus ensures that no intermediary node suffers from unfair losses.

Representation of HTLCs in commitment transactions

Commitment transactions represent HTLCs in such a way that the conditions they impose on Lightning can be transferred to Bitcoin in the event of a forced channel closure during the lifespan of an HTLC. As a reminder, commitment transactions represent the current state of the channel between the two users and allow for a unilateral forced closure in case of issues. With each new state of the channel, 2 commitment transactions are created: one for each party. Let's revisit our example with Alice, Suzie, and Bob, but look more closely at what happens at the channel level between Alice and Suzie when the HTLC is created. LNP201

Before the start of the 40,000 sats payment between Alice and Bob, Alice has 100,000 sats in her channel with Suzie, while Suzie holds 30,000. Their commitment transactions are as follows:

LNP201

Alice has just received Bob's invoice, which notably contains r, the hash of the secret. She can thus construct an HTLC of 40,000 satoshis with Suzie. This HTLC is represented in the latest commitment transactions as an output called "HTLC Out" on Alice's side, since the funds are outgoing, and "HTLC In" on Suzie's side, since the funds are incoming.

LNP201

These outputs associated with the HTLC share exactly the same conditions, namely:

These conditions apply only if the channel is closed (i.e., a commitment transaction is published on-chain) while the HTLC is still active on Lightning, meaning the payment between Alice and Bob has not yet been finalized, and the HTLCs have not yet expired. Thanks to these conditions, Suzie can recover the 40,000 satoshis of the HTLC owed to her by providing s. Otherwise, Alice recovers the funds after the expiration of the timelock, because if Suzie does not know s, it means she has not transferred the 40,000 satoshis to Bob, and therefore, Alice's funds are not owed to her.

Furthermore, if the channel is closed while several HTLCs are pending, there will be as many additional outputs as there are ongoing HTLCs. If the channel is not closed, then after the expiration or success of the Lightning payment, new commitment transactions are created to reflect the new, now stable, state of the channel, that is, without any pending HTLCs. The outputs related to the HTLCs can therefore be removed from the commitment transactions. LNP201

Finally, in the case of a cooperative channel closure while an HTLC is active, Alice and Suzie stop accepting new payments and wait for the resolution or expiration of the ongoing HTLCs. This allows them to publish a lighter closing transaction, without the outputs related to the HTLCs, thereby reducing fees and avoiding the wait for a possible timelock.

What should you take away from this chapter?

HTLCs enable the routing of Lightning payments through multiple nodes without having to trust them. Here are the key points to remember:

In the next chapter, we will discover how a node issuing a Lightning transaction finds and selects routes for its payment to reach the recipient node.

Finding Your Way

In the previous chapters, we saw how to use other nodes' channels to route payments and reach a node without being directly connected to it via a channel. We also discussed how to ensure the security of the transfer without trusting the intermediary nodes. In this chapter, we will focus on finding the best possible route to reach a target node.

The Problem of Routing in Lightning

As we have seen, in Lightning, it is the payment-sending node that must calculate the complete route to the recipient, because we use an onion routing system. The intermediary nodes do not know either the point of origin or the final destination. They only know where the payment comes from and to which node they must transfer it next. This means that the sending node must maintain a dynamic local topology of the network, with the existing Lightning nodes and the channels between each, taking into account openings, closures, and state updates.

LNP201 Even with this topology of the Lightning Network, there is essential information for routing that remains inaccessible to the sending node, which is the exact distribution of liquidity in the channels at any given moment. Indeed, each channel only displays its total capacity, but the internal distribution of funds is only known to the two participating nodes. This poses challenges for efficient routing, as the success of the payment depends notably on whether its amount is less than the lowest liquidity on the chosen route. However, the liquidities are not all visible to the sending node. LNP201

Network Map Update

To keep their network map up to date, nodes regularly exchange messages through an algorithm called "gossip". This is a distributed algorithm used to spread information in an epidemic manner to all the nodes in the network, which allows for the exchange and synchronization of the global state of the channels in a few communication cycles. Each node propagates information to one or more neighbors chosen at random or not, these, in turn, propagate the information to other neighbors and so on until a globally synchronized state is achieved.

The 2 main messages exchanged between Lightning nodes are as follows:

Lightning nodes also monitor the Bitcoin blockchain to detect channel closing transactions. The closed channel is then removed from the map since it can no longer be used to route our payments.

Routing a Payment

Let's take an example of a small Lightning Network with 7 nodes: Alice, Bob, 1, 2, 3, 4, and 5. Imagine that Alice wants to send a payment to Bob but must go through intermediary nodes.

LNP201

Here is the actual distribution of funds in these channels:

LNP201

To make a payment of 100,000 sats from Alice to Bob, the routing options are limited by the available liquidity in each channel. The optimal route for Alice, based on the known liquidity distributions, could be the sequence Alice → 1 → 2 → 4 → 5 → Bob:

LNP201

But since Alice does not know the exact distribution of funds in each channel, she must estimate the optimal route probabilistically, taking into account the following criteria:

By analyzing these criteria, the sending node can test the most probable routes and attempt to optimize them. In our example, Alice could rank the best routes as follows:

Payment Execution

Alice decides to test her first route (Alice → 1 → 2 → 5 → Bob). She therefore sends a HTLC of 100,000 sats to node 1. This node checks that it has sufficient liquidity with node 2, and continues the transmission. Node 2 then receives the HTLC from node 1, but realizes it does not have enough liquidity in its channel with node 5 to route a payment of 100,000 sats. It then sends an error message back to node 1, who transmits it to Alice. This route has failed.

LNP201

Alice then attempts to route her payment using her second route (Alice → 1 → 2 → 4 → 5 → Bob). She sends a HTLC of 100,000 sats to node 1, who transmits it to node 2, then to node 4, to node 5, and finally to Bob. This time, the liquidity is sufficient, and the route is functional. Each node unlocks its HTLC in cascade using the preimage provided by Bob (the secret s), which allows Alice's payment to Bob to be successfully finalized.

LNP201

The search for a route is conducted as follows: the sending node starts by identifying the best possible routes, then attempts payments successively until a functional route is found.

It's worth noting that Bob can provide Alice with information in the invoice to facilitate routing. For example, he can indicate nearby channels with sufficient liquidity or reveal the existence of private channels. These indications allow Alice to avoid routes with little chance of success and to first attempt the paths recommended by Bob.

What should you take away from this chapter?

In the following chapter, we will specifically study the functioning of invoices, in addition to some other tools used on the Lightning Network.

The Tools of the Lightning Network

Invoice, LNURL, and Keysend

e34c7ecd-2327-52e3-b61e-c837d9e5e8b0 :::video id=309c3412-506e-4189-ad46-5e5088c55008:::

In this chapter, we will take a closer look at the operation of Lightning invoices, that is, payment requests sent by the recipient node to the sender node. The goal is to understand how to pay and receive payments on Lightning. We will also discuss 2 alternatives to classic invoices: LNURL and Keysend.

LNP201

The Structure of Lightning Invoices

As explained in the chapter on HTLCs, each payment begins with the generation of an invoice by the recipient. This invoice is then transmitted to the payer (via a QR code or by copy-pasting) to initiate the payment. An invoice consists of two main parts:

The typical structure of an invoice starts with an identifier ln for "Lightning", followed by bc for Bitcoin, then the amount of the invoice. A separator 1 distinguishes the human-readable part from the data (payload) part.

Let's take the following invoice as an example:

lnbc100u1p0x7x7dpp5l7r9y50wrzz0lwnsqgxdks50lxtwkl0mhd9lslr4rcgdtt2n6lssp5l3pkhdx0cmc9gfsqvw5xjhph84my2frzjqxqyz5vq9qsp5k4mkzv5jd8u5n89d2yc50x7ptkl0zprx0dfjh3km7g0x98g70hsqq7sqqqgqqyqqqqlgqqvnv2k5ehwnylq3rhpd9g2y0sq9ujyxsqqypjqqyqqqqqqqqqqqsqqqqq9qsq3vql5f6e45xztgj7y6xw6ghrcz3vmh8msrz8myvhsarxg42ce9yyn53lgnryx0m6qqld8fql

We can already divide it into 2 parts. First, there's the Human Readable Part:

lnbc100u

Then the part intended for the payload:


p0x7x7dpp5l7r9y50wrzz0lwnsqgxdks50lxtwkl0mhd9lslr4rcgdtt2n6lssp5l3pkhdx0cmc9gfsqvw5xjhph84my2frzjqxqyz5vq9qsp5k4mkzv5jd8u5n89d2yc50x7ptkl0zprx0dfjh3km7g0x98g70hsqq7sqqqgqqyqqqqlgqqvnv2k5ehwnylq3rhpd9g2y0sq9ujyxsqqypjqqyqqqqqqqqqqqsqqqqq9qsq3vql5f6e45xztgj7y6xw6ghrcz3vmh8msrz8myvhsarxg42ce9yyn53lgnryx0m6qqld8fql

The two parts are separated by a 1. This separator was chosen instead of a special character to allow for easy copy-pasting of the entire invoice by double-clicking.

In the first part, we can see that:

To designate the payment amount, it is expressed in sub-units of bitcoin. Here are the units used:

The Payload of an Invoice

The payload of an invoice includes several pieces of information necessary for processing the payment:

The invoices are then encoded in bech32, the same format as for Bitcoin SegWit addresses (format starting with bc1).

LNURL Withdrawal

In a traditional transaction, such as a store purchase, the invoice is generated for the total amount to be paid. Once the invoice is presented (in the form of a QR code or string of characters), the customer can scan it and finalize the transaction. The payment then follows the traditional process that we studied in the previous section. However, this process can sometimes be very cumbersome for the user experience, as it requires the receiver to send information to the sender via the invoice.

For certain situations, like withdrawing bitcoins from an online service, the traditional process is too cumbersome. In such cases, the LNURL withdrawal solution simplifies this process by displaying a QR code that the recipient's wallet scans to automatically create the invoice. The service then pays the invoice, and the user simply sees an instant withdrawal.

LNP201

LNURL is a communication protocol that specifies a set of functionalities designed to simplify interactions between Lightning nodes and clients, as well as third-party applications. The LNURL withdrawal, as we have just seen, is thus just one example among other functionalities. This protocol is based on HTTP and allows the creation of links for various operations, such as a payment request, a withdrawal request, or other functionalities that enhance the user experience. Each LNURL is a bech32 encoded URL with the lnurl prefix, which, once scanned, triggers a series of automatic actions on the Lightning wallet. For example, the LNURL-withdraw (LUD-03) feature allows withdrawing funds from a service by scanning a QR code, without the need to manually generate an invoice. Similarly, LNURL-auth (LUD-04) enables logging into online services using a private key on one's Lightning wallet instead of a password.

Sending a Lightning Payment without an Invoice: Keysend

Another interesting case is the transfer of funds without having received an invoice beforehand, known as "Keysend". This protocol allows sending funds by adding a preimage in the encrypted payment data, accessible only by the recipient. This preimage enables the recipient to unlock the HTLC, thus retrieving the funds without having generated an invoice beforehand.

To simplify, in this protocol, it is the sender who generates the secret used in the HTLCs, rather than the recipient. Practically, this allows the sender to make a payment without having had to interact with the recipient beforehand.

LNP201

What should you take away from this chapter?

In the following chapter, we will see how a node operator can manage liquidity in their channels, to never be blocked and always be able to send and receive payments on the Lightning Network.

Managing Your Liquidity

In this chapter, we will explore strategies for effectively managing liquidity on the Lightning Network. Liquidity management varies depending on the type of user and context. We will look at the main principles and existing techniques to better understand how to optimize this management.

Liquidity Needs

There are three main user profiles on Lightning, each with specific liquidity needs:

These profiles are obviously not fixed; a user can switch between payer and payee depending on the transactions. For example, Bob could receive his salary on Lightning from his employer, placing him in the position of a "seller" requiring incoming liquidity. Subsequently, if he wants to use his salary to buy food, he becomes a "payer" and must then have outgoing liquidity.

To better understand, let's take the example of a simple network composed of three nodes: the buyer (Alice), the router (Suzie), and the seller (Bob).

LNP201

Imagine that the buyer wants to send 30,000 sats to the seller and that the payment goes through the router's node. Each party must then have a minimum amount of liquidity in the direction of the payment:

LNP201

Liquidity Management Strategies

Payers must ensure to maintain sufficient liquidity on their side of the channels to guarantee outgoing liquidity. This proves to be relatively simple, as it is enough to open new Lightning channels to have this liquidity. Indeed, the initial funds locked in the multisig on-chain are entirely on the payer's side in the Lightning channel at the start. The payment capacity is thus assured as long as channels are opened with enough funds. When the outgoing liquidity is exhausted, it is enough to open new channels. On the other hand, for the seller, the task is more complex. To be able to receive payments, they must have liquidity on the opposite side of their channels. Therefore, opening a channel is not enough: they must also make a payment in this channel to move the liquidity to the other side before they can receive payments themselves. For certain Lightning user profiles, such as merchants, there is a clear disproportion between what their node sends and what it receives, since the goal of a business is primarily to collect more than it spends, in order to generate a profit. Fortunately, for these users with specific incoming liquidity needs, several solutions exist:

LNP201 LNP201

Finally, for routers, whose goal is to maximize the number of payments processed and the fees collected, they must:

The Loop Out Service

The Loop Out service, offered by Lightning Labs, allows for moving liquidity to the opposite side of the channel while reclaiming the funds on the Bitcoin blockchain. For example, Alice sends 1 million satoshis via Lightning to a loop node, which then returns those funds to her in on-chain bitcoins. This balances her channel with 1 million satoshis on each side, optimizing her capacity to receive payments.

LNP201

Therefore, this service enables incoming liquidity while reclaiming one's bitcoins on-chain, which helps to limit the immobilization of cash needed to accept payments with Lightning.

What should you take away from this chapter?

In the next chapter, I propose to review the most important concepts of this training.

Go Further

Course Summary

In this final chapter marking the end of the LNP201 training, I propose to revisit the important concepts we have covered together.

The goal of this training was to provide you with a comprehensive and technical understanding of the Lightning Network. We discovered how the Lightning Network relies on the Bitcoin blockchain to perform off-chain transactions, while retaining the fundamental characteristics of Bitcoin, notably the absence of the need to trust other nodes.

Payment Channels

In the initial chapters, we explored how two parties, by opening a payment channel, can conduct transactions outside of the Bitcoin blockchain. Here are the steps covered:

LNP201 2. Transactions in the Channel: In this channel, it is then possible to carry out numerous transactions without having to publish them on the blockchain. Each Lightning transaction creates a new state of the channel reflected in a commitment transaction. LNP201

LNP201

The Network of Channels

After studying isolated channels, we extended our analysis to the network of channels:

LNP201 LNP201 LNP201

Liquidity Management

We have seen that liquidity management is a challenge on Lightning to ensure the smooth flow of payments. Sending payments is relatively simple: it just requires opening a channel. However, receiving payments requires having liquidity on the opposite side of one's channels. Here are some strategies discussed:

LNP201 LNP201

Final Section

Reviews & Ratings

38814c99-eb7b-5772-af49-4386ee2ce9b0 true

Final exam

7ed33400-aef7-5f3e-bfb1-7867e445d708 true

Conclusion

afc0d72b-4fbc-5893-90b2-e27fb519ad02 true