fbpx
Articles

Bitcoin Transactions on the Lightning Network10 min read

June 23, 2021 8 min read

author:

Bitcoin Transactions on the Lightning Network10 min read

Reading Time: 8 minutes

With the Bitcoin network continuing to grow, it’s important to understand the scaling solutions that are in place to grow the Bitcoin network to billions of people around the world. With more and more people starting to use Bitcoin, the base layer where transactions happen on the Bitcoin blockchain is becoming bloated and transactions cannot scale. With the adoption of the Lightning Network as the second layer scaling solution for Bitcoin transactions, Bitcoin can efficiently be used for micropayments.

What is the Lightning Network?

Lightning is a decentralized network that uses smart contract functionality to enable near-instant payments over a network of peer-peer participants. The lightning network is a second layer protocol built on top of the Bitcoin network; A layer 2 solution means that although the lightning network is built separately, it can still interact with the Bitcoin blockchain. It allows transactions in the network to scale while still keeping the security and decentralization of the Bitcoin network. 

Bitcoin Scalability Problem

Although the Bitcoin base layer provides excellent security, transparency, and decentralization, it can be slow and costly for widespread everyday transactions. In Bitcoin, new blocks are generated on average every 10 minutes and support less than 7 transactions per second with a 1-megabyte block limit. In contrast, a payment processor like Visa or Mastercard can support thousands of transactions per second. Additionally, as the Bitcoin network grows, mining fees in the network are expected to rise due to higher network traffic and demand, making it impractical for everyday use. Transactions confirmed on the bitcoin blockchain may even take several hours before it is confirmed. To achieve much higher transaction throughput without compromising on decentralization and security, lightning network was proposed to make transactions extremely fast and cheap.

How does Lightning work

The lightning network works by opening a bi-directional payment channel between two users. A bi-directional payment channel allows payments to flow in both directions. Using payment channels, users can send or receive bitcoin transactions locally and privately outside of the Bitcoin blockchain. Transaction between users occurs through the payment channels, and the ledger is updated locally between the participants. Unlike on-chain transactions, there is no need for miners to mine blocks and broadcast them to the entire Bitcoin network.

Image of payment layers with Bitcoin and the Lightning Network

Direct Payment Channels

Funding Transaction

To demonstrate how the lightning network works, we will use an example of a transaction between Bob and Alice. To start transacting on the lightning network, Bob and Alice will open up a payment channel by depositing bitcoin in a 2-of-2 multisig address. The multi-sig wallet acts as a vault and requires two private keys to authorize, unlocking each user’s funds. This means that both parties must be present to approve the funds. This initial transaction, called the funding transaction, is on-chain and is recorded on the Bitcoin blockchain. When this transaction is confirmed, the Lightning channel is established. The amount of the funding transaction represents the channel’s maximum capacity and is the maximum amount of funds that can be transacted via the channel.

Commitment Transaction

After the channel is opened, Bob and Alice can start signing transactions with each other as many times as they want. These transactions take place outside of the blockchain and are called commitment transactions. The transactions are cheap and instant as they do not have to be mined and propagated throughout the Bitcoin layer 1 blockchain network. Both Alice and Bob will keep a local private copy of the ledger for their balance, updating it for every transaction. In lightning, transactions move between two points; thus, every time a transaction occurs between them, the channel’s state is updated, and the previous state will become invalid. This prevents fraud as neither Bob nor Alice can refer back to the channel’s old state when deciding to settle the transaction on the blockchain.

Closing Transaction

When Bob and Alice decide to settle the funds, they can sign a closing transaction called a settlement transaction. The closing transaction will be mined and will be recorded in the blockchain. The closing transaction will only reflect the final settlement balance between the two users. The Lightning Network does not require cooperation from each of them to exit the channel. Either Bob or Alice can decide to close the channel, ending the relationship. This prevents fraud as it prevents one of them from going offline and locking the other’s funds inside the channel.

Routing Bitcoin Transactions on LN

Routing 

Routing allows nodes to transact even if they are not connected directly. Since all parties have multiple multi-signature channels with different users on the network, users can transact with any other party across the network as long as an interconnected channel path exists between them. If a user wants to transact with a user, but they aren’t connected through a direct channel, they do not have to establish another payment channel. They might be connected indirectly through a single or even multiple nodes; hence, the sender can route the transaction using other nodes’ paths. There must be enough liquidity on the channel paths for routing to work correctly on the Lightning Network. As long as there is an interconnected channel and enough funds on the channel path, transactions can occur.

Let’s say that a user named Alice wants to pay Charlie but does not have a direct payment channel between them. However, Alice has a payment channel with Bob, who has a payment channel with Charlie. If Alice wants to send 1000 sats to Charlie, she will move 1000 sats (plus a small fee) to Bob’s side of the channel. Then Bob will move 1000 sats from his side to Charlie. As a result, Bob’s balance remains the same, and Alice can send a payment to Charlie.

