diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 52075794..6c164752 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -92,19 +92,19 @@ pub struct PublicKey { impl PublicKey { /// Constructs compressed ECDSA public key from the provided generic Secp256k1 public key - pub fn new(key: secp256k1::PublicKey) -> PublicKey { + pub fn new(key: impl Into) -> PublicKey { PublicKey { compressed: true, - inner: key, + inner: key.into(), } } /// Constructs uncompressed (legacy) ECDSA public key from the provided generic Secp256k1 /// public key - pub fn new_uncompressed(key: secp256k1::PublicKey) -> PublicKey { + pub fn new_uncompressed(key: impl Into) -> PublicKey { PublicKey { compressed: false, - inner: key, + inner: key.into(), } } @@ -544,14 +544,17 @@ impl<'de> serde::Deserialize<'de> for PublicKey { #[cfg(test)] mod tests { - use crate::io; - use super::{PrivateKey, PublicKey, SortKey}; - use secp256k1::Secp256k1; + use super::*; + use std::str::FromStr; + + use secp256k1::Secp256k1; + + use crate::address::Address; use crate::hashes::hex::FromHex; + use crate::io; use crate::network::constants::Network::Testnet; use crate::network::constants::Network::Bitcoin; - use crate::address::Address; #[test] fn test_key_derivation() { @@ -836,4 +839,16 @@ mod tests { assert_eq!(vector.input, vector.expect); } } + + #[test] + #[cfg(feature = "rand-std")] + fn public_key_constructors() { + use crate::secp256k1::rand; + + let secp = Secp256k1::new(); + let kp = KeyPair::new(&secp, &mut rand::thread_rng()); + + let _ = PublicKey::new(kp); + let _ = PublicKey::new_uncompressed(kp); + } }