I'm in the process of attempting to generate a transaction using the Python Bitcoin Utils library. that involves P2WSH-over-P2SH (P2SH-P2WSH). My activity is on the regtest network. Below is the code snippet and address that I've prepared.
txin_redeemScript = Script([
'OP_2',
private_key1.get_public_key().to_hex(),
private_key2.get_public_key().to_hex(),
'OP_2',
'OP_CHECKMULTISIG'
])
script_bytes = txin_redeemScript.to_bytes()
hashsha256 = hashlib.sha256(script_bytes).digest()
hashsha256 = hexlify(hashsha256).decode('utf-8')
txin_scriptPubKey = Script(['OP_0', hashsha256])
txin_p2wsh_address = P2shAddress.from_script(txin_scriptPubKey)
This address is where I intend to send the coins, and this is the transaction I'm forming:
p2pkh_addr = P2pkhAddress('n4bkvTyU1dVdzsrhWBqBw8fEMbHjJvtmJR')
txout = TxOutput(to_satoshis(amount_to_send), p2pkh_addr.to_script_pub_key())
tx = Transaction(inputs=p2sh_utxo, outputs=[txout], has_segwit=False)
This is the transaction and raw transaction ID
[{'address': '2N4vHiXTCCVMt4TYimTGXRCySzNLXkdiPyH', 'category': 'send', 'amount': Decimal('-0.10000000'), 'vout': 1, 'fee': Decimal('-0.00001430'), 'confirmations': 0, 'trusted': True, 'txid': 'f5f9c98b0c7b0731cba11f5a1b0494a62d2a2de5c09aaf3ac37fa56d48c271bd', 'wtxid': 'bed63897ca9c23ee891a9752b143ee063bf9aa97c813c5a455210a5f8437be6a', 'walletconflicts': [], 'time': 1693271904, 'timereceived': 1693271904, 'bip125-replaceable': 'yes', 'abandoned': False}]
0200000001bd71c2486da57fc33aaf9ac0e52d2a2da694041b5a1fa1cb31077b0c8bc9f9f50100000000ffffffff015f5d9800000000001976a914fd337ad3bf81e086d96a68e1f8d6a0a510f8c24a88ac00000000
The following code is utilized to sign the transaction:
for tx_idx, txin in enumerate(p2sh_utxo):
sig1 = sk1.sign_input(tx, tx_idx, txin_redeemScript)
sig2 = sk2.sign_input(tx, tx_idx, txin_redeemScript)
unlocking_script = Script(['OP_0', txin_redeemScript.to_hex()])
txin.script_sig = unlocking_script
witness_data = [b'','OP_0', sig1, sig2]
tx.witnesses.append(TxWitnessInput(witness_data))
The mempool acceptance test is failing with the error 'reject-reason': 'mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)'}
My familiarity with bitcoin programming is limited, and I've been unable to locate any sample code or references concerning P2WSH over P2SH. Despite searching various online resources, I have yet to find any explanations on how the spending process operates. Any help would be appreciated.
You can find the complete code here
https://github.com/Deepanshu2017/bitcoingist/blob/main/P2SH-P2WSH.ipynb
I have modified the question to attach the notebook URL
– Deepanshu Aug 31 '23 at 11:35bitcoin-cli -regtest sendtoaddress "2N4vHiXTCCVMt4TYimTGXRCySzNLXkdiPyH" 0.1Post this starting from cell number 5 in the notebook, I am trying to spend this. The choice of env (regtest/test) doesn't matter in my case. All I am trying to do is P2WSH in P2SH – Deepanshu Sep 01 '23 at 14:48sign_segwit_inputdid not help. Thank you for the help. Whenever you have time, you can fix the issue, this may help the community to have some sort of reference and would help me as well to eventually figure out the issue. – Deepanshu Sep 03 '23 at 07:28