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,
    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: u32

Transaction version number.

§inputs: Vec<TransactionInput>

Transaction inputs.

§outputs: Vec<TransactionOutput>

Transaction outputs.

§lock_time: u32

Lock time.

§merkle_path: Option<MerklePath>

Merkle path for SPV verification (populated from BEEF).

Implementations§

Source§

impl Transaction

Source

pub fn new() -> Transaction

Create a new empty transaction with default values.

Source

pub fn from_binary( reader: &mut impl Read, ) -> Result<Transaction, TransactionError>

Deserialize a transaction from binary wire format.

Source

pub fn from_hex(hex: &str) -> Result<Transaction, TransactionError>

Deserialize a transaction from a hex string.

Source

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).

Source

pub fn to_binary(&self, writer: &mut impl Write) -> Result<(), TransactionError>

Serialize a transaction to binary wire format.

Source

pub fn to_hex(&self) -> Result<String, TransactionError>

Serialize a transaction to a hex string.

Source

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

Serialize a transaction to a byte vector.

Source

pub fn hash(&self) -> Result<[u8; 32], TransactionError>

Compute the transaction hash (double SHA-256).

Returns the hash in internal byte order (LE).

Source

pub fn id(&self) -> Result<String, TransactionError>

Compute the transaction ID (hash reversed, hex-encoded).

Returns the txid in display format (BE hex).

Source

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

Add an input to the transaction.

Source

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

Add an output to the transaction.

Source

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.

Source

pub fn from_hex_ef(hex: &str) -> Result<Transaction, TransactionError>

Deserialize a transaction from an EF format hex string.

Source

pub fn to_ef(&self, writer: &mut impl Write) -> Result<(), TransactionError>

Serialize a transaction to EF format (BRC-30).

Source

pub fn to_hex_ef(&self) -> Result<String, TransactionError>

Serialize a transaction to an EF format hex string.

Source

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 signed
  • scope: sighash flags (e.g., SIGHASH_ALL | SIGHASH_FORKID)
  • source_satoshis: value of the UTXO being spent
  • source_locking_script: locking script of the UTXO being spent
Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> Transaction

Returns a duplicate 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<(), Error>

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

impl Default for Transaction

Source§

fn default() -> Transaction

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

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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, 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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more