Skip to content

Commit 5e80b85

Browse files
author
ChallengeDev210
committed
Merge rust-bitcoin/rust-bitcoin#757: Minimally-invasive separation of bitcoin keys from ECDSA signature types
8a993e8 Properly deprecate util::ecdsa key re-exports (Dr Maxim Orlovsky) bcb8932 Re-org keys and ecdsa mods - pt.3 (Dr Maxim Orlovsky) d1c2213 Re-org keys and ecdsa mods - pt.2 (Dr Maxim Orlovsky) b917016 Re-org keys and ecdsa mods - pt.1 (Dr Maxim Orlovsky) 2d9de78 Re-export all key types under `util::key`. Deprecate other exports. (Dr Maxim Orlovsky) Pull request description: This PR tries to do a minimally-invazive separation of signature- and key-related types, previously mixed in a single `util::ecdsa` module. Rationale: bitcoin key types are not specific for signature algorithm. See discussion at #588. This PR became possible after we moved on new `secp256k1` version exposing `XonlyPublicKey` type, since now all key types may co-exist in a single module under different names The PR goal is achieved through - Renaming ecdsa mod into private ec module such that the code is not copied and diff size is small; - Introducing dummy ecdsa mod back in the next commit and re-exporiting only signature types from internal `ec` mod in it; - Re-exporting all key types under `key` module, removing previous depreciation message for bitcoin keys. ACKs for top commit: apoelstra: ACK 8a993e8 sanket1729: utACK 8a993e8 Tree-SHA512: 9f71edaa2cf4cdab4b239cb1d57576e2ba0fc3c2ec0ea19ae232005967b9400da6ded992b33d10b190ca617a66dca9b99be430bc5058a064f0be1489723c4a3a
2 parents 7d4698c + e452e0d commit 5e80b85

File tree

10 files changed

+614
-609
lines changed

10 files changed

+614
-609
lines changed

src/blockdata/script.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use policy::DUST_RELAY_TX_FEE;
3939
#[cfg(feature="bitcoinconsensus")] use core::convert::From;
4040
#[cfg(feature="bitcoinconsensus")] use OutPoint;
4141

42-
use util::ecdsa::PublicKey;
42+
use util::key::PublicKey;
4343
use util::address::WitnessVersion;
4444
use util::taproot::{LeafVersion, TapBranchHash, TapLeafHash};
4545
use secp256k1::{Secp256k1, Verification};
@@ -1031,7 +1031,7 @@ mod test {
10311031
use hashes::hex::{FromHex, ToHex};
10321032
use consensus::encode::{deserialize, serialize};
10331033
use blockdata::opcodes;
1034-
use util::ecdsa::PublicKey;
1034+
use util::key::PublicKey;
10351035
use util::psbt::serialize::Serialize;
10361036

10371037
#[test]

src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,7 @@ pub use util::sighash::SchnorrSigHashType;
138138

139139
pub use util::ecdsa::{self, EcdsaSig, EcdsaSigError};
140140
pub use util::schnorr::{self, SchnorrSig, SchnorrSigError};
141-
#[deprecated(since = "0.26.1", note = "Please use `ecdsa::PrivateKey` instead")]
142-
pub use util::ecdsa::PrivateKey;
143-
#[deprecated(since = "0.26.1", note = "Please use `ecdsa::PublicKey` instead")]
144-
pub use util::ecdsa::PublicKey;
141+
pub use util::key::{PrivateKey, PublicKey, XOnlyPublicKey, KeyPair};
145142
#[allow(deprecated)]
146143
pub use blockdata::transaction::SigHashType;
147144

src/util/address.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
//! ```rust
2121
//! use bitcoin::network::constants::Network;
2222
//! use bitcoin::util::address::Address;
23-
//! use bitcoin::util::ecdsa;
23+
//! use bitcoin::PublicKey;
2424
//! use bitcoin::secp256k1::Secp256k1;
2525
//! use bitcoin::secp256k1::rand::thread_rng;
2626
//!
2727
//! // Generate random key pair.
2828
//! let s = Secp256k1::new();
29-
//! let public_key = ecdsa::PublicKey::new(s.generate_keypair(&mut thread_rng()).1);
29+
//! let public_key = PublicKey::new(s.generate_keypair(&mut thread_rng()).1);
3030
//!
3131
//! // Generate pay-to-pubkey-hash address.
3232
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);
@@ -47,8 +47,8 @@ use blockdata::{script, opcodes};
4747
use blockdata::constants::{PUBKEY_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST, SCRIPT_ADDRESS_PREFIX_TEST, MAX_SCRIPT_ELEMENT_SIZE};
4848
use network::constants::Network;
4949
use util::base58;
50-
use util::ecdsa;
5150
use util::taproot::TapBranchHash;
51+
use util::key::PublicKey;
5252
use blockdata::script::Instruction;
5353
use util::schnorr::{TapTweak, UntweakedPublicKey, TweakedPublicKey};
5454

