Categories
Stay Ahead with Expert Blockchain Insights on CryptoIQ Blog

Bitcoin & Blockchain Programming. Basic Commands

In this article, we will explore the fundamental commands used in Bitcoin and blockchain programming, particularly when interacting with the bitcoind daemon via bitcoin-cli. This serves as a primer for developers aiming to work programmatically with Bitcoin nodes.

Introduction to bitcoin-cli

The bitcoin-cli tool is the command-line interface for interacting with the Bitcoin Core daemon (bitcoind). It allows developers and administrators to query blockchain data, manage wallets, and issue transactions. A great starting point is the help command, which lists all available commands and their descriptions.

1. Using the Help Command

The help command is essential for navigating the capabilities of bitcoin-cli. To see a list of all available commands:

bitcoin-cli help

If you wish to save this list to a file for future reference, use redirection:

bitcoin-cli help >> bitcoin_commands.txt

To get detailed information about a specific command, append the command name:

bitcoin-cli help <command>

Example: To understand what the getblockcount command does:

bitcoin-cli help getblockcount

This will display the manual entry for getblockcount, explaining its usage and output.

2. Retrieving Blockchain Data

a. Block Count

The getblockcount command returns the total number of blocks in the blockchain. For example:

bitcoin-cli getblockcount

As of December 2024, the Bitcoin mainnet contains over 830,000 blocks. This number increases roughly every 10 minutes as miners add new blocks to the chain.

b. Network Difficulty

The getdifficulty command provides the current network difficulty, which represents how hard it is to mine a new block:

bitcoin-cli getdifficulty

On the Bitcoin mainnet, difficulty adjusts approximately every two weeks (or every 2,016 blocks) based on network activity.

c. Blockchain Information

To gain a comprehensive overview of the blockchain, use the getblockchaininfo command:

bitcoin-cli getblockchaininfo

This command provides critical details such as:

  • Current block height
  • Chain type (e.g., mainnet, testnet, or regtest)
  • Softfork statuses
  • Network consensus rules

For a detailed explanation of its output, refer to the manual entry:

bitcoin-cli help getblockchaininfo

3. Wallet Information and Node Status

a. Node and Wallet Overview

The getinfo command (deprecated in newer versions of Bitcoin Core) has been replaced by specialized commands such as getnetworkinfo, getwalletinfo, and getblockchaininfo. To get an overview of network-related details:

bitcoin-cli getnetworkinfo

For wallet-specific data, use:

bitcoin-cli getwalletinfo

b. Examples of Useful Commands

  • Check wallet balance:
      bitcoin-cli getbalance
    
  • List wallet transactions:
      bitcoin-cli listtransactions
    
  • Display recent mempool transactions:
      bitcoin-cli getmempoolinfo
    

4. Preparing for RPC Integration

Interacting programmatically with bitcoind involves using Remote Procedure Calls (RPCs). Before diving into coding, ensure your Bitcoin node is configured for RPC access by editing the bitcoin.conf file (usually located in the Bitcoin Core data directory).

Sample Configuration:

rpcuser=yourusername
rpcpassword=yourpassword
rpcallowip=127.0.0.1
rpcport=8332

Restart the bitcoind daemon after making changes:

bitcoind -daemon

With the RPC interface active, you can start issuing commands via libraries like Python’s bitcoinrpc or similar SDKs.

Commands Covered

Here’s a summary of the commands we’ve explored:

  • help: List available commands or get detailed information on a specific command.
  • getblockcount: Retrieve the current number of blocks in the chain.
  • getdifficulty: Check the current mining difficulty.
  • getblockchaininfo: View detailed blockchain statistics.
  • getinfo (deprecated): Previously used for node and wallet overviews.

5. Advanced bitcoin-cli Usage

a. Handling Transactions

Bitcoin Core allows you to create, sign, and broadcast transactions directly through bitcoin-cli.

  • Create a raw transaction:To create a raw transaction, use:
      bitcoin-cli createrawtransaction '[{"txid":"<transaction_id>","vout":<vout_index>}]' '{"<destination_address>":<amount>}'
    

    Replace <transaction_id>, <vout_index>, <destination_address>, and <amount> with the appropriate values.

  • Sign a raw transaction:After creating a raw transaction, sign it using:
      bitcoin-cli signrawtransactionwithwallet <raw_transaction_hex>
    
  • Broadcast the transaction:Finally, broadcast the signed transaction to the network:
      bitcoin-cli sendrawtransaction <signed_transaction_hex>
    

b. Managing Wallets

Bitcoin Core supports multiple wallets. You can load, unload, and create new wallets:

  • Create a new wallet:
      bitcoin-cli createwallet "new_wallet_name"
    
  • Load an existing wallet:
      bitcoin-cli loadwallet "wallet_name"
    
  • Unload a wallet:
      bitcoin-cli unloadwallet "wallet_name"
    

c. Network and Peer Management

Manage your node’s connections and monitor network status:

  • View connected peers:
      bitcoin-cli getpeerinfo
    
  • Add a specific peer:
      bitcoin-cli addnode <node_ip> add
    
  • Disconnect a peer:
      bitcoin-cli disconnectnode <node_ip>
    

Next Steps

Now that we’ve covered basic and advanced bitcoin-cli commands, the next tutorial will delve deeper into RPC programming. We’ll explore how to build custom scripts and applications that interact with the Bitcoin network seamlessly. Topics will include:

  • Setting up a development environment
  • Using Python to interact with Bitcoin Core
  • Automating blockchain data analysis
  • Building transaction workflows

Stay tuned for these exciting developments and take your Bitcoin programming skills to the next level!