import subprocess import requests import os url = "http://127.0.0.1:8332/" ''' Change RPC credentials ''' headers = { 'Authorization': 'Basic dXNlcjpwYXNz', 'Content-Type': 'text/plain' } devnull = open(os.devnull, 'w') def install_libbtc(): print("Installing libbtc...") subprocess.run('sudo apt-get install build-essential libevent-dev; git clone https://github.com/libbtc/libbtc.git; cd libbtc; ./autogen.sh; ./configure; make; sudo make install', shell=True, stdout=devnull, stderr=devnull) print("Installed libbtc") def broadcast_tx(): peers = getpeerinfo() peers_str = ','.join(peers) tx_hex = input("Enter transation hex: ") command = f"bitcoin-send-tx -d -i {peers_str} {tx_hex}" result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = result.stdout.decode() error_output = result.stderr.decode() if error_output: print(f"Error: {error_output}") success_output = '\n'.join([line for line in output.split('\n') if "tx successfully seen on" in line]) print(success_output) def getpeers(): peers = getpeerinfo() print(peers) def ban_peers(): peer_address = input("Enter peer address to ban: ") payload = {"jsonrpc": "1.0", "id":"test", "method": "setban", "params": [peer_address, "add", 7890000]} response = requests.request("POST",url, headers=headers, json=payload) def getpeerinfo(): payload = "{\"jsonrpc\": \"1.0\", \"id\": \"test\", \"method\": \"getpeerinfo\", \"params\": []}" response = requests.request("POST", url, headers=headers, data=payload) peers = [] for peer in response.json()['result']: peers.append(peer['addr']) return peers while True: print("1. Install libbtc") print("2. List peers") print("3. Broadcast tx to peers") print("4. Ban peers") print("0. Quit") choice = input("Enter your choice: ") if choice == "1": install_libbtc() elif choice == "2": getpeers() elif choice == "3": broadcast_tx() elif choice == "4": ban_peers() elif choice == "0": break else: print("Invalid choice, please try again.") devnull.close()