<p>The data to be signed is called <em>Hash Preimage</em>. Then you sign the double SHA256 of hash preimage, which is 256-bits long. Before hashing, the data must be formed as shown in <a href="https://bitcoin.stackexchange.com/a/5241/38618">this answer</a>. If you're signing a SegWit transaction, the contents of the preimage changes, as defined in <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="nofollow noreferrer">BIP 143</a>.</p>

<p>Then you can use the formulae <a href="https://www.maximintegrated.com/en/app-notes/index.mvp/id/5767" rel="nofollow noreferrer">here</a>, Eq. 2:</p>

<pre><code>(x1, y1) = k × G(x, y) mod p
r = x1 mod n
</code></pre>

<p><code>k</code> is a random number, <code>G</code> is the generator, <code>p</code> is the prime modulo, etc.</p>

<p>Then,</p>

<pre><code>s = (k^-1 (h(m) + d * r)) mod n
</code></pre>

<p><code>d</code> is the private key, <code>h(m)</code> is the double SHA256 result. Now, you can use <code>r</code>, <code>s</code> to create the DER-encoded signature.</p>

CC BY-SA 4.0
2018-08-19T10:38:17.480Z
I read [this](https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx/5241#5241) but this solution was for only a transaction with one output. if my outputs becomes 2 or 3 now how must make scriptsig?
saeid ezzati
CC BY-SA 4.0
2018-08-19T13:42:16.557Z
Here's [an example](https://en.bitcoin.it/wiki/OP_CHECKSIG#Code_samples_and_raw_dumps) with multiple outputs. Here's the specification for outputs in [BIP143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification), scroll down to `hashOutputs`.
MCCCS
CC BY-SA 4.0
2018-08-19T16:22:37.757Z
I read [this link](https://en.bitcoin.it/wiki/OP_CHECKSIG#Code_samples_and_raw_dumps) But it's not explained at all about how the script signature section was created !!
saeid ezzati
CC BY-SA 4.0
2018-08-19T19:18:22.033Z
After you formed the data in the examples, you double sha256 it and use the equations above. Do you want to learn how to do that using libsecp256k1?
MCCCS
CC BY-SA 4.0
2018-08-21T07:30:16.597Z
no. I have a library that do the sign operation for me. I only want to know exactly which parts of the transaction do I need to put together, and what order should I use before sign operation with more than one input and one output ?
saeid ezzati
CC BY-SA 4.0
2018-08-19T07:01:16.493Z
2018-08-19T07:01:16.493Z
MCCCS
<p>I studied many links about bitcoin trxs and about generating scriptSig.
Finally I couldn't understand how to make this part.</p>

<p>for example I read this links:</p>

<p><a href="https://bitcoin.stackexchange.com/questions/8250/what-is-relation-between-scriptsig-and-scriptpubkey">What is relation between scriptSig and scriptPubKey?</a></p>

<p><a href="https://bitcoin.org/en/developer-guide#transactions" rel="nofollow noreferrer">https://bitcoin.org/en/developer-guide#transactions</a></p>

<p><a href="https://bitcoin.stackexchange.com/questions/58076/how-do-you-create-a-scriptsig-for-a-new-raw-tx/58089">How do you create a scriptsig for a new raw tx?</a></p>

<p><a href="https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx">How to redeem a basic Tx?</a></p>

<p><a href="https://bitcoin.stackexchange.com/questions/36665/how-to-create-the-sig-pubk-part-in-scriptsig">How to create the &lt;sig&gt; &lt;PubK&gt; part in &quot;scriptSig&quot;?</a></p>

<p><a href="http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html" rel="nofollow noreferrer">http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html</a></p>

<p><a href="https://en.bitcoin.it/wiki/Protocol_documentation#tx" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_documentation#tx</a></p>

<p><a href="https://en.bitcoin.it/wiki/Transaction" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Transaction</a></p>

<p>I found that I must sign a thing but whats that thing ?</p>

<p>exactly and step by step What should I put together and then sign it?</p>

<p>specially if I had more than 1 input how make sigscript ?</p>

<p>I have a library that does the sign operation for me. however, I don't know what structure I need to use for the message that is going to be signed especially when there are multiple outputs.</p>

<raw-transaction><input-script>
how generate <sig> part of scriptSig in bitcoin transactions
saeid ezzati
# how generate <sig> part of scriptSig in bitcoin transactions

(Asked by: username_0 on 2018-08-18)

I studied many links about bitcoin trxs and about generating scriptSig.
Finally I couldn't understand how to make this part.

for example I read this links:

[What is relation between scriptSig and scriptPubKey?](https://bitcoin.stackexchange.com/questions/8250/what-is-relation-between-scriptsig-and-scriptpubkey)

<https://bitcoin.org/en/developer-guide#transactions>

[How do you create a scriptsig for a new raw tx?](https://bitcoin.stackexchange.com/questions/58076/how-do-you-create-a-scriptsig-for-a-new-raw-tx/58089)

[How to redeem a basic Tx?](https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx)

[How to create the <sig> <PubK> part in "scriptSig"?](https://bitcoin.stackexchange.com/questions/36665/how-to-create-the-sig-pubk-part-in-scriptsig)

<http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html>

<https://en.bitcoin.it/wiki/Protocol_documentation#tx>

<https://en.bitcoin.it/wiki/Transaction>

I found that I must sign a thing but whats that thing ?

exactly and step by step What should I put together and then sign it?

specially if I had more than 1 input how make sigscript ?

I have a library that does the sign operation for me. however, I don't know what structure I need to use for the message that is going to be signed especially when there are multiple outputs.

**Tags:** raw-transaction, input-script


---

## 1 answers

### Answer by username_1 on 2018-08-19. Score: 1

The data to be signed is called *Hash Preimage*. Then you sign the double SHA256 of hash preimage, which is 256-bits long. Before hashing, the data must be formed as shown in [this answer](https://bitcoin.stackexchange.com/a/5241/38618). If you're signing a SegWit transaction, the contents of the preimage changes, as defined in [BIP 143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki).

Then you can use the formulae [here](https://www.maximintegrated.com/en/app-notes/index.mvp/id/5767), Eq. 2:

```
(x1, y1) = k × G(x, y) mod p
r = x1 mod n

```

`k` is a random number, `G` is the generator, `p` is the prime modulo, etc.

Then,

```
s = (k^-1 (h(m) + d * r)) mod n

```

`d` is the private key, `h(m)` is the double SHA256 result. Now, you can use `r`, `s` to create the DER-encoded signature.

> **Comments:**
> **username_0**: I read [this](https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx/5241#5241) but this solution was for only a transaction with one output. if my outputs becomes 2 or 3 now how must make scriptsig?  
> **username_1**: Here's [an example](https://en.bitcoin.it/wiki/OP_CHECKSIG#Code_samples_and_raw_dumps) with multiple outputs. Here's the specification for outputs in [BIP143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification), scroll down to `hashOutputs`.  
> **username_0**: I read [this link](https://en.bitcoin.it/wiki/OP_CHECKSIG#Code_samples_and_raw_dumps) But it's not explained at all about how the script signature section was created !!  
> **username_1**: After you formed the data in the examples, you double sha256 it and use the equations above. Do you want to learn how to do that using libsecp256k1?  
> **username_0**: no. I have a library that do the sign operation for me. I only want to know exactly which parts of the transaction do I need to put together, and what order should I use before sign operation with more than one input and one output ?  

---

