Merge pull request #174 from elichai/2019-10-examples

Adding usage examples
This commit is contained in:
Andrew Poelstra 2019-11-05 18:36:49 +00:00 committed by GitHub
commit 5de62f80f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 0 deletions

View File

@ -37,6 +37,9 @@ script:
- cargo test --verbose
- cargo build --verbose --release
- cargo test --verbose --release
- cargo run --example sign_verify
- cargo run --example sign_verify_recovery --features=recovery
- 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} == "nightly" ]; then cargo test --verbose --benches --features=unstable; fi
- if [ ${TRAVIS_RUST_VERSION} == "nightly" -a "$TRAVIS_OS_NAME" = "linux" ]; then

View File

@ -13,6 +13,7 @@ keywords = [ "crypto", "ECDSA", "secp256k1", "libsecp256k1", "bitcoin" ]
readme = "README.md"
build = "build.rs"
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
[package.metadata.docs.rs]
@ -43,6 +44,7 @@ fuzztarget = []
rand = "0.6"
rand_core = "0.4"
serde_test = "1.0"
bitcoin_hashes = "0.7"
[dependencies.rand]
version = "0.6"
@ -53,3 +55,14 @@ default-features = false
version = "1.0"
optional = true
default-features = false
[[example]]
name = "sign_verify_recovery"
required-features = ["recovery"]
[[example]]
name = "sign_verify"
[[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);
}

35
examples/sign_verify.rs Normal file
View File

@ -0,0 +1,35 @@
extern crate bitcoin_hashes;
extern crate secp256k1;
use bitcoin_hashes::{sha256, Hash};
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signature, Signing, Verification};
fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubkey: [u8; 33]) -> Result<bool, Error> {
let msg = sha256::Hash::hash(msg);
let msg = Message::from_slice(&msg)?;
let sig = Signature::from_compact(&sig)?;
let pubkey = PublicKey::from_slice(&pubkey)?;
Ok(secp.verify(&msg, &sig, &pubkey).is_ok())
}
fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<Signature, Error> {
let msg = sha256::Hash::hash(msg);
let msg = Message::from_slice(&msg)?;
let seckey = SecretKey::from_slice(&seckey)?;
Ok(secp.sign(&msg, &seckey))
}
fn main() {
let secp = Secp256k1::new();
let seckey = [59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28];
let pubkey = [2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54];
let msg = b"This is some message";
let signature = sign(&secp, msg, seckey).unwrap();
let serialize_sig = signature.serialize_compact();
assert!(verify(&secp, msg, serialize_sig, pubkey).unwrap());
}

View File

@ -0,0 +1,47 @@
extern crate bitcoin_hashes;
extern crate secp256k1;
use bitcoin_hashes::{sha256, Hash};
use secp256k1::recovery::{RecoverableSignature, RecoveryId};
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
fn recover<C: Verification>(secp: &Secp256k1<C>,msg: &[u8],sig: [u8; 64],recovery_id: u8) -> Result<PublicKey, Error> {
let msg = sha256::Hash::hash(msg);
let msg = Message::from_slice(&msg)?;
let id = RecoveryId::from_i32(recovery_id as i32)?;
let sig = RecoverableSignature::from_compact(&sig, id)?;
secp.recover(&msg, &sig)
}
fn sign_recovery<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<RecoverableSignature, Error> {
let msg = sha256::Hash::hash(msg);
let msg = Message::from_slice(&msg)?;
let seckey = SecretKey::from_slice(&seckey)?;
Ok(secp.sign_recoverable(&msg, &seckey))
}
fn main() {
let secp = Secp256k1::new();
let seckey = [
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107,
94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
];
let pubkey = PublicKey::from_slice(&[
2,
29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231,
245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
]).unwrap();
let msg = b"This is some message";
let signature = sign_recovery(&secp, msg, seckey).unwrap();
let (recovery_id, serialize_sig) = signature.serialize_compact();
assert_eq!(
recover(&secp, msg, serialize_sig, recovery_id.to_i32() as u8),
Ok(pubkey)
);
}