Source code for btc_networks

"""
Bitcoin Network Configuration Module

Provides configurations for Bitcoin mainnet and testnet,
including API endpoints for balance and transaction queries.
"""

from typing import Dict, Any, Optional
from dataclasses import dataclass


[docs] @dataclass class BTCNetworkConfig: """Configuration for a Bitcoin network.""" name: str network_name: str is_testnet: bool api_base_url: str api_backup_url: Optional[str] = None explorer_url: Optional[str] = None
[docs] def to_dict(self) -> Dict[str, Any]: """Convert to dict representation. Returns: Dict[str, Any]: The result. """ return { "name": self.name, "network_name": self.network_name, "is_testnet": self.is_testnet, "api_base_url": self.api_base_url, "explorer_url": self.explorer_url }
BTC_NETWORKS: Dict[str, BTCNetworkConfig] = { "bitcoin": BTCNetworkConfig( name="Bitcoin Mainnet", network_name="bitcoin", is_testnet=False, api_base_url="https://blockstream.info/api", api_backup_url="https://blockchain.info", explorer_url="https://blockstream.info"), "btc": BTCNetworkConfig( name="Bitcoin Mainnet", network_name="bitcoin", is_testnet=False, api_base_url="https://blockstream.info/api", api_backup_url="https://blockchain.info", explorer_url="https://blockstream.info"), "mainnet": BTCNetworkConfig( name="Bitcoin Mainnet", network_name="bitcoin", is_testnet=False, api_base_url="https://blockstream.info/api", api_backup_url="https://blockchain.info", explorer_url="https://blockstream.info"), "testnet": BTCNetworkConfig( name="Bitcoin Testnet", network_name="testnet", is_testnet=True, api_base_url="https://blockstream.info/testnet/api", explorer_url="https://blockstream.info/testnet"), "btc_testnet": BTCNetworkConfig( name="Bitcoin Testnet", network_name="testnet", is_testnet=True, api_base_url="https://blockstream.info/testnet/api", explorer_url="https://blockstream.info/testnet"), "signet": BTCNetworkConfig( name="Bitcoin Signet", network_name="signet", is_testnet=True, api_base_url="https://mempool.space/signet/api", explorer_url="https://mempool.space/signet"), }
[docs] def get_btc_network(network_name: str = "bitcoin") -> Optional[BTCNetworkConfig]: """Retrieve the btc network. Args: network_name (str): The network name value. Returns: Optional[BTCNetworkConfig]: The result. """ return BTC_NETWORKS.get(network_name.lower().strip())
[docs] def list_btc_networks(include_testnets: bool = True) -> list: """List btc networks. Args: include_testnets (bool): The include testnets value. Returns: list: List of results. """ seen = set() networks = [] for name, config in BTC_NETWORKS.items(): if config.network_name in seen: continue if not include_testnets and config.is_testnet: continue seen.add(config.network_name) networks.append({ "name": config.name, "network": config.network_name, "is_testnet": config.is_testnet }) return sorted(networks, key=lambda n: (n["is_testnet"], n["name"]))