Skip to content

Commit c324eb1

Browse files
committed
add try_finalize to SignOptions
1 parent 6759e32 commit c324eb1

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

CHANGELOG.md

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

2424
## [v0.18.0] - [v0.17.0]
2525

src/wallet/mod.rs

Lines changed: 41 additions & 4 deletions
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
@@ -3895,18 +3899,18 @@ pub(crate) mod test {
38953899
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
38963900

38973901
for remove_partial_sigs in &[true, false] {
3898-
let mut psbt_1 = create_drain_all_and_consolidate_psbt(&wallet);
3902+
let mut psbt = create_drain_all_and_consolidate_psbt(&wallet);
38993903
assert!(wallet
39003904
.sign(
3901-
&mut psbt_1,
3905+
&mut psbt,
39023906
SignOptions {
39033907
remove_partial_sigs: *remove_partial_sigs,
39043908
..Default::default()
39053909
},
39063910
)
39073911
.unwrap());
39083912

3909-
psbt_1.inputs.iter().for_each(|input| {
3913+
psbt.inputs.iter().for_each(|input| {
39103914
// remove finalized input partial_sigs if remove_partial_sigs is set as true in SignOptions
39113915
// do not remove finalized input partial_sigs if remove_partial_sigs is set as false in SignOptions.
39123916
if *remove_partial_sigs {
@@ -3918,6 +3922,39 @@ pub(crate) mod test {
39183922
}
39193923
}
39203924

3925+
#[test]
3926+
fn test_try_finalize_sign_option() {
3927+
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
3928+
3929+
for try_finalize in &[true, false] {
3930+
let mut psbt = create_drain_all_and_consolidate_psbt(&wallet);
3931+
3932+
let finalized = wallet
3933+
.sign(
3934+
&mut psbt,
3935+
SignOptions {
3936+
try_finalize: *try_finalize,
3937+
..Default::default()
3938+
},
3939+
)
3940+
.unwrap();
3941+
3942+
// sign but do not finalize if try_finalize is set as false in SignOptions
3943+
// sign and do finalize if try_finalize is set as true in SignOptions
3944+
psbt.inputs.iter().for_each(|input| {
3945+
if *try_finalize {
3946+
assert!(finalized);
3947+
assert!(input.final_script_sig.is_some());
3948+
assert!(input.final_script_witness.is_some());
3949+
} else {
3950+
assert!(!finalized);
3951+
assert!(input.final_script_sig.is_none());
3952+
assert!(input.final_script_witness.is_none());
3953+
}
3954+
});
3955+
}
3956+
}
3957+
39213958
#[test]
39223959
fn test_sign_nonstandard_sighash() {
39233960
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;

src/wallet/signer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,11 @@ pub struct SignOptions {
671671
///
672672
/// Defaults to `true` which will remove partial_sigs in inputs after signing.
673673
pub remove_partial_sigs: bool,
674+
/// Whether the wallet should try to finalize psbt input should be removed after the inputs are
675+
/// signed.
676+
///
677+
/// Defaults to `true` which will try to fianlize psbt after inputs are signed.
678+
pub try_finalize: bool,
674679
}
675680

676681
#[allow(clippy::derivable_impls)]
@@ -681,6 +686,7 @@ impl Default for SignOptions {
681686
assume_height: None,
682687
allow_all_sighashes: false,
683688
remove_partial_sigs: true,
689+
try_finalize: true,
684690
}
685691
}
686692
}

0 commit comments

Comments
 (0)