Skip to main content

pczt/roles/spend_finalizer/
mod.rs

1//! The Spend Finalizer role (anyone can execute).
2//!
3//! - Combines partial transparent signatures into `script_sig`s.
4
5use ::transparent::sighash::SighashPolicy;
6
7use crate::Pczt;
8
9pub struct SpendFinalizer {
10    pczt: Pczt,
11    sighash_policy: SighashPolicy,
12}
13
14impl SpendFinalizer {
15    /// Instantiates the Spend Finalizer role with the given PCZT.
16    pub fn new(pczt: Pczt) -> Self {
17        Self {
18            pczt,
19            sighash_policy: SighashPolicy::ALL_ONLY,
20        }
21    }
22
23    /// Sets the sighash policy that this Spend Finalizer applies to partial signatures.
24    ///
25    /// The default is [`SighashPolicy::ALL_ONLY`]; see [`SighashPolicy`] for why.
26    ///
27    /// This policy governs which signatures are assembled into `script_sig`s; it does not
28    /// protect whoever produced them. A partial signature that does not commit to the
29    /// whole transaction can be lifted out of the PCZT and used directly, without this
30    /// role ever running, so the decision that protects a signer is the Signer's own
31    /// [`Signer::with_transparent_sighash_policy`].
32    ///
33    /// [`Signer::with_transparent_sighash_policy`]: crate::roles::signer::Signer::with_transparent_sighash_policy
34    pub fn with_sighash_policy(mut self, sighash_policy: SighashPolicy) -> Self {
35        self.sighash_policy = sighash_policy;
36        self
37    }
38
39    /// Finalizes the spends of the PCZT.
40    pub fn finalize_spends(self) -> Result<Pczt, Error> {
41        let Self {
42            pczt,
43            sighash_policy,
44        } = self;
45
46        let Pczt {
47            global,
48            transparent,
49            sapling,
50            orchard,
51            ironwood,
52        } = pczt;
53
54        let mut transparent = transparent.into_parsed().map_err(Error::TransparentParse)?;
55
56        transparent
57            .finalize_spends_with_sighash_policy(sighash_policy)
58            .map_err(Error::TransparentFinalize)?;
59
60        Ok(Pczt {
61            global,
62            transparent: crate::transparent::Bundle::serialize_from(transparent),
63            sapling,
64            orchard,
65            ironwood,
66        })
67    }
68}
69
70/// Errors that can occur while finalizing the spends of a PCZT.
71#[derive(Debug)]
72pub enum Error {
73    TransparentFinalize(transparent::pczt::SpendFinalizerError),
74    TransparentParse(transparent::pczt::ParseError),
75}