Skip to content

Commit 9d995a4

Browse files
committed
Update finalize API to return vector of errors
1 parent 68cccb3 commit 9d995a4

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/psbt/finalizer.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,7 @@ pub fn finalize_helper<C: secp256k1::Verification>(
361361
for index in 0..psbt.inputs.len() {
362362
finalize_input(psbt, index, secp, allow_mall)?;
363363
}
364-
// Double check everything with the interpreter
365-
// This only checks whether the script will be executed
366-
// correctly by the bitcoin interpreter under the current
367-
// psbt context.
368-
interpreter_check(&psbt, secp)?;
364+
// Interpreter is already run inside finalize_input for each input
369365
Ok(())
370366
}
371367

src/psbt/mod.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -425,23 +425,26 @@ fn sanity_check(psbt: &Psbt) -> Result<(), Error> {
425425
pub trait PsbtExt {
426426
/// Finalize the psbt. This function takes in a mutable reference to psbt
427427
/// and populates the final_witness and final_scriptsig
428-
/// of the psbt assuming all of the inputs are miniscript as per BIP174.
429-
/// If any of the inputs is not miniscript, this returns a parsing error
430-
/// For satisfaction of individual inputs, use the satisfy API.
431-
/// This function also performs a sanity interpreter check on the
428+
/// for all miniscript inputs.
429+
///
430+
/// Finalizes all inputs that it can finalize, and returns an error for each input
431+
/// that it cannot finalize. Also performs a sanity interpreter check on the
432432
/// finalized psbt which involves checking the signatures/ preimages/timelocks.
433-
/// The functions fails it is not possible to satisfy any of the inputs non-malleably
433+
///
434+
/// Input finalization also fails if it is not possible to satisfy any of the inputs non-malleably
434435
/// See [finalizer::finalize_mall] if you want to allow malleable satisfactions
436+
///
437+
/// For finalizing individual inputs, see also [`PsbtExt::finalize_inp`]
435438
fn finalize<C: secp256k1::Verification>(
436439
&mut self,
437440
secp: &secp256k1::Secp256k1<C>,
438-
) -> Result<(), Error>;
441+
) -> Result<(), Vec<Error>>;
439442

440443
/// Same as [finalize], but allows for malleable satisfactions
441444
fn finalize_mall<C: secp256k1::Verification>(
442445
&mut self,
443446
secp: &Secp256k1<C>,
444-
) -> Result<(), Error>;
447+
) -> Result<(), Vec<Error>>;
445448

446449
/// Same as [finalize], but only tries to finalize a single input leaving other
447450
/// inputs as is. Use this when not all of inputs that you are trying to
@@ -517,15 +520,42 @@ impl PsbtExt for Psbt {
517520
fn finalize<C: secp256k1::Verification>(
518521
&mut self,
519522
secp: &secp256k1::Secp256k1<C>,
520-
) -> Result<(), Error> {
521-
finalizer::finalize_helper(self, secp, /*allow_mall*/ false)
523+
) -> Result<(), Vec<Error>> {
524+
// Actually construct the witnesses
525+
let mut errors = vec![];
526+
for index in 0..self.inputs.len() {
527+
match finalizer::finalize_input(self, index, secp, /*allow_mall*/ false) {
528+
Ok(..) => {}
529+
Err(e) => {
530+
errors.push(e);
531+
}
532+
}
533+
}
534+
if errors.is_empty() {
535+
Ok(())
536+
} else {
537+
Err(errors)
538+
}
522539
}
523540

524541
fn finalize_mall<C: secp256k1::Verification>(
525542
&mut self,
526543
secp: &secp256k1::Secp256k1<C>,
527-
) -> Result<(), Error> {
528-
finalizer::finalize_helper(self, secp, /*allow_mall*/ true)
544+
) -> Result<(), Vec<Error>> {
545+
let mut errors = vec![];
546+
for index in 0..self.inputs.len() {
547+
match finalizer::finalize_input(self, index, secp, /*allow_mall*/ true) {
548+
Ok(..) => {}
549+
Err(e) => {
550+
errors.push(e);
551+
}
552+
}
553+
}
554+
if errors.is_empty() {
555+
Ok(())
556+
} else {
557+
Err(errors)
558+
}
529559
}
530560

531561
fn finalize_inp<C: secp256k1::Verification>(

0 commit comments

Comments
 (0)