Post
Topic
Board Bitcoin Discussion
Re: Bitcoin puzzle transaction ~32 BTC prize to who solves it
by
gaguka
on 05/10/2024, 13:07:57 UTC
I never trust other people's code. Only the programs I write myself are the most stable.(linux) Grin Grin Grin Grin Grin Grin Grin Grin Grin Grin Grin Grin
 Reward address: 1JWM1RsRFuct6y9JCwhx3TibvhDJVB2Ldn

import hashlib
import binascii
import base58
import threading
import ecdsa

# 匹配的比特币地址列表
target_addresses = {
    "1MVDYgVaSN6iKKEsbzRUAYFrYJadLYZvvZ",
    "19vkiEajfhuZ8bs8Zu2jgmC6oqZbWqhxhG"
}

# 初始私钥列表(8个私钥)
initial_private_keys = [
    "000000000000000000000000000000000000000000000024351b116fe05a4ef1",
    "00000000000000000000000000000000000000000000001b0f680b1092d881e6",
    "00000000000000000000000000000000000000000000001c478360fcc5f7e4c3",
    "00000000000000000000000000000000000000000000001de29e256292c39398",
    "000000000000000000000000000000000000000000000024351b116fe05a426d",
    "0000000000000000000000000000000000000000000000288707c8ae1738f142",
    "000000000000000000000000000000000000000000000020cbae528b05b2a017",
    "00000000000000000000000000000000000000000000001ea2011cc8a10e4eec"
]

# 检查私钥是否符合规则(不允许三个连续相同的字符)
def is_valid_key_suffix(private_key_hex):
    for i in range(len(private_key_hex) - 2):
        if private_key_hex == private_key_hex[i + 1] == private_key_hex[i + 2]:
            return False
    return True

# 生成比特币地址(P2PKH Legacy)
def pubkey_to_address(public_key):
    sha256_hash = hashlib.sha256(public_key).digest()
    ripemd160_hash = hashlib.new('ripemd160', sha256_hash).digest()
    extended_ripemd160 = b'\x00' + ripemd160_hash  # 比特币地址前缀 0x00
    checksum = hashlib.sha256(hashlib.sha256(extended_ripemd160).digest()).digest()[:4]
    binary_address = extended_ripemd160 + checksum
    return base58.b58encode(binary_address).decode()

# 生成公钥(压缩)
def private_key_to_public_key(private_key_hex):
    private_key_bytes = binascii.unhexlify(private_key_hex)
    sk = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1)
    vk = sk.verifying_key
    public_key = b'\x02' + vk.to_string()[:32] if int.from_bytes(vk.to_string()[32:], 'big') % 2 == 0 else b'\x03' + vk.to_string()[:32]
    return public_key

# 线程函数:从初始私钥开始递减,并生成对应的比特币地址
def worker_thread(start_private_key, thread_id):
    private_key_int = int(start_private_key, 16)
    total_generated = 0
    matched_count = 0
   
    while True:
        private_key_hex = format(private_key_int, '064x')

        # 检查私钥是否符合规则
        if is_valid_key_suffix(private_key_hex):
            public_key = private_key_to_public_key(private_key_hex)
            address = pubkey_to_address(public_key)

            # 检查生成的地址是否在目标地址列表中
            if address in target_addresses:
                with open("add.txt", "a") as f:
                    f.write(f"Private Key: {private_key_hex}, Address: {address}\n")
                matched_count += 1

        private_key_int -= 1
        total_generated += 1
       
        # 打印每个线程生成的私钥总数
        if total_generated % 100000 == 0:
            print(f"Thread {thread_id}: Total keys generated: {total_generated}, Matched: {matched_count}", end='\r')

# 启动8个线程,每个线程从不同的私钥开始递减
def start_threads():
    threads = []
    for idx, initial_key in enumerate(initial_private_keys):
        t = threading.Thread(target=worker_thread, args=(initial_key, idx + 1))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

# 启动程序
if __name__ == "__main__":
    start_threads()