Merge rust-bitcoin/rust-bitcoin#1582: Take `Into<secp256k1::PublicKey>` in PublicKey constructors

1d3d5a9c5b Take Into<secp256k1::PublicKey> in PublicKey constructors (Tobin C. Harding)
b13a76407b keys: Clean up test imports (Tobin C. Harding)

Pull request description:

  We can make the API more ergonomic by taking a generic argument that implements `Into<secp256k1::PublicKey>` in the `bitcoin::PublicKey` constructors.

  The only thing than this is useful for is passing in `KeyPair` and the `From` implementation already exists. Add a unit test to verify.

  Fix: #1453

  ## Note

  As per the discussion in #1453 I checked secp and bitcoin for all keys that can be converted using `From` and it turns out its only `KeyPair` which already has `From` impls - good rust-bitcoin devs :)

ACKs for top commit:
  Kixunil:
    ACK 1d3d5a9c5b
  apoelstra:
    ACK 1d3d5a9c5b

Tree-SHA512: b5e5272561de15cdcfb15913aa5d42ddc96bf2fd5835068a5a9aa0274074ffa698ec9e81707f102b7d1b244f1abd0fdbd0eb4b6b505c84c3d5719dcb01d46efb
This commit is contained in:
Andrew Poelstra 2023-01-24 14:52:43 +00:00
commit 07a5cccd42
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 23 additions and 8 deletions

View File

@ -92,19 +92,19 @@ pub struct PublicKey {
impl PublicKey { impl PublicKey {
/// Constructs compressed ECDSA public key from the provided generic Secp256k1 public key /// 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<secp256k1::PublicKey>) -> PublicKey {
PublicKey { PublicKey {
compressed: true, compressed: true,
inner: key, inner: key.into(),
} }
} }
/// Constructs uncompressed (legacy) ECDSA public key from the provided generic Secp256k1 /// Constructs uncompressed (legacy) ECDSA public key from the provided generic Secp256k1
/// public key /// public key
pub fn new_uncompressed(key: secp256k1::PublicKey) -> PublicKey { pub fn new_uncompressed(key: impl Into<secp256k1::PublicKey>) -> PublicKey {
PublicKey { PublicKey {
compressed: false, compressed: false,
inner: key, inner: key.into(),
} }
} }
@ -544,14 +544,17 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::io; use super::*;
use super::{PrivateKey, PublicKey, SortKey};
use secp256k1::Secp256k1;
use std::str::FromStr; use std::str::FromStr;
use secp256k1::Secp256k1;
use crate::address::Address;
use crate::hashes::hex::FromHex; use crate::hashes::hex::FromHex;
use crate::io;
use crate::network::constants::Network::Testnet; use crate::network::constants::Network::Testnet;
use crate::network::constants::Network::Bitcoin; use crate::network::constants::Network::Bitcoin;
use crate::address::Address;
#[test] #[test]
fn test_key_derivation() { fn test_key_derivation() {
@ -836,4 +839,16 @@ mod tests {
assert_eq!(vector.input, vector.expect); 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);
}
} }