1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::{
    fs::write,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
};

use log::info;
use solana_sdk::{signature::Keypair, signer::Signer};

pub async fn generate_custom_sol_address(
    prefixes: Vec<String>,
    found_flag: Arc<AtomicBool>,
) {
    info!("Starting search for {:?}", prefixes);
    let mut tries = 0;
    while !found_flag.load(Ordering::Relaxed) {
        let keypair = Keypair::new();
        if let Some(prefix) = prefixes.iter().find(|prefix| {
            keypair
                .pubkey()
                .to_string()
                .to_lowercase()
                .starts_with(prefix.as_str())
        }) {
            found_flag.store(true, Ordering::Relaxed);
            info!(
                "Match found for {}: {}",
                prefix,
                keypair.pubkey().to_string()
            );
            let mut id = [0u8; 64];
            id[..32].copy_from_slice(keypair.secret().as_bytes());
            id[32..].copy_from_slice(&keypair.pubkey().to_bytes());
            info!("id: {:#?}", id);
            write(
                format!("{}.json", prefix),
                serde_json::to_string(&Vec::from(&id)).expect("vec into json"),
            )
            .expect("write private key");
            break;
        }
        tries += 1;
        if tries % 100_000 == 0 {
            info!("tried {} so far", tries);
        }
    }
}

#[cfg(test)]
mod tests {

    // toil alert, remove tmp.json after manual check
    #[test]
    fn test_write_bytes() {
        let contents = [255u8; 64];
        super::write(
            "tmp.json",
            serde_json::to_string(&Vec::from(&contents)).unwrap(),
        )
        .unwrap();
    }
}