Skip to content

Commit

Permalink
Use bech32 checksum module
Browse files Browse the repository at this point in the history
Currently we have a custom implementation for the BCH codes used in
calculating the desciptor checksum. We recently released an
implementation in `bech32` of the same thing, we can use it here.

Encoding descriptors with a checksum differs from bech32 addresses
because of the `#` symbol that separates the descriptor from the
checksum so we cannot use the iterators from `bech32`, instead we define
our own iterator that adds the checksum and yields `chars`.
  • Loading branch information
tcharding committed Oct 6, 2023
1 parent 5018673 commit 22ee854
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 197 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ edition = "2018"

[features]
default = ["std"]
std = ["bitcoin/std", "bitcoin/secp-recovery"]
no-std = ["bitcoin/no-std"]
std = ["bitcoin/std", "bitcoin/secp-recovery", "bech32/std"]
no-std = ["bitcoin/no-std", "bech32/alloc"]
compiler = []
trace = []

Expand All @@ -22,6 +22,7 @@ rand = ["bitcoin/rand"]
base64 = ["bitcoin/base64"]

[dependencies]
bech32 = { version = "0.10.0-alpha", default-features = false }
bitcoin = { version = "0.30.0", default-features = false }
internals = { package = "bitcoin-private", version = "0.1.0", default_features = false }

Expand Down
34 changes: 24 additions & 10 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
//! Also includes pk, and pkh descriptors
//!
use core::fmt;
use core::fmt::{self, Write as _};

use bitcoin::script::{self, PushBytes};
use bitcoin::{Address, Network, ScriptBuf};

use super::checksum::{self, verify_checksum};
use super::checksum::{verify_checksum, Checksummed};
use crate::descriptor::DefiniteDescriptorKey;
use crate::expression::{self, FromTree};
use crate::miniscript::context::{ScriptContext, ScriptContextError};
Expand Down Expand Up @@ -157,10 +157,17 @@ 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()
let s = format!("{}", self.ms);

if f.alternate() {
write!(f, "{}", s)?;
} else {
for ch in Checksummed::new_unchecked(&s) {
f.write_char(ch)?;
}
}

Ok(())
}
}

Expand Down Expand Up @@ -354,10 +361,17 @@ 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()
let s = format!("pkh({})", self.pk);

if f.alternate() {
write!(f, "{}", s)?;
} else {
for ch in Checksummed::new_unchecked(&s) {
f.write_char(ch)?;
}
}

Ok(())
}
}

Expand Down
Loading

0 comments on commit 22ee854

Please sign in to comment.