Move the transaction hash types over to primitives

Move the `Txid` and `Wtxid` hash wrapper types over to `primitives`.
This introduces to `primitves` an unconditional dependency on
`hashes`.
This commit is contained in:
Tobin C. Harding 2024-09-03 11:17:51 +10:00
parent 7e454d756d
commit 0403e52ce3
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
6 changed files with 45 additions and 35 deletions

View File

@ -113,6 +113,7 @@ dependencies = [
"bitcoin-internals", "bitcoin-internals",
"bitcoin-io", "bitcoin-io",
"bitcoin-units", "bitcoin-units",
"bitcoin_hashes",
"mutagen", "mutagen",
"ordered", "ordered",
"serde", "serde",

View File

@ -112,6 +112,7 @@ dependencies = [
"bitcoin-internals", "bitcoin-internals",
"bitcoin-io", "bitcoin-io",
"bitcoin-units", "bitcoin-units",
"bitcoin_hashes",
"mutagen", "mutagen",
"ordered", "ordered",
"serde", "serde",

View File

@ -35,31 +35,9 @@ use crate::{Amount, FeeRate, SignedAmount, VarInt};
#[doc(inline)] #[doc(inline)]
pub use primitives::transaction::*; pub use primitives::transaction::*;
hashes::hash_newtype! {
/// A bitcoin transaction hash/transaction ID.
///
/// For compatibility with the existing Bitcoin infrastructure and historical and current
/// versions of the Bitcoin Core software itself, this and other [`sha256d::Hash`] types, are
/// serialized in reverse byte order when converted to a hex string via [`std::fmt::Display`]
/// trait operations.
///
/// See [`hashes::Hash::DISPLAY_BACKWARD`] for more details.
pub struct Txid(sha256d::Hash);
/// A bitcoin witness transaction ID.
pub struct Wtxid(sha256d::Hash);
}
impl_hashencode!(Txid); impl_hashencode!(Txid);
impl_hashencode!(Wtxid); impl_hashencode!(Wtxid);
impl Txid {
/// The `Txid` used in a coinbase prevout.
///
/// This is used as the "txid" of the dummy input of a coinbase transaction. This is not a real
/// TXID and should not be used in any other contexts. See [`OutPoint::COINBASE_PREVOUT`].
pub const COINBASE_PREVOUT: Self = Self::from_byte_array([0; 32]);
}
crate::internal_macros::define_extension_trait! { crate::internal_macros::define_extension_trait! {
/// Extension functionality for the [`Txid`] type. /// Extension functionality for the [`Txid`] type.
pub trait TxidExt impl for Txid { pub trait TxidExt impl for Txid {
@ -69,15 +47,6 @@ crate::internal_macros::define_extension_trait! {
} }
} }
impl Wtxid {
/// The `Wtxid` of a coinbase transaction.
///
/// This is used as the wTXID for the coinbase transaction when constructing blocks (in the
/// witness commitment tree) since the coinbase transaction contains a commitment to all
/// transactions' wTXIDs but naturally cannot commit to its own.
pub const COINBASE: Self = Self::from_byte_array([0; 32]);
}
crate::internal_macros::define_extension_trait! { crate::internal_macros::define_extension_trait! {
/// Extension functionality for the [`Wtxid`] type. /// Extension functionality for the [`Wtxid`] type.
pub trait WtxidExt impl for Wtxid { pub trait WtxidExt impl for Wtxid {

View File

@ -16,11 +16,12 @@ exclude = ["tests", "contrib"]
[features] [features]
default = ["std"] default = ["std"]
std = ["alloc", "internals/std", "io/std", "units/std"] std = ["alloc", "hashes/std", "internals/std", "io/std", "units/std"]
alloc = ["internals/alloc", "io/alloc", "units/alloc"] alloc = ["hashes/alloc", "internals/alloc", "io/alloc", "units/alloc"]
serde = ["dep:serde", "internals/serde", "units/serde", "alloc"] serde = ["dep:serde", "hashes/serde", "internals/serde", "units/serde", "alloc"]
[dependencies] [dependencies]
hashes = { package = "bitcoin_hashes", version = "0.14.0", default-features = false, features = ["bitcoin-io"] }
internals = { package = "bitcoin-internals", version = "0.3.0" } internals = { package = "bitcoin-internals", version = "0.3.0" }
io = { package = "bitcoin-io", version = "0.1.1", default-features = false } io = { package = "bitcoin-io", version = "0.1.1", default-features = false }
units = { package = "bitcoin-units", version = "0.1.0", default-features = false } units = { package = "bitcoin-units", version = "0.1.0", default-features = false }

View File

@ -42,7 +42,11 @@ pub use units::*;
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub use self::locktime::{absolute, relative}; pub use self::locktime::{absolute, relative};
#[doc(inline)] #[doc(inline)]
pub use self::{pow::CompactTarget, sequence::Sequence}; pub use self::{
pow::CompactTarget,
sequence::Sequence,
transaction::{Txid, Wtxid},
};
#[rustfmt::skip] #[rustfmt::skip]
#[allow(unused_imports)] #[allow(unused_imports)]

View File

@ -12,6 +12,40 @@
use core::fmt; use core::fmt;
use hashes::sha256d;
hashes::hash_newtype! {
/// A bitcoin transaction hash/transaction ID.
///
/// For compatibility with the existing Bitcoin infrastructure and historical and current
/// versions of the Bitcoin Core software itself, this and other [`sha256d::Hash`] types, are
/// serialized in reverse byte order when converted to a hex string via [`std::fmt::Display`]
/// trait operations.
///
/// See [`hashes::Hash::DISPLAY_BACKWARD`] for more details.
pub struct Txid(sha256d::Hash);
/// A bitcoin witness transaction ID.
pub struct Wtxid(sha256d::Hash);
}
impl Txid {
/// The `Txid` used in a coinbase prevout.
///
/// This is used as the "txid" of the dummy input of a coinbase transaction. This is not a real
/// TXID and should not be used in any other contexts. See `OutPoint::COINBASE_PREVOUT`.
pub const COINBASE_PREVOUT: Self = Self::from_byte_array([0; 32]);
}
impl Wtxid {
/// The `Wtxid` of a coinbase transaction.
///
/// This is used as the wTXID for the coinbase transaction when constructing blocks (in the
/// witness commitment tree) since the coinbase transaction contains a commitment to all
/// transactions' wTXIDs but naturally cannot commit to its own.
pub const COINBASE: Self = Self::from_byte_array([0; 32]);
}
/// The transaction version. /// The transaction version.
/// ///
/// Currently, as specified by [BIP-68], only version 1 and 2 are considered standard. /// Currently, as specified by [BIP-68], only version 1 and 2 are considered standard.