Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction {
    pub version: u32,
    pub inputs: Vec<TransactionInput>,
    pub outputs: Vec<TransactionOutput>,
    pub lock_time: u32,
}
Expand description

A BSV transaction consisting of a version, a set of inputs, a set of outputs, and a lock time.

§Wire format

FieldSize
version4 bytes (LE)
input countVarInt
inputsvariable (per input)
output countVarInt
outputsvariable (per output)
lock_time4 bytes (LE)

Fields§

§version: u32

Transaction format version. Currently 1 or 2.

§inputs: Vec<TransactionInput>

Ordered list of transaction inputs.

§outputs: Vec<TransactionOutput>

Ordered list of transaction outputs.

§lock_time: u32

Lock time. If non-zero, the transaction is not valid until the specified block height or Unix timestamp.

Implementations§

Source§

impl Transaction

Source

pub fn new() -> Self

Create a new empty transaction with version 1 and lock time 0.

§Returns

A Transaction with no inputs or outputs.

Source

pub fn from_hex(hex_str: &str) -> Result<Self, TransactionError>

Parse a transaction from a hex-encoded string.

§Arguments
  • hex_str - A hex string of the raw transaction bytes.
§Returns

Ok(Transaction) on success, or a TransactionError if the hex is invalid or the bytes do not form a valid transaction.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, TransactionError>

Parse a transaction from raw bytes.

This method requires the byte slice to contain exactly one complete transaction with no trailing data.

§Arguments
  • bytes - The raw transaction bytes.
§Returns

Ok(Transaction) on success, or a TransactionError if the data is truncated, malformed, or has trailing bytes.

Source

pub fn read_from(reader: &mut BsvReader<'_>) -> Result<Self, TransactionError>

Deserialize a transaction from a BsvReader.

Reads the version, input count, inputs, output count, outputs, and lock time in standard Bitcoin wire format.

§Arguments
  • reader - The reader positioned at the start of a serialized transaction.
§Returns

Ok(Transaction) on success, or a TransactionError on I/O or format errors.

Source

pub fn to_bytes(&self) -> Vec<u8>

Serialize this transaction to raw bytes.

§Returns

A Vec<u8> containing the standard wire-format bytes: version(4) + varint(n_in) + inputs + varint(n_out) + outputs + locktime(4).

Source

pub fn to_hex(&self) -> String

Serialize this transaction to a hex string.

§Returns

A lowercase hex-encoded string of the raw bytes.

Source

pub fn tx_id(&self) -> [u8; 32]

Compute the transaction ID (double SHA-256 of serialized bytes).

The txid bytes are in internal (little-endian) order. To get the conventional display string, use tx_id_hex().

§Returns

A 32-byte array containing the txid in internal byte order.

Source

pub fn tx_id_hex(&self) -> String

Compute the transaction ID as a human-readable hex string.

The hex string is byte-reversed from the internal hash, following Bitcoin’s convention where txids are displayed in big-endian order.

§Returns

A 64-character hex string of the txid.

Source

pub fn add_input(&mut self, input: TransactionInput)

Append a TransactionInput to this transaction.

§Arguments
  • input - The input to add.
Source

pub fn input_count(&self) -> usize

Return the number of inputs in the transaction.

§Returns

The input count.

Source

pub fn add_output(&mut self, output: TransactionOutput)

Append a TransactionOutput to this transaction.

§Arguments
  • output - The output to add.
Source

pub fn output_count(&self) -> usize

Return the number of outputs in the transaction.

§Returns

The output count.

Source

pub fn total_output_satoshis(&self) -> u64

Compute the sum of all output satoshi values.

§Returns

The total satoshis across all outputs.

Source

pub fn total_input_satoshis(&self) -> Result<u64, TransactionError>

Compute the sum of all input satoshi values from their source outputs.

Returns an error if any input does not have its source transaction set.

§Returns

Ok(total) with the sum of input satoshis, or an error if a source transaction is missing.

Source

pub fn is_coinbase(&self) -> bool

Determine whether this transaction is a coinbase transaction.

A coinbase transaction has exactly one input with an all-zero txid and either source_tx_out_index == 0xFFFFFFFF or sequence_number == 0xFFFFFFFF.

§Returns

true if this is a coinbase transaction.

Source

pub fn size(&self) -> usize

Return the size of this transaction in bytes.

§Returns

The byte length of the serialized transaction.

Source

pub fn add_input_from( &mut self, prev_tx_id: &str, vout: u32, prev_locking_script_hex: &str, satoshis: u64, ) -> Result<(), TransactionError>

Add an input from UTXO information.

Creates a new input referencing the given previous transaction output and stores the locking script and satoshi value for sighash computation during signing.

Matches the Go SDK’s Transaction.AddInputFrom(prevTxID, vout, prevTxLockingScript, satoshis, ...).

§Arguments
  • prev_tx_id - The hex txid of the previous transaction (display order).
  • vout - The output index being spent.
  • prev_locking_script_hex - Hex-encoded locking script of the previous output.
  • satoshis - The satoshi value of the previous output.
§Returns

Ok(()) on success, or a TransactionError if any hex is invalid.

Source

pub fn calc_input_signature_hash( &self, input_index: usize, sighash_flag: u32, ) -> Result<[u8; 32], TransactionError>

Compute the BIP-143-style signature hash for a given input.

Looks up the source output’s locking script and satoshi value from the input’s stored source info, then delegates to sighash::signature_hash.

Matches the Go SDK’s Transaction.CalcInputSignatureHash(inputNumber, sigHashFlag).

§Arguments
  • input_index - Index of the input being signed.
  • sighash_flag - The combined sighash flags (e.g. SIGHASH_ALL_FORKID).
§Returns

A 32-byte double-SHA256 hash to be signed by ECDSA.

Trait Implementations§

Source§

impl Clone for Transaction

Source§

fn clone(&self) -> Transaction

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Default for Transaction

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Transaction

Source§

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

Display the transaction as its hex-encoded serialization.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V