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 inputs

§output: Vec<TxOut>

List of outputs

Implementations§

source§

impl Transaction

source

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.

source

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.

source

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.

source

pub fn malfix_txid(&self) -> Txid

Computes an “immutable TXID”. The double SHA256 taken from a transaction after stripping it of all input scripts including their length prefixes.

source

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 SigHashType appended to the resulting sig, and a script written around this, but this is the general (and hard) part.

Warning This 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

Panics if input_index is greater than or equal to self.input.len()

source

pub fn get_weight(&self) -> usize

Gets the “weight” of this transaction, as defined by BIP141. For transactions with an empty witness, this is simply the consensus-serialized size times 4. For transactions with a witness, this is the non-witness consensus-serialized size multiplied by 3 plus the with-witness consensus-serialized size.

source

pub fn get_size(&self) -> usize

Gets the regular byte-wise consensus-serialized size of this transaction.

source

pub fn is_coin_base(&self) -> bool

Is this a coin base transaction?

In Tapyrus, a coinbase transaction has a default txid (“0000000000000000000000000000000000000000000000000000000000000000”) in its input. See https://github.com/chaintope/tapyrus-core/blob/master/doc/tapyrus/fix_transaction_malleability.md#tapyrus-transaction-structure for more information.

Trait Implementations§

source§

impl Clone for Transaction

source§

fn clone(&self) -> Transaction

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Transaction

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Decodable for Transaction

source§

fn consensus_decode<D: Read>(d: D) -> Result<Self, Error>

Decode an object with a well-defined format
source§

impl Deserialize for Transaction

source§

fn deserialize(bytes: &[u8]) -> Result<Self, Error>

Deserialize a value from raw data.
source§

impl Encodable for Transaction

source§

fn consensus_encode<S: Write>(&self, s: S) -> Result<usize, Error>

Encode an object with a well-defined format, should only ever error if the underlying Write errors. Returns the number of bytes written on success
source§

impl Hash for Transaction

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Transaction

source§

fn eq(&self, other: &Transaction) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Transaction

source§

fn serialize(&self) -> Vec<u8>

Serialize a value as raw data.
source§

impl Eq for Transaction

source§

impl StructuralEq for Transaction

source§

impl StructuralPartialEq for Transaction

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dstwhere T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,

Casts the value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,

Casts the value.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dstwhere T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dstwhere T: WrappingCast<Dst>,

Casts the value.