Tech, business, and useful tricks.
Vheüel Insight Network

Checking Bitcoin Balances from WIF Private Keys Using Python

5 min read

Learn how to read Bitcoin WIF private keys with Python, derive Bitcoin addresses, and check confirmed on-chain balances using the Bit library.

Vheüel

Vheüel

Author

Checking Bitcoin Balances from WIF Private Keys Using Python

This guide explains how to check Bitcoin balances from private keys stored in WIF (Wallet Import Format) using Python. The workflow is simple: read WIF keys from a local text file, derive the corresponding Bitcoin address, query blockchain data, and print the confirmed BTC balance.

Important: only use private keys that you own or are authorized to audit. A WIF private key gives full control over its funds. Never paste real private keys into websites, shared machines, public repositories, logs, screenshots, or third-party tools.


1. Requirements

Before running the script, make sure you have:

  • Python 3.8 or newer
  • pip
  • Internet access
  • A local text file containing your own WIF private keys

2. Install the Dependency

This tutorial uses the bit Python library. It handles WIF parsing, address derivation, and blockchain balance lookup.

pip install bit

3. Create the Input File

Create a file named keys.txt in the same directory as your Python script.

Use one WIF private key per line. Empty lines and lines starting with # will be ignored.

# Example only. Replace these with your own WIF keys. Lxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

4. Python Script

The script below reads WIF private keys from keys.txt, converts each key into a Bitcoin private key object, derives the default public address, and checks its confirmed BTC balance.

from bit import Key def load_wif_keys(file_path): with open(file_path, "r", encoding="utf-8") as file: return [ line.strip() for line in file if line.strip() and not line.strip().startswith("#") ] def check_balances(file_path="keys.txt"): wif_keys = load_wif_keys(file_path) if not wif_keys: print("No WIF keys found.") return print("\nChecking Bitcoin balances:\n") for wif in wif_keys: try: key = Key(wif) address = key.address balance = key.get_balance("btc") print(f"{address} - {balance} BTC") except Exception as error: print(f"{wif[:6]}... failed: {error}") if __name__ == "__main__": check_balances()

5. How the Script Works

The execution flow is straightforward:

  1. Open keys.txt
  2. Ignore empty lines and comment lines
  3. Parse each WIF private key using Key(wif)
  4. Derive the default Bitcoin address using key.address
  5. Fetch the confirmed balance using key.get_balance("btc")
  6. Print the address and balance in the terminal

6. Mainnet and Testnet

Key(wif) is used for Bitcoin mainnet. If you need to work with Bitcoin testnet keys, use PrivateKeyTestnet instead.

from bit import PrivateKeyTestnet key = PrivateKeyTestnet("your_testnet_wif_here") print(key.address) print(key.get_balance("btc"))

Do not mix mainnet and testnet keys. A mainnet WIF should be checked with the mainnet key class, while a testnet WIF should be checked with the testnet key class.


7. Address Type Notes

The default key.address value is the standard address derived by the library from the private key. In most cases, this is a legacy P2PKH address.

If the WIF key uses a compressed public key, the same private key may also be used by wallets with a SegWit-compatible address format. This matters because funds can exist on a different address format derived from the same private key.

from bit import Key key = Key("your_wif_here") print("Default address:", key.address) if key.is_compressed(): print("Nested SegWit address:", key.segwit_address)

If the balance looks wrong or returns zero, verify which address format the original wallet used.


8. Output Example

When the script runs successfully, the output will look like this:

Checking Bitcoin balances: 1ExampleBitcoinAddressxxxxxxxxxxxx - 0.00000000 BTC 1AnotherExampleAddressxxxxxxxxxxxx - 0.01500000 BTC

The balance is returned as a BTC value.


9. Network Behavior

The balance lookup requires an internet connection. The library checks blockchain data and calculates the current confirmed balance from available UTXOs.

This means the script depends on external blockchain data providers used by the library. If an upstream service is unavailable, rate-limited, or slow, balance checks may fail even when the WIF key itself is valid.


10. Common Errors

Invalid WIF: the input is not a valid Wallet Import Format private key.

Wrong network: a testnet WIF is being checked with the mainnet class, or a mainnet WIF is being checked with the testnet class.

Network failure: the blockchain lookup failed because of connectivity issues, rate limits, or service availability.

Unexpected zero balance: the funds may belong to another address format derived from the same private key.


11. Better Version with Address Preview

The version below prints both the default address and the nested SegWit address when available. This helps you verify which address format should be checked.

from bit import Key def load_wif_keys(file_path): with open(file_path, "r", encoding="utf-8") as file: return [ line.strip() for line in file if line.strip() and not line.strip().startswith("#") ] def check_balances(file_path="keys.txt"): wif_keys = load_wif_keys(file_path) if not wif_keys: print("No WIF keys found.") return for wif in wif_keys: try: key = Key(wif) print("\nKey:", f"{wif[:6]}...") print("Default address:", key.address) if key.is_compressed(): print("Nested SegWit address:", key.segwit_address) balance = key.get_balance("btc") print("Confirmed balance:", balance, "BTC") except Exception as error: print(f"{wif[:6]}... failed: {error}") if __name__ == "__main__": check_balances()

For production-grade tools, consider using a self-hosted Bitcoin node or a dedicated blockchain indexer instead of depending entirely on public API providers.


12. Possible Improvements

This basic script can be extended with:

  • CSV or JSON export
  • Separate logs for invalid keys and network failures
  • Testnet mode
  • Address type selection
  • Retry logic for failed API requests
  • Rate limiting for large key lists
  • Integration with a self-hosted Bitcoin node

13. Summary

Using Python and the Bit library, you can build a simple tool for checking confirmed Bitcoin balances from WIF private keys. The most important details are to handle private keys securely, use the correct network class, understand which address format is being checked, and avoid treating this script as a full wallet recovery system unless all relevant address formats are verified.

Join Bybit