Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to Regtest Address #63

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ target/
**/*.rs.bk

.idea
.vscode

*.swp

Expand Down
51 changes: 43 additions & 8 deletions src/bin/btc-cold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ pub enum Command {
/// Number of addresses to skip
#[clap(short, long, default_value = "0")]
skip: u16,

/// Show addresses using regtest prefix. Works only for testnet-based
/// wallet descriptors.
#[clap(long = "regtest")]
regtest: bool,
},

/// Read history of operations with descriptor controlled outputs from
Expand All @@ -160,6 +165,11 @@ pub enum Command {
/// Whether or not to show change addresses
#[clap(short = 'c', long = "change")]
show_change: bool,

/// Display address using regtest prefix. Works only for testnet-based
/// descriptors.
#[clap(long = "regtest")]
regtest: bool,
},

/// Construct new PSBT.
Expand Down Expand Up @@ -316,14 +326,16 @@ impl Args {
wallet_file,
look_ahead,
skip,
} => self.check(wallet_file, *look_ahead, *skip),
regtest,
} => self.check(wallet_file, *look_ahead, *skip, *regtest),
Command::History { .. } => self.history(),
Command::Address {
wallet_file,
count,
skip,
show_change,
} => self.address(wallet_file, *count, *skip, *show_change),
regtest,
} => self.address(wallet_file, *count, *skip, *show_change, *regtest),
Command::Construct {
locktime,
wallet_file,
Expand Down Expand Up @@ -405,12 +417,28 @@ impl Args {
Ok(())
}

fn address(&self, path: &Path, count: u16, skip: u16, show_change: bool) -> Result<(), Error> {
fn address(
&self,
path: &Path,
count: u16,
skip: u16,
show_change: bool,
regtest: bool,
) -> Result<(), Error> {
let secp = Secp256k1::new();

let file = fs::File::open(path)?;
let descriptor: Descriptor<DerivationAccount> = Descriptor::strict_decode(file)?;

let network = match (
DescrTrait::<bitcoin::PublicKey>::network(&descriptor).expect("Network not found"),
regtest,
) {
(network, false) => Ok(network),
(Network::Testnet | Network::Signet | Network::Regtest, true) => Ok(Network::Regtest),
(_, true) => Err(DeriveError::InconsistentKeyNetwork),
}?;

println!(
"{}\n{}\n",
"\nWallet descriptor:".bright_white(),
Expand All @@ -421,11 +449,12 @@ impl Args {
return Err(Error::DescriptorDerivePattern);
}
for index in skip..(skip + count) {
let address = DescrTrait::<bitcoin::PublicKey>::address(&descriptor, &secp, &[
let script = DescrTrait::<bitcoin::PublicKey>::script_pubkey(&descriptor, &secp, &[
crisdut marked this conversation as resolved.
Show resolved Hide resolved
UnhardenedIndex::from(if show_change { 1u8 } else { 0u8 }),
UnhardenedIndex::from(index),
])?;

let address = Address::from_script(&script, network)
.expect("Incorrect scriptPubkey to represents address");
println!("{:>6} {}", format!("#{}", index).dimmed(), address);
}

Expand All @@ -434,13 +463,19 @@ impl Args {
Ok(())
}

fn check(&self, path: &Path, batch_size: u16, skip: u16) -> Result<(), Error> {
fn check(&self, path: &Path, batch_size: u16, skip: u16, regtest: bool) -> Result<(), Error> {
let secp = Secp256k1::new();

let file = fs::File::open(path)?;
let descriptor: Descriptor<DerivationAccount> = Descriptor::strict_decode(file)?;

let network = DescrTrait::<bitcoin::PublicKey>::network(&descriptor)?;
let network = match (
DescrTrait::<bitcoin::PublicKey>::network(&descriptor).expect("Network not found"),
regtest,
) {
(network, false) => Ok(network),
(Network::Testnet | Network::Signet | Network::Regtest, true) => Ok(Network::Regtest),
(_, true) => Err(DeriveError::InconsistentKeyNetwork),
}?;
let client = self.electrum_client(network)?;

println!(
Expand Down