pub struct Transaction {
pub version: u32,
pub inputs: Vec<TransactionInput>,
pub outputs: Vec<TransactionOutput>,
pub lock_time: u32,
pub merkle_path: Option<MerklePath>,
}Expand description
BSV transaction type from the SDK. A Bitcoin transaction with inputs, outputs, and optional merkle proof.
Supports standard binary and Extended Format (EF) serialization, BEEF/Atomic BEEF packaging, and BIP-143 sighash preimage computation for signing. Translates the TS SDK Transaction.ts.
Fields§
§version: u32Transaction version number.
inputs: Vec<TransactionInput>Transaction inputs.
outputs: Vec<TransactionOutput>Transaction outputs.
lock_time: u32Lock time.
merkle_path: Option<MerklePath>Merkle path for SPV verification (populated from BEEF).
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn new() -> Transaction
pub fn new() -> Transaction
Create a new empty transaction with default values.
Sourcepub fn from_binary(
reader: &mut impl Read,
) -> Result<Transaction, TransactionError>
pub fn from_binary( reader: &mut impl Read, ) -> Result<Transaction, TransactionError>
Deserialize a transaction from binary wire format.
Sourcepub fn from_hex(hex: &str) -> Result<Transaction, TransactionError>
pub fn from_hex(hex: &str) -> Result<Transaction, TransactionError>
Deserialize a transaction from a hex string.
Sourcepub fn from_beef(beef_hex: &str) -> Result<Transaction, TransactionError>
pub fn from_beef(beef_hex: &str) -> Result<Transaction, TransactionError>
Parse a transaction from a BEEF hex string, returning the subject transaction.
Decodes the hex to bytes, parses the BEEF structure, and extracts the subject transaction (the last tx, or the atomic txid target).
Sourcepub fn to_binary(&self, writer: &mut impl Write) -> Result<(), TransactionError>
pub fn to_binary(&self, writer: &mut impl Write) -> Result<(), TransactionError>
Serialize a transaction to binary wire format.
Sourcepub fn to_hex(&self) -> Result<String, TransactionError>
pub fn to_hex(&self) -> Result<String, TransactionError>
Serialize a transaction to a hex string.
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, TransactionError>
pub fn to_bytes(&self) -> Result<Vec<u8>, TransactionError>
Serialize a transaction to a byte vector.
Sourcepub fn hash(&self) -> Result<[u8; 32], TransactionError>
pub fn hash(&self) -> Result<[u8; 32], TransactionError>
Compute the transaction hash (double SHA-256).
Returns the hash in internal byte order (LE).
Sourcepub fn id(&self) -> Result<String, TransactionError>
pub fn id(&self) -> Result<String, TransactionError>
Compute the transaction ID (hash reversed, hex-encoded).
Returns the txid in display format (BE hex).
Sourcepub fn add_input(&mut self, input: TransactionInput)
pub fn add_input(&mut self, input: TransactionInput)
Add an input to the transaction.
Sourcepub fn add_output(&mut self, output: TransactionOutput)
pub fn add_output(&mut self, output: TransactionOutput)
Add an output to the transaction.
Sourcepub fn from_ef(reader: &mut impl Read) -> Result<Transaction, TransactionError>
pub fn from_ef(reader: &mut impl Read) -> Result<Transaction, TransactionError>
Deserialize a transaction from EF format (BRC-30).
EF format: version(4) + EF_MARKER(6) + inputs_with_source_info + outputs + locktime(4) Each input additionally includes source satoshis (u64 LE) and source locking script.
Sourcepub fn from_hex_ef(hex: &str) -> Result<Transaction, TransactionError>
pub fn from_hex_ef(hex: &str) -> Result<Transaction, TransactionError>
Deserialize a transaction from an EF format hex string.
Sourcepub fn to_ef(&self, writer: &mut impl Write) -> Result<(), TransactionError>
pub fn to_ef(&self, writer: &mut impl Write) -> Result<(), TransactionError>
Serialize a transaction to EF format (BRC-30).
Sourcepub fn to_hex_ef(&self) -> Result<String, TransactionError>
pub fn to_hex_ef(&self) -> Result<String, TransactionError>
Serialize a transaction to an EF format hex string.
Sourcepub fn sighash_preimage(
&self,
input_index: usize,
scope: u32,
source_satoshis: u64,
source_locking_script: &LockingScript,
) -> Result<Vec<u8>, TransactionError>
pub fn sighash_preimage( &self, input_index: usize, scope: u32, source_satoshis: u64, source_locking_script: &LockingScript, ) -> Result<Vec<u8>, TransactionError>
Compute the BIP143/ForkID sighash preimage for the input at input_index.
This is the standard BSV post-fork sighash format. The scope flags
should include SIGHASH_FORKID for normal BSV transactions.
Parameters:
input_index: index of the input being signedscope: sighash flags (e.g., SIGHASH_ALL | SIGHASH_FORKID)source_satoshis: value of the UTXO being spentsource_locking_script: locking script of the UTXO being spent
Sourcepub fn sighash_preimage_legacy(
&self,
input_index: usize,
scope: u32,
sub_script: &[u8],
) -> Result<Vec<u8>, TransactionError>
pub fn sighash_preimage_legacy( &self, input_index: usize, scope: u32, sub_script: &[u8], ) -> Result<Vec<u8>, TransactionError>
Compute the legacy OTDA sighash preimage for the input at input_index.
Used when SIGHASH_FORKID is NOT set (pre-fork transactions or Chronicle mode). This is the original Bitcoin sighash algorithm.
The sub_script is the scriptCode bytes. OP_CODESEPARATOR opcodes will be
stripped automatically before inclusion in the preimage.
Sourcepub fn sign(
&mut self,
input_index: usize,
template: &dyn ScriptTemplateUnlock,
scope: u32,
source_satoshis: u64,
source_locking_script: &LockingScript,
) -> Result<(), TransactionError>
pub fn sign( &mut self, input_index: usize, template: &dyn ScriptTemplateUnlock, scope: u32, source_satoshis: u64, source_locking_script: &LockingScript, ) -> Result<(), TransactionError>
Sign the input at input_index using a ScriptTemplateUnlock implementation.
Computes the sighash preimage (BIP143/ForkID format) and passes it to the template’s sign() method, then sets the resulting unlocking script on the input.
Sourcepub fn sign_all_inputs(
&mut self,
template: &dyn ScriptTemplateUnlock,
scope: u32,
) -> Result<(), TransactionError>
pub fn sign_all_inputs( &mut self, template: &dyn ScriptTemplateUnlock, scope: u32, ) -> Result<(), TransactionError>
Sign all unsigned inputs using the same template.
A convenience method that reduces the per-input signing loop. For each
input that has no unlocking_script yet, this resolves source_satoshis
and source_locking_script from the input’s source_transaction and
signs with the given template and sighash scope.
Inputs that already have an unlocking script are skipped.
Each input must have its source_transaction set so that the source
output’s satoshis and locking script can be resolved. If you need
different templates or scopes per input, use the single-input sign().
Trait Implementations§
Source§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 moreSource§impl Debug for Transaction
impl Debug for Transaction
Source§impl Default for Transaction
impl Default for Transaction
Source§fn default() -> Transaction
fn default() -> Transaction
Auto Trait Implementations§
impl Freeze for Transaction
impl RefUnwindSafe for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl UnsafeUnpin for Transaction
impl UnwindSafe for Transaction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more