DEV Community

Cover image for Simulate your first Lightning transaction on the Bitcoin regtest network Part 2 (MacOS)
Abba Adamu
Abba Adamu

Posted on

Simulate your first Lightning transaction on the Bitcoin regtest network Part 2 (MacOS)

๐Ÿ“œ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

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 so you can fully maximize this second part.

๐Ÿ“œ Some boring but very important stuff to get us started

rick thinking

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

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:

nvim /Users/user/Library/Application Support/Bitcoin/bitcoin.conf
Enter fullscreen mode Exit fullscreen mode

Output:

# 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
Enter fullscreen mode Exit fullscreen mode

Now let's follow through the following steps

๐Ÿ“œ Spin up a Bitcoin network (regtest network)

Command:

bitcoind
Enter fullscreen mode Exit fullscreen mode

Output:

Bitcoin Core starting
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“œ Ensure we are on the regtest network

Command:

bitcoin-cli  getblockchaininfo
Enter fullscreen mode Exit fullscreen mode

output:

{
  "chain": "regtest",  // ๐Ÿ‘ˆ๐Ÿฟ just making sure we are on the 'regtest' network
  "blocks": 526,
  "headers": 526,
...
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“œ 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:

lnd1
Enter fullscreen mode Exit fullscreen mode

output:

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.
Enter fullscreen mode Exit fullscreen mode

Second Tab:

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

command:

 lncli1 create

Enter fullscreen mode Exit fullscreen mode

output:

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!
Enter fullscreen mode Exit fullscreen mode

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

command:

lncli1 getinfo
Enter fullscreen mode Exit fullscreen mode

output:

{
    "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"
        }
    ],
....
}
Enter fullscreen mode Exit fullscreen mode

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

command:

lncli1 newaddress np2wkh
Enter fullscreen mode Exit fullscreen mode

output:

{
  "address": "2N5qZ8TNWbrkaYxmsLYjEd8g6U7CrjDfHky"
}
Enter fullscreen mode Exit fullscreen mode

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

"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:

bitcoin-cli loadwallet my_first_regtest_wallet
Enter fullscreen mode Exit fullscreen mode

output:

{
  "name": "my_first_regtest_wallet"
}
Enter fullscreen mode Exit fullscreen mode

command 2:

bitcoin-cli sendtoaddress 2N5qZ8TNWbrkaYxmsLYjEd8g6U7CrjDfHky 100
Enter fullscreen mode Exit fullscreen mode

output:

3d36c8fc08f3ffd82bfa4e2aa33a63bf6c254bb46ffa1309fb9c7eb99adb3848
Enter fullscreen mode Exit fullscreen mode

command 3:

bitcoin-cli generatetoaddress 10 "mhF5zQHNn7wzaaQTpzRmtsNgPgFF94fxeY"
Enter fullscreen mode Exit fullscreen mode

output:

[
  "2fb9db66a292f9f37260be1490833d7cb4b1d5442478be67fd3d7c48281271c5",
  "7f284d403862117a21461da7cfdc3437a870fd608d0afe5e672c5c378ea2bc73",
  "4b4e867a02e49145f71b662fa4fea2a30eeff0d9a689953b655222ef2e336bd4",
  ...
]
Enter fullscreen mode Exit fullscreen mode

Let's go ahead and check our lnd1 wallet balance

command:

lncli1 walletbalance
Enter fullscreen mode Exit fullscreen mode

output:

{
  "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"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“œ 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

Top comments (0)