Skip to content

Commit

Permalink
use address compat to display address
Browse files Browse the repository at this point in the history
  • Loading branch information
crisdut committed Jan 6, 2023
1 parent 9007c2f commit 8fca9d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 10 additions & 4 deletions descriptors/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
// If not, see <https://opensource.org/licenses/Apache-2.0>.

use bitcoin::secp256k1::{Secp256k1, Verification};
use bitcoin::{Address, Network, Script};
use bitcoin::{Network, Script};
#[cfg(feature = "miniscript")]
use bitcoin_hd::{DerivationAccount, DerivePatternError};
use bitcoin_hd::{DeriveError, UnhardenedIndex};
use bitcoin_scripts::address::AddressCompat;

#[cfg(not(feature = "miniscript"))]
pub mod miniscript {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub trait Descriptor<Key> {
secp: &Secp256k1<C>,
pat: impl AsRef<[UnhardenedIndex]>,
regtest: bool,
) -> Result<Address, DeriveError>;
) -> Result<AddressCompat, DeriveError>;

/// Creates scriptPubkey for specific derive pattern in pre-taproot
/// descriptors
Expand All @@ -84,6 +85,7 @@ mod ms {
use bitcoin::XOnlyPublicKey;
use bitcoin_hd::account::DerivePublicKey;
use bitcoin_hd::{DeriveError, SegmentIndexes};
use bitcoin_scripts::address::{AddressCompat, AddressNetwork};
use miniscript::{translate_hash_fail, ForEachKey, TranslatePk, Translator};

use super::*;
Expand Down Expand Up @@ -215,10 +217,14 @@ mod ms {
secp: &Secp256k1<C>,
pat: impl AsRef<[UnhardenedIndex]>,
regtest: bool,
) -> Result<Address, DeriveError> {
) -> Result<AddressCompat, DeriveError> {
let network = self.network(regtest)?;
let network = AddressNetwork::from(network);
let spk = Descriptor::script_pubkey_pretr(self, secp, pat)?;
Address::from_script(&spk, network).map_err(|_| DeriveError::NoAddressForDescriptor)
match AddressCompat::from_script(&spk.into(), network) {
Some(address) => Ok(address),
_ => Err(DeriveError::NoAddressForDescriptor),
}
}

#[inline]
Expand Down
5 changes: 4 additions & 1 deletion src/bin/btc-cold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use bitcoin::{consensus, Address, Network};
use bitcoin_blockchain::locks::LockTime;
use bitcoin_hd::DeriveError;
use bitcoin_onchain::UtxoResolverError;
use bitcoin_scripts::address::AddressCompat;
use bitcoin_scripts::taproot::DfsPath;
use bitcoin_scripts::PubkeyScript;
use clap::Parser;
Expand Down Expand Up @@ -526,7 +527,9 @@ impl Args {
count += utxo_set.len();

let derive_term = format!("{}/{}", case, index);
if let Ok(address) = Address::from_script(&script, network) {
if let Some(address) =
AddressCompat::from_script(&script.clone().into(), network.into())
{
println!(
"\n {} address {}:",
derive_term.bright_white(),
Expand Down

0 comments on commit 8fca9d0

Please sign in to comment.