mirror of
https://github.com/LORDBABUINO/stealth.git
synced 2026-07-21 14:18:10 -07:00
move fast break things
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
"""
|
||||
bitcoin_rpc.py — Thin wrapper around bitcoin-cli for Python tests.
|
||||
Connection settings are read from config.ini in the same directory.
|
||||
bitcoin_rpc.py — Facade that selects regtest or testnet backend
|
||||
based on config.ini [bitcoin] network setting.
|
||||
|
||||
Exposes:
|
||||
NETWORK – "regtest" or "testnet"
|
||||
IS_REGTEST – True when running on regtest
|
||||
cli(...) – RPC call routed to the correct backend
|
||||
mine_blocks, get_tx, get_utxos, get_balance, send_raw, ...
|
||||
"""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
import os
|
||||
import configparser
|
||||
@@ -17,57 +22,31 @@ def _load_config():
|
||||
cfg.read(config_path)
|
||||
return cfg["bitcoin"] if "bitcoin" in cfg else {}
|
||||
|
||||
def _build_base_args(section):
|
||||
cli_bin = section.get("cli", "bitcoin-cli")
|
||||
network = section.get("network", "regtest").strip().lower()
|
||||
|
||||
args = [cli_bin]
|
||||
|
||||
network_flags = {
|
||||
"regtest": "-regtest",
|
||||
"testnet": "-testnet",
|
||||
"signet": "-signet",
|
||||
}
|
||||
if network in network_flags:
|
||||
args.append(network_flags[network])
|
||||
|
||||
for key, flag in [("rpchost", "-rpcconnect"), ("rpcport", "-rpcport"),
|
||||
("rpcuser", "-rpcuser"), ("rpcpassword", "-rpcpassword")]:
|
||||
value = section.get(key, "").strip()
|
||||
if value:
|
||||
args.append(f"{flag}={value}")
|
||||
|
||||
return args
|
||||
|
||||
_cfg = _load_config()
|
||||
_BASE_ARGS = _build_base_args(_cfg)
|
||||
NETWORK = _cfg.get("network", "regtest").strip().lower()
|
||||
IS_REGTEST = NETWORK == "regtest"
|
||||
|
||||
# ── Select the right cli backend ─────────────────────────────────────────────
|
||||
|
||||
if IS_REGTEST:
|
||||
from lib.clis import cli_regtest as _cli_backend
|
||||
else:
|
||||
from lib.clis import cli_testnet as _cli_backend
|
||||
|
||||
# Keep these for any scripts that might reference them directly
|
||||
CLI = _cfg.get("cli", "bitcoin-cli")
|
||||
SIGNET_ARGS = _BASE_ARGS
|
||||
|
||||
def cli(*args, wallet=None):
|
||||
"""Call bitcoin-cli [network] [wallet] <args> and return parsed JSON or string."""
|
||||
cmd = list(_BASE_ARGS)
|
||||
if wallet:
|
||||
cmd.append(f"-rpcwallet={wallet}")
|
||||
cmd.extend(str(a) for a in args)
|
||||
"""Route RPC calls to the active backend."""
|
||||
return _cli_backend(*args, wallet=wallet)
|
||||
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"bitcoin-cli error: {result.stderr.strip()}\n cmd: {' '.join(cmd)}")
|
||||
|
||||
output = result.stdout.strip()
|
||||
if not output:
|
||||
return None
|
||||
try:
|
||||
return json.loads(output)
|
||||
except json.JSONDecodeError:
|
||||
return output
|
||||
|
||||
# ── Block mining (regtest only) ──────────────────────────────────────────────
|
||||
|
||||
def mine_blocks(n=1):
|
||||
"""Mine n blocks on regtest using generatetoaddress."""
|
||||
"""Mine n blocks. Only works on regtest; no-op on testnet."""
|
||||
if not IS_REGTEST:
|
||||
# On testnet we cannot mine — just wait for propagation
|
||||
time.sleep(2)
|
||||
return get_block_count()
|
||||
miner_addr = cli("getnewaddress", "", "bech32", wallet="miner")
|
||||
cli("generatetoaddress", n, miner_addr)
|
||||
return int(cli("getblockcount"))
|
||||
@@ -160,10 +139,12 @@ def get_new_address(wallet_name, addr_type="bech32"):
|
||||
|
||||
def send_to_address(wallet_name, address, amount):
|
||||
"""Send BTC to an address."""
|
||||
print(f"Sending {amount:.8f} BTC from {wallet_name} to {address}...")
|
||||
return cli("sendtoaddress", address, f"{amount:.8f}", wallet=wallet_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Network mode: {NETWORK} (IS_REGTEST={IS_REGTEST})")
|
||||
print("Testing RPC connection...")
|
||||
info = cli("getblockchaininfo")
|
||||
print(f" Chain: {info['chain']}")
|
||||
|
||||
Reference in New Issue
Block a user