<p>Yes, this does work, just beware that when it says </p>

<pre><code>'script': '' # Nothing
</code></pre>

<p>you should actually place a 0x00 there.  Once I did that, I successfully managed to broadcast my multiple inputs transaction.  Initially, I literally had nothing there (not a single byte), which is not gonna work.</p>

CC BY-SA 4.0
2019-03-29T03:21:05.790Z
2019-03-29T03:26:54.697Z
2019-03-29T03:26:54.697Z
Michael
<p>It worked for me too.</p>

<p>You just have to </p>

<p>1) Compose signable for the first input, with zero length string as script for all inputs besides the one that is signed</p>

<p>2) Sign the first input with der encoding of signed signable from 1.</p>

<p>3) Have the same operation on every input</p>

CC BY-SA 3.0
2016-01-20T03:15:22.737Z
2016-01-20T03:15:22.737Z
Mikhail Baynov
<p>I found the way to do it, so, if anyone is interested, here is how to do it:</p>

<p>When you have more than 1 input, you don't have to remove the inputs that you are not going to sign, you have to remove only <strong>their scripts</strong>. So, if you want to sign the transaction posted in the question, the first hash would be calculated like so:</p>

<pre><code>'version': 1,
'inputs': (2)
    {
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
    }, {
    'output_tx_hash': 'bbbb',
    'output_position': 1,
    'script': '', # Nothing
    'sequence': ffffffff,
    }
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0
</code></pre>

<p>And the second hash:</p>

<pre><code>'version': 1,
'inputs': (2)
    {
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': '', # Nothing
    'sequence': ffffffff,
    }, {
    'output_tx_hash': 'bbbb',
    'output_position': 1,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
    }
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0
</code></pre>

<p>Hope it helps anyone.</p>

CC BY-SA 3.0
2017-02-23T00:09:14.043Z
Dude, I've been researching this all friggin' day. I wish I would've seen this five hours ago. Thanks so much.
Festus Martingale
CC BY-SA 4.0
2019-07-14T17:27:27.970Z
Same! I couldn't be more happy to find this.
rllola
CC BY-SA 4.0
2020-04-24T14:11:14.967Z
So what do you do with the two hashes?
mancini0
CC BY-SA 3.0
2015-11-04T16:01:52.037Z
2015-11-04T16:01:52.037Z
Nathan Parker
<p>I have been able to program an application in python that sends valid transactions that spend 1 input, but I really can't get it to work with 2 or more inputs.</p>

<p>So my question is, how exactly is the hash that has to be signed for each input calculated?</p>

<p>What I have been doing so far, is, for example, if we have 2 inputs, we generate a transaction that spends only that first input, and thats the hash that has to be signed for the first input. And the same with the second input, we generate a transaction that only spends that second input. Both transactions need to have the same outputs.</p>

<p>Is that correct? For example, we want to spend input <strong>aaaa:0</strong> and input <strong>bbbb:1</strong> and we want to send them to some address:</p>

<p>To calculate the hash that the first input has to sign, we need to generate this transaction:</p>

<pre><code>'version': 1,
'inputs': (1)
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0
</code></pre>

<p>And to calculate the hash that the second input has to sign:</p>

<pre><code>'version': 1,
'inputs': (1)
    'output_tx_hash': 'bbbb', # this changes
    'output_position': 1,     # this changes
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0
</code></pre>

<p>We serialize them, add them 01000000 (SIGHASH_ALL), double sha256 that, sign it, and add 01. That way we get the two signatures that we will use on each input respectively. My application does exactly that and doesn't work. What am I missing?</p>

CC BY-SA 3.0
2015-11-03T21:51:46.067Z
Perhaps take a look at [python-bitcoinlib](https://github.com/petertodd/python-bitcoinlib).
Claris
CC BY-SA 3.0
2015-11-04T03:30:32.097Z
The exact procedure is documented in this question: http://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx
Jimmy Song
CC BY-SA 3.0
2015-11-04T13:08:48.960Z
That procedure shows how to sign a transaction with only one input. Thats the answer I used to learn how to sign 1 input, and I achieved it, but with 2 or more inputs I just can't make it work, and that answer doesn't say how it should be done.
Nathan Parker
<transactions><signature><raw-transaction>
How to sign a transaction with multiple inputs?
Nathan Parker
# How to sign a transaction with multiple inputs?

(Asked by: username_0 on 2015-11-03)

I have been able to program an application in python that sends valid transactions that spend 1 input, but I really can't get it to work with 2 or more inputs.

So my question is, how exactly is the hash that has to be signed for each input calculated?

What I have been doing so far, is, for example, if we have 2 inputs, we generate a transaction that spends only that first input, and thats the hash that has to be signed for the first input. And the same with the second input, we generate a transaction that only spends that second input. Both transactions need to have the same outputs.

Is that correct? For example, we want to spend input **aaaa:0** and input **bbbb:1** and we want to send them to some address:

To calculate the hash that the first input has to sign, we need to generate this transaction:

```
'version': 1,
'inputs': (1)
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0

```

And to calculate the hash that the second input has to sign:

```
'version': 1,
'inputs': (1)
    'output_tx_hash': 'bbbb', # this changes
    'output_position': 1,     # this changes
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0

```

We serialize them, add them 01000000 (SIGHASH\_ALL), double sha256 that, sign it, and add 01. That way we get the two signatures that we will use on each input respectively. My application does exactly that and doesn't work. What am I missing?

**Tags:** transactions, signature, raw-transaction

> **Comments:**
> **username_1**: Perhaps take a look at [python-bitcoinlib](https://github.com/petertodd/python-bitcoinlib).  
> **username_2**: The exact procedure is documented in this question: http://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx  
> **username_0**: That procedure shows how to sign a transaction with only one input. Thats the answer I used to learn how to sign 1 input, and I achieved it, but with 2 or more inputs I just can't make it work, and that answer doesn't say how it should be done.  

---

## 3 answers

### Answer by username_0 on 2015-11-04. Score: 20 | Accepted answer

I found the way to do it, so, if anyone is interested, here is how to do it:

When you have more than 1 input, you don't have to remove the inputs that you are not going to sign, you have to remove only **their scripts**. So, if you want to sign the transaction posted in the question, the first hash would be calculated like so:

```
'version': 1,
'inputs': (2)
    {
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
    }, {
    'output_tx_hash': 'bbbb',
    'output_position': 1,
    'script': '', # Nothing
    'sequence': ffffffff,
    }
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0

```

And the second hash:

```
'version': 1,
'inputs': (2)
    {
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': '', # Nothing
    'sequence': ffffffff,
    }, {
    'output_tx_hash': 'bbbb',
    'output_position': 1,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
    }
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0

```

Hope it helps anyone.

> **Comments:**
> **username_5**: Dude, I've been researching this all friggin' day. I wish I would've seen this five hours ago. Thanks so much.  
> **username_6**: Same! I couldn't be more happy to find this.  
> **username_7**: So what do you do with the two hashes?  

---

### Answer by username_3 on 2019-03-29. Score: 1

Yes, this does work, just beware that when it says

```
'script': '' # Nothing

```

you should actually place a 0x00 there. Once I did that, I successfully managed to broadcast my multiple inputs transaction. Initially, I literally had nothing there (not a single byte), which is not gonna work.


---

### Answer by username_4 on 2016-01-20. Score: 1

It worked for me too.

You just have to

1) Compose signable for the first input, with zero length string as script for all inputs besides the one that is signed

2) Sign the first input with der encoding of signed signable from 1.

3) Have the same operation on every input


---

