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
| Field | Size |
|---|---|
| version | 4 bytes (LE) |
| input count | VarInt |
| inputs | variable (per input) |
| output count | VarInt |
| outputs | variable (per output) |
| lock_time | 4 bytes (LE) |
Fields§
§version: u32Transaction format version. Currently 1 or 2.
inputs: Vec<TransactionInput>Ordered list of transaction inputs.
outputs: Vec<TransactionOutput>Ordered list of transaction outputs.
lock_time: u32Lock time. If non-zero, the transaction is not valid until the specified block height or Unix timestamp.
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty transaction with version 1 and lock time 0.
§Returns
A Transaction with no inputs or outputs.
Sourcepub fn from_hex(hex_str: &str) -> Result<Self, TransactionError>
pub fn from_hex(hex_str: &str) -> Result<Self, TransactionError>
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, TransactionError>
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.
Sourcepub fn read_from(reader: &mut BsvReader<'_>) -> Result<Self, TransactionError>
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.
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
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).
Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Serialize this transaction to a hex string.
§Returns
A lowercase hex-encoded string of the raw bytes.
Sourcepub fn tx_id(&self) -> [u8; 32]
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.
Sourcepub fn tx_id_hex(&self) -> String
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.
Sourcepub fn add_input(&mut self, input: TransactionInput)
pub fn add_input(&mut self, input: TransactionInput)
Sourcepub fn input_count(&self) -> usize
pub fn input_count(&self) -> usize
Sourcepub fn add_output(&mut self, output: TransactionOutput)
pub fn add_output(&mut self, output: TransactionOutput)
Sourcepub fn output_count(&self) -> usize
pub fn output_count(&self) -> usize
Sourcepub fn total_output_satoshis(&self) -> u64
pub fn total_output_satoshis(&self) -> u64
Sourcepub fn total_input_satoshis(&self) -> Result<u64, TransactionError>
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.
Sourcepub fn is_coinbase(&self) -> bool
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.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Return the size of this transaction in bytes.
§Returns
The byte length of the serialized transaction.
Sourcepub fn add_input_from(
&mut self,
prev_tx_id: &str,
vout: u32,
prev_locking_script_hex: &str,
satoshis: u64,
) -> Result<(), TransactionError>
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.
Sourcepub fn calc_input_signature_hash(
&self,
input_index: usize,
sighash_flag: u32,
) -> Result<[u8; 32], TransactionError>
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
impl Clone for Transaction
Source§fn clone(&self) -> Transaction
fn clone(&self) -> Transaction
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more