Skip to main content

smplx_sdk/transaction/
partial_output.rs

1use elements_miniscript::bitcoin::PublicKey;
2
3use simplicityhl::elements::pset::Output;
4use simplicityhl::elements::{AssetId, Script};
5
6/// Represents partially prepared output data for Elements transactions.
7#[derive(Debug, Clone)]
8pub struct PartialOutput {
9    /// Represents a bound `ScriptPubKey` for arbitrary output.
10    pub script_pubkey: Script,
11    /// Amount of a certain transaction output
12    pub amount: u64,
13    /// Amount of a certain transaction output
14    pub asset: AssetId,
15    /// Public key of a blinding key
16    pub blinding_key: Option<PublicKey>,
17}
18
19impl PartialOutput {
20    /// Creates a new `PartialOutput` assigning a base script, amount, and `AssetId`.
21    #[must_use]
22    pub fn new(script: Script, amount: u64, asset: AssetId) -> Self {
23        Self {
24            script_pubkey: script,
25            amount,
26            asset,
27            blinding_key: None,
28        }
29    }
30
31    /// Creates a new `PartialOutput` representing an `OP_RETURN` data metadata output.
32    #[must_use]
33    pub fn new_metadata(data: &[u8]) -> Self {
34        Self {
35            script_pubkey: Script::new_op_return(data),
36            amount: 0,
37            asset: AssetId::default(),
38            blinding_key: None,
39        }
40    }
41
42    /// Attaches an optional blinding public key to the partial output.
43    #[must_use]
44    pub fn with_blinding_key(mut self, blinding_key: PublicKey) -> Self {
45        self.blinding_key = Some(blinding_key);
46
47        self
48    }
49
50    /// Converts this `PartialOutput` into a fully formed PSET `Output`.
51    #[must_use]
52    pub fn to_output(&self) -> Output {
53        let mut output = Output::new_explicit(self.script_pubkey.clone(), self.amount, self.asset, self.blinding_key);
54
55        // the index doesn't really matter as we are the only signer
56        if self.blinding_key.is_some() {
57            output.blinder_index = Some(0);
58        }
59
60        output
61    }
62}