From 42b07586ac0cc93f168edc4e709539d483bba94b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 28 Feb 2023 09:47:29 +1100 Subject: [PATCH] Improve the public API We created the `crypto` crate as a container for cryptography modules with the idea that it may be split out into a separate crate. There is no reason for users of the lib to know about this module. Also, we have two `taproot` modules, one in `crypto` and one at the crate root, this makes for un-ergonomic usage of the lib. Improve the public API by doing: - Make the `crypto` module private (`pub(crate)`). - Re-export `crypto::taproot::Signature` (and `Error`) from `crate::taproot` --- bitcoin/examples/taproot-psbt.rs | 7 ++----- bitcoin/src/blockdata/transaction.rs | 2 +- bitcoin/src/crypto/key.rs | 2 +- bitcoin/src/crypto/mod.rs | 3 ++- bitcoin/src/lib.rs | 5 +++-- bitcoin/src/taproot.rs | 6 ++++-- bitcoin/tests/serde.rs | 7 +++---- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 4b77ac05..6fd098eb 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -81,15 +81,12 @@ use std::str::FromStr; use bitcoin::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint}; use bitcoin::consensus::encode; use bitcoin::constants::COIN_VALUE; -use bitcoin::crypto::taproot; use bitcoin::key::{TapTweak, XOnlyPublicKey}; use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP}; use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType}; use bitcoin::secp256k1::Secp256k1; -use bitcoin::sighash::{self, TapSighashType, SighashCache}; -use bitcoin::taproot::{ - LeafVersion, TapLeafHash, TapSighash, TaprootBuilder, TaprootSpendInfo, -}; +use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType}; +use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo}; use bitcoin::{ absolute, script, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness, }; diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index c5885059..792906c5 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -589,8 +589,8 @@ impl EncodeSigningDataResult { /// /// ```rust /// # use bitcoin::consensus::deserialize; + /// # use bitcoin::sighash::{LegacySighash, SighashCache}; /// # use bitcoin::Transaction; - /// # use bitcoin::crypto::sighash::{LegacySighash, SighashCache}; /// # use bitcoin_hashes::{Hash, hex::FromHex}; /// # let mut writer = LegacySighash::engine(); /// # let input_index = 0; diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index e50d8750..e1646864 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -578,7 +578,7 @@ pub type UntweakedKeyPair = KeyPair; /// # Examples /// ``` /// # #[cfg(feature = "rand-std")] { -/// # use bitcoin::crypto::key::{KeyPair, TweakedKeyPair, TweakedPublicKey}; +/// # use bitcoin::key::{KeyPair, TweakedKeyPair, TweakedPublicKey}; /// # use bitcoin::secp256k1::{rand, Secp256k1}; /// # let secp = Secp256k1::new(); /// # let keypair = TweakedKeyPair::dangerous_assume_tweaked(KeyPair::new(&secp, &mut rand::thread_rng())); diff --git a/bitcoin/src/crypto/mod.rs b/bitcoin/src/crypto/mod.rs index 4c4d5129..a567fc68 100644 --- a/bitcoin/src/crypto/mod.rs +++ b/bitcoin/src/crypto/mod.rs @@ -9,4 +9,5 @@ pub mod ecdsa; pub mod key; pub mod sighash; -pub mod taproot; +// Contents re-exported in `bitcoin::taproot`. +pub(crate) mod taproot; diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 309ae9e8..75a3b69b 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -92,7 +92,8 @@ pub mod bip158; pub mod bip32; pub mod blockdata; pub mod consensus; -pub mod crypto; +// Private until we either make this a crate or flatten it - still to be decided. +pub(crate) mod crypto; pub mod error; pub mod hash_types; pub mod merkle_tree; @@ -127,7 +128,7 @@ pub use crate::blockdata::witness::{self, Witness}; pub use crate::blockdata::{constants, opcodes}; pub use crate::consensus::encode::VarInt; pub use crate::crypto::key::{self, PrivateKey, PublicKey}; -pub use crate::crypto::sighash; +pub use crate::crypto::{ecdsa, sighash}; pub use crate::error::Error; pub use crate::hash_types::{Txid, Wtxid, BlockHash, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash}; pub use crate::merkle_tree::MerkleBlock; diff --git a/bitcoin/src/taproot.rs b/bitcoin/src/taproot.rs index 619db781..e16a41b2 100644 --- a/bitcoin/src/taproot.rs +++ b/bitcoin/src/taproot.rs @@ -13,12 +13,13 @@ use bitcoin_internals::write_err; use secp256k1::{self, Scalar, Secp256k1}; use crate::consensus::Encodable; -use crate::crypto::key::{TapTweak, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey}; use crate::hashes::{sha256t_hash_newtype, Hash, HashEngine}; +use crate::crypto::key::{TapTweak, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey}; use crate::prelude::*; use crate::{io, Script, ScriptBuf}; -pub use crate::crypto::sighash::{TapSighash, TapSighashTag}; +// Re-export these so downstream only has to use one `taproot` module. +pub use crate::crypto::taproot::{Signature, Error}; /// The SHA-256 midstate value for the TapLeaf hash. const MIDSTATE_TAPLEAF: [u8; 32] = [ @@ -1133,6 +1134,7 @@ mod test { use crate::hashes::hex::FromHex; use crate::hashes::sha256t::Tag; use crate::hashes::{sha256, Hash, HashEngine}; + use crate::sighash::{TapSighash, TapSighashTag}; use crate::{Address, Network}; extern crate serde_json; diff --git a/bitcoin/tests/serde.rs b/bitcoin/tests/serde.rs index 254a2016..9c5636d3 100644 --- a/bitcoin/tests/serde.rs +++ b/bitcoin/tests/serde.rs @@ -30,16 +30,15 @@ use bitcoin::bip32::{ChildNumber, ExtendedPrivKey, ExtendedPubKey, KeySource}; use bitcoin::blockdata::locktime::{absolute, relative}; use bitcoin::blockdata::witness::Witness; use bitcoin::consensus::encode::deserialize; -use bitcoin::crypto::key::UntweakedPublicKey; -use bitcoin::crypto::{ecdsa, taproot}; use bitcoin::hashes::hex::FromHex; use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash}; +use bitcoin::key::UntweakedPublicKey; use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey}; use bitcoin::psbt::{Input, Output, Psbt, PsbtSighashType}; use bitcoin::sighash::{EcdsaSighashType, TapSighashType}; -use bitcoin::taproot::{ControlBlock, LeafVersion, TaprootBuilder, TaprootSpendInfo}; +use bitcoin::taproot::{self, ControlBlock, LeafVersion, TaprootBuilder, TaprootSpendInfo}; use bitcoin::{ - Address, Block, Network, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, Target, + ecdsa, Address, Block, Network, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, Target, Transaction, TxIn, TxOut, Txid, Work, }; use secp256k1::Secp256k1;