This PR fixes a dormant bug in MiniWallet that exists since support for P2TR was initially added in #23371 (see commit 041abfebe49ae5e3e882c00cc5caea1365a27a49).
In the course of spending the output, the leaf version byte of the control block in the witness stack doesn’t set the parity bit, i.e. we were so far just lucky that the used combinations of relevant data (internal pubkey, leaf script / version) didn’t result in a tweaked pubkey with odd y-parity. If that was the case, we’d get the following validation error:
mandatory-script-verify-flag-failed (Witness program hash mismatch) (-26)
Since MiniWallets can now optionally be tagged (#29939), resulting in different internal pubkeys, the issue is more prevalent now. Fix it by passing the parity bit, as specified in BIP341.
Can be tested with the following patch (fails on master, succeeds on PR):
0diff --git a/test/functional/test_framework/mempool_util.py b/test/functional/test_framework/mempool_util.py
1index 148cc935ed..7ebe858681 100644
2--- a/test/functional/test_framework/mempool_util.py
3+++ b/test/functional/test_framework/mempool_util.py
4@@ -42,7 +42,7 @@ def fill_mempool(test_framework, node):
5 # Generate UTXOs to flood the mempool
6 # 1 to create a tx initially that will be evicted from the mempool later
7 # 75 transactions each with a fee rate higher than the previous one
8- ephemeral_miniwallet = MiniWallet(node, tag_name="fill_mempool_ephemeral_wallet")
9+ ephemeral_miniwallet = MiniWallet(node, tag_name="fill_mempool_ephemeral_wallet3")
10 test_framework.generate(ephemeral_miniwallet, 1 + num_of_batches * tx_batch_size)
11
12 # Mine enough blocks so that the UTXOs are allowed to be spent
In addition to that, another bug is fixed where the internal key derivation failed, as not every pseudorandom hash results in a valid x-only pubkey. Fix this by treating the hash result as private key and calculate the x-only public key out of that, to be used then as internal key.
Fixes #30528.