Remove private key from create-p2wpkh-address example
What changed, and why it matters
This commit removes a line from a code example that printed a randomly-generated private key. The example now only prints the Bitcoin address. It is a documentation/example cleanup, not a fix for a software vulnerability in the library itself.
No action required beyond applying the cleanup. Users who previously ran the example should treat any key they saved from it as an insecure demonstration key and not fund it.
Security signals we found
Removal of private-key material from example output
Reduction of example scope to address construction only
Evidence from the diff
The change is confined to bitcoin/examples/create-p2wpkh-address.rs. It stops logging a freshly-generated WIF private key and removes the now-unused PrivateKey/NetworkKind imports. The generated key is ephemeral and created at example runtime, so no real user funds were at risk from the library code. The patch reduces the chance that someone running the example might mistakenly save or reuse the printed key.
Changed components
bitcoin/examples/create-p2wpkh-address.rsInspect captured patch +3 / −9
diff --git a/bitcoin/examples/create-p2wpkh-address.rs b/bitcoin/examples/create-p2wpkh-address.rs
index fe5d2a16..bb81c1df 100644
--- a/bitcoin/examples/create-p2wpkh-address.rs
+++ b/bitcoin/examples/create-p2wpkh-address.rs
@@ -1,15 +1,10 @@
use bitcoin::secp256k1::rand;
-use bitcoin::{Address, CompressedPublicKey, Network, NetworkKind, PrivateKey};
+use bitcoin::{Address, CompressedPublicKey, Network};
-/// Generate a P2WPKH (pay-to-witness-public-key-hash) address and print it
-/// along with the associated private key needed to transact.
+/// Generate a P2WPKH (pay-to-witness-public-key-hash) address and print it.
fn main() {
// Generate secp256k1 public and private key pair.
- let (secret_key, public_key) = secp256k1::generate_keypair(&mut rand::rng());
-
- // Create a Bitcoin private key to be used on the Bitcoin mainnet.
- // Equivalent to `PrivateKey::from_secp(secret_key, Network::Bitcoin)`.
- let private_key = PrivateKey::from_secp(secret_key, NetworkKind::Main);
+ let (_secret_key, public_key) = secp256k1::generate_keypair(&mut rand::rng());
// Create a compressed Bitcoin public key from the secp256k1 public key.
let public_key = CompressedPublicKey::from_secp(public_key);
@@ -17,6 +12,5 @@ fn main() {
// Create a Bitcoin P2WPKH address.
let address = Address::p2wpkh(public_key, Network::Bitcoin);
- println!("Private Key: {}", private_key.to_wif());
println!("Address: {address}");
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.