Skip to content

gyanranjanpanda/btc-explorer-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸͺ™ Bitcoin Explorer CLI

A beginner-friendly command-line tool to explore Bitcoin blockchain data directly from your terminal. Built with Python 3, this project uses public APIs to fetch and display block, transaction, and address information without requiring a Bitcoin node.

Built as preparation for Summer of Bitcoin πŸš€


✨ Features

πŸ“¦ Block Explorer

  • Fetch block information by height
  • View block hash, timestamp, transaction count, size, and miner reward
  • Display additional details like Merkle root, difficulty, and nonce

πŸ’Έ Transaction Viewer

  • Look up any transaction by its TXID
  • See inputs, outputs, and total BTC sent
  • View transaction fees and confirmation status
  • Display fee rate in satoshis per byte

🏦 Address Inspector

  • Check balance for any Bitcoin address
  • View total received and total sent amounts
  • See the last 5 transactions for the address
  • Support for Legacy (P2PKH, P2SH) and SegWit (Bech32) addresses

πŸ› οΈ Tech Stack

  • Python 3.8+ - Core programming language
  • requests - HTTP library for API calls
  • argparse - Command-line argument parsing
  • Blockstream API - Public Bitcoin blockchain data source

πŸ“ Project Structure

btc-explorer-cli/
β”œβ”€β”€ explorer.py       # Main entry point and CLI argument handling
β”œβ”€β”€ api.py           # API client for Blockstream Bitcoin API
β”œβ”€β”€ formatter.py     # Output formatting and display logic
β”œβ”€β”€ utils.py         # Validation and helper functions
β”œβ”€β”€ requirements.txt # Python dependencies
└── README.md        # This file

Clean, modular architecture makes it easy to understand and extend! 🎯


πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • pip (Python package manager)
  • Internet connection (for API calls)

Setup Steps

  1. Clone or download this repository

    cd btc-explorer-cli
  2. Install dependencies

    pip install -r requirements.txt
  3. Make the script executable (optional, for Unix/Linux/Mac)

    chmod +x explorer.py

That's it! You're ready to explore the Bitcoin blockchain. πŸŽ‰


πŸ“– Usage

Basic Command Structure

python explorer.py <command> <argument>

Available Commands

1️⃣ Block Information

Fetch details about a specific block by its height:

python explorer.py block <height>

Example:

python explorer.py block 800000

Output includes:

  • Block hash
  • Block height
  • Timestamp (human-readable)
  • Number of transactions
  • Block size and weight
  • Miner reward
  • Merkle root, difficulty, and nonce

2️⃣ Transaction Information

Look up a transaction by its TXID:

python explorer.py tx <txid>

Example:

python explorer.py tx 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a

Output includes:

  • Transaction ID
  • Confirmation status
  • Size and weight
  • Total BTC sent
  • Transaction fee and fee rate
  • List of inputs (with amounts and addresses)
  • List of outputs (with amounts and addresses)

3️⃣ Address Information

Check the balance and transaction history of a Bitcoin address:

python explorer.py address <btc_address>

Example:

python explorer.py address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

Output includes:

  • Current balance (confirmed and unconfirmed)
  • Total received
  • Total sent
  • Transaction count
  • Last 5 transactions with amounts and status

πŸ’‘ Example Usage Session

# Check the genesis block (block 0)
python explorer.py block 0

# Look up Satoshi's first transaction
python explorer.py tx 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b

# Check Satoshi's address
python explorer.py address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

πŸŽ“ Learning Objectives

This project demonstrates:

βœ… API Integration - Making HTTP requests and handling responses
βœ… Error Handling - Graceful handling of network errors and invalid inputs
βœ… CLI Development - Using argparse for command-line interfaces
βœ… Code Organization - Modular design with separation of concerns
βœ… Data Formatting - Converting raw API data into readable output
βœ… Bitcoin Basics - Understanding blocks, transactions, and addresses

Perfect for students preparing for Summer of Bitcoin or anyone learning Bitcoin development! 🎯


πŸ–ΌοΈ Screenshots

Block Information

======================================================================
πŸ“¦ BLOCK INFORMATION
======================================================================

πŸ”— Block Hash:       00000000000000000001c9e6d0f0e6f8b3c8d2e5f7a9b1c3d5e7f9a1b3c5d7e9
πŸ“Š Height:           800,000
⏰ Timestamp:        2023-07-14 18:32:15
πŸ“ Transactions:     3,251
πŸ’Ύ Size:             1.45 MB
βš–οΈ  Weight:           3,992,547 WU
πŸ’° Miner Reward:     6.28491055 BTC

======================================================================

Transaction Information

======================================================================
πŸ’Έ TRANSACTION INFORMATION
======================================================================

πŸ†” Transaction ID:   3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a
πŸ“Š Status:           Confirmed βœ“
πŸ’Ύ Size:             225 bytes
βš–οΈ  Weight:           900 WU

πŸ’° FINANCIAL SUMMARY:
   Total Sent:       0.05000000 BTC
   Fee Paid:         0.00002250 BTC
   Fee Rate:         10.00 sat/byte

======================================================================

Address Information

======================================================================
🏦 ADDRESS INFORMATION
======================================================================

πŸ”‘ Address:          1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

πŸ’° BALANCE:
   Confirmed:        72.48391234 BTC

πŸ“Š STATISTICS:
   Total Received:   72.48391234 BTC
   Total Sent:       0.00000000 BTC
   Transaction Count: 3,891

======================================================================

πŸ”§ Error Handling

The tool handles various error scenarios gracefully:

  • ❌ Invalid block height - "Block not found at height X"
  • ❌ Invalid TXID - "Invalid transaction ID format"
  • ❌ Invalid address - "Invalid Bitcoin address format"
  • ❌ Network errors - "Connection error. Please check your internet connection"
  • ❌ Rate limiting - Automatic delays between requests to respect API limits

🌐 API Information

This project uses the Blockstream API, a free public API for Bitcoin blockchain data:


πŸš€ Future Enhancements

Ideas for extending this project:

  • Add mempool statistics viewer
  • Support for testnet exploration
  • Export data to JSON/CSV
  • Interactive mode with command history
  • Colored terminal output
  • Transaction graph visualization
  • UTXO set analysis
  • Lightning Network integration

🀝 Contributing

This is a learning project built for Summer of Bitcoin preparation. Feel free to:

  • Fork and experiment
  • Add new features
  • Improve error handling
  • Enhance the output formatting
  • Add tests

πŸ“š Resources


πŸ“ License

This project is open source and available for educational purposes.


πŸ‘¨β€πŸ’» Author

Built with ❀️ as preparation for Summer of Bitcoin

Happy Exploring! πŸͺ™βœ¨


πŸ™ Acknowledgments

  • Blockstream for providing the free public API
  • Summer of Bitcoin program for inspiration
  • Bitcoin community for excellent documentation

About

A beginner-to-intermediate level Python CLI project for exploring Bitcoin data.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages