Struct bitcoin::blockdata::transaction::Transaction
source · [−]pub struct Transaction {
pub version: i32,
pub lock_time: u32,
pub input: Vec<TxIn>,
pub output: Vec<TxOut>,
}
Expand description
A Bitcoin transaction, which describes an authenticated movement of coins.
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.
Fields
version: i32
The protocol version, is currently expected to be 1 or 2 (BIP 68).
lock_time: u32
Block number before which this transaction is valid, or 0 for valid immediately.
input: Vec<TxIn>
List of transaction inputs.
output: Vec<TxOut>
List of transaction outputs.
Implementations
sourceimpl Transaction
impl Transaction
sourcepub fn ntxid(&self) -> Hash
pub fn 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 txid(&self) -> Txid
pub fn txid(&self) -> Txid
Computes the txid. For non-segwit transactions this will be identical
to the output of wtxid()
, but for segwit transactions,
this will give the correct txid (not including witnesses) while wtxid
will also hash witnesses.
sourcepub fn wtxid(&self) -> Wtxid
pub fn wtxid(&self) -> Wtxid
Computes SegWit-version of the transaction id (wtxid). For transaction with the witness data this hash includes witness, for pre-witness transaction it is equal to the normal value returned by txid() function.
sourcepub fn encode_signing_data_to<Write: Write, U: Into<u32>>(
&self,
writer: Write,
input_index: usize,
script_pubkey: &Script,
sighash_type: U
) -> Result<(), Error>
pub fn encode_signing_data_to<Write: Write, U: Into<u32>>(
&self,
writer: Write,
input_index: usize,
script_pubkey: &Script,
sighash_type: U
) -> Result<(), Error>
Encodes the signing data from which a signature hash for a given input index with a given sighash flag can be computed.
To actually produce a scriptSig, this hash needs to be run through an ECDSA signer, the
EcdsaSigHashType
appended to the resulting sig, and a script written around this, but
this is the general (and hard) part.
The sighash_type
supports an arbitrary u32
value, instead of just EcdsaSigHashType
,
because internally 4 bytes are being hashed, even though only the lowest byte is appended to
signature in a transaction.
Warning
- Does NOT attempt to support OP_CODESEPARATOR. In general this would require evaluating
script_pubkey
to determine which separators get evaluated and which don’t, which we don’t have the information to determine. - Does NOT handle the sighash single bug, you should either handle that manually or use
Self::signature_hash()
instead.
Panics
If input_index
is out of bounds (greater than or equal to self.input.len()
).
sourcepub fn signature_hash(
&self,
input_index: usize,
script_pubkey: &Script,
sighash_u32: u32
) -> SigHash
pub fn signature_hash(
&self,
input_index: usize,
script_pubkey: &Script,
sighash_u32: u32
) -> SigHash
Computes a signature hash for a given input index with a given sighash flag.
To actually produce a scriptSig, this hash needs to be run through an ECDSA signer, the
EcdsaSigHashType
appended to the resulting sig, and a script written around this, but
this is the general (and hard) part.
The sighash_type
supports an arbitrary u32
value, instead of just EcdsaSigHashType
,
because internally 4 bytes are being hashed, even though only the lowest byte is appended to
signature in a transaction.
This function correctly handles the sighash single bug by returning the ‘one array’. The
sighash single bug becomes exploitable when one tries to sign a transaction with
SIGHASH_SINGLE
and there is not a corresponding output with the same index as the input.
Warning
Does NOT attempt to support OP_CODESEPARATOR. In general this would require evaluating
script_pubkey
to determine which separators get evaluated and which don’t, which we don’t
have the information to determine.
Panics
If input_index
is out of bounds (greater than or equal to self.input.len()
).
sourcepub fn get_weight(&self) -> usize
👎 Deprecated since 0.28.0: Please use transaction::weight
instead.
pub fn get_weight(&self) -> usize
Please use transaction::weight
instead.
Returns the “weight” of this transaction, as defined by BIP141.
sourcepub fn weight(&self) -> usize
pub fn weight(&self) -> usize
Returns the “weight” of this transaction, as defined by BIP141.
For transactions with an empty witness, this is simply the consensus-serialized size times four. For transactions with a witness, this is the non-witness consensus-serialized size multiplied by three plus the with-witness consensus-serialized size.
sourcepub fn get_size(&self) -> usize
👎 Deprecated since 0.28.0: Please use transaction::size
instead.
pub fn get_size(&self) -> usize
Please use transaction::size
instead.
Returns the regular byte-wise consensus-serialized size of this transaction.
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the regular byte-wise consensus-serialized size of this transaction.
sourcepub fn get_vsize(&self) -> usize
👎 Deprecated since 0.28.0: Please use transaction::vsize
instead.
pub fn get_vsize(&self) -> usize
Please use transaction::vsize
instead.
Returns the “virtual size” (vsize) of this transaction.
sourcepub fn vsize(&self) -> usize
pub fn vsize(&self) -> usize
Returns the “virtual size” (vsize) of this transaction.
Will be ceil(weight / 4.0)
. Note this implements the virtual size as per BIP141
, which
is different to what is implemented in Bitcoin Core. The computation should be the same for
any remotely sane transaction, and a standardness-rule-correct version is available in the
policy
module.
sourcepub fn get_strippedsize(&self) -> usize
👎 Deprecated since 0.28.0: Please use transaction::strippedsize
instead.
pub fn get_strippedsize(&self) -> usize
Please use transaction::strippedsize
instead.
Returns the size of this transaction excluding the witness data.
sourcepub fn strippedsize(&self) -> usize
pub fn strippedsize(&self) -> usize
Returns the size of this transaction excluding the witness data.
sourcepub fn verify<S>(&self, spent: S) -> Result<(), Error> where
S: FnMut(&OutPoint) -> Option<TxOut>,
This is supported on crate feature bitcoinconsensus
only.
pub fn verify<S>(&self, spent: S) -> Result<(), Error> where
S: FnMut(&OutPoint) -> Option<TxOut>,
bitcoinconsensus
only.Shorthand for Self::verify_with_flags
with flag bitcoinconsensus::VERIFY_ALL
.
sourcepub fn verify_with_flags<S, F>(&self, spent: S, flags: F) -> Result<(), Error> where
S: FnMut(&OutPoint) -> Option<TxOut>,
F: Into<u32>,
This is supported on crate feature bitcoinconsensus
only.
pub fn verify_with_flags<S, F>(&self, spent: S, flags: F) -> Result<(), Error> where
S: FnMut(&OutPoint) -> Option<TxOut>,
F: Into<u32>,
bitcoinconsensus
only.Verify that this transaction is able to spend its inputs.
The spent
closure should not return the same TxOut
twice!
sourcepub fn is_coin_base(&self) -> bool
pub fn is_coin_base(&self) -> bool
Is this a coin base transaction?
sourcepub fn is_explicitly_rbf(&self) -> bool
pub fn is_explicitly_rbf(&self) -> bool
Returns true
if the transaction itself opted in to be BIP-125-replaceable (RBF). This
does not cover the case where a transaction becomes replaceable due to ancestors being
RBF.
Trait Implementations
sourceimpl Clone for Transaction
impl Clone for Transaction
sourcefn clone(&self) -> Transaction
fn clone(&self) -> Transaction
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Transaction
impl Debug for Transaction
sourceimpl Decodable for Transaction
impl Decodable for Transaction
sourcefn consensus_decode<D: Read>(d: D) -> Result<Self, Error>
fn consensus_decode<D: Read>(d: D) -> Result<Self, Error>
Decode an object with a well-defined format
sourceimpl Deserialize for Transaction
impl Deserialize for Transaction
sourceimpl<'de> Deserialize<'de> for Transaction
impl<'de> Deserialize<'de> for Transaction
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Encodable for Transaction
impl Encodable for Transaction
sourceimpl Hash for Transaction
impl Hash for Transaction
sourceimpl Ord for Transaction
impl Ord for Transaction
sourceimpl PartialEq<Transaction> for Transaction
impl PartialEq<Transaction> for Transaction
sourcefn eq(&self, other: &Transaction) -> bool
fn eq(&self, other: &Transaction) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &Transaction) -> bool
fn ne(&self, other: &Transaction) -> bool
This method tests for !=
.
sourceimpl PartialOrd<Transaction> for Transaction
impl PartialOrd<Transaction> for Transaction
sourcefn partial_cmp(&self, other: &Transaction) -> Option<Ordering>
fn partial_cmp(&self, other: &Transaction) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl Serialize for Transaction
impl Serialize for Transaction
sourceimpl Serialize for Transaction
impl Serialize for Transaction
impl Eq for Transaction
impl StructuralEq for Transaction
impl StructuralPartialEq for Transaction
Auto Trait Implementations
impl RefUnwindSafe for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl UnwindSafe for Transaction
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more