@@ -408,7 +408,7 @@ impl Payload {
408408

409409
/// Creates a pay to (compressed) public key hash payload from a public key
410410
#[inline]
411-
pub fn p2pkh(pk: &ecdsa::PublicKey) -> Payload {
411+
pub fn p2pkh(pk: &PublicKey) -> Payload {
412412
Payload::PubkeyHash(pk.pubkey_hash())
413413
}
414414

@@ -422,15 +422,15 @@ impl Payload {
422422
}
423423

424424
/// Create a witness pay to public key payload from a public key
425-
pub fn p2wpkh(pk: &ecdsa::PublicKey) -> Result<Payload, Error> {
425+
pub fn p2wpkh(pk: &PublicKey) -> Result<Payload, Error> {
426426
Ok(Payload::WitnessProgram {
427427
version: WitnessVersion::V0,
428428
program: pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?.to_vec(),
429429
})
430430
}
431431

432432
/// Create a pay to script payload that embeds a witness pay to public key
433-
pub fn p2shwpkh(pk: &ecdsa::PublicKey) -> Result<Payload, Error> {
433+
pub fn p2shwpkh(pk: &PublicKey) -> Result<Payload, Error> {
434434
let builder = script::Builder::new()
435435
.push_int(0)
436436
.push_slice(&pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?);
@@ -543,7 +543,7 @@ impl Address {
543543
///
544544
/// This is the preferred non-witness type address.
545545
#[inline]
546-
pub fn p2pkh(pk: &ecdsa::PublicKey, network: Network) -> Address {
546+
pub fn p2pkh(pk: &PublicKey, network: Network) -> Address {
547547
Address {
548548
network,
549549
payload: Payload::p2pkh(pk),
@@ -568,7 +568,7 @@ impl Address {
568568
///
569569
/// # Errors
570570
/// Will only return an error if an uncompressed public key is provided.
571-
pub fn p2wpkh(pk: &ecdsa::PublicKey, network: Network) -> Result<Address, Error> {
571+
pub fn p2wpkh(pk: &PublicKey, network: Network) -> Result<Address, Error> {
572572
Ok(Address {
573573
network,
574574
payload: Payload::p2wpkh(pk)?,
@@ -581,7 +581,7 @@ impl Address {
581581
///
582582
/// # Errors
583583
/// Will only return an Error if an uncompressed public key is provided.
584-
pub fn p2shwpkh(pk: &ecdsa::PublicKey, network: Network) -> Result<Address, Error> {
584+
pub fn p2shwpkh(pk: &PublicKey, network: Network) -> Result<Address, Error> {
585585
Ok(Address {
586586
network,
587587
payload: Payload::p2shwpkh(pk)?,
@@ -878,7 +878,7 @@ mod tests {
878878

879879
use blockdata::script::Script;
880880
use network::constants::Network::{Bitcoin, Testnet};
881-
use util::ecdsa::PublicKey;
881+
use util::key::PublicKey;
882882
use secp256k1::XOnlyPublicKey;
883883

884884
use super::*;

src/util/bip143.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ mod tests {
194194
use consensus::encode::deserialize;
195195
use network::constants::Network;
196196
use util::address::Address;
197-
use util::ecdsa::PublicKey;
197+
use util::key::PublicKey;
198198
use hashes::hex::FromHex;
199199

200200
use super::*;

src/util/bip32.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
2020
use prelude::*;
2121

22+
use io::Write;
2223
use core::{fmt, str::FromStr, default::Default};
2324
#[cfg(feature = "std")] use std::error;
2425
#[cfg(feature = "serde")] use serde;
@@ -28,9 +29,8 @@ use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
2829
use secp256k1::{self, Secp256k1, XOnlyPublicKey};
2930

3031
use network::constants::Network;
31-
use util::{base58, endian};
32-
use util::{key, ecdsa, schnorr};
33-
use io::Write;
32+
use util::{base58, endian, key};
33+
use util::key::{PublicKey, PrivateKey, KeyPair};
3434

3535
/// A chain code
3636
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -528,8 +528,8 @@ impl ExtendedPrivKey {
528528
}
529529

530530
/// Constructs ECDSA compressed private key matching internal secret key representation.
531-
pub fn to_priv(&self) -> ecdsa::PrivateKey {
532-
ecdsa::PrivateKey {
531+
pub fn to_priv(&self) -> PrivateKey {
532+
PrivateKey {
533533
compressed: true,
534534
network: self.network,
535535
inner: self.private_key
@@ -538,8 +538,8 @@ impl ExtendedPrivKey {
538538

539539
/// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal
540540
/// secret key representation.
541-
pub fn to_keypair<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> schnorr::KeyPair {
542-
schnorr::KeyPair::from_seckey_slice(secp, &self.private_key[..]).expect("BIP32 internal private key representation is broken")
541+
pub fn to_keypair<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> KeyPair {
542+
KeyPair::from_seckey_slice(secp, &self.private_key[..]).expect("BIP32 internal private key representation is broken")
543543
}
544544

545545
/// Attempts to derive an extended private key from a path.
@@ -660,8 +660,8 @@ impl ExtendedPubKey {
660660
}
661661

662662
/// Constructs ECDSA compressed public key matching internal public key representation.
663-
pub fn to_pub(&self) -> ecdsa::PublicKey {
664-
ecdsa::PublicKey {
663+
pub fn to_pub(&self) -> PublicKey {
664+
PublicKey {
665665
compressed: true,
666666
inner: self.public_key
667667
}

0 commit comments

Comments
 (0)