I am trying to make a SIGHASH_SINGLE transaction.
I have two inputs, one output. I understand that second input can be not signed (really?).
I use Python 3.11.4.
I use these libraries:
apt install libssl-dev
pip3 install python-bitcoinlib
Script I have is:
#!/usr/bin/env python3
import hashlib
import subprocess
from bitcoin import SelectParams
from bitcoin.core import b2x, lx, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160
from bitcoin.core.script import CScript, OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL, SIGHASH_SINGLE
from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret
SelectParams('mainnet')
h = b'private key bytes in python hex format'
seckey = CBitcoinSecret.from_secret_bytes(h)
txid = lx('tx number 1')
vout = 0
txid2 = lx('tx number 2')
vout2 = 1
txin = CMutableTxIn(COutPoint(txid, vout))
txin2 = CMutableTxIn(COutPoint(txid2, vout2))
txin_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
txout = CMutableTxOut(10000, CBitcoinAddress('output address').to_scriptPubKey())
tx = CMutableTransaction([txin]+[txin2], [txout])
sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_SINGLE)
sig = seckey.sign(sighash) + bytes([SIGHASH_SINGLE])
txin.scriptSig = CScript([sig, seckey.pub])
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print(b2x(tx.serialize()))
r=subprocess.run(["/mnt/c/Program Files/Bitcoin/daemon/bitcoin-cli.exe", "sendrawtransaction", b2x(tx.serialize())])
I am getting error:
error code: -25
error message:
bad-txns-inputs-missingorspent
Both inputs have balance, I am sure of that. I am running script from WSL1 Bash on Windows.
What am I missing here?