pub struct Transaction {
pub version: Version,
pub lock_time: LockTime,
pub input: Vec<TxIn>,
pub output: Vec<TxOut>,
}
alloc
only.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-BIP141 Segwit format which includes a list of witnesses. If all inputs have empty witnesses, the transaction is serialized in the pre-BIP141 format.
There is one major exception to this: to avoid deserialization ambiguity, if the transaction has no inputs, it is serialized in the BIP141 style. Be aware that this differs from the transaction format in PSBT, which never uses BIP141. (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 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: Version
The protocol version, is currently expected to be 1, 2 (BIP 68) or 3 (BIP 431).
lock_time: LockTime
Block height or timestamp. Transaction cannot be included in a block until this height/time.
§Relevant BIPs
input: Vec<TxIn>
List of transaction inputs.
output: 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 inputs_mut(&mut self) -> &mut [TxIn]
pub fn inputs_mut(&mut self) -> &mut [TxIn]
Returns a mutable reference to the transaction inputs.
Sourcepub fn outputs_mut(&mut self) -> &mut [TxOut]
pub fn outputs_mut(&mut self) -> &mut [TxOut]
Returns a mutable reference to the transaction outputs.
Sourcepub fn compute_ntxid(&self) -> Hash
pub fn compute_ntxid(&self) -> Hash
Computes a “normalized TXID” which does not include any signatures.
This gives a way to identify a transaction that is “the same” as another in the sense of having 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()
.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Transaction
Available on crate feature arbitrary
only.
impl<'a> Arbitrary<'a> for Transaction
arbitrary
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 more