C 525: Lightning Network (20 pts extra)

What you need:

Purpose

To set up the Lightning Network, allowing off-chain transactions in Bitcoin. This makes transactions faster and cheaper.

We'll set up three nodes, for users Alice, Bob, and Charlie, and send funds between them.

In Lightning, a channel is opened and funded with some Bitcoin. Then off-chain transactions may be made, and they don't create any on-chain transactions until the channel is closed.

Installing lnd

This is the binary that runs the Lightning network.

If you execute the precise commands below, you will be installing the version that was current in Nov, 2021. You may prefer to go to this page and find the latest version:

https://github.com/lightningnetwork/lnd/releases

On your Linux machine, in a Terminal window, execute these commands:

cd /tmp
wget https://github.com/lightningnetwork/lnd/releases/download/v0.14.1-beta/lnd-linux-amd64-v0.14.1-beta.tar.gz

tar -xf lnd-linux-amd64-v0.14.1-beta.tar.gz
cd lnd-linux-amd64-v0.14.1-beta/
sudo cp lncli /usr/local/bin
sudo cp lnd /usr/local/bin

cd /tmp
wget https://github.com/btcsuite/btcd/releases/download/v0.22.0-beta/btcd-linux-amd64-v0.22.0-beta.tar.gz

tar -xf btcd-linux-amd64-v0.22.0-beta.tar.gz
cd btcd-linux-amd64-v0.22.0-beta
sudo cp btcctl /usr/local/bin
sudo cp btcd /usr/local/bin

cd
btcd --txindex --simnet --rpcuser=simuser --rpcpass=simpass
Your server is listening on port 18556, as shown below.

Leave this window open.

Launching Alice's lnd Node

On your Linux machine, in a new Terminal window, execute these commands:
cd
mkdir -p go/dev/alice
cd go/dev/alice

lnd --rpclisten=localhost:10001 --listen=localhost:10011 \
  --restlisten=localhost:8001 --datadir=data --logdir=log \
  --debuglevel=info --bitcoin.simnet --bitcoin.active \
  --bitcoin.node=btcd --btcd.rpcuser=simuser --btcd.rpcpass=simpass 
It says "Waiting for wallet encryption password", as shown below.

Leave this window open.

Launching Bob's lnd Node

On your Linux machine, in a new Terminal window, execute these commands:
cd
mkdir -p go/dev/bob
cd go/dev/bob

lnd --rpclisten=localhost:10002 --listen=localhost:10012 \
  --restlisten=localhost:8002 --datadir=data --logdir=log \
  --debuglevel=info --bitcoin.simnet --bitcoin.active \
  --bitcoin.node=btcd --btcd.rpcuser=simuser --btcd.rpcpass=simpass 
It says "Waiting for wallet encryption password", as shown below.

Leave this window open.

Launching Charlie's lnd Node

On your Linux machine, in a new Terminal window, execute these commands:
cd
mkdir -p go/dev/charlie
cd go/dev/charlie

lnd --rpclisten=localhost:10003 --listen=localhost:10013 \
  --restlisten=localhost:8003 --datadir=data --logdir=log \
  --debuglevel=info --bitcoin.simnet --bitcoin.active \
  --bitcoin.node=btcd --btcd.rpcuser=simuser --btcd.rpcpass=simpass 
It says "Waiting for wallet encryption password", as shown below.

Leave this window open.

Creating Alice's Wallet

On your Linux machine, in a new Terminal window, execute these commands:
cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 create
Enter a password when you are prompted to.

Enter n to create a new seed.

Press Enter to proceed without a cipher seed passphrase.

Alice's wallet is created, as shown below.

Creating Bob's Wallet

On your Linux machine, in the same Terminal window, execute these commands:
cd
cd go/dev/bob

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10002 create
Enter a password when you are prompted to.

Enter n to create a new seed.

Press Enter to proceed without a cipher seed passphrase.

Bob's wallet is created, as shown below.

Creating Charlie's Wallet

On your Linux machine, in the same Terminal window, execute these commands:
cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 create
Enter a password when you are prompted to.

Enter n to create a new seed.

Press Enter to proceed without a cipher seed passphrase.

Charlie's wallet is created, as shown below.

Setting Up Bitcoin Addresses

On your Linux machine, in the same Terminal window, execute these commands:
cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 newaddress np2wkh

cd
cd go/dev/bob

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10002 newaddress np2wkh

cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 newaddress np2wkh
Three addresses are created, as shown below.

Mining Bitcoin for Alice

We need Alice to have some Simnet Bitcoin, so we'll do some mining.

Find the Terminal window running btcd and stop it by pressing Ctrl+C.

Relaunch it by executing this command, replacing the address at the end with Alice's address:

btcd --txindex --simnet --rpcuser=simuser --rpcpass=simpass \
  --miningaddr=rim7Yu1VppqmS3zfyiiQ5zvVw1znwU1mJ1
The simnet starts again, as shown below.

In a new Terminal window, execute this command:

btcctl --simnet --rpcuser=simuser --rpcpass=simpass \
  generate 400
You see a long list of six hexadecimal values, as shown below.

Checking for Segwit

Lightning network requires Segwit, as explained here.

In the same Terminal window, execute this command:

btcctl --simnet --rpcuser=simuser --rpcpass=simpass \
  getblockchaininfo
"segwit" is active, as shown below.

Checking Alice's Balance

In the same Terminal window, execute these commands:
cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 walletbalance
Alice has a large balance, as shown below.

