Simulate your first Lightning transaction on the Bitcoin regtest network Part 2 (MacOS)
Tags: bitcoin, lightning
Published: 2024-03-26 10:56:19

### 📜Objective

The goal of this article is to help you

- confirm that we have the correct bitcoin.conf settings
- spin up a Bitcoin network
- Ensure we are on the regtest network
- Prepare the first lightning node aka `lnd1` for some action
  - Spin up node
  - Create wallet
  - Get some information about the node
  - Generate an Address
  - Fund the `lnd1` wallet with some bitcoin
    - load up our existing local Bitcoin wallet
    - send some bitcoin to generated `lnd1` wallet address (to be sent to `lnd2` later on)
  - Check wallet balance
- Prepare the second lightning node aka `lnd2` for some action

### 📜 Introduction

Welcome to Part 2 of our series, fellow Bitcoin/Lightning developers. I'm sure you're itching for some action...

![morty cop](https://media4.giphy.com/media/v1.Y2lkPTc5MGI3NjExaGs0czQycTVkamp0ZmlxbTF0eDYxa21rcHM0dTRnemh4NWZwenVxMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/YRiRZGlioGVLAwo86B/giphy.gif)

This is the second part of a two-part series and in case you haven't already read the first part go over here [Part 1](https://hashnode.com/post/clsctrvcp00000ale7n0pfur8) so you can fully maximize this second part.

### 📜 Some boring but very important stuff to get us started

![rick thinking](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExb3I0OXlvMGVuaHRseGJmZ25xdjIzczRuazVteWZ3MGs4M3ZpNnFobiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/6SPT4vjEWBPjECMXwr/giphy.gif)

The Lightning Network is a second-layer protocol built on top of the `Bitcoin blockchain` and it enables faster and cheaper transactions by creating off-chain payment `channels` between `users`. These `channels` allow multiple transactions to occur without needing to be recorded on the main `Bitcoin blockchain`. Instead, only the opening and closing `transactions` of the channel are settled on the `blockchain`.

The Regtest (regression test) Bitcoin network mode creates a `local private blockchain` where you can adjust the parameters to what you want. For this demonstration, we would run our lightning network on top of an active Bitcoin network running a local private blockchain (regtest)

> _Quick note: throughout this article, long `output` codeblock data are truncated_

**The easy part:**

### ⚙️ Confirming our Bitcoin environment and spinning up a Bitcoin daemon

![rick inspecting](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYXA4aHE4dnp5MWE5bnlkN2p0Z3FtNnF0Z3J4eTJ4MjJkbjA0aDZyZSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o85xLbmtO1CTYvwDC/giphy.gif)

To confirm that our settings are correct to spin up the required Bitcoin network mode, open up the autogenerated `bitcoin.conf` located at `/Users/user/Library/Application Support/Bitcoin/bitcoin.conf` with a text editor

**Command:**

```zsh
nvim /Users/user/Library/Application Support/Bitcoin/bitcoin.conf
```

**Output:**

```zsh
# Daemon Options
server=1
daemon=1
fallbackfee=0.00072
txindex=1
mintxfee=0.0001
txconfirmtarget=6

# Network Options
regtest=1
#signet=1
#testnet=1

[regtest]
# RPC Options
rpcport=18443
rpcuser=bitcoin
rpcpassword=bitcoin

# ZMQ Options
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
zmqpubhashtx=tcp://127.0.0.1:28332
zmqpubhashblock=tcp://127.0.0.1:28332
```

Now let's follow through the following steps

### 📜 Spin up a Bitcoin network (regtest network)

**Command:**

```zsh
bitcoind
```

**Output:**

```zsh
Bitcoin Core starting
```

### 📜 Ensure we are on the regtest network

**Command:**

```zsh
bitcoin-cli  getblockchaininfo
```

**output:**

```json
{
  "chain": "regtest",  // 👈🏿 just making sure we are on the 'regtest' network
  "blocks": 526,
  "headers": 526,
...
}
```

### 📜 Prepare the first lightning node aka `lnd1` for some action

We'll be preparing our first lightning node now so go ahead and open up two new shell tabs or terminal windows and follow the following steps to spin up both of our previously configured lightning nodes respectively.

**First Tab:**

Let's go ahead and start up our first node

**command:**

```zsh
lnd1
```

**output:**

```zsh
2024-02-14 02:38:18.958 [INF] LTND: Version: 0.17.0-beta commit=fn/v1.0.1-85-ge31d15989, build=production, logging=default, debuglevel=info
2024-02-14 02:38:18.958 [INF] LTND: Active chain: Bitcoin (network=regtest)
2024-02-14 02:38:18.958 [INF] RPCS: Generating TLS certificates...
2024-02-14 02:38:18.960 [INF] RPCS: Done generating TLS certificates
2024-02-14 02:38:18.961 [INF] RPCS: RPC server listening on 127.0.0.1:10009
2024-02-14 02:38:18.963 [INF] RPCS: gRPC proxy started at 127.0.0.1:8080
2024-02-14 02:38:18.963 [INF] LTND: Opening the main database, this might take a few minutes...
2024-02-14 02:38:18.963 [INF] LTND: Opening bbolt database, sync_freelist=false, auto_compact=false
2024-02-14 02:38:19.003 [INF] LTND: Creating local graph and channel state DB instances
2024-02-14 02:38:19.069 [INF] CHDB: Checking for schema update: latest_version=31, db_version=31
2024-02-14 02:38:19.069 [INF] CHDB: Checking for optional update: prune_revocation_log=false, db_version=empty
2024-02-14 02:38:19.069 [INF] LTND: Database(s) now open (time_to_open=106.471482ms)!
2024-02-14 02:38:19.069 [INF] LTND: We're not running within systemd or the service type is not 'notify'
2024-02-14 02:38:19.069 [INF] LTND: Waiting for wallet encryption password. Use `lncli create` to create a wallet, `lncli unlock` to unlock an existing wallet, or `lncli changepassword` to change the password of an existing wallet and unlock it.
```

**Second Tab:**

Now let's create a wallet, run the command below and follow through the wallet creation prompts

**command:**

```zsh
 lncli1 create

```

**output:**

```zsh
Input wallet password:
Confirm password:

Do you have an existing cipher seed mnemonic or extended master root key you want to use?
Enter 'y' to use an existing cipher seed mnemonic, 'x' to use an extended master root key
or 'n' to create a new seed (Enter y/x/n): n

Your cipher seed can optionally be encrypted.
Input your passphrase if you wish to encrypt it (or press enter to proceed without a cipher seed passphrase):

Generating fresh cipher seed...

!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!!

---------------BEGIN LND CIPHER SEED---------------
 1. absorb     2. mammal    3. thank    4. isolate
 5. depart     6. trigger   7. legal    8. unhappy
 9. purchase  10. strong   11. effort  12. kangaroo
13. entry     14. solve    15. flower  16. mule
17. buyer     18. today    19. cancel  20. material
21. pupil     22. napkin   23. employ  24. ill
---------------END LND CIPHER SEED-----------------

!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!!

lnd successfully initialized!
```

Let's go ahead and fetch some basic information about our node. Run the command below

**command:**

```zsh
lncli1 getinfo
```

**output:**

```json
{
    "version":  "0.17.0-beta commit=fn/v1.0.1-85-ge31d15989",
    "commit_hash":  "e31d1598932a814c2d44b774e5110746295df711",
    "identity_pubkey":  "02e0810d836946eebaeb9186c45ebb0c2d17294890ec67f488c9127de4d51dd3cd",
    "alias":  "02e0810d836946eebaeb",
    "color":  "#3399ff",
    "num_pending_channels":  0,
    "num_active_channels":  0,
    "num_inactive_channels":  0,
    "num_peers":  0, //👈🏿 take note of this number (our node is a bit lonely for now)
    "block_height":  547,
    "block_hash":  "10f9fb091fd2b83736ffde6621100d6db40b09fad3676c7930dce1821c31066a",
    "best_header_timestamp":  "1707215728",
    "synced_to_chain":  false,
    "synced_to_graph":  false,
    "testnet":  false,
    "chains":  [
        {
            "chain":  "bitcoin",
            "network":  "regtest"
        }
    ],
....
}
```

Time to create an address that would receive the bitcoin sent to the lightning node wallet

**command:**

```zsh
lncli1 newaddress np2wkh
```

**output:**

```json
{
  "address": "2N5qZ8TNWbrkaYxmsLYjEd8g6U7CrjDfHky"
}
```

Let's load up our Bitcoin wallet from the previous tutorial, send some juicy `100` Bitcoin from it to our lightning address above and then generate some blocks to give it some confirmations

![dancing](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExbWt6anVjdXY4amFsa2M0emx1cjQ0dGNscDdpMnViZ2FnMnAwb2tuaSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/JTa7LMZnXcrF0oyMxo/giphy.gif)

_"my oh my!!, what a man could do with some real `100` btc just sitting in a wallet 🤤. Alight alright that's enough buddy, don't be weird let's get back to the article."_

Run the commands below in sequence

**command 1:**

```zsh
bitcoin-cli loadwallet my_first_regtest_wallet
```

**output:**

```json
{
  "name": "my_first_regtest_wallet"
}
```

**command 2:**

```zsh
bitcoin-cli sendtoaddress 2N5qZ8TNWbrkaYxmsLYjEd8g6U7CrjDfHky 100
```

**output:**

```zsh
3d36c8fc08f3ffd82bfa4e2aa33a63bf6c254bb46ffa1309fb9c7eb99adb3848
```

**command 3:**

```zsh
bitcoin-cli generatetoaddress 10 "mhF5zQHNn7wzaaQTpzRmtsNgPgFF94fxeY"
```

**output:**

```json
[
  "2fb9db66a292f9f37260be1490833d7cb4b1d5442478be67fd3d7c48281271c5",
  "7f284d403862117a21461da7cfdc3437a870fd608d0afe5e672c5c378ea2bc73",
  "4b4e867a02e49145f71b662fa4fea2a30eeff0d9a689953b655222ef2e336bd4",
  ...
]
```

Let's go ahead and check our `lnd1` wallet balance

**command:**

```zsh
lncli1 walletbalance
```

**output:**

```json
{
  "total_balance": "10000000000", //👈🏿 sweet, our wallet was credited successfully
  "confirmed_balance": "10000000000",
  "unconfirmed_balance": "0",
  "locked_balance": "0",
  "reserved_balance_anchor_chan": "0",
  "account_balance": {
    "default": {
      "confirmed_balance": "10000000000",
      "unconfirmed_balance": "0"
    }
  }
}
```

### 📜 Prepare the second lightning node aka `lnd2` for some action

To get the second lnd node configured and running, repeat all the steps we followed above but this time around, funding the `lnd2` wallet with some bitcoin is optional as this node would be on the receiving end of our transaction.

### 📜 Conclusion

We'll conclude here. Thank you for staying with us until the end of the second part. In the final part, we'll walk through connecting peers, opening payment channels, and other processes necessary for a valid lightning transaction.

### 📜 Useful links

- [part 1 of the series](https://hashnode.com/post/clsctrvcp00000ale7n0pfur8)