The Lightning Network: How to install, send and receive Bitcoin (money)

The Lightning Network: How to install, send and receive Bitcoin (money)

Lightning Network is the next big thing in Bitcoin. And that big thing is almost here. So here in this tutorial, we will see…

  1. How to set-up a Lightning Network Node?

What is the Lightning Network?

To Simply put it Lightning Network is a layer 2 protocol, which enables users to open payment channels between two parties and send-receive bitcoins via that channel, all of this without having to write it to the blockchain main layer every time. This makes payments almost instantaneous and allows room for massive scalability.

The only time transactions are recorded on the Blockchain main layer is when channels are opened and closed.

@alexbosworth recently tweeted…

“The Lightning Network is actually two scaling solutions in one package. The first is that payment channels scale transaction volume between two points. This is probably enough for many many use-cases. The second is that it scales users. One channel gives access to N destinations.”

There are three major implementations of Lightning Network that we can use.

At the time of writing this article, all the three implementations are interoperable. However, based on the research I have done, LND is the most used implementation and is more up-to-date with security features and enhancements. For eg: At the moment LND is the only implementation to enable Data Loss Protection (DLP), an important feature to recover funds in case of a disaster.

Update: It has come to my knowledge that the other two implementations viz: C-lightning and eclair support DLP to some extent now.

Prerequisite

At the moment LND requires a full bitcoin node synced up and running. We will be setting up with Testnet, but setting up on Mainnet is no different. The reason for choosing Testnet is that it’s blockchain size is lesser when compared to Mainnet and hence it syncs up faster.

We will need a VM with a disk space around 100GB. For this tutorial, we will be setting up the Lightning Network node on a Digital Ocean VPS.

Setup Droplet on Digital Ocean

  1. Create an account

Use the given referral link to create an account on Digital Ocean to get 100$ free credit. (I’ll get 25$)

Once you signup, click on create a droplet.

On the create droplet page, select ubuntu 18 as our operating system.

Next, select the droplet configuration, If you are setting up a Mainnet node select the 320 GB option, otherwise you can select 160GB.

Next, we need an ssh key pair. On windows, we can use puttygen to create one.

  1. Download and start puttygen

Copy the generated key from the puttygen onto DigitalOcean.

Once the droplet is created you can ssh into it using …

ssh root@<droplet's ip-address> -i <path to ssh private key>

On Windows, you can use Putty.

Setting Up Lightning Node

Now that we have our droplet ready, let’s prepare to install the Lightning Network Node.

LND is GO implementation of Lightning Network. So we will first need to set up Go.

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

The final output should be efce59fac5ebc7302263ca1093fe2c3252c1b936f5b1ae08afc328eea0403c79

Next extract the tar and export the go path.

tar -C /usr/local -xzf go1.12.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

Now we need to set $GOPATH environment variable to point to the path of your workspace. We will also need to add $GOPATH/bin to the PATH variable. This enable shell to detect the installed binaries.

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

To make sure we don’t have to set these variables every time we start a new terminal, we will add it to ~/.profile file.

sudo nano ~/.profile

//add the below lines to the file
export PATH=$PATH:/usr/local/go/bin
export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin

//reload the file

source ~/.profile

Installing LND

To install LND run the below commands…

go get -d github.com/lightningnetwork/lnd
cd $GOPATH/src/github.com/lightningnetwork/lnd

Install Build Essentials, this is required for building the LND program.

sudo apt-get update
sudo apt-get install build-essential

Build LND.

make && make install

Once this is complete, we have LND installed. But before we can start it we need to have a fully-synced bticoin node. We will use btcd as our backend.

make btcd

We need to run btcd in background. So we will run it within the screen.

screen -S btcd

Run btcd…

btcd --testnet --rpcuser=ciphertrick --rpcpass=ciphertrick

Press Ctrl+A+D to exit from the screen.

Now we have to wait for the node to sync. We can check the status by running…

btcctl --testnet --rpcuser=ciphertrick --rpcpass=ciphertrick getinfo

