One of the biggest pain points when using Bitcoin is the long transaction times. This can be frustrating to new users that are just getting into this digital currency. One of the easiest ways to improve this is to use a secondary payment layer such as Bitcoin Lightning.
Bitcoin Lightning is a system that uses custom Bitcoin transactions to move assets quickly outside of the regular blockchain process. In this regard, Lightning is similar to the Tor Network where it uses TCP/IP as its backbone when routing anonymous connections.
Also read: How to Manage your Finances with Ledger Mode in Emacs
Bitcoin allows you to easily exchange money over the Internet without a central authority. Every transaction you make is direct to the payee, and you distribute it over the entire network.
Why Is Bitcoin Slow?
As a result, any payment that you make within Bitcoin takes time to complete and confirm. For example, a regular Bitcoin transaction normally takes 10 to 30 minutes to clear.
How Does the Lightning Network Work?
At its core, Lightning works by creating an “on-chain” payment that parks an amount of Bitcoin to your node. Lightning then uses your parked Bitcoin to create a channel between other Lightning nodes.
This allows you to create additional “off-chain” payments through Lightning that do not need to clear in the blockchain, significantly reducing the payment time between two users. For example, a regular Lightning transaction only takes about a second to clear.
Also read: How to Fix High CPU Usage in Linux
Setting Up Your Own Bitcoin Lightning Node
To get started with Lightning, it’s good to have your own personal node. Similar to setting up a Bitcoin node, a Lightning node allows you to have full control over your transactions in its network.
To start the installation, you need to have a number of resources available and ready:
- Machine with a decent amount of storage and memory. In my case, I am using an old Dell Optiplex desktop with 1TB of storage and 2GB of RAM.
- Constant Internet connection, as a Lightning node needs to regularly communicate with other nodes to maintain its state.
- Small amount of Bitcoin that you can use to “fund” your Lightning node.
Knowing these factors, this tutorial focuses on installing c-lightning to an Ubuntu 22.04.1 LTS machine.
Obtaining the Dependencies
The first step in installing c-lightning is to create a new user account to ensure that any files and settings for your node will be separated from your main user:
sudo useradd -s /bin/bash -d /home/lightning -m -G sudo lightning sudo passwd lightning
Switch to this new user by running: su lightning
, then add a third-party repository that will contain all the programs for c-lightning to work:
sudo add-apt-repository -u ppa:lightningnetwork/ppa
Install the node’s dependencies:
sudo apt install software-properties-common lightningd snapd
Also read: 4 Ways to Increase the Battery Life of Your Linux Laptop
Setting Up a Simple Bitcoin Node
From here, you need to install and configure a simple Bitcoin node. As discussed above, Lightning is an overlay network that works by using custom Bitcoin transactions to create “fund channels.”
To create these channels, your Lightning node needs to run alongside a simple Bitcoin node that can be installed by running the following command:
sudo snap install bitcoin-core
Create a symbolic link between your root “snap” directory and “bin” folder:
sudo ln -s /snap/bitcoin-core/current/bin/bitcoin{d,-cli} /usr/local/bin/
Start the Initial Block Download for your Bitcoin node:
bitcoind -daemon
This command will send the node to the background to download and verify the entire Bitcoin blockchain. You can check its progress by running the following command:
tail -f /home/$USER/.bitcoin/debug.log
It is important to note that this process can take between a few hours and a few days. For example, my Dell Optiplex desktop took 5 days to fully synchronize with the Bitcoin network.
Configuring Your Simple Bitcoin Node
Once your node is in sync with the network, make sure it is compatible with Lightning by first stopping the Bitcoin node:
bitcoind stop
Edit its configuration file under “/etc/bitcoin.conf”:
nano /home/$USER/.bitcoin/bitcoin.conf
Disable two options: walletbroadcast
and prune
. The first option tells your Bitcoin node to only use its data with Lightning, and the second option ensures that you are saving the entire blockchain to your hard drive.
walletbroadcast=0 prune=0
Restart your Bitcoin node by running the following command:
bitcoind -daemon
Starting Up Your Bitcoin Lightning Node
Start your Lightning node by running the following command:
lightningd --network=bitcoin --log-level=debug --daemon
This command will send the node to the background and create its configuration files under “/home/$USER/.lightning.” Test whether your Lightning node is running properly with this command:
lightning-cli --lightning-dir=$HOME/.lightning getinfo
Also read: The Best Open Source Tools to Secure Your Linux Server
Finalizing Your Bitcoin Lightning Node
With c-lightning running, you can finalize your node by creating the “fund channels” between different Lightning peers. As discussed above, a “fund channel” is a custom Bitcoin transaction that allows you to move your Bitcoin without committing to the blockchain.
To create this, first “deposit” Bitcoin to your Lightning node by generating a receiving address for your node with the following command:
lightning-cli newaddr
Go to an external wallet and send Bitcoin to that receiving address. In my case, I will send 0.00050000 BTC using my Bitcoin Core wallet.
Once the transaction is in the blockchain, your Lightning node will automatically update its fund balance. View this by running the following command:
lightning-cli listfunds
Peering With Remote Bitcoin Lightning Nodes
Start to connect your node to its peers in the network to allow you to send and receive Bitcoin through Lightning.
To start, find both its public key and IP address. Similar to using PGP Encryption, this allows you to connect and verify a node’s authenticity through public-key cryptography.
One of the most common tools for doing this is 1ML: a website that lists all the active Lightning nodes in the network.
Once you have an active node, use the lightning-connect
command to establish a peer request. For example, this command will peer my node to ACINQ:
lightning-cli connect 1 03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f 3.33.236.230
- The first value indicates the slot number that you want to assign to the current peer. For this node, I am using slot 1.
- The second value is the remote node’s public key. In this case, I am using ACINQ’s public key.
- The third value is the remote node’s public IP address.
To finalize the request, send some of your node’s Bitcoin balance to the remote node to create a “fund channel” between you and a peering node.
For example, this command will create a “fund channel” for 0.00030000 BTC to the first peer in my list:
lightning-cli fundchannel 1 30000
Sending and Receiving Lightning Payments
With an active “fund channel” running, use your node to send and receive payments. By default, Lightning uses the BOLT11 format that starts with “lnbc” to differentiate it from Bitcoin.
To send Bitcoin through Lightning, you must know the BOLT11 address of your recipient. For example, the following command sends 0.00005000 BTC to a remote address:
lightning-cli pay lnbc20m1[...]qdhhwkj 50000
You can also receive Bitcoin through Lightning using the invoice
command and specify the exact amount of Bitcoin you want to receive. For example, this command creates an invoice for 0.00010000 BTC:
lightning-cli invoice 100000
Also read: How to Find Large Files in Linux
Frequently Asked Questions
I am using Dojo as my Bitcoin node. Is it possible to use it with c-lightning?
No. By default, Dojo is a program that uses Docker containers to run a sandboxed instance of Bitcoin Core. A Dojo Bitcoin node will not be able to communicate with any program outside of its sandbox container.
My wallet does not support a "bc1" address. Is it still possible to deposit to my Lightning node?
Yes! While c-lightning natively generates in a “bc1” address format, it is possible to transfer Bitcoin to a Lightning node using the older P2SH format. To do this, run the following command: lightning-cli newaddr p2sh-segwit
.
My Lightning payment occasionally fails. Is my node broken?
No! This issue is most likely due to a lack of node connections between you and the recipient. The Lightning network, by design, relies on each node consistently and directly talking to each other.
One way to fix this issue is to increase the amount of peers and “fund channels” that your local node connects to. For example, having at least two to three active peers will allow you to connect to almost any Lightning user.
Image Credit: Unsplash and Wikimedia Commons. All alterations and screenshots by Ramces Red.
Our latest tutorials delivered straight to your inbox