Skip to content

Commit a73ceea

Browse files
committed
Re-org keys and ecdsa mods - pt.1
This commit tries to achieve separation of signature- and key-related types, previously mixed in a single ECDSA module. Rationale: bitcoin key types are not specific for signature algorithm. This is achieved through - Remove key mod with its content moved to ecdsa mod - Re-export keys under key module in util mod - to make git generate diff for the rename of ecdsa mod in the next commit correctly.
1 parent 09932b6 commit a73ceea

File tree

8 files changed

+79
-96
lines changed

8 files changed

+79
-96
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/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
}

src/util/ecdsa.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,70 @@
1515
//!
1616
//! This module provides ECDSA keys used in Bitcoin that can be roundtrip
1717
//! (de)serialized.
18-
//!
18+
19+
pub use secp256k1::{XOnlyPublicKey, KeyPair};
1920

2021
use prelude::*;
2122

2223
use core::{ops, str::FromStr};
2324
use core::fmt::{self, Write as _fmtWrite};
2425
use io;
26+
#[cfg(feature = "std")] use std::error;
2527

2628
use secp256k1::{self, Secp256k1};
2729
use network::constants::Network;
2830
use hashes::{Hash, hash160, hex};
2931
use hashes::hex::FromHex;
3032
use hash_types::{PubkeyHash, WPubkeyHash};
3133
use util::base58;
32-
use util::key::Error;
3334
use blockdata::transaction::{EcdsaSigHashType, NonStandardSigHashType};
3435

3536

37+
/// A key-related error.
38+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
39+
pub enum Error {
40+
/// Base58 encoding error
41+
Base58(base58::Error),
42+
/// secp256k1-related error
43+
Secp256k1(secp256k1::Error),
44+
}
45+
46+
47+
impl fmt::Display for Error {
48+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49+
match *self {
50+
Error::Base58(ref e) => write!(f, "Key base58 error: {}", e),
51+
Error::Secp256k1(ref e) => write!(f, "Key secp256k1 error: {}", e),
52+
}
53+
}
54+
}
55+
56+
#[cfg(feature = "std")]
57+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
58+
impl ::std::error::Error for Error {
59+
fn cause(&self) -> Option<&dyn error::Error> {
60+
match *self {
61+
Error::Base58(ref e) => Some(e),
62+
Error::Secp256k1(ref e) => Some(e),
63+
}
64+
}
65+
}
66+
67+
#[doc(hidden)]
68+
impl From<base58::Error> for Error {
69+
fn from(e: base58::Error) -> Error {
70+
Error::Base58(e)
71+
}
72+
}
73+
74+
#[doc(hidden)]
75+
impl From<secp256k1::Error> for Error {
76+
fn from(e: secp256k1::Error) -> Error {
77+
Error::Secp256k1(e)
78+
}
79+
}
80+
81+
3682
/// A Bitcoin ECDSA public key
3783
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3884
pub struct PublicKey {

src/util/key.rs

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/util/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mod message_signing {
4242
use secp256k1;
4343
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
4444

45-
use util::ecdsa::PublicKey;
45+
use util::key::PublicKey;
4646
use util::address::{Address, AddressType};
4747

4848
/// An error used for dealing with Bitcoin Signed Messages.

src/util/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//!
1919
2020
pub mod ecdsa;
21-
pub mod key;
2221
pub mod schnorr;
2322
pub mod address;
2423
pub mod amount;
@@ -37,6 +36,14 @@ pub mod sighash;
3736

3837
pub(crate) mod endian;
3938

39+
pub mod key {
40+
//! Bitcoin keys.
41+
//!
42+
//! This module provides keys used in Bitcoin that can be roundtrip (de)serialized.
43+
44+
pub use super::ecdsa::{XOnlyPublicKey, PublicKey, PrivateKey, KeyPair, Error};
45+
}
46+
4047
use prelude::*;
4148
use io;
4249
use core::fmt;

0 commit comments

Comments
 (0)