Tutorial : From Scratch to a C-lightning Transaction

Last week, we illustrated the Lightning network protocol, BOLTs (Basics of Lightning Technology), through a visual diagram that we hope has contributed to the community’s understanding of this technology. If you didn’t have time to check it out, don’t hesitate to give it a quick look here!

 

This week, we want to get into the implementation of this technology to understand how it works in the real world. We have built tutorials for both major implementations, LND & C-lightning. To view the LND one, click here.

 

C-lightning is a lightning network implementation mostly written in C and the gRPC is done on Python. It is an open-source maintained by Blockstream. Rusty Russell and Christian Decker are the main developers on this project but there are a lot of other contributors. It is only available on Linux for the moment.

 

This will be a tutorial that will be a step-by-step guide to c-lightning.

 

Part 1 : Setting up Bitcoind

 

To install bitcoind on a linux machine like ubuntu, refer to StopandDecrypt awesome step-by-step guide on Medium, this is far better from anything I could have written. In this process, he completely builds from source while verifying the authenticity of the packages.

https://hackernoon.com/a-complete-beginners-guide-to-installing-a-bitcoin-full-node-on-linux-2018-edition-cb8e384479ea

 

If you don’t want to build from source for whatever reason, there is an easier way to install bitcoind and bitcoin-qt (GUI version), you simply have to run these commands :

 

$ sudo apt-add-repository ppa:bitcoin/bitcoin

$ sudo apt-get update

$ sudo apt-get install bitcoin-qt bitcoind

 

Once it is installed, you can create a configuration file by running this command.

 

$ nano ~/.bitcoin/bitcoin.conf

 

My configuration file looks like this and it is the minimum necessary to run c-lightning :

 

server=1

listen=1

daemon=1

rpcuser=insert_a_username

rpcpassword=insert_a_password

#uncomment the following if desired, it will be a longer and heavier installation but will make lnd run faster.

#txindex=1

 

but you can also modify your configuration file with this great tool by Jameson Lopp.

https://jlopp.github.io/bitcoin-core-config-generator/

 

you can then launch bitcoind by simply entering this in the cli.

 

$ bitcoind

 

or for the gui version :

 

$ bitcoin-qt

 

You have now launched bitcoind, and it has started syncing the blockchain. This can take several days, to check its progress, enter this command :

 

$ bitcoin-cli getblockchaininfo

 

Once this step is finished, you are ready to move forward to c-lightning.

 

Part 2 : Installing c-lightning

 

First, we have to install the dependencies, run these commands :

 

$ sudo apt-get update
$ sudo apt-get install -y \
 autoconf automake build-essential git libtool libgmp-dev \
 libsqlite3-dev python python3 net-tools zlib1g-dev libsodium\
 libbase58-dev

 

This should download all the dependencies but if libsodium’s installation doesn’t work out, here is how to do it!

 

$ wget "https://download.libsodium.org/libsodium/releases/LATEST.tar.gz"

$ tar -xvf ./LATEST.tar.gz

$ cd libsodium-stable

$ ./configure

$ make && make check

$ sudo make install

 

Now for c-lightning, here are the steps, they are very straightforward.

 

$ git clone https://github.com/ElementsProject/lightning.git

$ cd lightning

$ ./configure

$ make

 

The installation is complete and you’re ready to proceed to create and modify the configuration file.

 

Part 3 : Configuration file

 

$ ./lightningd/lightningd --network bitcoin --log-level debug &

$ ./cli/lightning-cli help

 

the config file is written in the .lightning folder under the name config.

 

$ nano ~/.lightning/config

 

my config file looks like this which basically means that the network of the lightning node will be Bitcoin’s mainnet, that the log level to print out will be at the debug level, that :

 

network=bitcoin

log-level=debug

addr=0.0.0.0:9735

bitcoin-rpcuser=insert_the_same_ username_as_bitcoin_rpc

bitcoin-rpcpassword=insert_the_same_ password_as_bitcoin_rpc

 

Save and exit.

 

You can then launch it in an easier way.

 

$ ~/lightning/cli/lightning-cli stop

$ ~/lightning/lightningd/lightningd &

 

Part 4 : adding lightningd and lightning-cli to PATH

 

To add lightningd and lightning-cli to your PATH, type in these commands, first to stop lightningd and then to go on the .profile file.

 

$ ~/lightning/cli/lightning-cli stop

$ nano ~/.profile

 

Then add this at the bottom of the file :

 

export PATH=$PATH:~/lightning/lightningd

export PATH=~/lightning/lightnind:$PATH

export PATH=$PATH:~/lightning/cli

export PATH=~/lightning/cli:$PATH

 