Note. If in case you get a btcctl command not found error on running the above command, then we can fix this by building btcd separately.

$ cd $GOPATH/src/github.com/btcsuite/btcd
$ GO111MODULE=on go install -v . ./cmd/...

Now it’s time to wait to let btcd get fully synced.

Once btcd is synced. We can start LND. Now open another screen tab buy running…

$ screen -S lnd

$ lnd --bitcoin.active --bitcoin.testnet --debuglevel=debug --btcd.rpcuser=ciphertrick --btcd.rpcpass=ciphertrick --externalip=167.99.231.18

Starting in Mainnet will be similar just replace--bitcoin.testnet with--bitcoin.mainnet. If all is well you should see LND asking you to unlock your wallet or create a new one. We can do that using the lncli tool.

Detach from the screen by pressing Ctrl + A + D.

Now since this is the first time we are setting up a Lightning Network node, we will have to create a wallet.

$ lncli create

Enter a password of your choice. It will ask if you have an existing cipher seed, select No. Enter the passphrase to encrypt your cipher seed. Once done, LND wallet will generate a new cipher seed for you, make sure you write it down at some safe place. Also, note down your wallet-password and passphrase.

Once our wallet is created, let’s have some fun. First, we will need some funds to open up new channels. To do so, generate an address on your wallet.

LND’s authentication works on macaroons, hence while using any command we will have to pass the macaroon path to lncli.

$ lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon newaddress np2wkh

{
"address": "2NFJ1aHfX1aq9fv95BxsYbn6R4EYaXZv4zW"
}

Since we are trying out on Testnet we can just use a faucet to get some balance. In the case of Mainnet, you will have to transfer bitcoin from elsewhere.

Below are some of the Bitcoin Testnet faucets available, head over and paste your address to get some coins.

Let’s check if we have received the funds from the faucet.

lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon walletbalance


{
"total_balance": "399515",
"confirmed_balance": "399515",
"unconfirmed_balance": "0"
}

Once we have the funds we can start playing around with the Lightning Network.

A few concepts

Before we start using the lightning network to send and receive money, let’s just first understand a few things.

  • Node — A node is a server running Lightning Network.

Making Payments on Lightning Network

Before we can make a Lightning Transaction we need to open a channel with a node. It’s good to find out highly connected and active nodes to connect to. We can browse Lightning Nodes here.

To open a channel with a node, it should first be added as a peer. For the demo, I will be connecting to a channel named endurance.

$ lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon connect 03933884aaf1d6b108397e5efe5c86bcf2d8ca8d2f700eda99db9214fc2712b134@34.250.234.192:9735

Once connected, we can open up a direct channel with the Node.

$ lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon openchannel 03933884aaf1d6b108397e5efe5c86bcf2d8ca8d2f700eda99db9214fc2712b134 600000

//output
{
"funding_txid": "3be6f4be4446baf5fb4c434495f49280e1081bf8c4c0df5ce55023fa5d8d99f5"
}

If successful, you will be provided with a funding_txid.

The company behind this node ACINQ has set up a simple demo coffee shop, where you can simulate a coffee purchasing flow by spending Bitcoins via Lightning network, of course, all on Testnet.

Now let’s try and make a payment to this shop. Perform the following steps in order to get an invoice.

  • Head of to this link

You should see an invoice being generated.

Copy this invoice and come back to your terminal. LnD grpc provides a sendpayment method to make payments over lightning network.

$ lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon sendpayment --pay_req=lntb15u1pww57cgpp5tp2nyq4eu886jgmxq88mmxg7vk4l5tfv39lj4urzjyg2saqkxs9qdq4xysyymr0vd4kzcmrd9hx7cqp5f79syh3d6rj6zal59a2jm96dvau9rzrlq6q32jezt58pyna75e5kz0cjeq5wtvh7jn9jhpw5q3kkdlp4szyjf4009jyjx3kttjmlzeqqas94ke

When it asks for confirmation, type yes. If the payment is successful it should give you the payment details.

