Merge rust-bitcoin/rust-bitcoin#1837: feat: generate PrivateKey

995c797e0d feat: generate PrivateKey (kshitjj)

Pull request description:

  added a function to generate a private key

  Resolves: #1823

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

Tree-SHA512: 29ba54be8cb777e71a4683835686cbf2978b23736f629d7bbff468074235fece261ca170c23f358d1bd878987566d09e4488c3f1a106c59a5c8bdf52b98abffe
This commit is contained in:
Andrew Poelstra 2023-05-08 01:30:06 +00:00
commit dea628276c
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 9 additions and 0 deletions

View File

@ -12,6 +12,8 @@ use core::str::FromStr;
use hashes::hex::FromHex; use hashes::hex::FromHex;
use hashes::{hash160, hex, Hash}; use hashes::{hash160, hex, Hash};
use internals::write_err; use internals::write_err;
#[cfg(feature = "rand-std")]
pub use secp256k1::rand;
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};
use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::hash_types::{PubkeyHash, WPubkeyHash};
@ -298,6 +300,13 @@ pub struct PrivateKey {
} }
impl 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 /// Constructs compressed ECDSA private key from the provided generic Secp256k1 private key
/// and the specified network /// and the specified network
pub fn new(key: secp256k1::SecretKey, network: Network) -> PrivateKey { pub fn new(key: secp256k1::SecretKey, network: Network) -> PrivateKey {