The lightning network
The lightning network is an off-chain layer2 network, that aims to scale the bitcoin network to be able to handle millions to billions of transactions per second, instantly, through the power of smart contracts.
Nodes can connect to each other and form the Lightning network. Connecting to another node means opening a channel to it, a payment channel connects to other people, companies or services who are the nodes.
Lightning is a decentralized network using smart contract functionality in the blockchain to enable instant payments across a network of participants.
The lightning network for bitcoin is already live on mainnet, and the number of nodes and channels is growing exponentially, and along with it, the network capacity. With the Lightning Network, users are able to transact with anyone who is connected to their network of payment channels through multiple hops, which is conceptually similar to internet routing, and how browsing on the internet hops through multiple servers to get you to the website you would like to visit.
The lightning network is meant to be for smaller value payments and micro transactions of a few cents. For large payments of significant value with bitcoin you should use a regular on chain transaction. The bigger the BTC value of your transaction at the moment, the less likely it will be for your transaction to reach its destination, but as capacity of the network grows, the transaction size limitation can grow too.
Millions of smaller transactions for insignificant purchases like a cup of coffee, do not all need to be written to the blockchain, and are perfect for the lightning network! Since the blockchain is a permanent record of every on-chain transaction, it is really overkill to expect every insignificant transaction to be stored forever on the blockchain.
What benefits does the lighting network provide?
- Instant Payments – Lightning-fast blockchain payments without worrying about block confirmation times. Security is enforced by blockchain smart-contracts without creating a on-blockchain transaction for individual payments. Payment speed measured in milliseconds to seconds.
- Scalability – Capable of millions to billions of transactions per second across the network. Capacity blows away legacy payment rails by many orders of magnitude. Attaching payment per action/click is now possible without custodians.
- Low Cost – By transacting and settling off-blockchain, the Lightning Network allows for exceptionally low fees, which allows for emerging use cases such as instant micro-payments.
- Cross Blockchains – Cross-chain atomic swaps can occur off-chain instantly with heterogeneous blockchain consensus rules. So long as the chains can support the same cryptographic hash function, it is possible to make transactions across blockchains without trust in 3rd party custodians.
How can you make lightning payments?
In order to use Lightning, you’ll need a Lightning-enabled Bitcoin wallet. A Lightning wallet has all the same functionality as a regular wallet, but also allows one to instantly send/receive payments via Lightning.
Desktop wallets to try:
Zap — a desktop wallet with a user-friendly interface, designed by Jack Mallers. – “Trustlessly send and receive bitcoin instantly with minimal fees via the Lightning Network” (mobile wallet coming soon!)
Lightning-app — a desktop wallet developed by Lightning Labs, available for Mac and windows.
Here are some options for Android:
Eclair wallet – Eclair Wallet is a next generation, Lightning-ready Bitcoin wallet for Android. It can be used as a regular Bitcoin wallet, and can also connect to the Lightning Network for cheap and instant payments. Eclair Wallet is a real, self-contained lightning node that runs on your phone. It does not require you to run another Lightning Node node at home or in the cloud.
Bitcoin Lightning Wallet – This app features a standalone SPV Bitcoin wallet with a fully functional built-in Lightning node. It allows for sending and receiving of regular on-chain transactions as well as off-chain Lightning payments.
Using a lightning enabled wallet will make lightning payments super easy. Let the wallet do all the technical stuff in the background for you.
You can test your lightning wallet by purchasing some stickers from the Blockstream online store here: https://store.blockstream.com/product/elements-sticker/
How to accept a lightning payment on your website
If you would like to easily accept a bitcoin lightning payment on your website, you should check out GloBee, which was the first payment provider to integrate lightning payments.
Self hosted solution
BTCPayServer is an open-source, self-hosted open source payment processor for Bitcoin and other cryptocurrencies by @NicolasDorier. With no 3rd party involved, you process your own payments on your store, and are in control of your own funds at all times.
Deploy the system on your own hosting, there are tutorials out there for Azure and Docker deployment to make things a little easier, as well as tutorials for WooCommerce and Drupal integration. With this solution you will have the costs of hosting which you will not have if you use a payment processor.
How exactly does the lightning network work?
The Lightning Network is dependent upon the underlying technology of the blockchain. By using real Bitcoin/blockchain transactions and using its native smart-contract scripting language, it is possible to create a secure network of participants which are able to transact at high volume and high speed.
A payment channel is basically when two parties are doing many transactions, and instead of every transaction being broadcast to the blockchain, you just net all the transactions together at the end, and broadcast the final total transaction to the blockchain.
In layman’s terms, if I owe you R10, and you borrow R20 from me, then we just net the two together, and you pay me the difference of R10 in the final settlement. This is what is happening in a payment channel. There can be thousands of transactions between us as an IOU, all happening off-chain, and you add up the amounts and only settle the difference at the end when you close the channel.
Payment channels use shared multi-signature addresses and exist between two users for a set period of time, for example 10 Days, which is represented by the nLockTime. The nLockTime parameter of a transaction mandates a minimal time (specified in either unix time or block height), before which the transaction cannot be accepted into a block. This means the funds are locked until a specific time is reached.
A Uni-Directional Lightning Channel explained
In a uni-directional channel, before sending money to a provider the customer must first get the provider to send a refund transaction to them for the amount that the customer is going to send, as well as an nLockTime for the example of 10 days. This ensures that at the very worst, the customer can add their signature to that transaction to get a full refund for their funds if the provider does not deliver the goods. With this, the customer can now comfortably send coins to the multi signature wallet, and start making transactions from that multi-signature address to the provider.
During the life time of the channel (10 days in this example) the customer can make many half signed payments to the provider instantaneously, without incurring any transaction fees. Once the provider adds their signature to the transaction, since it does NOT have nLockTime, the transaction is written to the blockchain, and the channel is closed.
A Bi-Directional lightning Channel explained
If the buyer signs a transaction with an nLockTime of 9 days, one day before the refund transaction will occur, the merchant by signing it will know that in 9 days time, he will own those coins. He can then change direction, and use those coins to pay the customer by signing a new transaction for 8 days, one day before the customers payment to him, and so on.
Every time the payment channel changes direction, the nLockTime must be brought forward 1 day in order to overwrite the previous transactions.
3+ Party Payment Channel explained
Things get slightly more complicated when you add more people, and you start having 3+ party payments. A simple way to think about this, is if you owe me R10, and I owe Bob R10, you can just pay Bob the R10 and we are square. This is basically what is happening with multiple people securely using the lightning network on the bitcoin blockchain. The lightning network allows you to send a payment through other peoples payment channels.
If you have two people who would like to pay each other, but they do not have a channel open between them. If both parties are already customers of a store where they already have a payment channel setup. In this situation Hashed Time-Lock Contracts (HTLC) are used to make sure that everyone in the chain of payments can prove that they have paid the next person in the chain, no matter how many people are involved.
By creating a network of these two-party ledger entries, it is possible to find a path across the network similar to routing packets on the internet. The nodes along the path are not trusted, as the payment is enforced using a script which enforces the atomicity (either the entire payment succeeds or fails) via decrementing time-locks.
HTLCs in payment channels example:
Payment channels already use timelocks, and it can be relatively simple conceptually to extend them with hashlocks. This provides the useful benefit of making payments routable across two or more payment channels.
- If Alice wants to pay Charlie, and they both have a channel open to Bob, then the payment can go through Bob to get to Charlie.
- Charlie generates a random string (called a pre-image) which will be used to sign transactions and generates its SHA256 hash. Charlie gives that hash to Alice.
- Alice can use her payment channel to Bob to pay Charlie as the payment will go through Bob to get to Charlie. This is done by creating a Hash Time-Lock Contract (HTLC) between Alice and Bob comprised of two transactions. The first transaction is a refund transaction with an NLockTime of 2 days, and the second is the payment to Bob that he can only claim if he can provide the original pre-image Charlie used to create the hash.
- Bob can use his payment channel to Charlie, to pay him the money that Alice sent him. He first sends Charlie the hash to prove that he has been paid by Alice, after which they create a Hash Time-Lock Contract (HTLC) between them comprised of two transactions.
- Again, the first is the refund transaction back to Bob with an NLockTime of 1 day this time, so that it closes before the contract between Bob and Alice. The other transaction is the payment from Bob to Charlie, that Charlie can only claim if he can provide the original pre-image used to create the hash.
- Charlie then sends the pre-image to Bob, to prove he is the one who created the hash, and therefore the person who is getting paid. Instead of using the pre-image to broadcast the transaction to the blockchain, the contract between Bob and Charlie is removed and the transaction longer require the pre-image when the channel closes.
- Bob then sends Alice the pre-image, proving that he has paid Bob on her behalf and the contract is changed between them too, removing the pre-image requirement for the transaction to take place when the channel closes.
At this point Alice has effectively paid Charlie by sending the transaction through Bob, and the entire transaction still has not touched the blockchain. In this example, everyone has worked well together, and there have been no problems.
If however Alice had decided to ignore Bob after he has paid Charlie, he could broadcast the transaction with the pre-image to the blockchain, closing the channel with Alice, forcibly pulling the funds from her. This will work as long as he does this before the refund nLockTime on their channel runs out. Otherwise Alice is free to redeem her payment and Bob will lose his funds.
The nLockTime method provides an incentive for every member in the chain to react and both protects and ensures that each parties channel does not close before the other. So each can pull funds safely with that knowledge.
Increased privacy with lightning
One great feature of the lightning network is increased privacy. As your payment finds a route through to its destination, it might have 10 hops, or 20 hops before it gets to its destination, and every participant along that route is getting the incoming information and the outgoing destination to the next hop, not knowing if the incoming payment is the person who started the transaction or if the outgoing payment to the next hop is the end of the destination. Each hop in the route only has the information for the previous and the next destination in the route.
Running your own lightning node
You can add your own node to the lightning network by opening a channel from your node to another bitcoin node, thereby joining the lightning network. Running a lightning node requires running a bitcoin full node, either with your own hardware, on your computer, or hosted on a remote server.
If you would like to setup a hardware node there are a few options available where you build your own, as well as some pre-made nodes that just need to be plugged in and synced.
How to run a Lightning Network node on Windows
Casa Lightning node
Lightning in a box – Pre-built bitcoin lightning nodes with BTCPay server installed!
How to build your own portable plug-in Lightning node!
The perfect Bitcoin ⚡️Lightning️⚡ node
Here are some tutorials to run a hosted node:
Run your own mainnet Lightning Node