Post
Topic
Board Bitcoin Discussion
Re: Bitcoin puzzle transaction ~32 BTC prize to who solves it
by
rosengold
on 25/01/2024, 08:47:39 UTC
share with us sir


I can share puzzle search app in Rust. I'm sick of code in Python. Grin

main.rs
Code:
extern crate bitcoin;
extern crate rand;
extern crate reqwest;
extern crate secp256k1;
extern crate num_cpus;
extern crate thousands;
extern crate threadpool;
extern crate chrono;

use bitcoin::network::Network;
use bitcoin::address::Address;
use bitcoin::key::PrivateKey;
use rand::Rng;
use std::env;
use std::fs::File;
use std::io::Write;
use std::sync::{Arc, Mutex};
use threadpool::ThreadPool;
use chrono::Local;

const TARGET: &str = "1QCbW9HWnwQWiQqVo5exhAnmfqKRrCRsvW";

fn main() {
    // Print the current time when the script starts
    let current_time = Local::now();
    println!("[+] Puzzle search\n[+] Script started at: {}", current_time);

    let args: Vec<String> = env::args().collect();
    let num_threads = if args.len() < 2 {
        num_cpus::get() as u32
    } else {
        args[1].parse().expect("Failed to parse number of threads")
    };

    let begin: u128 = 16383;
    let end: u128 = 32767;

    println!(
        "[+] concurrency:{}\n[+] from:{} to:{}\n[+] target:{}",
        num_threads,
        begin,
        end,
        TARGET
    );

    let found_flag = Arc::new(Mutex::new(false));
    let pool = ThreadPool::new(num_threads.try_into().unwrap());

    for _ in 0..num_threads {
        let pool = pool.clone();
        let found_flag = found_flag.clone();
        pool.execute(move || {
            let mut rng = rand::thread_rng();
            random_lookfor(rng.gen_range(begin..end), end, found_flag);
        });
    }

    pool.join();
}

fn random_lookfor(begin: u128, end: u128, found_flag: Arc<Mutex<bool>>) {
    let secp = bitcoin::secp256k1::Secp256k1::new();

    loop {
        let value: u128 = rand::thread_rng().gen_range(begin..end);
        let private_key_hex = format!("{:0>64x}", value);
        let private_key_bytes = hex::decode(&private_key_hex).expect("Failed to decode private key hex");

        let private_key: PrivateKey = PrivateKey {
            compressed: true,
            network: Network::Bitcoin,
            inner: bitcoin::secp256k1::SecretKey::from_slice(&private_key_bytes).unwrap(),
        };

        let public_key = private_key.public_key(&secp);
        let address = Address::p2pkh(&public_key, Network::Bitcoin).to_string();
        print!(
            "\r[+] WIF: {}",
            private_key
        );

        // Check if a match has been found by another thread
        let mut found_flag = found_flag.lock().unwrap();
        if *found_flag {
            break;
        }

        if address == TARGET {
            let current_time = Local::now();
            let line_of_dashes = "-".repeat(80);
            println!(
                "\n[+] {}\n[+] KEY FOUND! {}\n[+] decimal: {} \n[+] private key: {} \n[+] public key: {} \n[+] address: {}\n[+] {}",
                line_of_dashes,
                current_time,
                value,
                private_key,
                public_key,
                address,
                line_of_dashes          
            );

            // Set the flag to true to signal other threads to exit
            *found_flag = true;

            if let Ok(mut file) = File::create("KEYFOUNDKEYFOUND.txt") {
                let line_of_dashes = "-".repeat(130);
                writeln!(
                    &mut file,
                "\n{}\nKEY FOUND! {}\ndecimal: {} \nprivate key: {} \npublic key: {} \naddress: {}\n{}",
                line_of_dashes,
                current_time,
                value,
                private_key,
                public_key,
                address,
                line_of_dashes      
                )
                .expect("Failed to write to file");
            } else {
                eprintln!("Error: Failed to create or write to KEYFOUNDKEYFOUND.txt");
            }
            break;
        }
    }
}

Cargo.toml
Code:
[package]
name = "puzzle"
version = "0.1.0"
edition = "2021"

[dependencies]
threadpool = "1.8.0"
bitcoin_hashes = "0.13.0"                
bitcoin = "0.31.1"
hex = "0.4.3"
rand = "0.8.5"
reqwest = "0.11.23"
secp256k1 = "0.28.1"
num_cpus = "1.16.0"
thousands = "0.2.0"
chrono = "0.4"

  • --------------------------------------------------------------------------------
  • KEY FOUND!
  • decimal: 26867
  • private key: KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFY5iMZbuRxj
  • public key: 02fea58ffcf49566f6e9e9350cf5bca2861312f422966e8db16094beb14dc3df2c
  • address: 1QCbW9HWnwQWiQqVo5exhAnmfqKRrCRsvW
  • --------------------------------------------------------------------------------

Install Rust:
Code:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Configure Rust Environment:
Code:
source $HOME/.cargo/env

Code:
export PATH="$HOME/.cargo/bin:$PATH"

Create a Puzzle Rust Project:
Code:
cargo new puzzle

copy/paste above main.rs & Cargo.toml

Code:
cd  puzzle

Build program
Code:
cargo build --release

Start
Code:
cargo run

or directly as a bin application
Code:
./target/release/puzzle

p.s.
You can remove :      
Code:
       print!(
            "\r[+] WIF: {}",
            private_key
        );

for more speed....

I have similar levels of performance in Rust and C. But much less bugs in Rust  Wink

amazing, thanks.

I will study your code and implement some own logic of mine.