Skip to content

Commit 0eb1ac2

Browse files
committed
Merge #1310: Remove extra taproot fields when finalizing PSBT
5840ce4 fix(bdk): Remove extra taproot fields when finalizing Psbt (vmammal) 8c78a42 test(psbt): Fixup test_psbt_multiple_internalkey_signers (vmammal) Pull request description: We currently allow removing `partial_sigs` from a finalized PSBT, which is relevant to non-taproot inputs, however taproot related PSBT fields were left in place despite the recommendation of BIP371 to remove them once the `final_script_witness` is constructed. This can cause confusion for parsers that encounter extra taproot metadata in an already satisfied input. Fix this by introducing a new member to SignOptions `remove_taproot_extras`, which when true will remove extra taproot related data from a PSBT upon successful finalization. This change makes removal of all taproot extras the default but configurable. fixes #1243 ### Notes to the reviewers If there's a better or more descriptive name for `remove_taproot_extras`, I'm open to changing it. ### Changelog notice Fixed an [issue](#1243) finalizing taproot inputs following BIP371 ### 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 #### Bugfixes: <!-- * [ ] This pull request breaks the existing API --> * [x] I've added tests to reproduce the issue which are now passing * [x] I'm linking the issue being fixed by this PR ACKs for top commit: evanlinjin: ACK 5840ce4 Tree-SHA512: 69f022c6f736500590e36880dd2c845321464f89af6a2c67987d1c520f70a298328363cade0f55f270c4e7169e740bd4ada752ec2c75afa02b6a5a851f9030c3
2 parents 6e8a4a8 + 5840ce4 commit 0eb1ac2

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,15 @@ impl<D> Wallet<D> {
19711971
if sign_options.remove_partial_sigs {
19721972
psbt_input.partial_sigs.clear();
19731973
}
1974+
if sign_options.remove_taproot_extras {
1975+
// We just constructed the final witness, clear these fields.
1976+
psbt_input.tap_key_sig = None;
1977+
psbt_input.tap_script_sigs.clear();
1978+
psbt_input.tap_scripts.clear();
1979+
psbt_input.tap_key_origins.clear();
1980+
psbt_input.tap_internal_key = None;
1981+
psbt_input.tap_merkle_root = None;
1982+
}
19741983
}
19751984
Err(_) => finished = false,
19761985
}
@@ -1979,6 +1988,12 @@ impl<D> Wallet<D> {
19791988
}
19801989
}
19811990

1991+
if finished && sign_options.remove_taproot_extras {
1992+
for output in &mut psbt.outputs {
1993+
output.tap_key_origins.clear();
1994+
}
1995+
}
1996+
19821997
Ok(finished)
19831998
}
19841999

