Coinmonks
Published in

Coinmonks

A Complete Beginners Guide to Setting Up a Bitcoin/Lightning Network on Linux

Running LND With a Bitcoin Core in regtest mode

In this article, I am going to walk through my LND setup in regtest mode.

What is Regtest?

Bitcoin Core’s regression test mode (regtest mode) lets you instantly create a brand-new private blockchain with the same basic rules as testnet — but one major difference: you choose when to create new blocks, so you have complete control over the environment.

Setting Up Bitcoin Core

we’ll be installing the LND implementation of the lightning network. LND is compatible with specific Bitcoin node backends, so you will need to have one of these installed for LND to work.

  • btcd
  • neutrino
  • bitcoind

In this tutorial, we will be using bitcoind also known as Bitcoin core.

Installing Bitcoin Core

# Get the latest version of bitcoin core from bitcoincore.org
$ wget https://bitcoincore.org/bin/bitcoin-core-0.21.1/bitcoin-0.21.1-x86_64-linux-gnu.tar.gz
# Get the corresponding signature file
$ wget https://bitcoincore.org/bin/bitcoin-core-0.21.1/SHA256SUMS.asc
# match the signature
$ sha256sum --ignore-missing --check SHA256SUMS.asc
# get the fingerprint of Bitcoin Core's release key
$ gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 01EA5486DE18A882D4C2684590C8019E36C2E964
# verify the signature in the signature file
$ gpg --verify SHA256SUMS.asc
# Unpack Bitcoin Core
$ tar -zxvf bitcoin-0.21.1-x86_64-linux-gnu.tar.gz
$ cd bitcoin-0.21.1/bin

Configuration

First, run this command ./bitcoind -regtest -daemon It will start the bitcoin core in regtest mode. we’ll need to set up a configuration file It will be making our lives easier every time we run bitcoin core but before that stop, bitcoin core using ./bitcoin-cli -regtest stop. For Linux default configuration file should be inside the location ~/.bitcoin now, we will need to create there a bitcoin.conf file similar to this:

regtest=1
daemon=1
txindex=1
rpcuser=USERNAME_HERE
rpcpassword=PASSWORD_HERE
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333

This tells Bitcoin Core to use regtest mode, run as a daemon, and keep an index of all transactions. It also sets up RPC authentication and ZMQ ports for LND to use to communicate with the node. now run bitcoind bitcoin-core will start.

Now run:

$ ./bitcoin-cli getwalletinfo

you will see warnings this is because the latest version of bitcoind does NOT create a wallet automatically, so you will have to create one via the CLI

$ ./bitcoin-cli -stdin createwallet mywallet false false
[passphrase]
<ctrl-D><ctrl-D>

Now we can mine some blocks.

$ ./bitcoin-cli -generate 10

You must mine 101 blocks before your bitcoind node has matured bitcoin to transfer to another wallet after doing that Now you should see a positive balance:

$ ./bitcoin-cli getwalletinfo

Installing Go

Go is the programming language that LND is written in, so we’ll need to install it first.

To install, run one of the following commands:

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

The final output of the command above should be 013a489ebb3e24ef3d915abe5b94c3286c070dfe0818d5bca8108f1d6e8440d2. If it isn't, then the target REPO HAS BEEN MODIFIED, and you shouldn't install this version of Go. If it matches, then proceeds to install Go:

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

At this point, you should set your $GOPATH environment variable, which represents the path to your workspace. By default, $GOPATH is set to ~/go. You will also need to add $GOPATH/bin to your PATH. This ensures that your shell will be able to detect the binaries you install.

Set go paths, this can be done in .profile (which reads .bashrc) or directly in .bashrc:

$ nano ~/.bashrc

Add to the end:

export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin

Logout and log back in to reread variables, or you can type:

$ source ~/.bashrc

Check variables with:

$ go env

And if you see the output, go is set up correctly.

Installing LND

we will be installing lnd from the source

$ git clone https://github.com/lightningnetwork/lnd
$ cd lnd
$ "make" && "make" install

The command above will install the current master branch of lnd. If you wish to install a tagged release of lnd (as the master branch can at times be unstable), then visit the release page to locate the latest release.

To check that lnd was installed properly, run:

$   make check

Setting Up LND

We’re going to run two LND nodes so let’s make the directory

$ mkdir ~/.lnd
$ mkdir ~/.lnd2

Now let’s make configuration files. Save it as lnd.conf in the .lnd directory.

[Bitcoin]

bitcoin.active=1
bitcoin.regtest=1
bitcoin.node=bitcoind

[Bitcoind]

bitcoind.rpchost=localhost
bitcoind.rpcuser=YOUR_USERNAME
bitcoind.rpcpass=YOUR_PASSWORD
bitcoind.zmqpubrawblock=tcp://127.0.0.1:28332
bitcoind.zmqpubrawtx=tcp://127.0.0.1:28333

Make sure you use the Bitcoin RPC username/password you used in the script earlier.

Now we will make lnd.conf in the .lnd2 directory:

[Application Options]

listen=0.0.0.0:9734
rpclisten=localhost:11009
restlisten=0.0.0.0:8180

[Bitcoin]

bitcoin.active=1
bitcoin.regtest=1
bitcoin.node=bitcoind

[Bitcoind]

