clementine_core/builder/transaction/
output.rs

1//! # Transaction Output Types and Utilities
2//!
3//! This module defines types and utilities for representing and handling transaction outputs used in [`super::TxHandler`].
4//! Main purpose of it is to store the scripts used in the taproot outputs.
5//!
6
7use crate::builder::address::create_taproot_address;
8use crate::builder::script::SpendableScript;
9use bitcoin::{taproot::TaprootSpendInfo, Amount, ScriptBuf, TxOut, XOnlyPublicKey};
10use std::sync::Arc;
11
12#[derive(Debug, Clone)]
13/// Represents a spendable transaction output, including scripts and optional Taproot spend info.
14pub struct UnspentTxOut {
15    txout: TxOut,
16    scripts: Vec<Arc<dyn SpendableScript>>,
17    spendinfo: Option<TaprootSpendInfo>,
18}
19
20impl UnspentTxOut {
21    /// Constructs an [`UnspentTxOut`] from a partial [`TxOut`] (no scripts or spend info).
22    ///
23    /// # Arguments
24    /// * `txout` - The Bitcoin transaction output.
25    ///
26    /// # Returns
27    /// An [`UnspentTxOut`] with no scripts or spend info.
28    pub fn from_partial(txout: TxOut) -> UnspentTxOut {
29        UnspentTxOut {
30            txout,
31            scripts: vec![],
32            spendinfo: None,
33        }
34    }
35    /// Constructs an [`UnspentTxOut`] from all fields.
36    ///
37    /// # Arguments
38    /// * `txout` - The Bitcoin transaction output.
39    /// * `scripts` - Scripts associated with this output (for script path spends).
40    /// * `spendinfo` - Optional Taproot spend info for this output.
41    ///
42    /// # Returns
43    /// An [`UnspentTxOut`] with the specified parameters.
44    pub fn new(
45        txout: TxOut,
46        scripts: Vec<Arc<dyn SpendableScript>>,
47        spendinfo: Option<TaprootSpendInfo>,
48    ) -> UnspentTxOut {
49        UnspentTxOut {
50            txout,
51            scripts,
52            spendinfo,
53        }
54    }
55
56    /// Constructs an [`UnspentTxOut`] from value, scripts, and key path.
57    ///
58    /// # Arguments
59    /// * `value` - The output value.
60    /// * `scripts` - Scripts for script path spends.
61    /// * `key_path` - The internal key for key path spends.
62    /// * `network` - Bitcoin network.
63    ///
64    /// # Returns
65    /// An [`UnspentTxOut`] with the specified parameters and Taproot spend info if applicable.
66    pub fn from_scripts(
67        value: Amount,
68        scripts: Vec<Arc<dyn SpendableScript>>,
69        key_path: Option<XOnlyPublicKey>,
70        network: bitcoin::Network,
71    ) -> UnspentTxOut {
72        let script_bufs: Vec<ScriptBuf> = scripts
73            .iter()
74            .map(|script| script.clone().to_script_buf())
75            .collect();
76        let (addr, spend_info) = create_taproot_address(&script_bufs, key_path, network);
77        Self::new(
78            TxOut {
79                value,
80                script_pubkey: addr.script_pubkey(),
81            },
82            scripts,
83            Some(spend_info),
84        )
85    }
86
87    /// Returns a reference to the underlying [`TxOut`].
88    pub fn txout(&self) -> &TxOut {
89        &self.txout
90    }
91
92    /// Returns a reference to the scripts for this output.
93    pub fn scripts(&self) -> &Vec<Arc<dyn SpendableScript>> {
94        &self.scripts
95    }
96
97    /// Returns a reference to the Taproot spend info for this output, if any.
98    pub fn spendinfo(&self) -> &Option<TaprootSpendInfo> {
99        &self.spendinfo
100    }
101
102    /// Sets the Taproot spend info for this output.
103    ///
104    /// # Arguments
105    /// * `spendinfo` - The Taproot spend info to set.
106    pub fn set_spendinfo(&mut self, spendinfo: Option<TaprootSpendInfo>) {
107        self.spendinfo = spendinfo;
108    }
109}