Merge pull request #174 from jeffbarg/master

Added example for creating an address from a public key
This commit is contained in:
Andrew Poelstra 2018-10-12 14:46:30 +00:00 committed by GitHub
commit c37ab1f9c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -15,6 +15,37 @@
//! //!
//! Support for ordinary base58 Bitcoin addresses and private keys //! Support for ordinary base58 Bitcoin addresses and private keys
//! //!
//! # Example: creating a new address from a randomly-generated key pair
//!
//! ```rust
//! extern crate rand;
//! extern crate secp256k1;
//! extern crate bitcoin;
//!
//! use bitcoin::network::constants::Network;
//! use bitcoin::util::address::Payload;
//! use bitcoin::util::address::Address;
//! use secp256k1::Secp256k1;
//! use secp256k1::key::PublicKey;
//! use rand::thread_rng;
//!
//! fn main() {
//! let network = Network::Bitcoin;
//!
//! // Generate random key pair
//! let s = Secp256k1::new();
//! let (secret_key, public_key) = s.generate_keypair(&mut thread_rng());
//!
//! // Generate pay-to-pubkey address
//! let address = Address::p2pk(&public_key, network);
//!
//! // Check address payload is public key given
//! assert_eq!(address.payload, Payload::Pubkey(public_key));
//!
//! // Check address can be unlocked by secret_key
//! assert_eq!(address.payload, Payload::Pubkey(PublicKey::from_secret_key(&s, &secret_key)));
//! }
//! ```
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use std::str::FromStr; use std::str::FromStr;