Tutorial : From scratch to a LND 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 c-lightning one, click here.

 

LND is a complete implementation of the protocol of the Lightning Network. It is an open-source project that anyone can contribute if you follow the guidelines and it is maintained and mostly done by Lightning Labs Inc. LND is a command line software, which means there is no GUI (Graphical User Interface) integrated in it, it is meant to work as a back-end server (node) for other applications that could bring the GUI. It is also a bitcoin mainnet wallet and a lightning network wallet.

 

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

 

 

Windows or MacOS

 

First of all, to run LND locally on your Windows 10 or MacOS computer, refer to Pierre Rochard’s Node Launcher, it is a very straightforward software that will execute very easily and you don’t even have to install bitcoind beforehand it will do all for you. You can always contact us if you have difficulties using it.

https://github.com/lightning-power-users/node-launcher

 

For Linux, it goes differently, so here is a tutorial to install LND on a Debian-based machine like Ubuntu 18.04. I will always be using bitcoind as the backend.

 

Linux 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 lnd :

 

server=1

listen=1

daemon=1

rpcuser=insert_a_username

rpcpassword=insert_a_password

zmqpubrawblock=tcp://127.0.0.1:28332

zmqpubrawtx=tcp://127.0.0.1:28333

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

#txindex=1

 

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 LND.

 

Part 2 : Installing LND

 

You can pretty much follow this tutorial step-by-step and it will work, but I will do my own version too.

https://github.com/lightningnetwork/lnd/blob/master/docs/INSTALL.md

 

First off, install Git and Make which will be necessary to install LND. Then download Go as an encrypted package.

 

$ sudo apt-get install git make

$ sudo apt-get update

$ wget https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz
$ sha256sum go1.11.5.linux-amd64.tar.gz | awk -F " " '{ print $1 }'

 

The output of the last command should be : ff54aafedff961eb94792487e827515da683d61a5f9482f668008832631e5d25.

If it matches, it is the right package and you can proceed with its installation by extracting it first.

 

$ tar -C /usr/local -xzf go1.11.5.linux-amd64.tar.gz

$ export PATH=$PATH:/usr/local/go/bin

 

Then you will need to set up your $GOPATH environment to the proper directory and to add $GOPATH/bin to your PATH. This simply means that you will be able to use lnd and lncli as direct commands, without indicating their locations. To avoid doing this every time you open a terminal window, do the following :

 

$ sudo nano ~/.profile

 

This will open the file .profile.  Add the following lines at the end of the file, and save it.

 

export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin

 

Open a new terminal window so that it can take effect and run these commands to install LND.

 

$ go get -d github.com/lightningnetwork/lnd

$ cd $GOPATH/src/github.com/lightningnetwork/lnd
$ make && make install

 

Run this command to make sure it was installed properly.

 

$ make check

 

You can then launch lnd to test if it would work.

 

$ lnd

February 22, 2019

gallery/lndfail
gallery/lndsuccess
gallery/screenshot_2019-02-13_18-06-10
gallery/screenshot_2019-02-13_19-14-09
gallery/screenshot_2019-02-13_20-02-49
gallery/screenshot_2019-02-13_21-00-55

Copy the payment request and paste it on this command :

$ lncli sendpayment --pay_req=PASTE_IT_HERE

 

It will ask you to confirm, simply input: yes.

There you go, you have completed your first lightning transaction on LND!

 

Part 9 : Receive a Payment

 

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 for someone to make an incoming channel to you. You can also just spend money on your outcoming channel, and that money spent will be able to be paid back to you by other nodes that can route to yours or by the node you are connected to.

 

To make an invoice, run this command, replace 50000 by the number of satoshis you wish to invoice :

 

$ lncli addinvoice 50000

 

To verify you have received the payment, run this command :

 

$ lncli listinvoices

 

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.

$ lncli listchaintxns

$ lncli listunspent

 

Once your balance is confirmed, you can open a channel to use the lightning network.

 

Part 7 : Opening a Channel

 

I have chosen Pierre Rochard’s LightningPowerUsers node as an example since it is one of the best nodes in the network. https://lightningpowerusers.com/home/

 

First, connect on the p2p layer to the other node by running this command :

 

$ lncli connect 0331f80652fb840239df8dc99205792bba2e559a05469915804

c08420230e23c7c

@74.108.13.152:9735

 

