<p>I'm trying to consolidate multiple inputs into a single output, all left over is for the miner.</p>

<p>When I send my transaction to a peer, it is never mined.
I believe something is invalid in the multi-input signatures.</p>

<p>This is a sample input transaction for <code>1MeTVq6X8LCtcRDG4MNz1QVezxzxyffSTL</code>:</p>

<pre><code>in_transactions = []
txid = 
lx('bad6a00b1ab3c453a3569b4b5373fcaaeefaa01b1e48efcb118c1e1e08597382')
vout = 4
txin = CMutableTxIn(COutPoint(txid, vout))
in_transactions.append(txin)
</code></pre>

<p>This is how I create and validate signatures:</p>

<pre><code>seckey = CBitcoinSecret("L1WIFKey...L1WIFKey")

# Create that scriptPubKey from scratch using the pubkey
# This is our signature to open each input
our_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])


# Create the txout.
their_scriptPubKey = CBitcoinAddress(output_address).to_scriptPubKey()
txout = CMutableTxOut((btc_i_have - minner_fee) * COIN, their_scriptPubKey)

# Create the unsigned transaction.
# in_transactions is a list of [txin,txin,...]
tx = CMutableTransaction(in_transactions, [txout])


# Set the scriptSig of our transaction inputs appropriately.
# THE BUG *MIGHT* BE SOMEWHERE HERE
for idx, txin in enumerate(in_transactions):
  sighash = SignatureHash(our_scriptPubKey, tx, idx, SIGHASH_ALL)
  sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])
  txin.scriptSig = CScript([sig, seckey.pub])
  VerifyScript(txin.scriptSig, our_scriptPubKey, tx, idx, (SCRIPT_VERIFY_P2SH,))

# finally this is the raw transaction
raw_transaction = b2x(tx.serialize())
</code></pre>

<p>I wonder if I'm signing the inputs wrong when I do it one by one.</p>

<p>Thanks in advance.</p>

<p>EDIT:</p>

<p>The code works, the error is silent. The transaction was dropped silently by the network. It was a wrong transaction because one of my vout was pointing to a wrong output.</p>

CC BY-SA 3.0
2017-12-23T07:28:02.477Z
Are you sure that all of the inputs correspond to the same address that you are using? What is the exact error that you are getting?
Ava Chow
CC BY-SA 3.0
2017-12-23T09:43:47.747Z
also have a look here: https://bitcoin.stackexchange.com/questions/41209/how-to-sign-a-transaction-with-multiple-inputs
pebwindkraft
CC BY-SA 3.0
2017-12-24T08:24:11.347Z
indeed it works. my issue was a wrong vout.
David Rz Ayala
<bitcoin-core><bitcoin-core-development><python>
Help with multi-input transaction by code
David Rz Ayala
# Help with multi-input transaction by code

(Asked by: username_0 on 2017-12-23)

I'm trying to consolidate multiple inputs into a single output, all left over is for the miner.

When I send my transaction to a peer, it is never mined.
I believe something is invalid in the multi-input signatures.

This is a sample input transaction for `1MeTVq6X8LCtcRDG4MNz1QVezxzxyffSTL`:

```
in_transactions = []
txid = 
lx('bad6a00b1ab3c453a3569b4b5373fcaaeefaa01b1e48efcb118c1e1e08597382')
vout = 4
txin = CMutableTxIn(COutPoint(txid, vout))
in_transactions.append(txin)

```

This is how I create and validate signatures:

```
seckey = CBitcoinSecret("L1WIFKey...L1WIFKey")

# Create that scriptPubKey from scratch using the pubkey
# This is our signature to open each input
our_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])

# Create the txout.
their_scriptPubKey = CBitcoinAddress(output_address).to_scriptPubKey()
txout = CMutableTxOut((btc_i_have - minner_fee) * COIN, their_scriptPubKey)

# Create the unsigned transaction.
# in_transactions is a list of [txin,txin,...]
tx = CMutableTransaction(in_transactions, [txout])

# Set the scriptSig of our transaction inputs appropriately.
# THE BUG *MIGHT* BE SOMEWHERE HERE
for idx, txin in enumerate(in_transactions):
  sighash = SignatureHash(our_scriptPubKey, tx, idx, SIGHASH_ALL)
  sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])
  txin.scriptSig = CScript([sig, seckey.pub])
  VerifyScript(txin.scriptSig, our_scriptPubKey, tx, idx, (SCRIPT_VERIFY_P2SH,))

# finally this is the raw transaction
raw_transaction = b2x(tx.serialize())

```

I wonder if I'm signing the inputs wrong when I do it one by one.

Thanks in advance.

EDIT:

The code works, the error is silent. The transaction was dropped silently by the network. It was a wrong transaction because one of my vout was pointing to a wrong output.

**Tags:** bitcoin-core, bitcoin-core-development, python

> **Comments:**
> **username_1**: Are you sure that all of the inputs correspond to the same address that you are using? What is the exact error that you are getting?  
> **username_2**: also have a look here: https://bitcoin.stackexchange.com/questions/41209/how-to-sign-a-transaction-with-multiple-inputs  
> **username_0**: indeed it works. my issue was a wrong vout.  

---

## 0 answers

