Skip to content

Commit 09d4bc6

Browse files
committed
add try_finalize to SignOptions
1 parent a3e55dc commit 09d4bc6

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Creation of Taproot PSBTs (BIP-371)
1919
- Signing Taproot PSBTs (key spend and script spend)
2020
- Support for `tr()` descriptors in the `descriptor!()` macro
21-
- Added `remove_partial_sigs` to `SignOptions`
21+
- Added `remove_partial_sigs` and `try_finalize` to `SignOptions`
2222

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

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_some());
3952+
assert!(!input.final_script_witness.is_some());
3953+
}
3954+
});
3955+
}
3956+
}
3957+
39213958
#[test]
39223959
fn test_sign_nonstandard_sighash() {
39233960
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;

src/wallet/signer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ pub struct SignOptions {
670670
/// Whether partial_sigs in input should be removed when the psbt_input is signed in finalizing psbt.
671671
/// Defaults to `true` which will remove partial_sigs after signing.
672672
pub remove_partial_sigs: bool,
673+
/// Whether the wallet should try to finalize psbt input should be removed when the psbt_input is signed after
674+
///
675+
/// Defaults to `true` which will try to fianlize psbt after inputi are signed/
676+
pub try_finalize: bool,
673677
}
674678

675679
#[allow(clippy::derivable_impls)]
@@ -680,6 +684,7 @@ impl Default for SignOptions {
680684
assume_height: None,
681685
allow_all_sighashes: false,
682686
remove_partial_sigs: true,
687+
try_finalize: true,
683688
}
684689
}
685690
}

0 commit comments

Comments
 (0)