forked from rust-bitcoin/rust-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c47a41a
commit 3e8dd8d
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
extern crate bitcoin; | ||
|
||
use bitcoin::{KnownHrp, Address, CompressedPublicKey, PrivateKey, Network}; | ||
use bitcoin::secp256k1::{rand, Secp256k1}; | ||
|
||
// Note: creating a new address requires the rand-std feature flag. | ||
// Build with cargo build --examples --features=rand-std | ||
|
||
fn main() { | ||
// Create a new Secp256k1 elliptic curve. | ||
let s = Secp256k1::new(); | ||
|
||
// Generate a Secp256k1 secret key. | ||
let (secret_key, _) = s.generate_keypair(&mut rand::thread_rng()); | ||
|
||
// Create a Bitcoin private key. | ||
let private_key = PrivateKey::new(secret_key, Network::Bitcoin); | ||
|
||
// Create a compressed public key derived from the private key. | ||
let public_key = CompressedPublicKey::from_private_key(&s, private_key).unwrap(); | ||
|
||
// Create a Bitcoin P2wpkh (Pay-to-Witness-Public-Key-Hash) address. | ||
let address = Address::p2wpkh(public_key, KnownHrp::Mainnet); | ||
|
||
println!("Address: {}", address); | ||
} |