8000 Minimally-invasive separation of bitcoin keys from ECDSA signature types by dr-orlovsky · Pull Request #757 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Minimally-invasive separation of bitcoin keys from ECDSA signature types #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10000
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, for the future, this warning does not really work. If you want to deprecate a type, you would have to have to

  • Rename the existing type
  • Create a new type pub type OldName = NewName
  • Add deprecated above it

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
Loading
0