In this tutorial, we will guide you through setting up a Bitcoin Core bitcoind
node in regtest
mode. We'll cover basic operations like creating wallets, generating blocks, checking balances, and sending Bitcoin between two wallets.
To start working with Bitcoin, it's necessary for us to install the Bitcoin Core software, which includes the bitcoind
daemon.
-
Get Bitcoin Core:
- We will visit the official Bitcoin Core website and download the appropriate version for our operating system.
- After downloading, we’ll follow the instructions on the site to verify the downloaded files by checking the signatures to ensure authenticity.
-
Verify Installation:
- After installation, we can run the following command to confirm that
bitcoind
is properly installed:bitcoind --version
- This will output the version of
bitcoind
if it's installed correctly.
- After installation, we can run the following command to confirm that
While bitcoind
can be run on various Bitcoin networks, we will focus on the regtest
network in this tutorial, which is a local blockchain environment ideal for development and testing. Before running bitcoind
, we need to configure the node with a bitcoin.conf
file to set up the regtest
network.
First, we’ll create a directory for Bitcoin data and configuration files if it doesn’t already exist:
mkdir -p ~/.bitcoin
Then, we’ll create a bitcoin.conf
file in ~/.bitcoin/
and add the following lines:
regtest=1 # change this to change Bitcoin Network
server=1
fallbackfee=0.0001 # for regtest only
rpcuser=user
rpcpassword=password
rpcallowip=0.0.0.0/0
txindex=1
regtest=1
: Runs the node in regtest mode.server=1
: Enablesbitcoind
to run as a server and accept RPC (Remote Procedure Call) commands.rpcuser
andrpcpassword
: Set the username and password forbitcoin-cli
RPC access. We can customize these values or leave them as provided.rpcallowip=0.0.0.0/0
: Allows RPC connections from any IP address. We should be cautious when using this in a non-development environment.txindex=1
: Enables a full transaction index for our node, which is useful for querying historical transactions.
After setting up the configuration file, our node will be ready to run in regtest
mode.
NOTE:
Regtest
is a toy network that allows for creating custom blocks and generating coins, making it ideal for easy integration testing of applications.
For actual swap markets, usetestnet4
. Switch totestnet4
by settingtestnet4=1
in the configuration.
Once the bitcoin.conf
file is configured, we can start bitcoind
and perform basic operations using bitcoin-cli
.
We run the following command to start the Bitcoin node:
$ bitcoind
- Note: We don’t need to specify the network explicitly since the
bitcoin.conf
file already defines the network.
To check the status of the node and confirm that it's running, we can use:
$ bitcoin-cli getblockchaininfo
This will output the current state of the blockchain, including the number of blocks and synchronization status, as shown:
{
"chain": "regtest",
"blocks": 0,
"headers": 0,
"bestblockhash": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206",
"difficulty": 4.656542373906925e-10,
"time": 1296688602,
"mediantime": 1296688602,
"verificationprogress": 1,
"initialblockdownload": true,
"chainwork": "0000000000000000000000000000000000000000000000000000000000000002",
"size_on_disk": 293,
"pruned": false,
"warnings": ""
}
We create a wallet called alice
to perform wallet-related operations in the regtest
environment:
$ bitcoin-cli createwallet "alice"
The response will confirm that the wallet alice
has been created:
{
"name": "alice"
}
Similarly, we create another wallet called bob
:
$ bitcoin-cli createwallet "bob"
The response will confirm that the wallet bob
has been created:
{
"name": "bob"
}
We generate a new Bitcoin address for alice
to receive funds:
$ bitcoin-cli -rpcwallet=alice getnewaddress
This returns a new address for alice
:
bcrt1qfvgecwpwtn77f7vv6wfc78zdcxseq4pjpyn9jv
Since we’re using regtest
, we can generate new blocks to the generated address for alice
and receive Bitcoin as block rewards:
$ bitcoin-cli -rpcwallet=alice generatetoaddress 101 <alice_address>
This will return a list of block hashes in hex format, corresponding to the 101 newly generated blocks as shown:
[
"00b968e6627d8f160369a06a3169719487cf246a5d43afa113c42a236305b7d3",
"3ac6820a58627c9e69dcd349d9d152909b018a2afe923bed73dae0c7b7134104",
"0d4b1870a18216450e5da68c7349e7cb26beb144c671524c23798728ca222cd8",
"41c1c331c93e75f2048a285c925e15eca423b0a93f1f7354261adef8f29186c3",
... till 101 block hashes
]
We can now check the balance in alice
's wallet:
$ bitcoin-cli -rpcwallet=alice getbalance
This will show a balance corresponding to the block rewards for the generated blocks:
{
"mine": {
"trusted": 50.00000000,
"untrusted_pending": 0.00000000,
"immature": 0.00000000
}
}
Now, let’s send 1 BTC from alice
to bob
using the sendtoaddress
RPC command.
We generate a new Bitcoin address for bob
:
$ bitcoin-cli -rpcwallet=bob getnewaddress
This returns a new address for bob
:
bcrt1q2nys4aedf448ngt5gpw5gmun6gdjqgy04qj6cq
Next, we send 1 BTC from alice
to bob
using the sendtoaddress
command:
$ bitcoin-cli -rpcwallet=alice sendtoaddress <bob_address> 1
This will create and broadcast a signed transaction, returning the transaction hash:
0b0c6a25e16f987e5a68fe59c301e06615376837b11a3ed678b0f0bd8a69a18a
We generate a block to confirm the transaction to bob
:
$ bitcoin-cli -rpcwallet=bob generatetoaddress 1 <bob_address>
This will return the block hash in hex format:
"43e5d06abcf026e3faec392626545fb4880592682fd61645dc99d51e277bd761"
Finally, we check the balance of both wallets to confirm that the transaction was successful.
$ bitcoin-cli -rpcwallet=bob getbalances
The response should show that bob
has received the 1 BTC:
{
"mine": {
"trusted": 1.00000000,
"untrusted_pending": 0.00000000,
"immature": 0.00000000
}
}
$ bitcoin-cli -rpcwallet=alice getbalances
The response will show that alice
's balance has been reduced by a little more than 1 BTC, where 1 BTC has been sent to bob
, and the remaining amount goes toward mining fees for confirming the transaction:
{
"mine": {
"trusted": 48.99998590,
"untrusted_pending": 0.00000000,
"immature": 0.00000000
}
}
We’ve set up bitcoind
, created two wallets (alice
and bob
), generated blocks, and sent Bitcoin between the wallets. This provides everything we need to start with bitcoind
on regtest
. Now, we’re ready to explore Bitcoin transactions and experiment with coinswap CLI apps.