bitcoind.rpchost=localhost
bitcoind.rpcuser=YOUR_USERNAME
bitcoind.rpcpass=YOUR_PASSWORD
bitcoind.zmqpubrawblock=tcp://127.0.0.1:28332
bitcoind.zmqpubrawtx=tcp://127.0.0.1:28333

This sets up different ports for network, RPC, and REST connections

Now let’s set up some aliases.

$ nano ~/.bashrc

Add to the end so we don’t have to type those out every time we make a command.

export LND1_DIR="$HOME/.lnd"
export LND2_DIR="$HOME/.lnd2"
alias lnd1="lnd --lnddir=$LND1_DIR";
alias lncli1="lncli -n regtest --lnddir=$LND1_DIR"
alias lnd2="lnd --lnddir=$LND2_DIR";
alias lncli2="lncli -n regtest --lnddir=$LND2_DIR --rpcserver=localhost:11009"

Running LND

Open up a terminal window and run:

$ lnd1

We need to create or unlock the wallet. Open up another terminal window.

$ lncli1 create

Follow the prompt and save the mnemonic.

$ lncli1 getinfo

This should bring up some basic info about the node similar to this:

Next, do the same but with lnd2 / lncli2

Connecting the Nodes

If you run lncli1 listpeers , you’ll find out your node is still all alone. Let’s get them connected. Let’s get the second node’s information.

$ lncli2 getinfo

The output should have the following:

Let’s connect the first node to the second with the following command:

$ lncli1 connect 02ddda5e36c254c9821ca3d8c17e1ecf9fd2f8ff703cf221eb59b4529224b79f92@localhost:9734

Now run lncli1 listpeers again. Your output will be similar to this:

Now you’re ready to create a channel and send money.

Creating a Channel

$ lncli2 newaddress p2wkh{"address": "bcrt1q7nf2d70la3ys27ckf8lgeg67fk4e3dfejuag2y"}

This will produce a Bech32 format address, which is a native segwit address format, with lower fees in fee calculations for opening/closing channels. you can also use lncli newaddress np2wkh to get a different format address that looks more like a traditional multisig address, but won't be able to take advantage of segwit fees benefits.

Now, send one of those bitcoins you mined earlier to the node. We’ll generate some blocks to give it confirmations.

$ ./bitcoin-cli -named sendtoaddress address="bcrt1q7nf2d70la3ys27ckf8lgeg67fk4e3dfejuag2y" amount=0.5 fee_rate=1 replaceable=true$ ./bitcoin-cli generate 6

Now, we can open a channel. Let’s get the pubkey of the first node:

 $ lncli1 getinfo

Start a channel using the value you get there.

$ lncli2 openchannel 02de337106d04ede8b49af4533669bea94f67c605011f49f711402c11912879b02 100000

Now mine some more blocks so the channel has adequate confirmations.

$ ./bitcoin-cli generate 10

Let’s see if it worked:

Creating and Sending an Invoice

To create an invoice type in the terminal following:

$ lncli1 addinvoice -amt 100
{
"r_hash": "d998c9e35d73dec5b0cf7d4c35be8c4ec919aee66b698d912665684d6b69098c",
"payment_request": "lnbcrt1u1psjhly3pp5mxvvnc6aw00vtvx004xrt05vfmy3nthxdd5cmyfxv45y66mfpxxqdqqcqzpgxqyz5vqsp5v399u058lal7u3dzswtntktg93wzdtggr2sqqkzy5t6ffd0n4pgq9qyyssqk204pw6as2599mdrefqx5lrycjax5559xnv2lrp9m4wpqavk6enkmtme09yxdt56552mx6v8eg9gpwxvl9mn0t5dea2gtmajzmukffsph8074z","add_index": "1","payment_addr": "644a5e3e87ff7fee45a2839735d9682c5c26ad081aa0005844a2f494b5f3a850
}

Copy the pay_req value and take note of r_hash for later.

Let’s inspect the invoice on the other node:

$ lncli2 decodepayreq lnbcrt1u1psjhly3pp5mxvvnc6aw00vtvx004xrt05vfmy3nthxdd5cmyfxv45y66mfpxxqdqqcqzpgxqyz5vqsp5v399u058lal7u3dzswtntktg93wzdtggr2sqqkzy5t6ffd0n4pgq9qyyssqk204pw6as2599mdrefqx5lrycjax5559xnv2lrp9m4wpqavk6enkmtme09yxdt56552mx6v8eg9gpwxvl9mn0t5dea2gtmajzmukffsph8074z

Your output will be similar to this:

Now let’s pay!

$ lncli2 payinvoice lnbcrt1u1psjhly3pp5mxvvnc6aw00vtvx004xrt05vfmy3nthxdd5cmyfxv45y66mfpxxqdqqcqzpgxqyz5vqsp5v399u058lal7u3dzswtntktg93wzdtggr2sqqkzy5t6ffd0n4pgq9qyyssqk204pw6as2599mdrefqx5lrycjax5559xnv2lrp9m4wpqavk6enkmtme09yxdt56552mx6v8eg9gpwxvl9mn0t5dea2gtmajzmukffsph8074z

Your output will be similar to this:

Let’s look up the invoice on the receiving node using the payment hash:

If you see settled: true this was the success message

Conclusion

This was a quick walkthrough of my basic setup for a Bitcoin/Lightning at Summer of Bitcoin 2021.

Join Coinmonks Telegram Channel and learn about crypto trading and investing

Also, Read

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store