Merge rust-bitcoin/rust-bitcoin#1678: Improve the public API

42b07586ac Improve the public API (Tobin C. Harding)

Pull request description:

  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`

  Fix: #1668

ACKs for top commit:
  apoelstra:
    ACK 42b07586ac
  Kixunil:
    ACK 42b07586ac

Tree-SHA512: 5713b98b2a48d2776cdd6ca2c0e798d6e2df604d780e5182bd770fb2df807cf1f65bc24663ee6f7734fc1e0c80494c5a87dc60e44c83acefcffec7f059203a47
This commit is contained in:
Andrew Poelstra 2023-03-01 15:31:35 +00:00
commit 2cf1a4c088
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
7 changed files with 16 additions and 16 deletions

View File

@ -81,15 +81,12 @@ use std::str::FromStr;
use bitcoin::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint}; use bitcoin::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint};
use bitcoin::consensus::encode; use bitcoin::consensus::encode;
use bitcoin::constants::COIN_VALUE; use bitcoin::constants::COIN_VALUE;
use bitcoin::crypto::taproot;
use bitcoin::key::{TapTweak, XOnlyPublicKey}; use bitcoin::key::{TapTweak, XOnlyPublicKey};
use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP}; use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP};
use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType}; use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType};
use bitcoin::secp256k1::Secp256k1; use bitcoin::secp256k1::Secp256k1;
use bitcoin::sighash::{self, TapSighashType, SighashCache}; use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType};
use bitcoin::taproot::{ use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};
LeafVersion, TapLeafHash, TapSighash, TaprootBuilder, TaprootSpendInfo,
};
use bitcoin::{ use bitcoin::{
absolute, script, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness, absolute, script, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness,
}; };

View File

@ -589,8 +589,8 @@ impl<E> EncodeSigningDataResult<E> {
/// ///
/// ```rust /// ```rust
/// # use bitcoin::consensus::deserialize; /// # use bitcoin::consensus::deserialize;
/// # use bitcoin::sighash::{LegacySighash, SighashCache};
/// # use bitcoin::Transaction; /// # use bitcoin::Transaction;
/// # use bitcoin::crypto::sighash::{LegacySighash, SighashCache};
/// # use bitcoin_hashes::{Hash, hex::FromHex}; /// # use bitcoin_hashes::{Hash, hex::FromHex};
/// # let mut writer = LegacySighash::engine(); /// # let mut writer = LegacySighash::engine();
/// # let input_index = 0; /// # let input_index = 0;

View File

@ -578,7 +578,7 @@ pub type UntweakedKeyPair = KeyPair;
/// # Examples /// # Examples
/// ``` /// ```
/// # #[cfg(feature = "rand-std")] { /// # #[cfg(feature = "rand-std")] {
/// # use bitcoin::crypto::key::{KeyPair, TweakedKeyPair, TweakedPublicKey}; /// # use bitcoin::key::{KeyPair, TweakedKeyPair, TweakedPublicKey};
/// # use bitcoin::secp256k1::{rand, Secp256k1}; /// # use bitcoin::secp256k1::{rand, Secp256k1};
/// # let secp = Secp256k1::new(); /// # let secp = Secp256k1::new();
/// # let keypair = TweakedKeyPair::dangerous_assume_tweaked(KeyPair::new(&secp, &mut rand::thread_rng())); /// # let keypair = TweakedKeyPair::dangerous_assume_tweaked(KeyPair::new(&secp, &mut rand::thread_rng()));

View File

@ -9,4 +9,5 @@
pub mod ecdsa; pub mod ecdsa;
pub mod key; pub mod key;
pub mod sighash; pub mod sighash;
pub mod taproot; // Contents re-exported in `bitcoin::taproot`.
pub(crate) mod taproot;

View File

@ -92,7 +92,8 @@ pub mod bip158;
pub mod bip32; pub mod bip32;
pub mod blockdata; pub mod blockdata;
pub mod consensus; 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 error;
pub mod hash_types; pub mod hash_types;
pub mod merkle_tree; pub mod merkle_tree;
@ -127,7 +128,7 @@ pub use crate::blockdata::witness::{self, Witness};
pub use crate::blockdata::{constants, opcodes}; pub use crate::blockdata::{constants, opcodes};
pub use crate::consensus::encode::VarInt; pub use crate::consensus::encode::VarInt;
pub use crate::crypto::key::{self, PrivateKey, PublicKey}; 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::error::Error;
pub use crate::hash_types::{Txid, Wtxid, BlockHash, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash}; pub use crate::hash_types::{Txid, Wtxid, BlockHash, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
pub use crate::merkle_tree::MerkleBlock; pub use crate::merkle_tree::MerkleBlock;

View File

@ -13,12 +13,13 @@ use bitcoin_internals::write_err;
use secp256k1::{self, Scalar, Secp256k1}; use secp256k1::{self, Scalar, Secp256k1};
use crate::consensus::Encodable; use crate::consensus::Encodable;
use crate::crypto::key::{TapTweak, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey};
use crate::hashes::{sha256t_hash_newtype, Hash, HashEngine}; use crate::hashes::{sha256t_hash_newtype, Hash, HashEngine};
use crate::crypto::key::{TapTweak, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey};
use crate::prelude::*; use crate::prelude::*;
use crate::{io, Script, ScriptBuf}; 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. /// The SHA-256 midstate value for the TapLeaf hash.
const MIDSTATE_TAPLEAF: [u8; 32] = [ const MIDSTATE_TAPLEAF: [u8; 32] = [
@ -1133,6 +1134,7 @@ mod test {
use crate::hashes::hex::FromHex; use crate::hashes::hex::FromHex;
use crate::hashes::sha256t::Tag; use crate::hashes::sha256t::Tag;
use crate::hashes::{sha256, Hash, HashEngine}; use crate::hashes::{sha256, Hash, HashEngine};
use crate::sighash::{TapSighash, TapSighashTag};
use crate::{Address, Network}; use crate::{Address, Network};
extern crate serde_json; extern crate serde_json;

View File

@ -30,16 +30,15 @@ use bitcoin::bip32::{ChildNumber, ExtendedPrivKey, ExtendedPubKey, KeySource};
use bitcoin::blockdata::locktime::{absolute, relative}; use bitcoin::blockdata::locktime::{absolute, relative};
use bitcoin::blockdata::witness::Witness; use bitcoin::blockdata::witness::Witness;
use bitcoin::consensus::encode::deserialize; use bitcoin::consensus::encode::deserialize;
use bitcoin::crypto::key::UntweakedPublicKey;
use bitcoin::crypto::{ecdsa, taproot};
use bitcoin::hashes::hex::FromHex; use bitcoin::hashes::hex::FromHex;
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash}; use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use bitcoin::key::UntweakedPublicKey;
use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey}; use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey};
use bitcoin::psbt::{Input, Output, Psbt, PsbtSighashType}; use bitcoin::psbt::{Input, Output, Psbt, PsbtSighashType};
use bitcoin::sighash::{EcdsaSighashType, TapSighashType}; use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
use bitcoin::taproot::{ControlBlock, LeafVersion, TaprootBuilder, TaprootSpendInfo}; use bitcoin::taproot::{self, ControlBlock, LeafVersion, TaprootBuilder, TaprootSpendInfo};
use bitcoin::{ 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, Transaction, TxIn, TxOut, Txid, Work,
}; };
use secp256k1::Secp256k1; use secp256k1::Secp256k1;