From c44ce81d94e80cd1c814146d9b6e91c7def3ff9a Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 10 Oct 2023 10:42:22 +1100 Subject: [PATCH] Implement write_descriptor macro Currently we have a bunch of places in the code where we create a checksum formatter wrapper then write a descriptor to it then write the checksum. This can all be wrapped up in a macro, has to be a macro because the number of args is variable. This reduces the line count with no real loss of clarity `write_descriptor!` is pretty self explanatory. --- src/descriptor/bare.rs | 16 ++++------------ src/descriptor/mod.rs | 15 +++++++++++++++ src/descriptor/segwitv0.rs | 16 +++++----------- src/descriptor/sh.rs | 15 ++++++--------- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/descriptor/bare.rs b/src/descriptor/bare.rs index c4ca14b14..a72953d3a 100644 --- a/src/descriptor/bare.rs +++ b/src/descriptor/bare.rs @@ -12,8 +12,8 @@ use core::fmt; use bitcoin::script::{self, PushBytes}; use bitcoin::{Address, Network, ScriptBuf}; -use super::checksum::{self, verify_checksum}; -use crate::descriptor::DefiniteDescriptorKey; +use super::checksum::verify_checksum; +use crate::descriptor::{write_descriptor, DefiniteDescriptorKey}; use crate::expression::{self, FromTree}; use crate::miniscript::context::{ScriptContext, ScriptContextError}; use crate::miniscript::satisfy::{Placeholder, Satisfaction, Witness}; @@ -156,12 +156,7 @@ impl fmt::Debug for Bare { } impl fmt::Display for Bare { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use fmt::Write; - let mut wrapped_f = checksum::Formatter::new(f); - write!(wrapped_f, "{}", self.ms)?; - wrapped_f.write_checksum_if_not_alt() - } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write_descriptor!(f, "{}", self.ms) } } impl Liftable for Bare { @@ -354,10 +349,7 @@ impl fmt::Debug for Pkh { impl fmt::Display for Pkh { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use fmt::Write; - let mut wrapped_f = checksum::Formatter::new(f); - write!(wrapped_f, "pkh({})", self.pk)?; - wrapped_f.write_checksum_if_not_alt() + write_descriptor!(f, "pkh({})", self.pk) } } diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index 616bb37cc..5ceb512e4 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -978,6 +978,21 @@ impl fmt::Display for Descriptor { serde_string_impl_pk!(Descriptor, "a script descriptor"); +macro_rules! write_descriptor { + ($fmt:expr, $s:literal $(, $args:expr)*) => { + { + use fmt::Write as _; + + let mut wrapped_f = $crate::descriptor::checksum::Formatter::new($fmt); + write!(wrapped_f, $s $(, $args)*)?; + wrapped_f.write_checksum_if_not_alt()?; + + fmt::Result::Ok(()) + } + } +} +pub(crate) use write_descriptor; + #[cfg(test)] mod tests { use core::convert::TryFrom; diff --git a/src/descriptor/segwitv0.rs b/src/descriptor/segwitv0.rs index 0cc665468..241c19dc0 100644 --- a/src/descriptor/segwitv0.rs +++ b/src/descriptor/segwitv0.rs @@ -9,9 +9,9 @@ use core::fmt; use bitcoin::{Address, Network, ScriptBuf}; -use super::checksum::{self, verify_checksum}; +use super::checksum::verify_checksum; use super::SortedMultiVec; -use crate::descriptor::DefiniteDescriptorKey; +use crate::descriptor::{write_descriptor, DefiniteDescriptorKey}; use crate::expression::{self, FromTree}; use crate::miniscript::context::{ScriptContext, ScriptContextError}; use crate::miniscript::satisfy::{Placeholder, Satisfaction, Witness}; @@ -260,13 +260,10 @@ impl fmt::Debug for Wsh { impl fmt::Display for Wsh { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use fmt::Write; - let mut wrapped_f = checksum::Formatter::new(f); match self.inner { - WshInner::SortedMulti(ref smv) => write!(wrapped_f, "wsh({})", smv)?, - WshInner::Ms(ref ms) => write!(wrapped_f, "wsh({})", ms)?, + WshInner::SortedMulti(ref smv) => write_descriptor!(f, "wsh({})", smv), + WshInner::Ms(ref ms) => write_descriptor!(f, "wsh({})", ms), } - wrapped_f.write_checksum_if_not_alt() } } @@ -461,10 +458,7 @@ impl fmt::Debug for Wpkh { impl fmt::Display for Wpkh { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use fmt::Write; - let mut wrapped_f = checksum::Formatter::new(f); - write!(wrapped_f, "wpkh({})", self.pk)?; - wrapped_f.write_checksum_if_not_alt() + write_descriptor!(f, "wpkh({})", self.pk) } } diff --git a/src/descriptor/sh.rs b/src/descriptor/sh.rs index 53b4107ce..b4fffd893 100644 --- a/src/descriptor/sh.rs +++ b/src/descriptor/sh.rs @@ -13,9 +13,9 @@ use core::fmt; use bitcoin::script::PushBytes; use bitcoin::{script, Address, Network, ScriptBuf}; -use super::checksum::{self, verify_checksum}; +use super::checksum::verify_checksum; use super::{SortedMultiVec, Wpkh, Wsh}; -use crate::descriptor::DefiniteDescriptorKey; +use crate::descriptor::{write_descriptor, DefiniteDescriptorKey}; use crate::expression::{self, FromTree}; use crate::miniscript::context::ScriptContext; use crate::miniscript::satisfy::{Placeholder, Satisfaction}; @@ -72,15 +72,12 @@ impl fmt::Debug for Sh { impl fmt::Display for Sh { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use fmt::Write; - let mut wrapped_f = checksum::Formatter::new(f); match self.inner { - ShInner::Wsh(ref wsh) => write!(wrapped_f, "sh({:#})", wsh)?, - ShInner::Wpkh(ref pk) => write!(wrapped_f, "sh({:#})", pk)?, - ShInner::SortedMulti(ref smv) => write!(wrapped_f, "sh({})", smv)?, - ShInner::Ms(ref ms) => write!(wrapped_f, "sh({})", ms)?, + ShInner::Wsh(ref wsh) => write_descriptor!(f, "sh({:#})", wsh), + ShInner::Wpkh(ref pk) => write_descriptor!(f, "sh({:#})", pk), + ShInner::SortedMulti(ref smv) => write_descriptor!(f, "sh({})", smv), + ShInner::Ms(ref ms) => write_descriptor!(f, "sh({})", ms), } - wrapped_f.write_checksum_if_not_alt() } }