Post
Topic
Board Bitcoin Discussion
Re: Bitcoin puzzle transaction ~32 BTC prize to who solves it
by
sssergy2705
on 21/07/2024, 20:49:23 UTC
Here is the script.

Nice script i really like it, i may learn something from you Smiley

Here there is a function that I use for my keyhunt bsgsd server:

Code:
def send_and_receive_line(host, port, message):
    # Create a TCP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        # Connect to the server
        sock.connect((host, port))

        # Send the message
        start_time = time.time()
        sock.sendall(message.encode())

        # Receive the reply
        reply = sock.recv(1024).decode()
        end_time = time.time()

        # Calculate the elapsed time
        elapsed_time = end_time - start_time
        sock.close()
        return reply, elapsed_time
    except ConnectionResetError:
        print("Server closed the connection without replying.")
        return None, None
    except ConnectionRefusedError:
        print("Connection refused. Make sure the server is running and the host/port are correct.")
        return None, None
    except AttributeError:
        return None, None

This function only send a message to a IP:port and wait for the reply returning it also the time elapsed (For speed calculation)

I call the previous function with this one that format the publickey with range_A:range_B

Code:
def keyhunt_client(target):
    host = 'localhost'  # Change this to the server's hostname or IP address
    port = 8080       # Change this to the server's port number
    message = "{} {}:{}".format(target["publickey"],target["start"],target["end"])
    reply, elapsed_time = send_and_receive_line(host, port, message)
    if( reply != "404 Not Found"):
        return reply
    else:
        return None

I use some target because i have a list of all puzzles with their respective ranks

With those codes you don't depend of Netcat  or shell commands.



Here's my option.

Code:
    def send_request(self, pub_key, range):
        global key_found
        request_str = f"{pub_key} {range}"
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.connect((self.server_address, 8181))
            s.sendall(request_str.encode())
            response = s.recv(1024)
            response_str = response.decode().strip()
            if response_str not in ("404 Not Found", "400 Bad Request"):
                print(f"\nThe private key for {pub_key} is: {response_str}")
                with open('Found.txt', 'a') as f:
                    f.write(f"Public key: {pub_key}, Private key: {response_str}\n")
                key_found = True
        except ConnectionRefusedError:
            print(f"Connection refused by {self.server_address}")
            self.connected = False
        finally:
            s.close()

    def check_server(self):
        self.connected = check_server(self.server_address)

def check_server(server_address):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(5)
    try:
        s.connect((server_address, 8181))
        return True
    except socket.error:
        return False
    finally:
        s.close()