make btc address
This commit is contained in:
@@ -1,14 +1,50 @@
|
||||
use rand::rngs::OsRng;
|
||||
use secp256k1::Secp256k1;
|
||||
use secp256k1::{Secp256k1, key::PublicKey, };
|
||||
use sha2::Sha256;
|
||||
use ripemd160::Ripemd160;
|
||||
use digest::{ Input, FixedOutput, };
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let secp = Secp256k1::new();
|
||||
let mut rng = OsRng::new().expect("OsRng");
|
||||
let (secret_key, public_key) = secp.generate_keypair(&mut rng);
|
||||
|
||||
println!("{}", secret_key);
|
||||
println!("{}", public_key);
|
||||
println!("{}", hex::encode(public_key.serialize_uncompressed().to_vec()));
|
||||
for i in 0..1_000_000 {
|
||||
let (secret_key, public_key) = secp.generate_keypair(&mut rng);
|
||||
|
||||
let s = make_btc_address(&public_key);
|
||||
|
||||
if i > 0 && i % 10000 == 0 {
|
||||
println!("> {}", i);
|
||||
}
|
||||
if s.starts_with("100") {
|
||||
println!("{}", s);
|
||||
println!("{}", secret_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_btc_address(public_key: &PublicKey) -> String {
|
||||
let public_key_bytes = public_key.serialize_uncompressed().to_vec();
|
||||
let riphemd160_sha256_pub_key = calc_ripemd160(&calc_sha256(&public_key_bytes));
|
||||
|
||||
let mut btc_addr = Vec::<u8>::with_capacity(25);
|
||||
btc_addr.push(0x00 as u8);
|
||||
btc_addr.extend_from_slice(&riphemd160_sha256_pub_key);
|
||||
|
||||
let chcksum = &calc_sha256(&calc_sha256(&btc_addr))[0..4];
|
||||
btc_addr.extend_from_slice(chcksum);
|
||||
|
||||
bs58::encode(&btc_addr).into_string()
|
||||
}
|
||||
|
||||
fn calc_sha256(i: &[u8]) -> Vec<u8> {
|
||||
let mut hasher = Sha256::default();
|
||||
hasher.input(&i);
|
||||
hasher.fixed_result().to_vec()
|
||||
}
|
||||
|
||||
fn calc_ripemd160(i: &[u8]) -> Vec<u8> {
|
||||
let mut hasher = Ripemd160::default();
|
||||
hasher.input(&i);
|
||||
hasher.fixed_result().to_vec()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user