Skip to content

Commit dd51380

Browse files
committed
Merge #621: Add remove_partial_sigs and try_finalize to SignOptions
e3a17f6 add try_finalize to SignOptions (KaFai Choi) c2e4ba8 add remove_partial_sigs to SignOptions (KaFai Choi) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description This PR is to add 2 keys(`try_finalize` and `remove_partial_sigs`) in `SignOptions`. See this issue for detail #612 ### Notes to the reviewers ~I found the negative naming of these 2 new keys `do_not_finalize` and `do_not_remove_partial_sigs` are a bit confusing(like most negative named paremeter/variable). Should we actually change it back to positive naming(`do_finalize` and `do_remove_partial_sigs`)?~ ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature * [x] I've updated `CHANGELOG.md` #### Bugfixes: * [ ] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: notmandatory: ReACK e3a17f6 Tree-SHA512: 781b31d3ecf0bcd605206c0481fd5de3125f1c8ff18a463dbf4c821e5557847f7d70a3fe8618e100fb89f4f6899655ac0efa3593f77f915ad5bcb7e558bb2a7a
2 parents 73d4f6d + e3a17f6 commit dd51380

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

CHANGELOG.md

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

3031
## [v0.18.0] - [v0.17.0]
3132

src/wallet/mod.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,11 @@ where
10751075
}
10761076

10771077
// attempt to finalize
1078-
self.finalize_psbt(psbt, sign_options)
1078+
if sign_options.try_finalize {
1079+
self.finalize_psbt(psbt, sign_options)
1080+
} else {
1081+
Ok(false)
1082+
}
10791083
}
10801084

10811085
/// Return the spending policies for the wallet's descriptor
@@ -1186,6 +1190,9 @@ where
11861190
let psbt_input = &mut psbt.inputs[n];
11871191
psbt_input.final_script_sig = Some(tmp_input.script_sig);
11881192
psbt_input.final_script_witness = Some(tmp_input.witness);
1193+
if sign_options.remove_partial_sigs {
1194+
psbt_input.partial_sigs.clear();
1195+
}
11891196
}
11901197
Err(e) => {
11911198
debug!("satisfy error {:?} for input {}", e, n);
@@ -4106,6 +4113,70 @@ pub(crate) mod test {
41064113
)
41074114
}
41084115

4116+
#[test]
4117+
fn test_remove_partial_sigs_after_finalize_sign_option() {
4118+
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
4119+
4120+
for remove_partial_sigs in &[true, false] {
4121+
let addr = wallet.get_address(New).unwrap();
4122+
let mut builder = wallet.build_tx();
4123+
builder.drain_to(addr.script_pubkey()).drain_wallet();
4124+
let mut psbt = builder.finish().unwrap().0;
4125+
4126+
assert!(wallet
4127+
.sign(
4128+
&mut psbt,
4129+
SignOptions {
4130+
remove_partial_sigs: *remove_partial_sigs,
4131+
..Default::default()
4132+
},
4133+
)
4134+
.unwrap());
4135+
4136+
psbt.inputs.iter().for_each(|input| {
4137+
if *remove_partial_sigs {
4138+
assert!(input.partial_sigs.is_empty())
4139+
} else {
4140+
assert!(!input.partial_sigs.is_empty())
4141+
}
4142+
});
4143+
}
4144+
}
4145+
4146+
#[test]
4147+
fn test_try_finalize_sign_option() {
4148+
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");
4149+
4150+
for try_finalize in &[true, false] {
4151+
let addr = wallet.get_address(New).unwrap();
4152+
let mut builder = wallet.build_tx();
4153+
builder.drain_to(addr.script_pubkey()).drain_wallet();
4154+
let mut psbt = builder.finish().unwrap().0;
4155+
4156+
let finalized = wallet
4157+
.sign(
4158+
&mut psbt,
4159+
SignOptions {
4160+
try_finalize: *try_finalize,
4161+
..Default::default()
4162+
},
4163+
)
4164+
.unwrap();
4165+
4166+
psbt.inputs.iter().for_each(|input| {
4167+
if *try_finalize {
4168+
assert!(finalized);
4169+
assert!(input.final_script_sig.is_some());
4170+
assert!(input.final_script_witness.is_some());
4171+
} else {
4172+
assert!(!finalized);
4173+
assert!(input.final_script_sig.is_none());
4174+
assert!(input.final_script_witness.is_none());
4175+
}
4176+
});
4177+
}
4178+
}
4179+
41094180
#[test]
41104181
fn test_sign_nonstandard_sighash() {
41114182
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;

src/wallet/signer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,14 @@ pub struct SignOptions {
667667
///
668668
/// Defaults to `false` which will only allow signing using `SIGHASH_ALL`.
669669
pub allow_all_sighashes: bool,
670+
/// Whether to remove partial_sigs from psbt inputs while finalizing psbt.
671+
///
672+
/// Defaults to `true` which will remove partial_sigs after finalizing.
673+
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,
670678
}
671679

672680
#[allow(clippy::derivable_impls)]
@@ -676,6 +684,8 @@ impl Default for SignOptions {
676684
trust_witness_utxo: false,
677685
assume_height: None,
678686
allow_all_sighashes: false,
687+
remove_partial_sigs: true,
688+
try_finalize: true,
679689
}
680690
}
681691
}

0 commit comments

Comments
 (0)