Skip to content

Commit

Permalink
Implement write_descriptor macro
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed Oct 9, 2023
1 parent c5c08fe commit c44ce81
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
16 changes: 4 additions & 12 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -156,12 +156,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
}

impl<Pk: MiniscriptKey> fmt::Display for Bare<Pk> {
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<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
Expand Down Expand Up @@ -354,10 +349,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {

impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
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)
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,21 @@ impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {

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;
Expand Down
16 changes: 5 additions & 11 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -260,13 +260,10 @@ impl<Pk: MiniscriptKey> fmt::Debug for Wsh<Pk> {

impl<Pk: MiniscriptKey> fmt::Display for Wsh<Pk> {
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()
}
}

Expand Down Expand Up @@ -461,10 +458,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Wpkh<Pk> {

impl<Pk: MiniscriptKey> fmt::Display for Wpkh<Pk> {
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)
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -72,15 +72,12 @@ impl<Pk: MiniscriptKey> fmt::Debug for Sh<Pk> {

impl<Pk: MiniscriptKey> fmt::Display for Sh<Pk> {
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()
}
}

Expand Down

0 comments on commit c44ce81

Please sign in to comment.