Add p2wpkh address creation example

This commit is contained in:
yancy 2024-11-19 16:05:38 -06:00
parent c47a41a076
commit d9b48cc2ce
2 changed files with 28 additions and 0 deletions

View File

@ -71,6 +71,10 @@ required-features = ["std", "bitcoinconsensus"]
name = "ecdsa-psbt-simple" name = "ecdsa-psbt-simple"
required-features = ["rand-std"] required-features = ["rand-std"]
[[example]]
name = "create-p2wpkh-address"
required-features = ["rand-std"]
[[example]] [[example]]
name = "sign-tx-segwit-v0" name = "sign-tx-segwit-v0"
required-features = ["rand-std"] required-features = ["rand-std"]

View File

@ -0,0 +1,24 @@
use bitcoin::secp256k1::{rand, Secp256k1};
use bitcoin::{Address, CompressedPublicKey, Network, PrivateKey};
/// Generate a P2WPKH (pay-to-witness-public-key-hash) address and print it
/// along with the associated private key needed to transact.
fn main() {
// Create new secp256k1 instance.
let secp = Secp256k1::new();
// Generate secp256k1 public and private key pair.
let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
// Create a Bitcoin private key to be used on the Bitcoin mainnet.
let private_key = PrivateKey::new(secret_key, Network::Bitcoin);
// Create a compressed Bitcoin public key from the secp256k1 public key.
let public_key = CompressedPublicKey(public_key);
// Create a Bitcoin P2WPKH address.
let address = Address::p2wpkh(public_key, Network::Bitcoin);
println!("Private Key: {}", private_key);
println!("Address: {}", address);
}