Skip to main content

pczt/roles/redactor/
transparent.rs

1use crate::transparent::{Bundle, Input, Output};
2
3impl super::Redactor {
4    /// Redacts the transparent bundle with the given closure.
5    pub fn redact_transparent_with<F>(mut self, f: F) -> Self
6    where
7        F: FnOnce(TransparentRedactor<'_>),
8    {
9        f(TransparentRedactor(&mut self.pczt.transparent));
10        self
11    }
12}
13
14/// A Redactor for the transparent bundle.
15pub struct TransparentRedactor<'a>(&'a mut Bundle);
16
17impl TransparentRedactor<'_> {
18    /// Redacts all inputs in the same way.
19    pub fn redact_inputs<F>(&mut self, f: F)
20    where
21        F: FnOnce(InputRedactor<'_>),
22    {
23        f(InputRedactor(Inputs::All(&mut self.0.inputs)));
24    }
25
26    /// Redacts the input at the given index.
27    ///
28    /// Does nothing if the index is out of range.
29    pub fn redact_input<F>(&mut self, index: usize, f: F)
30    where
31        F: FnOnce(InputRedactor<'_>),
32    {
33        if let Some(input) = self.0.inputs.get_mut(index) {
34            f(InputRedactor(Inputs::One(input)));
35        }
36    }
37
38    /// Redacts all outputs in the same way.
39    pub fn redact_outputs<F>(&mut self, f: F)
40    where
41        F: FnOnce(OutputRedactor<'_>),
42    {
43        f(OutputRedactor(Outputs::All(&mut self.0.outputs)));
44    }
45
46    /// Redacts the output at the given index.
47    ///
48    /// Does nothing if the index is out of range.
49    pub fn redact_output<F>(&mut self, index: usize, f: F)
50    where
51        F: FnOnce(OutputRedactor<'_>),
52    {
53        if let Some(output) = self.0.outputs.get_mut(index) {
54            f(OutputRedactor(Outputs::One(output)));
55        }
56    }
57}
58
59/// A Redactor for transparent inputs.
60pub struct InputRedactor<'a>(Inputs<'a>);
61
62enum Inputs<'a> {
63    All(&'a mut [Input]),
64    One(&'a mut Input),
65}
66
67impl InputRedactor<'_> {
68    fn redact<F>(&mut self, f: F)
69    where
70        F: Fn(&mut Input),
71    {
72        match &mut self.0 {
73            Inputs::All(inputs) => {
74                for input in inputs.iter_mut() {
75                    f(input);
76                }
77            }
78            Inputs::One(input) => {
79                f(input);
80            }
81        }
82    }
83
84    /// Removes the `script_sig`.
85    pub fn clear_script_sig(&mut self) {
86        self.redact(|input| {
87            input.script_sig = None;
88        });
89    }
90
91    /// Removes the `redeem_script`.
92    pub fn clear_redeem_script(&mut self) {
93        self.redact(|input| {
94            input.redeem_script = None;
95        });
96    }
97
98    /// Redacts the signature for the given pubkey.
99    pub fn redact_partial_signature(&mut self, pubkey: [u8; 33]) {
100        self.redact(|input| {
101            input.partial_signatures.remove(&pubkey);
102        });
103    }
104
105    /// Removes all signatures.
106    pub fn clear_partial_signatures(&mut self) {
107        self.redact(|input| {
108            input.partial_signatures.clear();
109        });
110    }
111
112    /// Redacts the BIP 32 derivation path for the given pubkey.
113    pub fn redact_bip32_derivation(&mut self, pubkey: [u8; 33]) {
114        self.redact(|input| {
115            input.bip32_derivation.remove(&pubkey);
116        });
117    }
118
119    /// Removes all BIP 32 derivation paths.
120    pub fn clear_bip32_derivation(&mut self) {
121        self.redact(|input| {
122            input.bip32_derivation.clear();
123        });
124    }
125
126    /// Redacts the RIPEMD160 preimage for the given hash.
127    pub fn redact_ripemd160_preimage(&mut self, hash: [u8; 20]) {
128        self.redact(|input| {
129            input.ripemd160_preimages.remove(&hash);
130        });
131    }
132
133    /// Removes all RIPEMD160 preimages.
134    pub fn clear_ripemd160_preimages(&mut self) {
135        self.redact(|input| {
136            input.ripemd160_preimages.clear();
137        });
138    }
139
140    /// Redacts the SHA256 preimage for the given hash.
141    pub fn redact_sha256_preimage(&mut self, hash: [u8; 32]) {
142        self.redact(|input| {
143            input.sha256_preimages.remove(&hash);
144        });
145    }
146
147    /// Removes all SHA256 preimages.
148    pub fn clear_sha256_preimages(&mut self) {
149        self.redact(|input| {
150            input.sha256_preimages.clear();
151        });
152    }
153
154    /// Redacts the HASH160 preimage for the given hash.
155    pub fn redact_hash160_preimage(&mut self, hash: [u8; 20]) {
156        self.redact(|input| {
157            input.hash160_preimages.remove(&hash);
158        });
159    }
160
161    /// Removes all HASH160 preimages.
162    pub fn clear_hash160_preimages(&mut self) {
163        self.redact(|input| {
164            input.hash160_preimages.clear();
165        });
166    }
167
168    /// Redacts the HASH256 preimage for the given hash.
169    pub fn redact_hash256_preimage(&mut self, hash: [u8; 32]) {
170        self.redact(|input| {
171            input.hash256_preimages.remove(&hash);
172        });
173    }
174
175    /// Removes all HASH256 preimages.
176    pub fn clear_hash256_preimages(&mut self) {
177        self.redact(|input| {
178            input.hash256_preimages.clear();
179        });
180    }
181
182    /// Redacts the proprietary value at the given key.
183    pub fn redact_proprietary(&mut self, key: &str) {
184        self.redact(|input| {
185            input.proprietary.remove(key);
186        });
187    }
188
189    /// Removes all proprietary values.
190    pub fn clear_proprietary(&mut self) {
191        self.redact(|input| {
192            input.proprietary.clear();
193        });
194    }
195}
196
197/// A Redactor for transparent outputs.
198pub struct OutputRedactor<'a>(Outputs<'a>);
199
200enum Outputs<'a> {
201    All(&'a mut [Output]),
202    One(&'a mut Output),
203}
204
205impl OutputRedactor<'_> {
206    fn redact<F>(&mut self, f: F)
207    where
208        F: Fn(&mut Output),
209    {
210        match &mut self.0 {
211            Outputs::All(outputs) => {
212                for output in outputs.iter_mut() {
213                    f(output);
214                }
215            }
216            Outputs::One(output) => {
217                f(output);
218            }
219        }
220    }
221
222    /// Removes the `redeem_script`.
223    pub fn clear_redeem_script(&mut self) {
224        self.redact(|output| {
225            output.redeem_script = None;
226        });
227    }
228
229    /// Redacts the BIP 32 derivation path for the given pubkey.
230    pub fn redact_bip32_derivation(&mut self, pubkey: [u8; 33]) {
231        self.redact(|output| {
232            output.bip32_derivation.remove(&pubkey);
233        });
234    }
235
236    /// Removes all BIP 32 derivation paths.
237    pub fn clear_bip32_derivation(&mut self) {
238        self.redact(|output| {
239            output.bip32_derivation.clear();
240        });
241    }
242
243    /// Removes the user-facing address to which this output is being sent, if any.
244    pub fn clear_user_address(&mut self) {
245        self.redact(|output| {
246            output.user_address = None;
247        });
248    }
249
250    /// Redacts the proprietary value at the given key.
251    pub fn redact_proprietary(&mut self, key: &str) {
252        self.redact(|output| {
253            output.proprietary.remove(key);
254        });
255    }
256
257    /// Removes all proprietary values.
258    pub fn clear_proprietary(&mut self) {
259        self.redact(|output| {
260            output.proprietary.clear();
261        });
262    }
263}