crates/bdk/src/wallet/signer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,16 @@ pub struct SignOptions {
782782
/// Defaults to `true` which will remove partial signatures during finalization.
783783
pub remove_partial_sigs: bool,
784784

785+
/// Whether to remove taproot specific fields from the PSBT on finalization.
786+
///
787+
/// For inputs this includes the taproot internal key, merkle root, and individual
788+
/// scripts and signatures. For both inputs and outputs it includes key origin info.
789+
///
790+
/// Defaults to `true` which will remove all of the above mentioned fields when finalizing.
791+
///
792+
/// See [`BIP371`](https://github.com/bitcoin/bips/blob/master/bip-0371.mediawiki) for details.
793+
pub remove_taproot_extras: bool,
794+
785795
/// Whether to try finalizing the PSBT after the inputs are signed.
786796
///
787797
/// Defaults to `true` which will try finalizing PSBT after inputs are signed.
@@ -827,6 +837,7 @@ impl Default for SignOptions {
827837
assume_height: None,
828838
allow_all_sighashes: false,
829839
remove_partial_sigs: true,
840+
remove_taproot_extras: true,
830841
try_finalize: true,
831842
tap_leaves_options: TapLeavesOptions::default(),
832843
sign_with_tap_internal_key: true,

crates/bdk/tests/psbt.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,26 @@ fn test_psbt_fee_rate_with_missing_txout() {
162162
fn test_psbt_multiple_internalkey_signers() {
163163
use bdk::signer::{SignerContext, SignerOrdering, SignerWrapper};
164164
use bdk::KeychainKind;
165-
use bitcoin::{secp256k1::Secp256k1, PrivateKey};
166-
use miniscript::psbt::PsbtExt;
165+
use bitcoin::key::TapTweak;
166+
use bitcoin::secp256k1::{schnorr, KeyPair, Message, Secp256k1, XOnlyPublicKey};
167+
use bitcoin::sighash::{Prevouts, SighashCache, TapSighashType};
168+
use bitcoin::{PrivateKey, TxOut};
167169
use std::sync::Arc;
168170

169171
let secp = Secp256k1::new();
170-
let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig());
172+
let wif = "cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG";
173+
let desc = format!("tr({})", wif);
174+
let prv = PrivateKey::from_wif(wif).unwrap();
175+
let keypair = KeyPair::from_secret_key(&secp, &prv.inner);
176+
177+
let (mut wallet, _) = get_funded_wallet(&desc);
178+
let to_spend = wallet.get_balance().total();
171179
let send_to = wallet.get_address(AddressIndex::New);
172180
let mut builder = wallet.build_tx();
173-
builder.add_recipient(send_to.script_pubkey(), 10_000);
181+
builder.drain_to(send_to.script_pubkey()).drain_wallet();
174182
let mut psbt = builder.finish().unwrap();
183+
let unsigned_tx = psbt.unsigned_tx.clone();
184+
175185
// Adds a signer for the wrong internal key, bdk should not use this key to sign
176186
wallet.add_signer(
177187
KeychainKind::External,
@@ -184,10 +194,32 @@ fn test_psbt_multiple_internalkey_signers() {
184194
},
185195
)),
186196
);
187-
let _ = wallet.sign(&mut psbt, SignOptions::default()).unwrap();
188-
// Checks that we signed using the right key
189-
assert!(
190-
psbt.finalize_mut(&secp).is_ok(),
191-
"The wrong internal key was used"
192-
);
197+
let finalized = wallet.sign(&mut psbt, SignOptions::default()).unwrap();
198+
assert!(finalized);
199+
200+
// To verify, we need the signature, message, and pubkey
201+
let witness = psbt.inputs[0].final_script_witness.as_ref().unwrap();
202+
assert!(!witness.is_empty());
203+
let signature = schnorr::Signature::from_slice(witness.iter().next().unwrap()).unwrap();
204+
205+
// the prevout we're spending
206+
let prevouts = &[TxOut {
207+
script_pubkey: send_to.script_pubkey(),
208+
value: to_spend,
209+
}];
210+
let prevouts = Prevouts::All(prevouts);
211+
let input_index = 0;
212+
let mut sighash_cache = SighashCache::new(unsigned_tx);
213+
let sighash = sighash_cache
214+
.taproot_key_spend_signature_hash(input_index, &prevouts, TapSighashType::Default)
215+
.unwrap();
216+
let message = Message::from(sighash);
217+
218+
// add tweak. this was taken from `signer::sign_psbt_schnorr`
219+
let keypair = keypair.tap_tweak(&secp, None).to_inner();
220+
let (xonlykey, _parity) = XOnlyPublicKey::from_keypair(&keypair);
221+
222+
// Must verify if we used the correct key to sign
223+
let verify_res = secp.verify_schnorr(&signature, &message, &xonlykey);
224+
assert!(verify_res.is_ok(), "The wrong internal key was used");
193225
}

crates/bdk/tests/wallet.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,32 @@ fn test_get_address_no_reuse_single_descriptor() {
28312831
});
28322832
}
28332833

2834+
#[test]
2835+
fn test_taproot_remove_tapfields_after_finalize_sign_option() {
2836+
let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree());
2837+
2838+
let addr = wallet.get_address(New);
2839+
let mut builder = wallet.build_tx();
2840+
builder.drain_to(addr.script_pubkey()).drain_wallet();
2841+
let mut psbt = builder.finish().unwrap();
2842+
let finalized = wallet.sign(&mut psbt, SignOptions::default()).unwrap();
2843+
assert!(finalized);
2844+
2845+
// removes tap_* from inputs
2846+
for input in &psbt.inputs {
2847+
assert!(input.tap_key_sig.is_none());
2848+
assert!(input.tap_script_sigs.is_empty());
2849+
assert!(input.tap_scripts.is_empty());
2850+
assert!(input.tap_key_origins.is_empty());
2851+
assert!(input.tap_internal_key.is_none());
2852+
assert!(input.tap_merkle_root.is_none());
2853+
}
2854+
// removes key origins from outputs
2855+
for output in &psbt.outputs {
2856+
assert!(output.tap_key_origins.is_empty());
2857+
}
2858+
}
2859+
28342860
#[test]
28352861
fn test_taproot_psbt_populate_tap_key_origins() {
28362862
let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig_xprv());

0 commit comments

Comments
 (0)