pub struct Transaction {
pub version: Version,
pub lock_time: LockTime,
pub inputs: Vec<TxIn>,
pub outputs: Vec<TxOut>,
}Expand description
Bitcoin transaction.
An authenticated movement of coins.
See Bitcoin Wiki: Transaction for more information.
§Bitcoin Core References
§Serialization notes
If any inputs have nonempty witnesses, the entire transaction is serialized in the post-BIP-0141 SegWit format which includes a list of witnesses. If all inputs have empty witnesses, the transaction is serialized in the pre-BIP-0141 format.
There is one major exception to this: to avoid deserialization ambiguity, if the transaction has no inputs, it is serialized in the BIP-0141 style. Be aware that this differs from the transaction format in PSBT, which never uses BIP-0141. (Ordinarily there is no conflict, since in PSBT transactions are always unsigned and therefore their inputs have empty witnesses.)
The specific ambiguity is that SegWit uses the flag bytes 0001 where an old
serializer would read the number of transaction inputs. The old serializer
would interpret this as “no inputs, one output”, which means the transaction
is invalid, and simply reject it. SegWit further specifies that this encoding
should only be used when some input has a nonempty witness; that is,
witness-less transactions should be encoded in the traditional format.
However, in protocols where transactions may legitimately have 0 inputs, e.g.
when parties are cooperatively funding a transaction, the “00 means SegWit”
heuristic does not work. Since SegWit requires such a transaction to be encoded
in the original transaction format (since it has no inputs and therefore
no input witnesses), a traditionally encoded transaction may have the 0001
SegWit flag in it, which confuses most SegWit parsers including the one in
Bitcoin Core.
We therefore deviate from the spec by always using the SegWit witness encoding for 0-input transactions, which results in unambiguously parseable transactions.
§A note on ordering
This type implements Ord, even though it contains a locktime, which is not
itself Ord. This was done to simplify applications that may need to hold
transactions inside a sorted container. We have ordered the locktimes based
on their representation as a u32, which is not a semantically meaningful
order, and therefore the ordering on Transaction itself is not semantically
meaningful either.
The ordering is, however, consistent with the ordering present in this library before this change, so users should not notice any breakage (here) when transitioning from 0.29 to 0.30.
Fields§
§version: VersionThe protocol version, is currently expected to be 1, 2 (BIP-0068) or 3 (BIP-0431).
lock_time: LockTimeBlock height or timestamp. Transaction cannot be included in a block until this height/time.
§Relevant BIPs
inputs: Vec<TxIn>List of transaction inputs.
outputs: Vec<TxOut>List of transaction outputs.
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub const MAX_STANDARD_WEIGHT: Weight
pub const MAX_STANDARD_WEIGHT: Weight
Maximum transaction weight for Bitcoin Core 25.0.
Sourcepub fn compute_ntxid(&self) -> Ntxid
pub fn compute_ntxid(&self) -> Ntxid
Computes a “normalized TXID” which does not include any signatures.
This function is needed only for legacy (pre-Segwit or P2SH-wrapped segwit version 0)
applications. This method clears the script_sig field of each input, which in Segwit
transactions is already empty, so for Segwit transactions the ntxid will be equal to the
txid, and you should simply use the latter.
This gives a way to identify a transaction that is “the same” as another in the sense of having the same inputs and outputs.
Sourcepub fn compute_txid(&self) -> Txid
pub fn compute_txid(&self) -> Txid
Computes the Txid.
Hashes the transaction excluding the SegWit data (i.e. the marker, flag bytes, and the
witness fields themselves). For non-SegWit transactions which do not have any SegWit data,
this will be equal to Transaction::compute_wtxid().
Sourcepub fn compute_wtxid(&self) -> Wtxid
pub fn compute_wtxid(&self) -> Wtxid
Computes the SegWit version of the transaction id.
Hashes the transaction including all SegWit data (i.e. the marker, flag bytes, and the
witness fields themselves). For non-SegWit transactions which do not have any SegWit data,
this will be equal to Transaction::compute_txid().
Sourcepub fn is_coinbase(&self) -> bool
pub fn is_coinbase(&self) -> bool
Checks if this is a coinbase transaction.
The first transaction in the block distributes the mining reward and is called the coinbase transaction. It is impossible to check if the transaction is first in the block, so this function checks the structure of the transaction instead - the previous output must be all-zeros (creates satoshis “out of thin air”).
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Transaction
Available on crate features arbitrary and alloc only.
impl<'a> Arbitrary<'a> for Transaction
arbitrary and alloc only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Clone for Transaction
impl Clone for Transaction
Source§fn clone(&self) -> Transaction
fn clone(&self) -> Transaction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Transaction
impl Debug for Transaction
Source§impl Decodable for Transaction
Available on crate feature alloc only.
impl Decodable for Transaction
alloc only.Source§impl Display for Transaction
Available on crate features hex and alloc only.
impl Display for Transaction
hex and alloc only.Source§impl Encodable for Transaction
Available on crate feature alloc only.
impl Encodable for Transaction
alloc only.Source§impl From<&Transaction> for Txid
Available on crate feature alloc only.
impl From<&Transaction> for Txid
alloc only.Source§fn from(tx: &Transaction) -> Self
fn from(tx: &Transaction) -> Self
Source§impl From<&Transaction> for Wtxid
Available on crate feature alloc only.
impl From<&Transaction> for Wtxid
alloc only.Source§fn from(tx: &Transaction) -> Self
fn from(tx: &Transaction) -> Self
Source§impl From<Transaction> for Txid
Available on crate feature alloc only.
impl From<Transaction> for Txid
alloc only.Source§fn from(tx: Transaction) -> Self
fn from(tx: Transaction) -> Self
Source§impl From<Transaction> for Wtxid
Available on crate feature alloc only.
impl From<Transaction> for Wtxid
alloc only.Source§fn from(tx: Transaction) -> Self
fn from(tx: Transaction) -> Self
Source§impl FromStr for Transaction
Available on crate features hex and alloc only.
impl FromStr for Transaction
hex and alloc only.Source§impl Hash for Transaction
impl Hash for Transaction
Source§impl LowerHex for Transaction
Available on crate features hex and alloc only.
impl LowerHex for Transaction
hex and alloc only.Source§impl Ord for Transaction
Available on crate feature alloc only.
impl Ord for Transaction
alloc only.Source§impl PartialEq for Transaction
impl PartialEq for Transaction
Source§impl PartialOrd for Transaction
Available on crate feature alloc only.
impl PartialOrd for Transaction
alloc only.Source§impl UpperHex for Transaction
Available on crate features hex and alloc only.
impl UpperHex for Transaction
hex and alloc only.