How can Alice pay Charlie without Bob stealing the bitcoin in the process

To prevent cheating in the system, Hash Time Locked Contracts (HTLC) are used. HTLCs allow transactions to be routed across multiple nodes. HTLCs enable two parties to transact without a direct channel between them, using intermediary channels instead. HTLC is a regular bitcoin transaction with a unique script inside it, also known as a smart contract. It is a conditional payment where the transaction is locked for a period of time. HTLCs function as a promising mechanism by producing conditional payments in Bitcoin.  A HTLC  uses hashlocks and timelocks to require that the receiver of payment either acknowledge receiving the payment before a deadline by generating cryptographic proof of payment or forfeit the ability to claim the amount, returning it to the payer

  1. Timelocks are used to limit how long a payment channel will remain open. This prevents a user from disappearing from the network, leaving the wallet’s funds locked. In the case that one party disappears the other party can still retrieve their funds when the timelock expires.
  1. Hashlock: A Hashlock is a type of mechanism that restricts an output’s spending until a specified piece of data is publicly revealed.

How a Hashed Timelock Contract Works (HTLC)

Let’s illustrate with an example. Alice is in a new town to visit his friend Bob. Alice has a payment channel with Bob but doesn’t have a channel with the local coffee shop. It would not make sense to open up a new payment channel with the coffee shop as Alice does not live in the town and she would not be transacting with the shop often. Alice can simply route the payment through Bob who happens to have a channel with the coffee shop

Image of Alice and Bob model to explain the Bitcoin Lightning Network

For Alice to buy a coffee from the shop, the shop can create an invoice requesting 1000 satoshis for the coffee. In the invoice, the coffee shop will generate a random data(x) called the preimage and then generates its SHA256 hash — h(x)  

Image of payment invoice settled with a coffee shop using the Lightning Network

Because the shop isn’t directly connected to Alice, the shop’s lightning node will check if a path exists between them that it can use. When the shop’s node sees that it has a payment channel with Bob, who happens to have a channel with Alice, Alice can then accept the invoice in her wallet. 

Alice can then use her payment channel with Bob to pay him 1,000 satoshis (plus a small fee). She will also add the hash the coffee shop gave her in the invoice along with an extra condition: for Bob to claim the payment, he has to provide the data used to produce that hash. This is a contingent payment with a timelock.

Image of a payment channel between Alice and Bob explaining Lightning Network transactions

Bob uses his payment channel with the coffee shop to pay for the coffee, and Bob adds a copy of Alice’s same condition on the payment she gave Bob.

The coffee shop has the original data used to produce the hash (called a pre-image), so the shop can use it to finalize its payment and fully receive Bob’s payment. By doing so, the shop makes the pre-image available to Bob.

Image of Bob and Coffee shop icon to display Bob settling a Lightning Network Transaction with a coffee shop.

Bob uses the pre-image to finalize his payment from Alice. This is all done automatically by the lightning nodes. 

Bitcoin transaction fees on the Lightning Network

The Lightning Network allows nodes to charge fees for routing payments, however these fees are much smaller as no cryptographic puzzle needs to be solved by the nodes. There are two types of fees in the lightning network: 

  1. Base Fee
  2. Liquidity Provider Fee. 

There are no miners in the lightning network, and all fees go to the node that routes the payment. The Lightning Network is set up to compensate people for providing liquidity to the network. A user who has a lightning node can set fees that other Lightning nodes will have to pay to use the users capital for routing.

  • Base fee: The base fee is the fee charged when a transaction is routed through a node. This is a flat fee that the node user set based on how much they think it is worth.
  • Liquidity Provider Fee: This is a fee charged based on how much liquidity a user has in the channel. It is a fee based on how many sats are being sent through a payment channel. For example, if the lightning provider fee is 1 sat per 100,000 sats, it would cost user 1 sat per 100,000 sats payment.

Summary

The lightning network lets users transfer bitcoin by opening a payment channel with another node, which takes the form of a multi-sig smart contract on the blockchain. All transactions are then off-chain, and transactions are updated locally in payment channels between nodes. When one of the nodes of the channel decides to close it, the most recent balance is then pushed to the blockchain. Nodes in the lightning network can transact without opening a payment channel for every transaction. The network allows payments to be routed via interconnected nodes using smart contract functionality.


At OpenNode we believe that merchants should have the ability to leverage both on-chain payments and Lightning Network payments and that’s why we offer both options in our checkout. Are you interested in earning Bitcoin payments with your business using both on-chain & Lighting Network payments? Visit our website to learn more about us and get started for free today!