From c48d9d6523425aad1e7e4bc9b62492ad04a26a66 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 3 Sep 2024 11:01:29 +1000 Subject: [PATCH] Move transaction::Version to primitives We would like to move the `Transaction` type to `primitives`, as a step towards this move the `transaction::Version` and its trait imps (just `Display`) over there. --- bitcoin/src/blockdata/transaction.rs | 29 +++------------------ primitives/src/lib.rs | 1 + primitives/src/transaction.rs | 38 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 primitives/src/transaction.rs diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index d77022a99..ef84cb67c 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -31,6 +31,10 @@ use crate::sighash::{EcdsaSighashType, TapSighashType}; use crate::witness::Witness; use crate::{Amount, FeeRate, SignedAmount, VarInt}; +#[rustfmt::skip] // Keep public re-exports separate. +#[doc(inline)] +pub use primitives::transaction::*; + hashes::hash_newtype! { /// A bitcoin transaction hash/transaction ID. /// @@ -925,27 +929,6 @@ impl std::error::Error for IndexOutOfBoundsError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } } -/// The transaction version. -/// -/// Currently, as specified by [BIP-68], only version 1 and 2 are considered standard. -/// -/// Standardness of the inner `i32` is not an invariant because you are free to create transactions -/// of any version, transactions with non-standard version numbers will not be relayed by the -/// Bitcoin network. -/// -/// [BIP-68]: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki -#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Version(pub i32); - -impl Version { - /// The original Bitcoin transaction version (pre-BIP-68). - pub const ONE: Self = Self(1); - - /// The second Bitcoin transaction version (post-BIP-68). - pub const TWO: Self = Self(2); -} - crate::internal_macros::define_extension_trait! { /// Extension functionality for the [`Version`] type. pub trait VersionExt impl for Version { @@ -969,10 +952,6 @@ impl Decodable for Version { } } -impl fmt::Display for Version { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) } -} - impl_consensus_encoding!(TxOut, value, script_pubkey); impl Encodable for OutPoint { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 3b90a8507..9e27c72fb 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -33,6 +33,7 @@ pub mod locktime; pub mod opcodes; pub mod pow; pub mod sequence; +pub mod transaction; #[doc(inline)] pub use units::*; diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs new file mode 100644 index 000000000..7b0701002 --- /dev/null +++ b/primitives/src/transaction.rs @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! Bitcoin transactions. +//! +//! A transaction describes a transfer of money. It consumes previously-unspent +//! transaction outputs and produces new ones, satisfying the condition to spend +//! the old outputs (typically a digital signature with a specific key must be +//! provided) and defining the condition to spend the new ones. The use of digital +//! signatures ensures that coins cannot be spent by unauthorized parties. +//! +//! This module provides the structures and functions needed to support transactions. + +use core::fmt; + +/// The transaction version. +/// +/// Currently, as specified by [BIP-68], only version 1 and 2 are considered standard. +/// +/// Standardness of the inner `i32` is not an invariant because you are free to create transactions +/// of any version, transactions with non-standard version numbers will not be relayed by the +/// Bitcoin network. +/// +/// [BIP-68]: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki +#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct Version(pub i32); + +impl Version { + /// The original Bitcoin transaction version (pre-BIP-68). + pub const ONE: Self = Self(1); + + /// The second Bitcoin transaction version (post-BIP-68). + pub const TWO: Self = Self(2); +} + +impl fmt::Display for Version { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) } +}