Skip to content

Commit

Permalink
add try_finalize to SignOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kafaichoi committed Jul 2, 2022
1 parent 236e5c3 commit 6a79f39
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Signing Taproot PSBTs (key spend and script spend)
- Support for `tr()` descriptors in the `descriptor!()` macro
- Add support for Bitcoin Core 23.0 when using the `rpc` blockchain
- Add `remove_partial_sigs` to `SignOptions`
- Add `remove_partial_sigs` and `try_finalize` to `SignOptions`

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

Expand Down
37 changes: 36 additions & 1 deletion src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,11 @@ where
}

// attempt to finalize
self.finalize_psbt(psbt, sign_options)
if sign_options.try_finalize {
self.finalize_psbt(psbt, sign_options)
} else {
Ok(false)
}
}

/// Return the spending policies for the wallet's descriptor
Expand Down Expand Up @@ -3916,6 +3920,37 @@ pub(crate) mod test {
}
}

#[test]
fn test_try_finalize_sign_option() {
let (wallet, _, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)");

for try_finalize in &[true, false] {
let mut psbt = create_drain_all_and_consolidate_psbt(&wallet);

let finalized = wallet
.sign(
&mut psbt,
SignOptions {
try_finalize: *try_finalize,
..Default::default()
},
)
.unwrap();

psbt.inputs.iter().for_each(|input| {
if *try_finalize {
assert!(finalized);
assert!(input.final_script_sig.is_some());
assert!(input.final_script_witness.is_some());
} else {
assert!(!finalized);
assert!(input.final_script_sig.is_none());
assert!(input.final_script_witness.is_none());
}
});
}
}

#[test]
fn test_sign_nonstandard_sighash() {
let sighash = EcdsaSighashType::NonePlusAnyoneCanPay;
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ pub struct SignOptions {
///
/// Defaults to `true` which will remove partial_sigs after finalizing.
pub remove_partial_sigs: bool,
/// Whether to try finalizing psbt input after the inputs are signed.
///
/// Defaults to `true` which will try fianlizing psbt after inputs are signed.
pub try_finalize: bool,
}

#[allow(clippy::derivable_impls)]
Expand All @@ -681,6 +685,7 @@ impl Default for SignOptions {
assume_height: None,
allow_all_sighashes: false,
remove_partial_sigs: true,
try_finalize: true,
}
}
}
Expand Down

0 comments on commit 6a79f39

Please sign in to comment.