diff --git a/src/cli/command.rs b/src/cli/command.rs index b990186..e1d6349 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -84,6 +84,35 @@ pub enum Command { #[clap(short = 'C', long, default_value = "1")] count: u8, }, + + /// Finalize a PSBT, optionally extracting and publishing the signed transaction + #[display("finalize")] + Finalize { + /// Extract and send the signed transaction to the network. + #[clap(short, long)] + publish: bool, + + /// Name of PSBT file to finalize. + psbt: PathBuf, + + /// File to save the extracted signed transaction. + tx: Option, + }, + + /// Extract a signed transaction from PSBT. The PSBT file itself is not modified + #[display("finalize")] + Extract { + /// Send the extracted transaction to the network. + #[clap(short, long)] + publish: bool, + + /// Name of PSBT file to take the transaction from + psbt: PathBuf, + + /// File to save the extracted signed transaction. If not provided, the transaction is + /// print to STDOUT. + tx: Option, + }, } #[derive(Subcommand, Clone, PartialEq, Eq, Debug, Display)] @@ -146,35 +175,6 @@ pub enum BpCommand { /// Name of a PSBT file to save. If not given, prints PSBT to STDOUT psbt: Option, }, - - /// Finalize a PSBT, optionally extracting and publishing the signed transaction - #[display("finalize")] - Finalize { - /// Extract and send the signed transaction to the network. - #[clap(short, long)] - publish: bool, - - /// Name of PSBT file to finalize. - psbt: PathBuf, - - /// File to save the extracted signed transaction. - tx: Option, - }, - - /// Extract a signed transaction from PSBT. The PSBT file itself is not modified. - #[display("finalize")] - Extract { - /// Send the extracted transaction to the network. - #[clap(short, long)] - publish: bool, - - /// Name of PSBT file to take the transaction from - psbt: PathBuf, - - /// File to save the extracted signed transaction. If not provided, the transaction is - /// print to STDOUT. - tx: Option, - }, } #[derive(Debug, Display, Error, From)] @@ -309,6 +309,49 @@ impl Exec for Args { println!("{}\t{}", derived_addr.terminal, derived_addr.addr); } } + Command::Finalize { + publish, + psbt: psbt_path, + tx, + } => { + let mut psbt = psbt_read(&psbt_path)?; + if psbt.is_finalized() { + eprintln!("The PSBT is already finalized"); + } else { + let wallet = self.bp_wallet::(&config)?; + psbt_finalize(&mut psbt, wallet.descriptor())?; + } + + psbt_write(&psbt, &psbt_path)?; + if let Ok(tx) = psbt_extract(&psbt, *publish, tx.as_deref()) { + if *publish { + let indexer = self.indexer()?; + eprint!("Publishing transaction via {} ... ", indexer.name()); + indexer.publish(&tx)?; + eprintln!("success"); + } + } + } + Command::Extract { + publish, + psbt: psbt_path, + tx, + } => { + let mut psbt = psbt_read(&psbt_path)?; + if !psbt.is_finalized() { + let wallet = self.bp_wallet::(&config)?; + psbt_finalize(&mut psbt, wallet.descriptor())?; + } + + if let Ok(tx) = psbt_extract(&psbt, *publish, tx.as_deref()) { + if *publish { + let indexer = self.indexer()?; + eprint!("Publishing transaction via {} ... ", indexer.name()); + indexer.publish(&tx)?; + eprintln!("success"); + } + } + } } Ok(()) @@ -488,49 +531,6 @@ impl Exec for Args { psbt.version = if *v2 { PsbtVer::V2 } else { PsbtVer::V0 }; psbt_write_or_print(&psbt, psbt_file.as_deref())?; } - BpCommand::Finalize { - publish, - psbt: psbt_path, - tx, - } => { - let mut psbt = psbt_read(&psbt_path)?; - if psbt.is_finalized() { - eprintln!("The PSBT is already finalized"); - } else { - let wallet = self.bp_wallet::(&config)?; - psbt_finalize(&mut psbt, wallet.descriptor())?; - } - - psbt_write(&psbt, &psbt_path)?; - if let Ok(tx) = psbt_extract(&psbt, *publish, tx.as_deref()) { - if *publish { - let indexer = self.indexer()?; - eprint!("Publishing transaction via {} ... ", indexer.name()); - indexer.publish(&tx)?; - eprintln!("success"); - } - } - } - BpCommand::Extract { - publish, - psbt: psbt_path, - tx, - } => { - let mut psbt = psbt_read(&psbt_path)?; - if !psbt.is_finalized() { - let wallet = self.bp_wallet::(&config)?; - psbt_finalize(&mut psbt, wallet.descriptor())?; - } - - if let Ok(tx) = psbt_extract(&psbt, *publish, tx.as_deref()) { - if *publish { - let indexer = self.indexer()?; - eprint!("Publishing transaction via {} ... ", indexer.name()); - indexer.publish(&tx)?; - eprintln!("success"); - } - } - } }; println!();