pczt/roles/redactor/
transparent.rs1use crate::transparent::{Bundle, Input, Output};
2
3impl super::Redactor {
4 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
14pub struct TransparentRedactor<'a>(&'a mut Bundle);
16
17impl TransparentRedactor<'_> {
18 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 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 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 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
59pub 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 pub fn clear_script_sig(&mut self) {
86 self.redact(|input| {
87 input.script_sig = None;
88 });
89 }
90
91 pub fn clear_redeem_script(&mut self) {
93 self.redact(|input| {
94 input.redeem_script = None;
95 });
96 }
97
98 pub fn redact_partial_signature(&mut self, pubkey: [u8; 33]) {
100 self.redact(|input| {
101 input.partial_signatures.remove(&pubkey);
102 });
103 }
104
105 pub fn clear_partial_signatures(&mut self) {
107 self.redact(|input| {
108 input.partial_signatures.clear();
109 });
110 }
111
112 pub fn redact_bip32_derivation(&mut self, pubkey: [u8; 33]) {
114 self.redact(|input| {
115 input.bip32_derivation.remove(&pubkey);
116 });
117 }
118
119 pub fn clear_bip32_derivation(&mut self) {
121 self.redact(|input| {
122 input.bip32_derivation.clear();
123 });
124 }
125
126 pub fn redact_ripemd160_preimage(&mut self, hash: [u8; 20]) {
128 self.redact(|input| {
129 input.ripemd160_preimages.remove(&hash);
130 });
131 }
132
133 pub fn clear_ripemd160_preimages(&mut self) {
135 self.redact(|input| {
136 input.ripemd160_preimages.clear();
137 });
138 }
139
140 pub fn redact_sha256_preimage(&mut self, hash: [u8; 32]) {
142 self.redact(|input| {
143 input.sha256_preimages.remove(&hash);
144 });
145 }
146
147 pub fn clear_sha256_preimages(&mut self) {
149 self.redact(|input| {
150 input.sha256_preimages.clear();
151 });
152 }
153
154 pub fn redact_hash160_preimage(&mut self, hash: [u8; 20]) {
156 self.redact(|input| {
157 input.hash160_preimages.remove(&hash);
158 });
159 }
160
161 pub fn clear_hash160_preimages(&mut self) {
163 self.redact(|input| {
164 input.hash160_preimages.clear();
165 });
166 }
167
168 pub fn redact_hash256_preimage(&mut self, hash: [u8; 32]) {
170 self.redact(|input| {
171 input.hash256_preimages.remove(&hash);
172 });
173 }
174
175 pub fn clear_hash256_preimages(&mut self) {
177 self.redact(|input| {
178 input.hash256_preimages.clear();
179 });
180 }
181
182 pub fn redact_proprietary(&mut self, key: &str) {
184 self.redact(|input| {
185 input.proprietary.remove(key);
186 });
187 }
188
189 pub fn clear_proprietary(&mut self) {
191 self.redact(|input| {
192 input.proprietary.clear();
193 });
194 }
195}
196
197pub 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 pub fn clear_redeem_script(&mut self) {
224 self.redact(|output| {
225 output.redeem_script = None;
226 });
227 }
228
229 pub fn redact_bip32_derivation(&mut self, pubkey: [u8; 33]) {
231 self.redact(|output| {
232 output.bip32_derivation.remove(&pubkey);
233 });
234 }
235
236 pub fn clear_bip32_derivation(&mut self) {
238 self.redact(|output| {
239 output.bip32_derivation.clear();
240 });
241 }
242
243 pub fn clear_user_address(&mut self) {
245 self.redact(|output| {
246 output.user_address = None;
247 });
248 }
249
250 pub fn redact_proprietary(&mut self, key: &str) {
252 self.redact(|output| {
253 output.proprietary.remove(key);
254 });
255 }
256
257 pub fn clear_proprietary(&mut self) {
259 self.redact(|output| {
260 output.proprietary.clear();
261 });
262 }
263}