The balance is in Satoshis, and it takes 100 million satoshis to make a bitcoin, so the balance below is 15,050 bitcoins.

Mining Bitcoin for Charlie

Find the Terminal window running btcd and stop it by pressing Ctrl+C.

Relaunch it by executing this command, replacing the address at the end with Charlie's address:

btcd --txindex --simnet --rpcuser=simuser --rpcpass=simpass \
  --miningaddr=rim7Yu1VppqmS3zfyiiQ5zvVw1znwU1mJ1
In an unused Terminal window, execute this command:
btcctl --simnet --rpcuser=simuser --rpcpass=simpass \
  generate 100
You see a long list of hexadecimal values.

Checking Charlie's Balance

In the same Terminal window, execute these commands:
cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 walletbalance
Charlie has a large balance (500 bitcoins), as shown below.

Creating the Peer-to-Peer Network

First, we'll connect Alice to Bob. To do that, we need Bob's public key.

In the same Terminal window, execute these commands:

cd
cd go/dev/bob

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10002 getinfo
Find Bob's public key, highlighted in the image below.

In the same Terminal window, execute these commands, replacing the address in the last command with Bob's public key on your system:

cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 connect \
 03d3560fae73efdffd2ac8bf88366ba38105118c354f263c9f153265e2aff72e4f@localhost:10012
The reply is curly braces with nothing inside them, as shown below.

To verify that Alice and Bob are connected, in the same Terminal window, execute this command:

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 listpeers
Bob's public key appears in the "peers" section, as shown below.

Now we'll connect Bob to Charlie.

In the same Terminal window, execute these commands, replacing the address in the last command with Bob's public key on your system:

cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 connect \
 03d3560fae73efdffd2ac8bf88366ba38105118c354f263c9f153265e2aff72e4f@localhost:10012
The reply is curly braces with nothing inside them, as shown below.

Setting Up Lightning Network

First, Alice needs to commit some Bitcoin to the channel with Bob.

In the same Terminal window, execute these commands, replacing the address in the last command with Bob's public key on your system:

cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 openchannel \
 --node_key=03d3560fae73efdffd2ac8bf88366ba38105118c354f263c9f153265e2aff72e4f \
 --local_amt=1000000
The reply shows a "funding_txid", as shown below.

We need to mine six blocks so the channel is considered valid.

In the same Terminal window, execute this command:

btcctl --simnet --rpcuser=simuser --rpcpass=simpass \
  generate 6
The reply shows six hexadecimal values, as shown below.

To verify that the Alice to Bob channel was created, execute these commands:

cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 listchannels
The reply shows a "remote_pubkey" containing Bob's public key, as shown below.

Sending a Single-Hop Payment

We'll send some Bitcoin from Alice to Bob. First Bob creates an invoice, and then Alice pays it.

Execute these commands:

cd
cd go/dev/bob

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10002 addinvoice --amt=10000
Note the "payment_request" value, as shown below.

To send the payment, execute these commands, inserting the correct "payment_request" value in the last command:

cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 sendpayment \
 --pay_req=lnsb100u1...kd2qc
Confirm the payment by entering yes

The payment succeeded, as shown below.

Setting Up a Bob-to-Charlie Channel

In the same Terminal window, execute these commands, replacing the address in the last command with Bob's public key on your system:
cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 openchannel \
 --node_key=03d3560fae73efdffd2ac8bf88366ba38105118c354f263c9f153265e2aff72e4f \
 --local_amt=800000 --push_amt=200000

btcctl --simnet --rpcuser=simuser --rpcpass=simpass \
  generate 6
The process succeeds, as shown below.

Sending a Multi-Hop Payment

We'll send some Bitcoin from Alice to Charlie via Bob.

Execute these commands:

cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 addinvoice --amt=10000
Note the "payment_request" value, as shown below.

To send the payment, execute these commands, inserting the correct "payment_request" value in the last command:

cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 sendpayment \
 --pay_req=lnsb100u1...03fl
The payment succeeded, as shown below.

To see Charlie's balance, execute these commands:

cd
cd go/dev/charlie

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10003 listchannels
Charlie's "remote_balance" has dropped from 200000 to 190000, as shown below.

Closing Channels

First, we'll find Alice's channel_point.

Execute these commands:

cd
cd go/dev/alice

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 listchannels
Alice's channel_point is two numbers separated by a colon, as shown below. The first number is the funding_txid and the second number is the output_index.

To close the channel, execute this command, replacing the funding_txid and the output_index with the correct values on your system.

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10001 closechannel \
 --funding_txid=c95f50d6652c53ac90755aa008f93e1203ef541ee8eb081eebd39dec638a5872 --output_index=0
The reply shows a "closing_txid", as shown below.

To finish closing the channel, execute this command, to mine a block.

btcctl --simnet --rpcuser=simuser --rpcpass=simpass \
  generate 1
The reply shows a hex value, as shown below.

C 525.1: Check Bob's Balance (20 pts)

Execute these commands:
cd
cd go/dev/bob

lncli --macaroonpath=data/chain/bitcoin/simnet/admin.macaroon \
 --rpcserver=localhost:10002 walletbalance
The flag is Bob's balance, covered by a green rectangle in the image below.

References

Lightning Network Developers Tutorial
Lightning Network Developers Installation
Setting up and Testing LND with the Testnet Lightning Faucet
lnd/docs/INSTALL.md
davecgh/using_btcd_simnet.md

Posted 11-28-21