You make sure you are well connected by running this command:

$ lncli listpeers

 

Then, to open a channel with this peer, run this command :

 

$ lncli openchannel --node_key= 03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370

e2c366597a3f8f --local_amt=10000

 

You can check it’s confirmation status by running either one of these commands :

 

$ lncli channelbalance

$ lncli listchannels

 

Once your transaction gets enough confirmations on the base layer, you are ready to finally use the lightning network!

 

Part 8 : Make a Transaction

 

Let’s test with the Lapp Yalls.org, choose a random article and select Pay to Continue Reading.

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 : Depositing Funds in the BTC wallet

 

Back to LND, let’s deposit funds in the BTC wallet. First you have to generate an address, it can either be a np2wkh or a p2wkh. They are both segwit addresses, p2wkh being native segwit and np2wkh starts with a 3 and is compatible with wallets that don’t support segwit. So if you’re withdrawing from most exchanges, you would have to use a np2wkh address since they still aren’t segwit compatible. So run either of these commands depending on your needs.

 

$ lncli newaddress np2wkh

$ lncli newaddress p2wkh

 

Simply paste the address on your other bitcoin wallet and choose the desired amount.

 

If you want to convert it to a QR CODE format, there is a simple way one can do this with a program called qrencode.

 

$ sudo apt-get install qrencode

$ qrencode -o qrcodefilename.png ‘ADDRESS’

 

Simply replace ADDRESS with your address, and replace the file name, and the QR code will be available in the folder you are located. You can do this for pretty much everything else, like a node public key, an invoice, etc.

 

Once you make the transaction on your other wallet, you can check if the funds are available by entering either one of these commands, they all pretty much define themselves :

 

$ lncli walletbalance

Back up your 24 words seed generated safely. Keep in mind that this is only a backup for your bitcoin main net coins and not your lightning channels. Then unlock your wallet with this command :

 

$ lncli unlock

 

LND will sync with the network, this might take some time, around half an hour. You can check its state with this command. Once this becomes true, you are synced and ready to proceed.

 

$ lncli getinfo

Part 3 : LND configuration file

 

This step is about modifying the LND configuration file according to your needs. First to create a config file, run this command.

 

$ nano ~/.lnd/lnd.conf


A text file will open inside your terminal. You can input the desired configuration in this file, to find all the potential properties, click here. If you wish to keep it simple as I did, here is my configuration file, the first is necessary, the second part is optional if you wish to receive incoming transactions from the network and the third part is if you wish to connect a wallet on another device to LND.
 

#first

bitcoin.active=1

bitcoin.mainnet=1

bitcoin.node=bitcoind

bitcoind.zmqpubrawblock=tcp://127.0.0.1:28332

bitcoind.zmqpubrawtx=tcp://127.0.0.1:28333

bitcoind.rpcuser=insert_the_same_username_as_on_bitcoin_rpc_configuration

bitcoind.rpcpass=insert_a_password_as_on_bitcoin_rpc_configuration

 

#second

externalip=YOUR_EXTERNAL_IP

listen=0.0.0.0:9735

 

#third

rpclisten=0.0.0.0:10009

tlsextraip=IP_OF_ANOTHER_DEVICE_YOU_WISH_TO_CONNECT

 

Save this file and your configuration is ready.

 

Part 4 : Launching LND

 

You can then launch lnd in various ways.

 

Either in a regular way, but you will have to open a new terminal window to use lncli and if you close the first terminal window, lnd will shut down.

 

$ lnd

 

Either as a background, but once you close the terminal, lnd will shut down. you should also tail the log file.


$ lnd &

$ tail $HOME/.lnd/logs/bitcoin/mainnet/lnd.log

 

Last option is to run lnd in the background and that it doesn’t shut down when you close the terminal window, run this command if this is what you wish.

 

$ nohup lnd > /dev/null 2> /home/USERNAME/.lnd/err.log &

 

Either of these commands will make lnd launch for the first time, so you will have to create a password. In the first scenario, you will have to open a new terminal, in the other two, you can simply continue on the same terminal. Enter the following command and follow the steps.

 

$ lncli create

if this is the output, it means the installation didn’t go well. Check to see that you didn't skip a step. If you didn't and it still doesn't work, you can either contact us through Twitter for a quick response or ask for help in LND's Slack Channel.

 

But if the following is the output instead, it is installed and you can proceed into the next part.