Not in new code but in older code - the checking for aggregate pubkey in both pubnonce and partialsig items is similar flow that can be made a common function.
diff --git a/test/functional/wallet_musig.py b/test/functional/wallet_musig.py
index 9cc4caff73..ff067f8079 100755
--- a/test/functional/wallet_musig.py
+++ b/test/functional/wallet_musig.py
@@ -112,6 +112,59 @@ class WalletMuSigTest(BitcoinTestFramework):
+ def assert_pubkey_in_musig_items(self, dec_psbt_input, pubkeyhash=False, pubnonce=False, partialsig=False):
+ assert_equal(pubnonce ^ partialsig, True) # Either of both should be True
+ if pubnonce:
+ items = dec_psbt_input["musig2_pubnonces"]
+ error_key = "pubnonce"
+ else:
+ items = dec_psbt_input["musig2_partial_sigs"]
+ error_key = "partial sig"
+
+ for i in range(0, len(items)):
+ item = items[i]
+ pubkey = item["aggregate_pubkey"][2:]
+ if pubkeyhash:
+ pubkey = hash160(bytes.fromhex(pubkey)).hex()
+ if pubkey in dec_psbt_input["witness_utxo"]["scriptPubKey"]["hex"]:
+ continue
+ elif "taproot_scripts" in dec_psbt_input:
+ for leaf_scripts in dec_psbt_input["taproot_scripts"]:
+ if pubkey in leaf_scripts["script"]:
+ break
+ else:
+ assert False, f"Aggregate pubkey for {error_key} not seen as output key or in any scripts"
+ else:
+ assert False, f"Aggregate pubkey for {error_key} not seen as output key or internal key"
+
def test_failure_case_1(self, comment, pat):
self.log.info(f"Testing {comment}")
wallets, psbt = self.setup_musig_scenario(pat)
@@ -264,34 +317,8 @@ class WalletMuSigTest(BitcoinTestFramework):
comb_nonce_psbt2 = self.nodes[0].combinepsbt(nonce_psbts2)
dec_psbt = self.nodes[0].decodepsbt(comb_nonce_psbt)
- assert_equal(len(dec_psbt["inputs"][0]["musig2_pubnonces"]), expected_pubnonces)
- for i in range(0, len(dec_psbt["inputs"][0]["musig2_pubnonces"])):
- pn = dec_psbt["inputs"][0]["musig2_pubnonces"][i]
- pubkey = pn["aggregate_pubkey"][2:]
- if "pkh" in pattern or "pk_h" in pattern:
- pubkey = hash160(bytes.fromhex(pubkey)).hex()
- if pubkey in dec_psbt["inputs"][0]["witness_utxo"]["scriptPubKey"]["hex"]:
- continue
- elif "taproot_scripts" in dec_psbt["inputs"][0]:
- for leaf_scripts in dec_psbt["inputs"][0]["taproot_scripts"]:
- if pubkey in leaf_scripts["script"]:
- break
- else:
- assert False, "Aggregate pubkey for pubnonce not seen as output key, or in any scripts"
- else:
- assert False, "Aggregate pubkey for pubnonce not seen as output key or internal key"
+ self.assert_pubkey_in_musig_items(dec_psbt["inputs"][0], "pkh" in pattern or "pk_h" in pattern, True, False)
# Add partial sigs
psig_psbts = []
@@ -308,40 +335,13 @@ class WalletMuSigTest(BitcoinTestFramework):
comb_psig_psbt2 = self.nodes[0].combinepsbt(psig_psbts2)
dec_psbt = self.nodes[0].decodepsbt(comb_psig_psbt)
- assert_equal(len(dec_psbt["inputs"][0]["musig2_partial_sigs"]), expected_partial_sigs)
- for i in range(0, len(dec_psbt["inputs"][0]["musig2_partial_sigs"])):
- ps = dec_psbt["inputs"][0]["musig2_partial_sigs"][i]
- pubkey = ps["aggregate_pubkey"][2:]
- if "pkh" in pattern or "pk_h" in pattern:
- pubkey = hash160(bytes.fromhex(pubkey)).hex()
- if pubkey in dec_psbt["inputs"][0]["witness_utxo"]["scriptPubKey"]["hex"]:
- continue
- elif "taproot_scripts" in dec_psbt["inputs"][0]:
- for leaf_scripts in dec_psbt["inputs"][0]["taproot_scripts"]:
- if pubkey in leaf_scripts["script"]:
- break
- else:
- assert False, "Aggregate pubkey for partial sig not seen as output key or in any scripts"
- else:
- assert False, "Aggregate pubkey for partial sig not seen as output key"
+ self.assert_pubkey_in_musig_items(dec_psbt["inputs"][0], "pkh" in pattern or "pk_h" in pattern, False, True)