Exercise: View Your Node Wallet Data

Now that we are familiar with the functions of nodes and wallets, let's see how they work together by using our Bitcoin regtest node to perform some wallet functions. In this exercise, we'll generate new addresses, have a look at our wallet seed, construct a transaction, sign and broadcast a transaction, and more.

Let's pick right up from where we left off in exercise 6.3. Let's check in on our regtest node.

$ bitcoin-cli -getinfo


If you don't get the expected response you'll want to check that bitcoind is still running

$ bitcoind


And now we're running across the same problem that we had in exercise 5.5, we can't look at our wallet data until we have wallet data! So let's go about creating some transactions.

Note: These commands are slightly different than the ones we used in exercise 5.5 as we are using a newer version of bitcoind here.

$ bitcoin-cli createwallet "testwallet"
$ bitcoin-cli getnewaddress
$ bitcoin-cli getblockcount
$ bitcoin-cli -regtest generatetoaddress 10 [your address]
$ bitcoin-cli -generate 101
$ bitcoin-cli -getinfo
$ bitcoin-cli getblockcount


A Bitcoin wallet has four primary functions:

  1. Generate and store keys
  2. Construct transactions
  3. Sign transactions with keys
  4. Track transactions

Let's use our new regtest node to walk through each of these functions. While our node/wallet is capable of creating complex transactions, what we are working with right now are simple Pay to Public Key Hash or P2PKH transactions. With P2PKH, addresses are derived from public keys. When we generate a new address what bitcoind does first is generate a new private key pair from the wallet seed as Bitcoin core now supports HD wallet seeds. To be sure that our new wallet is an HD wallet we can run the below command and look to see that there is a value for the "hdseedid" output.

$ bitcoin-cli getwalletinfo


And let's create a new address.

$ bitcoin-cli getnewaddress


Now let's ask bitcoind to check on our new address to be sure that it's valid.

$ bitcoin-cli validateaddress [our new address]


So now we've used our node to manage our keys, let's check to see if we can use it to construct and validate a transaction.

Remember Bitcoin works off of inputs and outputs. To create a transaction, what we need to do first is tell Bitcoin which inputs we will be consuming. Let's take a look at the transactions that we have received so that we can select a transaction output to use.

$ bitcoin-cli listtransactions


From the output of the listtransactions command, find the transaction ID, listed as "txid" from one of those transactions. Let's take a closer look. Run the below command and note the "amount" and "vout".

$ bitcoin-cli gettransaction [transaction ID]


Before we construct this transaction, we'll have to take a quick detour and discuss transaction fees. We'll cover this more in-depth later. However, what we need to know here is that a transaction fee needs to be included. Transaction fees are given to the miner that mines the block and these fees serve many purposes including the prevention of spam transactions. These fees, however, are not clearly labeled in a transaction. Instead, they are calculated from the difference between inputs and outputs… so do be careful to do your math well. And, back to our transaction!

Either select or generate two addresses to use in our transaction. Then we'll construct the transaction according to the below templates. Note that we are using two addresses here as we'll need a change transaction. All of the outputs that we have to use at the moment will be 50BTC, unless we want to send more than that, we'll need one address to send our transaction to, and one address to send the change to. The text in red below needs to be replaced with the correct values.

$ bitcoin-cli createrawtransaction '[{ "txid": "[transaction ID]", "vout": [vout number]}]' '{"[the address we are sending funds to]": 0.111, "[the change address]": 49.888}'


If all goes well, the above command will output a hex string which is the transaction details encoded in hexadecimal.

So now we have a transaction, but before we broadcast it we'll need to sign it.

$ bitcoin-cli signrawtransactionwithwallet [our hex encoded transaction]


If all goes well we should see an output similar to the below.

    "hex": "[hex encoded signed transaction]",
    "complete": true


We now have a complete, signed, hex encoded transaction, but it won't do anything for us until we broadcast it to the network and it is mined and incorporated into a block.

$ bitcoin-cli sendrawtransaction [hex encoded signed transaction]


You may get an error that looks like this…

bad-txns-premature-spend-of-coinbase, tried to spend coinbase at depth 10


While we won't cover the rationale here, Bitcoin will not allow the output of a coinbase transaction to be spent until it has a block depth of 100. This means that 100 blocks need to be mined after the transaction was created. If you see this error, mine a few more blocks and try again.

$ bitcoin-cli -generate 100


Once enough blocks have been mined and we can use our coinbase output, the transaction will be accepted and the command will output a transaction ID.

So, we've done a lot here! We've used our node to generate and store keys, construct transactions, and sign transactions. But we still need to track those transactions. Let's use our node to check in on our transaction.

$ bitcoin-cli gettransaction [our transaction ID]


Notice that the transaction has 0 confirmations(it has not yet been included in a block), and the two different outputs, vout 0 and vout 1. Now let's mine a few more blocks and then check our transaction again to see that it now has more confirmations.

$ bitcoin-cli -generate 10
$ bitcoin-cli gettransaction [our transaction ID]

Source: Saylor Academy
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Monday, January 3, 2022, 12:39 PM