2018-03-14 03:53:03 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Private key
|
|
|
|
//!
|
|
|
|
//! A private key represents the secret data associated with its proposed use
|
2018-03-14 03:53:03 +00:00
|
|
|
//!
|
2018-08-20 19:40:00 +00:00
|
|
|
|
2018-10-10 10:12:33 +00:00
|
|
|
use std::fmt::{self, Write};
|
2018-03-14 03:53:03 +00:00
|
|
|
use std::str::FromStr;
|
2018-07-27 20:15:48 +00:00
|
|
|
use secp256k1::{self, Secp256k1};
|
2018-03-14 03:53:03 +00:00
|
|
|
use secp256k1::key::{PublicKey, SecretKey};
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
use consensus::encode;
|
2018-03-14 03:53:03 +00:00
|
|
|
use network::constants::Network;
|
|
|
|
use util::base58;
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
/// A Bitcoin ECDSA private key
|
2018-11-15 21:28:37 +00:00
|
|
|
pub struct PrivateKey {
|
2018-03-14 03:53:03 +00:00
|
|
|
/// Whether this private key represents a compressed address
|
|
|
|
pub compressed: bool,
|
|
|
|
/// The network on which this key should be used
|
|
|
|
pub network: Network,
|
|
|
|
/// The actual ECDSA key
|
|
|
|
pub key: SecretKey
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:28:37 +00:00
|
|
|
impl PrivateKey {
|
2018-03-14 03:53:03 +00:00
|
|
|
/// Computes the public key as supposed to be used with this secret
|
2018-07-27 20:15:48 +00:00
|
|
|
pub fn public_key<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> PublicKey {
|
|
|
|
PublicKey::from_secret_key(secp, &self.key)
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 10:12:33 +00:00
|
|
|
/// Format the private key to WIF format.
|
|
|
|
pub fn fmt_wif(&self, fmt: &mut fmt::Write) -> fmt::Result {
|
2018-03-14 03:53:03 +00:00
|
|
|
let mut ret = [0; 34];
|
|
|
|
ret[0] = match self.network {
|
|
|
|
Network::Bitcoin => 128,
|
2018-05-17 13:08:02 +00:00
|
|
|
Network::Testnet | Network::Regtest => 239,
|
2018-03-14 03:53:03 +00:00
|
|
|
};
|
|
|
|
ret[1..33].copy_from_slice(&self.key[..]);
|
2018-08-20 19:40:00 +00:00
|
|
|
let privkey = if self.compressed {
|
2018-03-14 03:53:03 +00:00
|
|
|
ret[33] = 1;
|
|
|
|
base58::check_encode_slice(&ret[..])
|
|
|
|
} else {
|
|
|
|
base58::check_encode_slice(&ret[..33])
|
2018-08-20 19:40:00 +00:00
|
|
|
};
|
|
|
|
fmt.write_str(&privkey)
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 10:12:33 +00:00
|
|
|
/// Get WIF encoding of this private key.
|
|
|
|
pub fn to_wif(&self) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
buf.write_fmt(format_args!("{}", self)).unwrap();
|
|
|
|
buf.shrink_to_fit();
|
|
|
|
buf
|
|
|
|
}
|
2018-03-14 03:53:03 +00:00
|
|
|
|
2018-10-10 10:12:33 +00:00
|
|
|
/// Parse WIF encoded private key.
|
2018-11-15 21:28:37 +00:00
|
|
|
pub fn from_wif(wif: &str) -> Result<PrivateKey, encode::Error> {
|
2018-10-10 10:12:33 +00:00
|
|
|
let data = base58::from_check(wif)?;
|
2018-03-14 03:53:03 +00:00
|
|
|
|
|
|
|
let compressed = match data.len() {
|
|
|
|
33 => false,
|
|
|
|
34 => true,
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
_ => { return Err(encode::Error::Base58(base58::Error::InvalidLength(data.len()))); }
|
2018-03-14 03:53:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let network = match data[0] {
|
|
|
|
128 => Network::Bitcoin,
|
|
|
|
239 => Network::Testnet,
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
x => { return Err(encode::Error::Base58(base58::Error::InvalidVersion(vec![x]))); }
|
2018-03-14 03:53:03 +00:00
|
|
|
};
|
|
|
|
|
2019-01-15 17:05:41 +00:00
|
|
|
let key = SecretKey::from_slice(&data[1..33])
|
2018-08-12 16:47:31 +00:00
|
|
|
.map_err(|_| base58::Error::Other("Secret key out of range".to_owned()))?;
|
2018-03-14 03:53:03 +00:00
|
|
|
|
2018-11-15 21:28:37 +00:00
|
|
|
Ok(PrivateKey {
|
2018-03-14 03:53:03 +00:00
|
|
|
compressed: compressed,
|
|
|
|
network: network,
|
|
|
|
key: key
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:28:37 +00:00
|
|
|
impl fmt::Display for PrivateKey {
|
2018-10-10 10:12:33 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.fmt_wif(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:28:37 +00:00
|
|
|
impl fmt::Debug for PrivateKey {
|
2018-10-21 23:06:46 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "[private key data]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:28:37 +00:00
|
|
|
impl FromStr for PrivateKey {
|
2018-10-10 10:12:33 +00:00
|
|
|
type Err = encode::Error;
|
2018-11-15 21:28:37 +00:00
|
|
|
fn from_str(s: &str) -> Result<PrivateKey, encode::Error> {
|
|
|
|
PrivateKey::from_wif(s)
|
2018-10-10 10:12:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:53:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-11-15 21:28:37 +00:00
|
|
|
use super::PrivateKey;
|
2018-03-14 03:53:03 +00:00
|
|
|
use secp256k1::Secp256k1;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use network::constants::Network::Testnet;
|
|
|
|
use network::constants::Network::Bitcoin;
|
2018-11-15 21:28:37 +00:00
|
|
|
use util::address::Address;
|
2018-03-14 03:53:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_key_derivation() {
|
|
|
|
// testnet compressed
|
2018-11-15 21:28:37 +00:00
|
|
|
let sk = PrivateKey::from_wif("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
|
|
|
|
assert_eq!(sk.network, Testnet);
|
|
|
|
assert_eq!(sk.compressed, true);
|
2018-10-10 10:12:33 +00:00
|
|
|
assert_eq!(&sk.to_wif(), "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy");
|
2018-03-14 03:53:03 +00:00
|
|
|
|
|
|
|
let secp = Secp256k1::new();
|
2018-11-15 21:28:37 +00:00
|
|
|
let pk = Address::p2pkh(&sk.public_key(&secp), sk.network);
|
2018-03-14 03:53:03 +00:00
|
|
|
assert_eq!(&pk.to_string(), "mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx");
|
|
|
|
|
2018-10-10 10:12:33 +00:00
|
|
|
// test string conversion
|
|
|
|
assert_eq!(&sk.to_string(), "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy");
|
|
|
|
let sk_str =
|
2018-11-15 21:28:37 +00:00
|
|
|
PrivateKey::from_str("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
|
2018-10-10 10:12:33 +00:00
|
|
|
assert_eq!(&sk.to_wif(), &sk_str.to_wif());
|
|
|
|
|
2018-03-14 03:53:03 +00:00
|
|
|
// mainnet uncompressed
|
2018-11-15 21:28:37 +00:00
|
|
|
let sk = PrivateKey::from_wif("5JYkZjmN7PVMjJUfJWfRFwtuXTGB439XV6faajeHPAM9Z2PT2R3").unwrap();
|
|
|
|
assert_eq!(sk.network, Bitcoin);
|
|
|
|
assert_eq!(sk.compressed, false);
|
2018-10-10 10:12:33 +00:00
|
|
|
assert_eq!(&sk.to_wif(), "5JYkZjmN7PVMjJUfJWfRFwtuXTGB439XV6faajeHPAM9Z2PT2R3");
|
2018-03-14 03:53:03 +00:00
|
|
|
|
|
|
|
let secp = Secp256k1::new();
|
2018-11-15 21:28:37 +00:00
|
|
|
let pk = Address::p2upkh(&sk.public_key(&secp), sk.network);
|
2018-03-14 03:53:03 +00:00
|
|
|
assert_eq!(&pk.to_string(), "1GhQvF6dL8xa6wBxLnWmHcQsurx9RxiMc8");
|
|
|
|
}
|
2018-03-21 18:49:46 +00:00
|
|
|
}
|