There are other ways to do this but this will make it work. You have to pen a new terminal for it to take effect. You can now use lightningd and lightning-cli directly :

 

$ lightningd &

$ lightning-cli getinfo

 

Note : if something like Permission Denied or Trying to write on Read-mode appears, it means you have to change the ownership status of some files.

 

$ ls -l ~/.lightning

 

gossip_store, lightning-rpc, lightningd.sqlite3 and hsm_secret will probably show as owned by root like here.

February 22, 2019

gallery/root2
gallery/root
gallery/channels
gallery/screenshot_2019-02-13_21-00-55

Once your channel appears by running the previous command, you are now ready to transact on the Lightning network!

 

Part 7 : Make and receive a transaction

 

I usually test my node by making a micro-payment to yalls.org to read an article that interests me.

I would also like to add that backing up hsm_secret will backup your on chain funds. For more information about this, you can check out this issue request on the official repo: https://github.com/ElementsProject/lightning/issues/1156

 

It should now allow you to directly calling it without using sudo.

 

$ lightningd &

$ lightning-cli getinfo

 

You can also launch lightningd in a daemon mode :

 

$ lightningd --daemon

$ lightning-cli getinfo

 

Part 5 : Network and Port Configuration

 

To allow incoming connections for your bitcoin node and your lightning node, you need to have 8333 and 9735 ports open and correctly forwarding to your respective nodes.

 

Pierre Rochard’s has written an article that should answer all your questions about port forwarding for bitcoin and lightning. https://medium.com/@pierre_rochard/bitcoin-and-lightning-nodes-port-forwarding-faq-f92ca5c50d5d

 

This part is obviously optional, but if you decide to do it, here is a complete guide about it.

https://bitcoin.org/en/full-node#network-configuration

 

to check if your ports are open, use this simple tool.

https://www.yougetsignal.com/tools/open-ports/

 

Part 6 : Deposit funds and Open a Channel

 

We are now ready to go on to the lightning-cli commands. The first step, we have to connect to another node to sync the channel graph, I’ve chosen Pierre Rochard’s LightningPowerUsers node to connect and later to open a channel because I consider it one of the best nodes in the network :

 

$ lightning-cli connect 0331f80652fb840239df8dc99205792bba2e559a05469915804c08420230e23c7c@ 74.108.13.152:9735

 

The syncing is automatic and will probably take a few minutes. It’s now time to generate a bitcoin address to receive funds onchain.

 

$ lightning-cli newaddr

 

The previous command will return a segwit native bc1 address. If you need a p2sh segwit address that starts in a 3 and is compatible will non-segwit wallets or exchanges, run this command instead.

 

$ lightning-cli newaddr p2sh-segwit

 

Make the transaction by sending some bitcoins to the address #reckless, but always keep in mind that this software is still in beta phase, therefore experimental. Run this command to check if the bitcoin was well received.

 

$ lightning-cli listfunds

 

Once the transaction is confirmed, it’s now time to open a channel to go on the Lightning network by running this command.

 

$ lightning-cli fundchannel 0331f80652fb840239df8dc99205792bba2e559a05469915804c08420230e23c7c amount_in_satoshis

 

The funding transaction requires three confirmations for the channel to be available, so it will take around half an hour. Once it is confirmed, you will be able to find the channel by running this command :

 

$ lightning-cli listchannels | grep ‘your_node_pubkey’

run these commands to change the ownership of those files to get this result.

 

$ sudo chown USERNAME lightningd.sqlite3   

$ sudo chown USERNAME lightning-rpc

$ sudo chown USERNAME gossip_store

$ sudo chown root hsm_secret

This is the command to pay an invoice. Obviously, change the last part for the payment request id of the invoice that you want to pay.

 

$ lightning-cli pay lnbc1500n1pwfja4pp5xmw9m37spxhlppmd4y……………

 

To receive a payment, you have to either have an incoming channel or to have pushed funds on the other side of the channel by making a payment. First off, if you don’t have anyone that will make an incoming channel to your node, LightningPowerUsers will do it if you make an outcoming channel to the node first, to find out more, check out the website here.

 

 Keep in mind that you will have to have your port 9735 open and forwarded. But you can also just spend money on your outcoming channel, and that money spent will be able to payed back to you by other nodes that can route to yours.

 

This is the command to create an invoice, this will create an invoice for 50000000 msatoshi or 0.0005 btc with the label : reckless, and the description: craig_wright_is_a_loser.

 

$ lightning-cli invoice 50000000 reckless craig_wright_is_a_loser

 

I hope you have enjoyed this tutorial, contact us on Twitter if you have any issues and we will help you with our highest capacity.