Skip to content

Commit 5eac541

Browse files
committed
add try_finalize to SignOptions
1 parent 609ebc2 commit 5eac541

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Signing Taproot PSBTs (key spend and script spend)
2323
- Support for `tr()` descriptors in the `descriptor!()` macro
2424
- Add support for Bitcoin Core 23.0 when using the `rpc` blockchain
25-
- Added `remove_partial_sigs` to `SignOptions`
25+
- Added `remove_partial_sigs` and `try_finalize` to `SignOptions`
2626

2727
## [v0.18.0] - [v0.17.0]
2828

src/wallet/mod.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,11 @@ where
10501050
}
10511051

10521052
// attempt to finalize
1053-
self.finalize_psbt(psbt, sign_options)
1053+
if sign_options.try_finalize {
1054+
self.finalize_psbt(psbt, sign_options)
1055+
} else {
1056+
Ok(false)
1057+
}
10541058
}
10551059

10561060
/// Return the spending policies for the wallet's descriptor
@@ -3911,6 +3915,40 @@ pub(crate) mod test {
39113915
}
39123916
}
39133917

3918+
#[test]
3919+
fn test_try_finalize_sign_option() {
3920+
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
3921+
3922+
for try_finalize in &[true, false] {
3923+
let addr = wallet.get_address(New).unwrap();
3924+
let mut builder = wallet.build_tx();
3925+
builder.drain_to(addr.script_pubkey()).drain_wallet();
3926+
let mut psbt = builder.finish().unwrap().0;
3927+
3928+
let finalized = wallet
3929+
.sign(
3930+
&mut psbt,
3931+
SignOptions {
3932+
try_finalize: *try_finalize,
3933+
..Default::default()
3934+
},
3935+
)
3936+
.unwrap();
3937+
3938+
psbt.inputs.iter().for_each(|input| {
3939+
if *try_finalize {
3940+
assert!(finalized);
3941+
assert!(input.final_script_sig.is_some());
3942+
assert!(input.final_script_witness.is_some());
3943+
} else {
3944+
assert!(!finalized);
3945+
assert!(input.final_script_sig.is_none());
3946+
assert!(input.final_script_witness.is_none());
3947+
}
3948+
});
3949+
}
3950+
}
3951+
39143952
#[test]
39153953
fn test_sign_nonstandard_sighash() {
39163954
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;

src/wallet/signer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,10 @@ pub struct SignOptions {
671671
///
672672
/// Defaults to `true` which will remove partial_sigs after finalizing.
673673
pub remove_partial_sigs: bool,
674+
/// Whether to try finalizing psbt input after the inputs are signed.
675+
///
676+
/// Defaults to `true` which will try fianlizing psbt after inputs are signed.
677+
pub try_finalize: bool,
674678
}
675679

676680
#[allow(clippy::derivable_impls)]
@@ -681,6 +685,7 @@ impl Default for SignOptions {
681685
assume_height: None,
682686
allow_all_sighashes: false,
683687
remove_partial_sigs: true,
688+
try_finalize: true,
684689
}
685690
}
686691
}

0 commit comments

Comments
 (0)