use crate::transparent::{Bundle, Input, Output};
impl super::Redactor {
pub fn redact_transparent_with<F>(mut self, f: F) -> Self
where
F: FnOnce(TransparentRedactor<'_>),
{
f(TransparentRedactor(&mut self.pczt.transparent));
self
}
}
pub struct TransparentRedactor<'a>(&'a mut Bundle);
impl TransparentRedactor<'_> {
pub fn redact_inputs<F>(&mut self, f: F)
where
F: FnOnce(InputRedactor<'_>),
{
f(InputRedactor(Inputs::All(&mut self.0.inputs)));
}
pub fn redact_input<F>(&mut self, index: usize, f: F)
where
F: FnOnce(InputRedactor<'_>),
{
if let Some(input) = self.0.inputs.get_mut(index) {
f(InputRedactor(Inputs::One(input)));
}
}
pub fn redact_outputs<F>(&mut self, f: F)
where
F: FnOnce(OutputRedactor<'_>),
{
f(OutputRedactor(Outputs::All(&mut self.0.outputs)));
}
pub fn redact_output<F>(&mut self, index: usize, f: F)
where
F: FnOnce(OutputRedactor<'_>),
{
if let Some(output) = self.0.outputs.get_mut(index) {
f(OutputRedactor(Outputs::One(output)));
}
}
}
pub struct InputRedactor<'a>(Inputs<'a>);
enum Inputs<'a> {
All(&'a mut [Input]),
One(&'a mut Input),
}
impl InputRedactor<'_> {
fn redact<F>(&mut self, f: F)
where
F: Fn(&mut Input),
{
match &mut self.0 {
Inputs::All(inputs) => {
for input in inputs.iter_mut() {
f(input);
}
}
Inputs::One(input) => {
f(input);
}
}
}
pub fn clear_script_sig(&mut self) {
self.redact(|input| {
input.script_sig = None;
});
}
pub fn clear_redeem_script(&mut self) {
self.redact(|input| {
input.redeem_script = None;
});
}
pub fn redact_partial_signature(&mut self, pubkey: [u8; 33]) {
self.redact(|input| {
input.partial_signatures.remove(&pubkey);
});
}
pub fn clear_partial_signatures(&mut self) {
self.redact(|input| {
input.partial_signatures.clear();
});
}
pub fn redact_bip32_derivation(&mut self, pubkey: [u8; 33]) {
self.redact(|input| {
input.bip32_derivation.remove(&pubkey);
});
}
pub fn clear_bip32_derivation(&mut self) {
self.redact(|input| {
input.bip32_derivation.clear();
});
}
pub fn redact_ripemd160_preimage(&mut self, hash: [u8; 20]) {
self.redact(|input| {
input.ripemd160_preimages.remove(&hash);
});
}
pub fn clear_ripemd160_preimages(&mut self) {
self.redact(|input| {
input.ripemd160_preimages.clear();
});
}
pub fn redact_sha256_preimage(&mut self, hash: [u8; 32]) {
self.redact(|input| {
input.sha256_preimages.remove(&hash);
});
}
pub fn clear_sha256_preimages(&mut self) {
self.redact(|input| {
input.sha256_preimages.clear();
});
}
pub fn redact_hash160_preimage(&mut self, hash: [u8; 20]) {
self.redact(|input| {
input.hash160_preimages.remove(&hash);
});
}
pub fn clear_hash160_preimages(&mut self) {
self.redact(|input| {
input.hash160_preimages.clear();
});
}
pub fn redact_hash256_preimage(&mut self, hash: [u8; 32]) {
self.redact(|input| {
input.hash256_preimages.remove(&hash);
});
}
pub fn clear_hash256_preimages(&mut self) {
self.redact(|input| {
input.hash256_preimages.clear();
});
}
pub fn redact_proprietary(&mut self, key: &str) {
self.redact(|input| {
input.proprietary.remove(key);
});
}
pub fn clear_proprietary(&mut self) {
self.redact(|input| {
input.proprietary.clear();
});
}
}
pub struct OutputRedactor<'a>(Outputs<'a>);
enum Outputs<'a> {
All(&'a mut [Output]),
One(&'a mut Output),
}
impl OutputRedactor<'_> {
fn redact<F>(&mut self, f: F)
where
F: Fn(&mut Output),
{
match &mut self.0 {
Outputs::All(outputs) => {
for output in outputs.iter_mut() {
f(output);
}
}
Outputs::One(output) => {
f(output);
}
}
}
pub fn clear_redeem_script(&mut self) {
self.redact(|output| {
output.redeem_script = None;
});
}
pub fn redact_bip32_derivation(&mut self, pubkey: [u8; 33]) {
self.redact(|output| {
output.bip32_derivation.remove(&pubkey);
});
}
pub fn clear_bip32_derivation(&mut self) {
self.redact(|output| {
output.bip32_derivation.clear();
});
}
pub fn clear_user_address(&mut self) {
self.redact(|output| {
output.user_address = None;
});
}
pub fn redact_proprietary(&mut self, key: &str) {
self.redact(|output| {
output.proprietary.remove(key);
});
}
pub fn clear_proprietary(&mut self) {
self.redact(|output| {
output.proprietary.clear();
});
}
}