smplx_sdk/transaction/utxo.rs
1use simplicityhl::elements::{AssetId, OutPoint, TxOut, TxOutSecrets};
2
3/// Represents an Unspent Transaction Output (UTXO).
4#[derive(Debug, Clone)]
5pub struct UTXO {
6 /// Bounded outpoint for this object
7 pub outpoint: OutPoint,
8 /// Transaction output characteristics
9 pub txout: TxOut,
10 /// Already unblinded transaction output secrets
11 pub secrets: Option<TxOutSecrets>,
12}
13
14impl UTXO {
15 /// Retrieves the explicit `AssetId` from the transaction output (`txout`).
16 ///
17 /// # Panics
18 /// This function will panic if the UTXO's asset is confidential.
19 #[must_use]
20 pub fn explicit_asset(&self) -> AssetId {
21 self.txout.asset.explicit().expect("The UTXO's asset is not explicit")
22 }
23
24 /// Retrieves the explicit amount contained within the transaction output (UTXO).
25 ///
26 /// # Panics
27 /// This function will panic if the UTXO's amount is confidential.
28 #[must_use]
29 pub fn explicit_amount(&self) -> u64 {
30 self.txout.value.explicit().expect("The UTXO's amount is not explicit")
31 }
32
33 /// Retrieves the unblinded `AssetId` of the current UTXO.
34 ///
35 /// # Panics
36 ///
37 /// This function will panic if the UTXO is not blinded. The panic occurs when
38 /// `self.secrets` is `None`, as it expects the UTXO to be in an unblinded state to retrieve the `AssetId`.
39 #[must_use]
40 pub fn unblinded_asset(&self) -> AssetId {
41 self.secrets.expect("The UTXO is not unblinded").asset
42 }
43
44 /// Retrieves the unblinded amount from the UTXO.
45 ///
46 /// # Panics
47 /// This function will panic if the UTXO is not confidential.
48 #[must_use]
49 pub fn unblinded_amount(&self) -> u64 {
50 self.secrets.expect("The UTXO is not unblinded").value
51 }
52
53 /// Retrieves the `AssetId` associated with the instance.
54 #[must_use]
55 pub fn asset(&self) -> AssetId {
56 self.secrets
57 .map_or_else(|| self.explicit_asset(), |secrets| secrets.asset)
58 }
59
60 /// Retrieves the amount associated with the current instance.
61 ///
62 /// This function returns the `value` from the `secrets` field if it exists.
63 /// If no `secrets` are present, it falls back to calculating and returning
64 /// the explicitly defined amount using the `explicit_amount()` method.
65 #[must_use]
66 pub fn amount(&self) -> u64 {
67 self.secrets
68 .map_or_else(|| self.explicit_amount(), |secrets| secrets.value)
69 }
70}