{
"payment_error": "",
"payment_preimage": "1c3a50a17ce1d00cdcac78a761a6f6c0c6da9ec7b12682b253789aba70e06cfc",
"payment_route": {
"total_time_lock": 1518626,
"total_fees": 1,
"total_amt": 1501,
"hops": [
{
"chan_id": 1666157039780298752,
"chan_capacity": 20000,
"amt_to_forward": 1500,
"fee": 1,
"expiry": 1518482,
"amt_to_forward_msat": 1500000,
"fee_msat": 1001,
"pub_key": "0260d9119979caedc570ada883ff614c6efb93f7f7382e25d73ecbeba0b62df2d7"
},
{
"chan_id": 1632428421087494144,
"chan_capacity": 10000000,
"amt_to_forward": 1500,
"expiry": 1518482,
"amt_to_forward_msat": 1500000,
"pub_key": "03933884aaf1d6b108397e5efe5c86bcf2d8ca8d2f700eda99db9214fc2712b134"
}
],
"total_fees_msat": 1001,
"total_amt_msat": 1501001
}
}

As soon as the payment is complete the merchant is almost instantly notified about it. So you would see a payment completed message on the coffee shop page.

Receiving Payments on the Lightning Network

To receive payment on the Lightning network, we need to create an invoice, which will then be shared with the party sending bitcoins. For generating an invoice we can use the addinvoice method.

$ lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon addinvoice 100

{
"r_hash": "f8365558294b7b955b3116a9d3ba58ddd379a2569873bcbef0faec1b122797d9",
"pay_req": "lntb1u1pww5l6lpp5lqm92kpffdae2ke3z65a8wjcmhfhngjknpeme0hsltkpky38jlvsdqqcqzpgtfrardsqx95227kcfwcuzl4vz8wknaqqtvqujdfjcmd6wds902yp4a5twtflzy0kc4nncxhnkdd0rp760w3nyzlg38lswt9h6asw3ecp9n6zlm",
"add_index": 1
}

Above we are creating an invoice of amount 100. Once done you can share the created invoice/pay_req to the sender.

Let’s try and fulfill this invoice from a wallet available on the web. Hop over to htlc.me and add this invoice and click send.

We can verify if the payment was complete using the LookupInvoice method and the rhash value of the invocie.

eg: lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon lookupinvoice <r_hash>

$ lncli --macaroonpath /root/.lnd/data/chain/bitcoin/testnet/admin.macaroon lookupinvoice f8365558294b7b955b3116a9d3ba58ddd379a2569873bcbef0faec1b122797d9

//output
{
"memo": "",
"receipt": null,
"r_preimage": "L/5fvnp9CGdg3eaV5zr0WjeWOhsrEuzA+imNLm8MeMc=",
"r_hash": "+DZVWClLe5VbMRap07pY3dN5olaYc7y+8PrsGxInl9k=",
"value": "100",
"settled": true,
"creation_date": "1558871903",
"settle_date": "1558872022",
"payment_request": "lntb1u1pww5l6lpp5lqm92kpffdae2ke3z65a8wjcmhfhngjknpeme0hsltkpky38jlvsdqqcqzpgtfrardsqx95227kcfwcuzl4vz8wknaqqtvqujdfjcmd6wds902yp4a5twtflzy0kc4nncxhnkdd0rp760w3nyzlg38lswt9h6asw3ecp9n6zlm",
"description_hash": null,
"expiry": "3600",
"fallback_addr": "",
"cltv_expiry": "40",
"route_hints": [
],
"private": false,
"add_index": "1",
"settle_index": "1",
"amt_paid": "100000",
"amt_paid_sat": "100",
"amt_paid_msat": "100000",
"state": "SETTLED"
}

If the payment is complete, you should see the state set to SETTLED and settled flag set to true as shown above.

Conclusion

In this artilce, we have successfully set up a Lightning Network Node using LnD. We have seen how we can open up channels and send, receive bitcoins over the Lightning Network.

References

Keep Learning…

Originally published at ciphertrick.com on June 1, 2019.

Xổ số miền Bắc