Added an example for generating keys using OsRng

This commit is contained in:
Elichai Turkel 2019-10-28 22:09:18 +02:00
parent f5c8a005f9
commit 5ee0afbde4
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
3 changed files with 24 additions and 0 deletions

View File

@ -36,6 +36,7 @@ script:
- cargo test --verbose - cargo test --verbose
- cargo build --verbose --release - cargo build --verbose --release
- cargo test --verbose --release - cargo test --verbose --release
- cargo run --example generate_keys --features=rand
- if [ ${TRAVIS_RUST_VERSION} == "stable" ]; then cargo doc --verbose --features="rand,serde,recovery,endomorphism"; fi - if [ ${TRAVIS_RUST_VERSION} == "stable" ]; then cargo doc --verbose --features="rand,serde,recovery,endomorphism"; fi
- if [ ${TRAVIS_RUST_VERSION} == "nightly" ]; then cargo test --verbose --benches --features=unstable; fi - if [ ${TRAVIS_RUST_VERSION} == "nightly" ]; then cargo test --verbose --benches --features=unstable; fi
- if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then - if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then

View File

@ -13,6 +13,7 @@ keywords = [ "crypto", "ECDSA", "secp256k1", "libsecp256k1", "bitcoin" ]
readme = "README.md" readme = "README.md"
build = "build.rs" build = "build.rs"
links = "secp256k1" links = "secp256k1"
autoexamples = false # Remove when edition 2018 https://github.com/rust-lang/cargo/issues/5330
# Should make docs.rs show all functions, even those behind non-default features # Should make docs.rs show all functions, even those behind non-default features
[package.metadata.docs.rs] [package.metadata.docs.rs]
@ -38,6 +39,7 @@ lowmemory = []
rand = "0.6" rand = "0.6"
rand_core = "0.4" rand_core = "0.4"
serde_test = "1.0" serde_test = "1.0"
bitcoin_hashes = "0.7"
[dependencies.rand] [dependencies.rand]
version = "0.6" version = "0.6"
@ -48,3 +50,7 @@ default-features = false
version = "1.0" version = "1.0"
optional = true optional = true
default-features = false default-features = false
[[example]]
name = "generate_keys"
required-features = ["rand"]

17
examples/generate_keys.rs Normal file
View File

@ -0,0 +1,17 @@
extern crate secp256k1;
use secp256k1::rand::rngs::OsRng;
use secp256k1::{PublicKey, Secp256k1, SecretKey};
fn main() {
let secp = Secp256k1::new();
let mut rng = OsRng::new().unwrap();
// First option:
let (seckey, pubkey) = secp.generate_keypair(&mut rng);
assert_eq!(pubkey, PublicKey::from_secret_key(&secp, &seckey));
// Second option:
let seckey = SecretKey::new(&mut rng);
let _pubkey = PublicKey::from_secret_key(&secp, &seckey);
}