pyrxd.network — ElectrumX + BTC sources¶
pyrxd.network — network layer for Radiant / Bitcoin SPV.
Re-exports the public surface of the sub-modules so callers can do:
from pyrxd.network import ElectrumXClient, ChainTracker, …
- class pyrxd.network.BitcoinCoreRpcSource[source]¶
Bases:
BtcDataSourceBtcDataSource backed by a Bitcoin Core JSON-RPC endpoint.
Credentials are stored as
SecretBytesand never logged.- Parameters:
url – RPC endpoint URL, e.g.
http://localhost:8332/.user – RPC username.
password – RPC password (stored securely as SecretBytes).
- async get_block_hash(height)[source]¶
Return the 32-byte block hash at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_block_header_hex(height)[source]¶
Return the raw 80-byte block header at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_header_chain(start_height, count)[source]¶
Return count consecutive 80-byte headers starting at start_height.
- Parameters:
start_height (BlockHeight)
count (int)
- Return type:
- async get_merkle_proof(txid, height)[source]¶
Return
(branch_hashes_hex, leaf_position)for txid at height.
- async get_raw_tx(txid, min_confirmations=6)[source]¶
Return raw transaction bytes, enforcing min_confirmations.
- class pyrxd.network.BlockstreamSource[source]¶
Bases:
BtcDataSourceBtcDataSource backed by the blockstream.info HTTP API.
- __init__(base_url='https://blockstream.info/api')[source]¶
- Parameters:
base_url (str)
- Return type:
None
- async get_block_hash(height)[source]¶
Return the 32-byte block hash at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_block_header_hex(height)[source]¶
Return the raw 80-byte block header at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_header_chain(start_height, count)[source]¶
Return count consecutive 80-byte headers starting at start_height.
- Parameters:
start_height (BlockHeight)
count (int)
- Return type:
- async get_merkle_proof(txid, height)[source]¶
Return
(branch_hashes_hex, leaf_position)for txid at height.
- async get_raw_tx(txid, min_confirmations=6)[source]¶
Return raw transaction bytes, enforcing min_confirmations.
- class pyrxd.network.BtcDataSource[source]¶
Bases:
ABCAbstract interface for blockchain data providers.
- abstractmethod async close()[source]¶
Close any underlying connections held by this source.
- Return type:
None
- abstractmethod async get_block_hash(height)[source]¶
Return the 32-byte block hash at height.
- Parameters:
height (BlockHeight)
- Return type:
- abstractmethod async get_block_header_hex(height)[source]¶
Return the raw 80-byte block header at height.
- Parameters:
height (BlockHeight)
- Return type:
- abstractmethod async get_header_chain(start_height, count)[source]¶
Return count consecutive 80-byte headers starting at start_height.
- Parameters:
start_height (BlockHeight)
count (int)
- Return type:
- abstractmethod async get_merkle_proof(txid, height)[source]¶
Return
(branch_hashes_hex, leaf_position)for txid at height.
- abstractmethod async get_raw_tx(txid, min_confirmations=6)[source]¶
Return raw transaction bytes, enforcing min_confirmations.
- abstractmethod async get_tip_height()[source]¶
Return the current chain tip block height.
- Return type:
- class pyrxd.network.ChainTracker[source]¶
Bases:
objectVerifies Merkle inclusion proofs against confirmed block headers.
- Bitcoin block header layout (80 bytes, all fields little-endian):
version : 4 bytes [0:4]
prev_hash : 32 bytes [4:36]
merkle_root : 32 bytes [36:68] ← compared here
time : 4 bytes [68:72]
bits : 4 bytes [72:76]
nonce : 4 bytes [76:80]
The
merkle_rootin the header is stored in little-endian byte order, matching the convention used byMerklePath.compute_root().- __init__(btc_source)[source]¶
- Parameters:
btc_source (BtcDataSource)
- Return type:
None
- async is_valid_root(merkle_root, height)[source]¶
Fetch the block header at height and check its Merkle root.
- Parameters:
merkle_root (Hex32) – The 32-byte Merkle root to verify (as
Hex32).height (BlockHeight) – Block height of the header to check against.
- Returns:
Trueif the header’s Merkle root matches merkle_root.- Return type:
- class pyrxd.network.ElectrumXClient[source]¶
Bases:
objectAsync ElectrumX JSON-RPC client.
- Parameters:
urls – One or more ElectrumX server URLs. The client uses the first URL; on disconnect it attempts one reconnect, then raises
NetworkError.allow_insecure – If
False(default)ws://URLs raiseNetworkErrorimmediately. Set toTrueonly for local testing.timeout – Per-request timeout in seconds (default 30).
- async close()[source]¶
Close the underlying WebSocket connection.
Cancels the reader task, fails any in-flight requests with NetworkError, and closes the socket.
- Return type:
None
- async get_balance(script_hash)[source]¶
Return the confirmed and unconfirmed balance for script_hash.
The
script_hashissha256(locking_script)with bytes reversed (ElectrumX little-endian convention). AcceptsHex32, rawbytes(length 32), or a hexstr(length 64).
- async get_block_header(height)[source]¶
Return the raw 80-byte block header at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_history(script_hash)[source]¶
Return the transaction history for script_hash.
Returns a list of
{"tx_hash": str, "height": int}dicts. Unconfirmed transactions haveheightof 0 or negative.
- async get_tip_height()[source]¶
Return the current chain tip block height.
Uses blockchain.block.header with cp_height=0, which is a proper one-shot RPC call. The subscription method blockchain.headers.subscribe is avoided here because it installs a server-side push subscription; subsequent push notifications would arrive interleaved with other _call() responses on a long-lived connection.
Response format: {“height”: N, “hex”: “…”}
- Return type:
- async get_transaction_merkle(txid, height)[source]¶
Fetch the Merkle proof for txid at block height.
- Returns:
A parsed Merkle path object.
- Return type:
MerklePath
- Parameters:
txid (Txid)
height (BlockHeight)
- async get_transaction_verbose(txid)[source]¶
Fetch the verbose JSON-decoded form of a transaction.
Calls
blockchain.transaction.getwithverbose=Trueand returns the dict the server provides — includingconfirmations,blockhash,blocktime. Used by confirmation polling.Distinct from
get_transaction()(which returns raw bytes for cryptographic operations like merkle-proof checks). Callers polling for “is this tx confirmed yet?” want THIS one.
- class pyrxd.network.MempoolSpaceSource[source]¶
Bases:
BtcDataSourceBtcDataSource backed by the mempool.space HTTP API.
- Parameters:
base_url – Base URL of the API (default
https://mempool.space/api).
- __init__(base_url='https://mempool.space/api')[source]¶
- Parameters:
base_url (str)
- Return type:
None
- async get_block_hash(height)[source]¶
Return the 32-byte block hash at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_block_header_hex(height)[source]¶
Return the raw 80-byte block header at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_header_chain(start_height, count)[source]¶
Return count consecutive 80-byte headers starting at start_height.
- Parameters:
start_height (BlockHeight)
count (int)
- Return type:
- async get_merkle_proof(txid, height)[source]¶
Return
(branch_hashes_hex, leaf_position)for txid at height.
- async get_raw_tx(txid, min_confirmations=6)[source]¶
Return raw transaction bytes, enforcing min_confirmations.
- class pyrxd.network.MultiSourceBtcDataSource[source]¶
Bases:
BtcDataSourceA quorum-based composite data source.
For read operations, all sources are queried concurrently and the result is returned only if at least quorum sources agree. For broadcast-style operations, sources are tried in order until one succeeds.
- Parameters:
sources – Two or more
BtcDataSourceinstances.quorum – Minimum number of agreeing sources required (default 2).
- __init__(sources, quorum=2)[source]¶
- Parameters:
sources (list[BtcDataSource])
quorum (int)
- Return type:
None
- async get_block_hash(height)[source]¶
Return the 32-byte block hash at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_block_header_hex(height)[source]¶
Return the raw 80-byte block header at height.
- Parameters:
height (BlockHeight)
- Return type:
- async get_header_chain(start_height, count)[source]¶
Return count consecutive 80-byte headers starting at start_height.
- Parameters:
start_height (BlockHeight)
count (int)
- Return type:
- async get_merkle_proof(txid, height)[source]¶
Return
(branch_hashes_hex, leaf_position)for txid at height.
- async get_raw_tx(txid, min_confirmations=6)[source]¶
Return raw transaction bytes, enforcing min_confirmations.