Skip to content

Commit

Permalink
support regtest prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
crisdut committed Nov 24, 2022
1 parent 35bcde1 commit 4874880
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
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
41 changes: 33 additions & 8 deletions src/bin/btc-cold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ pub enum Command {
/// Number of addresses to skip
#[clap(short, long, default_value = "0")]
skip: u16,

/// Show address with regtest prefix
#[clap(long = "regtest")]
regtest: bool,
},

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

/// Show address with regtest prefix
#[clap(long = "regtest")]
regtest: bool,
},

/// Construct new PSBT.
Expand Down Expand Up @@ -316,14 +324,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,11 +415,23 @@ 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 target_network = if regtest {
Network::Regtest
} else {
DescrTrait::<bitcoin::PublicKey>::network(&descriptor).unwrap()
};

println!(
"{}\n{}\n",
Expand All @@ -421,11 +443,11 @@ 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, &[
UnhardenedIndex::from(if show_change { 1u8 } else { 0u8 }),
UnhardenedIndex::from(index),
])?;

let address = Address::from_script(&script, target_network).unwrap();
println!("{:>6} {}", format!("#{}", index).dimmed(), address);
}

Expand All @@ -434,13 +456,16 @@ 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 = if regtest {
Network::Regtest
} else {
DescrTrait::<bitcoin::PublicKey>::network(&descriptor).unwrap()
};
let client = self.electrum_client(network)?;

println!(
Expand Down

0 comments on commit 4874880

Please sign in to comment.