Skip to main content

pczt/roles/updater/
orchard.rs

1use orchard::pczt::{Updater, UpdaterError};
2
3use crate::{Pczt, common::AnchorRequirement, orchard::ParseError};
4
5impl super::Updater {
6    /// Updates the Orchard bundle with information in the given closure.
7    pub fn update_orchard_with<F>(self, f: F) -> Result<Self, OrchardError>
8    where
9        F: FnOnce(Updater<'_>) -> Result<(), UpdaterError>,
10    {
11        let Pczt {
12            global,
13            transparent,
14            sapling,
15            orchard,
16            ironwood,
17        } = self.pczt;
18        let anchor_requirement = AnchorRequirement::for_pre_authorization(global.tx_version);
19
20        let mut parsed = orchard
21            .into_parsed_with_version(
22                crate::orchard::orchard_bundle_version(&global)
23                    .ok_or(OrchardError::UnsupportedConsensusBranchId)?,
24                anchor_requirement,
25            )
26            .map_err(OrchardError::Parser)?;
27
28        parsed
29            .bundle
30            .update_with(f)
31            .map_err(OrchardError::Updater)?;
32
33        Ok(Self {
34            pczt: Pczt {
35                global,
36                transparent,
37                sapling,
38                orchard: parsed.reserialize(),
39                ironwood,
40            },
41        })
42    }
43
44    /// Updates the Ironwood bundle with information in the given closure.
45    pub fn update_ironwood_with<F>(self, f: F) -> Result<Self, OrchardError>
46    where
47        F: FnOnce(Updater<'_>) -> Result<(), UpdaterError>,
48    {
49        let Pczt {
50            global,
51            transparent,
52            sapling,
53            orchard,
54            ironwood,
55        } = self.pczt;
56        let anchor_requirement = AnchorRequirement::for_pre_authorization(global.tx_version);
57
58        let mut parsed = ironwood
59            .into_ironwood_parsed(anchor_requirement)
60            .map_err(OrchardError::Parser)?;
61
62        parsed
63            .bundle
64            .update_with(f)
65            .map_err(OrchardError::Updater)?;
66
67        Ok(Self {
68            pczt: Pczt {
69                global,
70                transparent,
71                sapling,
72                orchard,
73                ironwood: parsed.reserialize(),
74            },
75        })
76    }
77}
78
79/// Errors that can occur while updating the Orchard bundle of a PCZT.
80#[derive(Debug)]
81pub enum OrchardError {
82    Parser(ParseError),
83    /// The PCZT's consensus branch ID is unrecognized, or predates NU5 (under which
84    /// the Orchard protocol is not supported).
85    UnsupportedConsensusBranchId,
86    Updater(UpdaterError),
87}