Skip to content

Commit

Permalink
Merge pull request #59 from BP-WG/finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored Aug 14, 2024
2 parents 515ef01 + 52674cc commit 7d5105f
Showing 1 changed file with 72 additions and 72 deletions.
144 changes: 72 additions & 72 deletions src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
},

/// 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<PathBuf>,
},
}

#[derive(Subcommand, Clone, PartialEq, Eq, Debug, Display)]
Expand Down Expand Up @@ -146,35 +175,6 @@ pub enum BpCommand {
/// Name of a PSBT file to save. If not given, prints PSBT to STDOUT
psbt: Option<PathBuf>,
},

/// 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<PathBuf>,
},

/// 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<PathBuf>,
},
}

#[derive(Debug, Display, Error, From)]
Expand Down Expand Up @@ -309,6 +309,49 @@ impl<O: DescriptorOpts> Exec for Args<Command, O> {
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::<O::Descr>(&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::<O::Descr>(&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(())
Expand Down Expand Up @@ -488,49 +531,6 @@ impl<O: DescriptorOpts> Exec for Args<BpCommand, O> {
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::<O::Descr>(&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::<O::Descr>(&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!();
Expand Down

0 comments on commit 7d5105f

Please sign in to comment.