Skip to main content

smplx_sdk_test/transaction/
change_output.rs

1use elements_miniscript::bitcoin::PublicKey;
2
3use simplicityhl::elements::Script;
4
5/// Where a transaction's change should go, supplied by the caller.
6///
7/// Without this the signer would send change to the single address it derives internally.
8#[derive(Debug, Clone)]
9pub struct ChangeOutput {
10    /// The script the change output pays to.
11    pub script_pubkey: Script,
12    /// The blinding public key, when the change output is confidential.
13    pub blinding_key: Option<PublicKey>,
14}
15
16impl ChangeOutput {
17    /// Creates an explicit (unblinded) change target.
18    #[must_use]
19    pub fn new(script_pubkey: Script) -> Self {
20        Self {
21            script_pubkey,
22            blinding_key: None,
23        }
24    }
25
26    /// Attaches a blinding public key, making the change output confidential.
27    #[must_use]
28    pub fn with_blinding_key(mut self, blinding_key: PublicKey) -> Self {
29        self.blinding_key = Some(blinding_key);
30
31        self
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn change_output_defaults_to_explicit_change() {
41        let change = ChangeOutput::new(Script::new());
42
43        assert!(change.blinding_key.is_none());
44    }
45}