async def probe(svr, proto_code, use_tor): conn = StratumClient() try: await conn.connect(svr, proto_code, use_tor=(svr.is_onion or use_tor), short_term=True) except: failed.add(str(svr)) return None peers, _ = conn.subscribe('server.peers.subscribe') peers = await peers print("%s gave %d peers" % (svr, len(peers))) connected.add(str(svr)) # track them all. more = ks.add_peer_response(peers) if more: print("found %d more servers from %s: %s" % (len(more), svr, ', '.join(more))) conn.close() return str(svr)
async def find_utxos(server: ServerInfo, master_key: BIP32, max_gap: int, max_account: int, address: Optional[str], fee_rate: Optional[int], should_broadcast: bool): """ Connect to an electrum server and find all the UTXOs spendable by a master key. """ print('⏳ Connecting to electrum server, this might take a while') client = StratumClient() await client.connect(server, disable_cert_verify=True) print('🌍 Connected to electrum server successfully') utxos = await scanner.scan_master_key(client, master_key, max_gap, max_account) if len(utxos) == 0: print('😔 Didn\'t find any unspent outputs') client.close() return balance = sum([utxo.amount_in_sat for utxo in utxos]) print(f'💸 Total spendable balance found: {balance} sats') if master_key.master_privkey is None: print('✍️ Re-run with a private key to create a sweep transaction') client.close() return if address is None: print('ℹ️ Re-run with `--address` to create a sweep transaction') client.close() return if fee_rate is None: fee_rate_in_btc_per_kb = await client.RPC('blockchain.estimatefee', 1) if fee_rate_in_btc_per_kb == -1: print( '🔁 Couldn\'t fetch fee rates, try again with manual fee rates using `--fee-rate`' ) client.close() return fee_rate = int(fee_rate_in_btc_per_kb * 10**8 / 1024) print(f'🚌 Fetched next-block fee rate of {fee_rate} sat/vbyte') tx_without_fee = transactions.Transaction(master_key, utxos, address, balance) fee = tx_without_fee.virtual_size() * fee_rate tx = transactions.Transaction(master_key, utxos, address, balance - fee) bin_tx = tx.to_bytes() print('👇 This transaction sweeps all funds to the address provided') print() print(bin_tx.hex()) print() if not should_broadcast: print( '📋 Copy this transaction and broadcast it manually to the network, or re-run with `--broadcast`' ) client.close() return try: print('📣 Broadcasting transaction to the network') txid = await client.RPC('blockchain.transaction.broadcast', bin_tx.hex()) print(f'✅ Transaction {txid} successfully broadcasted') except connectrum.exc.ElectrumErrorResponse as err: print(f'⛔️ Transaction broadcasting failed: {err}') client.close()