Post
Topic
Board Bitcoin Discussion
Re: Bitcoin puzzle transaction ~32 BTC prize to who solves it
by
GR Sasa
on 24/07/2024, 20:39:25 UTC
I have managed to get a "Bot" but it's not a bot i have still to manually give the private key because i didn't know how to extract public key once its in the mempool plus i didn't know how to run Keyhunt or kangaroo after getting public key, sooooo i guess i'll have to be awake lol, i'm so bad


I would be happy for any improvement or any opinions if the following script may work?

I would like to ask for a small script that extract public key once it's seen on the mempool. I couldn't do it myself sadly. Could anyone help?

Thank you!


Code:
import requests
import time
from bitcoinlib.transactions import Transaction
from bitcoinlib.keys import Key

# Configuration
address = ''  # The address to monitor
new_address = ''  # The address to receive the new transaction
private_key_wif = ''  # Private key for the address that created the original transaction
initial_fee = 20000  # Initial fee in satoshis (0.0001 BTC)
fee_increment = 35000  # Increment for additional fees in satoshis
check_interval = 30  # Interval for checking new transactions in seconds
broadcast_interval = 60  # Interval for re-broadcasting transactions in seconds

def check_new_transactions(address):
    url = f'https://mempool.space/api/address/{address}/txs/chain'
    try:
        response = requests.get(url)
        response.raise_for_status()  # Raise an exception for HTTP errors
        data = response.json()
       
        # Print raw data to understand the response structure
        print(f"API response: {data}")
       
        if isinstance(data, list):
            return data
        else:
            print("Unexpected response format.")
            return []
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while checking transactions: {e}")
        return []

def get_raw_transaction(txid):
    url = f'https://mempool.space/api/tx/{txid}/hex'
    try:
        response = requests.get(url)
        response.raise_for_status()
        return response.text
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while getting raw transaction: {e}")
        return None

def create_replacement_transaction(original_tx_hex, private_key_wif, fee, new_address):
    try:
        key = Key.from_wif(private_key_wif)  # Use from_wif() to import the key
    except Exception as e:
        print(f"Error importing private key: {e}")
        return None

    try:
        # Load the original transaction
        tx = Transaction.import_raw(original_tx_hex)
       
        total_input_value = sum(input.value for input in tx.inputs)
        total_output_value = sum(output.value for output in tx.outputs)
        original_fee = total_input_value - total_output_value

        # Calculate the new fee in satoshis
        new_total_fee = original_fee + fee
        remaining_value = total_input_value - new_total_fee

        # Convert values to integers (satoshis)
        remaining_value = int(remaining_value)
        if remaining_value <= 0:
            print("Not enough funds to cover the new fee.")
            return None

        # Create a new transaction
        new_tx = Transaction()

        # Add all inputs from the original transaction
        for input in tx.inputs:
            new_tx.add_input(input.prev_txid, input.output_n, input.value)

        # Add a single output to the new address with the remaining value after fee
        new_tx.add_output(remaining_value, new_address)

        # Sign the new transaction
        new_tx.sign(key)

        return new_tx
    except Exception as e:
        print(f"Error creating replacement transaction: {e}")
        return None

def broadcast_transaction(tx_hex):
    url = 'https://mempool.space/api/tx'
    headers = {'Content-Type': 'application/json'}
    data = {'tx': tx_hex}
    try:
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()
        return response.text
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while broadcasting transaction: {e}")
        return None

# Monitor the address for new transactions
known_transactions = set()
current_fee = initial_fee

while True:
    print("Checking for new transactions...")
    transactions = check_new_transactions(address)
   
    if transactions:
        print("Address used in transactions. Retrieving public key...")
        # Skip public key retrieval if not supported

    for tx in transactions:
        txid = tx['txid']  # Adjust if necessary based on the actual response structure
        if txid not in known_transactions:
            print(f"New transaction found: {txid}")
            raw_tx_hex = get_raw_transaction(txid)
            if raw_tx_hex:
                print("Creating and broadcasting replacement transactions with increasing fees...")
               
                start_time = time.time()
                while True:
                    replacement_tx = create_replacement_transaction(raw_tx_hex, private_key_wif, current_fee, new_address)
                    if replacement_tx:
                        replacement_tx_hex = replacement_tx.as_hex()
                        broadcast_response = broadcast_transaction(replacement_tx_hex)
                        print(f"Broadcast response for fee {current_fee}: {broadcast_response}")
                       
                        # Increment the fee for the next transaction
                        current_fee += fee_increment
                       
                        # Check if 60 seconds have passed since the start of broadcasting
                        if time.time() - start_time >= broadcast_interval:
                            break  # Exit the loop after 60 seconds
                    else:
                        print("Failed to create replacement transaction.")
                       
                known_transactions.add(txid)
   
    print("Waiting before repeating the process...")
    time.sleep(check_interval)  # Check every 30 seconds