<blockquote>
<p>I understand that second input can be not signed (really?).</p>
</blockquote>
<p>No, it can and still must be signed. The use of <code>SIGHASH_SINGLE</code> does not remove the need to produce a valid signature. The script interpreter will not know or care that you wanted to use <code>SIGHASH_SINGLE</code> there or that other signatures in the transaction used <code>SIGHASH_SINGLE</code>. All it wants is a signature, the sighash type doesn't matter. Keep in mind that each signature can use a different sighash type, they don't all need to be the same.</p>
<p><code>SIGHASH_SINGLE</code> for an input that does not have an output at the same index use the number <code>1</code> (serialized as a 256 bit little endian integer) as the sighash. Python-bitcoinlib's <code>RawSignatureHash</code> will do this for you. <code>SignatureHash</code> does not because it's generally unsafe to produce such a signature since the input can be trivially included in another transaction by a someone else and your funds taken.</p>

CC BY-SA 4.0
2023-08-05T18:16:55.423Z
Do I need to make separate `CMutableTxIn`, `CScript`, `SignatureHash` and `RawSignatureHash` for second input?
pbies
CC BY-SA 4.0
2023-08-05T18:29:14.710Z
Yes. Inputs are independent of each other.
Ava Chow
CC BY-SA 4.0
2023-08-05T16:02:51.107Z
2023-08-05T16:02:51.107Z
Ava Chow
<p>I am trying to make a SIGHASH_SINGLE transaction.</p>
<p>I have two inputs, one output. I understand that second input can be not signed (really?).</p>
<p>Script I have is:</p>
<pre class="lang-python prettyprint-override"><code>#!/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([&quot;/mnt/c/Program Files/Bitcoin/daemon/bitcoin-cli.exe&quot;, &quot;sendrawtransaction&quot;, b2x(tx.serialize())])
</code></pre>
<p>I am getting error:</p>
<pre><code>error code: -25
error message:
bad-txns-inputs-missingorspent
</code></pre>
<p>Both inputs have balance, I am sure of that. Script is run from WSL1 Bash on Windows.</p>
<p>What am I missing here?</p>

<transactions><sighash><sighash-flags>
Trying to make SIGHASH_SINGLE transaction
pbies
# Trying to make SIGHASH_SINGLE transaction

(Asked by: username_0 on 2023-08-05)

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?).
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. Script is run from WSL1 Bash on Windows.
What am I missing here?

**Tags:** transactions, sighash, sighash-flags


---

## 1 answers

### Answer by username_1 on 2023-08-05. Score: 3 | Accepted answer

> I understand that second input can be not signed (really?).

No, it can and still must be signed. The use of `SIGHASH_SINGLE` does not remove the need to produce a valid signature. The script interpreter will not know or care that you wanted to use `SIGHASH_SINGLE` there or that other signatures in the transaction used `SIGHASH_SINGLE`. All it wants is a signature, the sighash type doesn't matter. Keep in mind that each signature can use a different sighash type, they don't all need to be the same.

`SIGHASH_SINGLE` for an input that does not have an output at the same index use the number `1` (serialized as a 256 bit little endian integer) as the sighash. Python-bitcoinlib's `RawSignatureHash` will do this for you. `SignatureHash` does not because it's generally unsafe to produce such a signature since the input can be trivially included in another transaction by a someone else and your funds taken.

> **Comments:**
> **username_0**: Do I need to make separate `CMutableTxIn`, `CScript`, `SignatureHash` and `RawSignatureHash` for second input?  
> **username_1**: Yes. Inputs are independent of each other.  

---

