use bitcoin::key::Secp256k1;
use bitcoin::{Address, CompressedPublicKey, Network, PrivateKey};
use rand::RngCore;

// generate btc address:
// https://coinb.in/#newSegWit
fn main() {
    let secp = Secp256k1::new();
    let mut sk_bytes = [0u8; 32];
    rand::rng().fill_bytes(&mut sk_bytes);
    let secret_key = PrivateKey::from_slice(&sk_bytes, Network::Bitcoin).unwrap();

    // let secret_key = PrivateKey::from_wif("L2HajtedHgDT16o3Er8L8tyRrPNbXkeBK69vhGi7K9eK4Yg3dJuj").unwrap();

    let public_key = secret_key.public_key(&secp);
    let compress_public_key = CompressedPublicKey::try_from(public_key).unwrap();

    // 构造 P2WPKH 地址 (bc1 开头)
    let address = Address::p2wpkh(&compress_public_key, Network::Bitcoin);
    let address = address.to_string();

    println!("Private Key (WIF): {}", secret_key.to_wif());
    println!("Public Key: {}", public_key);
    println!(
        "Public Key: {}",
        hex::encode(&public_key.inner.serialize_uncompressed())
    );
    println!("SegWit Address (bc1): {}", address);
}
