Skip to content

Commit

Permalink
Merge #1837: feat: generate PrivateKey
Browse files Browse the repository at this point in the history
995c797 feat: generate PrivateKey (kshitjj)

Pull request description:

  added a function to generate a private key

  Resolves: #1823

ACKs for top commit:
  apoelstra:
    ACK 995c797
  tcharding:
    ACK 995c797

Tree-SHA512: 29ba54be8cb777e71a4683835686cbf2978b23736f629d7bbff468074235fece261ca170c23f358d1bd878987566d09e4488c3f1a106c59a5c8bdf52b98abffe
  • Loading branch information
apoelstra committed May 8, 2023
2 parents 4abbdc2 + 995c797 commit dea6282
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use core::str::FromStr;
use hashes::hex::FromHex;
use hashes::{hash160, hex, Hash};
use internals::write_err;
#[cfg(feature = "rand-std")]
pub use secp256k1::rand;
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};

use crate::hash_types::{PubkeyHash, WPubkeyHash};
Expand Down Expand Up @@ -298,6 +300,13 @@ pub struct PrivateKey {
}

impl PrivateKey {
/// Constructs new compressed ECDSA private key using the secp256k1 algorithm and
/// a secure random number generator.
#[cfg(feature = "rand-std")]
pub fn generate(network: Network) -> PrivateKey {
let secret_key = secp256k1::SecretKey::new(&mut rand::thread_rng());
PrivateKey::new(secret_key, network)
}
/// Constructs compressed ECDSA private key from the provided generic Secp256k1 private key
/// and the specified network
pub fn new(key: secp256k1::SecretKey, network: Network) -> PrivateKey {
Expand Down

0 comments on commit dea6282

Please sign in to comment.