Skip to content

Commit

Permalink
Merge #757: Minimally-invasive separation of bitcoin keys from ECDSA …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sanket1729 committed Jan 15, 2022
2 parents 17c3547 + 8a993e8 commit d1f051c
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 609 deletions.
4 changes: 2 additions & 2 deletions src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use policy::DUST_RELAY_TX_FEE;
#[cfg(feature="bitcoinconsensus")] use core::convert::From;
#[cfg(feature="bitcoinconsensus")] use OutPoint;

use util::ecdsa::PublicKey;
use util::key::PublicKey;
use util::address::WitnessVersion;
use util::taproot::{LeafVersion, TapBranchHash, TapLeafHash};
use secp256k1::{Secp256k1, Verification};
Expand Down Expand Up @@ -1031,7 +1031,7 @@ mod test {
use hashes::hex::{FromHex, ToHex};
use consensus::encode::{deserialize, serialize};
use blockdata::opcodes;
use util::ecdsa::PublicKey;
use util::key::PublicKey;
use util::psbt::serialize::Serialize;

#[test]
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ pub use util::sighash::SchnorrSigHashType;

pub use util::ecdsa::{self, EcdsaSig, EcdsaSigError};
pub use util::schnorr::{self, SchnorrSig, SchnorrSigError};
#[deprecated(since = "0.26.1", note = "Please use `ecdsa::PrivateKey` instead")]
pub use util::ecdsa::PrivateKey;
#[deprecated(since = "0.26.1", note = "Please use `ecdsa::PublicKey` instead")]
pub use util::ecdsa::PublicKey;
pub use util::key::{PrivateKey, PublicKey, XOnlyPublicKey, KeyPair};
#[allow(deprecated)]
pub use blockdata::transaction::SigHashType;

Expand Down
20 changes: 10 additions & 10 deletions src/util/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
//! ```rust
//! use bitcoin::network::constants::Network;
//! use bitcoin::util::address::Address;
//! use bitcoin::util::ecdsa;
//! use bitcoin::PublicKey;
//! use bitcoin::secp256k1::Secp256k1;
//! use bitcoin::secp256k1::rand::thread_rng;
//!
//! // Generate random key pair.
//! let s = Secp256k1::new();
//! let public_key = ecdsa::PublicKey::new(s.generate_keypair(&mut thread_rng()).1);
//! let public_key = PublicKey::new(s.generate_keypair(&mut thread_rng()).1);
//!
//! // Generate pay-to-pubkey-hash address.
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);
Expand All @@ -47,8 +47,8 @@ use blockdata::{script, opcodes};
use blockdata::constants::{PUBKEY_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST, SCRIPT_ADDRESS_PREFIX_TEST, MAX_SCRIPT_ELEMENT_SIZE};
use network::constants::Network;
use util::base58;
use util::ecdsa;
use util::taproot::TapBranchHash;
use util::key::PublicKey;
use blockdata::script::Instruction;
use util::schnorr::{TapTweak, UntweakedPublicKey, TweakedPublicKey};

Expand Down Expand Up @@ -408,7 +408,7 @@ impl Payload {

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

Expand All @@ -422,15 +422,15 @@ impl Payload {
}

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

/// Create a pay to script payload that embeds a witness pay to public key
pub fn p2shwpkh(pk: &ecdsa::PublicKey) -> Result<Payload, Error> {
pub fn p2shwpkh(pk: &PublicKey) -> Result<Payload, Error> {
let builder = script::Builder::new()
.push_int(0)
.push_slice(&pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?);
Expand Down Expand Up @@ -543,7 +543,7 @@ impl Address {
///
/// This is the preferred non-witness type address.
#[inline]
pub fn p2pkh(pk: &ecdsa::PublicKey, network: Network) -> Address {
pub fn p2pkh(pk: &PublicKey, network: Network) -> Address {
Address {
network,
payload: Payload::p2pkh(pk),
Expand All @@ -568,7 +568,7 @@ impl Address {
///
/// # Errors
/// Will only return an error if an uncompressed public key is provided.
pub fn p2wpkh(pk: &ecdsa::PublicKey, network: Network) -> Result<Address, Error> {
pub fn p2wpkh(pk: &PublicKey, network: Network) -> Result<Address, Error> {
Ok(Address {
network,
payload: Payload::p2wpkh(pk)?,
Expand All @@ -581,7 +581,7 @@ impl Address {
///
/// # Errors
/// Will only return an Error if an uncompressed public key is provided.
pub fn p2shwpkh(pk: &ecdsa::PublicKey, network: Network) -> Result<Address, Error> {
pub fn p2shwpkh(pk: &PublicKey, network: Network) -> Result<Address, Error> {
Ok(Address {
network,
payload: Payload::p2shwpkh(pk)?,
Expand Down Expand Up @@ -878,7 +878,7 @@ mod tests {

use blockdata::script::Script;
use network::constants::Network::{Bitcoin, Testnet};
use util::ecdsa::PublicKey;
use util::key::PublicKey;
use secp256k1::XOnlyPublicKey;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/util/bip143.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ mod tests {
use consensus::encode::deserialize;
use network::constants::Network;
use util::address::Address;
use util::ecdsa::PublicKey;
use util::key::PublicKey;
use hashes::hex::FromHex;

use super::*;
Expand Down
18 changes: 9 additions & 9 deletions src/util/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use prelude::*;

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

use network::constants::Network;
use util::{base58, endian};
use util::{key, ecdsa, schnorr};
use io::Write;
use util::{base58, endian, key};
use util::key::{PublicKey, PrivateKey, KeyPair};

/// A chain code
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -528,8 +528,8 @@ impl ExtendedPrivKey {
}

/// Constructs ECDSA compressed private key matching internal secret key representation.
pub fn to_priv(&self) -> ecdsa::PrivateKey {
ecdsa::PrivateKey {
pub fn to_priv(&self) -> PrivateKey {
PrivateKey {
compressed: true,
network: self.network,
inner: self.private_key
Expand All @@ -538,8 +538,8 @@ impl ExtendedPrivKey {

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

/// Attempts to derive an extended private key from a path.
Expand Down Expand Up @@ -660,8 +660,8 @@ impl ExtendedPubKey {
}

/// Constructs ECDSA compressed public key matching internal public key representation.
pub fn to_pub(&self) -> ecdsa::PublicKey {
ecdsa::PublicKey {
pub fn to_pub(&self) -> PublicKey {
PublicKey {
compressed: true,
inner: self.public_key
}
Expand Down

0 comments on commit d1f051c

Please sign in to comment.