<p>Bitcoin Cash does not use the original Bitcoin sighash scheme (which looks like what you're trying to do here).</p>

<p>Instead, it uses a variant of the sighash scheme that was proposed in BIP143 (which is used for Segregated Witness transactions in Bitcoin).</p>

CC BY-SA 4.0
2018-05-09T01:04:57.700Z
Thank you Pieter! I hadn't realized that. I will get to reading that BIP!
craigpastro
CC BY-SA 4.0
2018-05-08T12:48:14.870Z
2018-05-08T12:48:14.870Z
Pieter Wuille
<p>Although you have followed the steps correctly, you are trying to "reinvent the wheel" by trying to sign it yourself via a python script. 
Bitcoin Cash software, just like bitcoin will provide you with the JSON RPC API's. These API's follow the reference implementation and already provide you with all the steps in built which you just performed. 
As an example/tutorial you can go through the <a href="https://bitcoin.org/en/developer-examples#transaction-tutorial" rel="nofollow noreferrer">tutorial</a> available on bitcoin.org which is very concise and explanatory.</p>

<p>Do note that you need a running node for you to be able to access the API's</p>

CC BY-SA 4.0
2018-05-09T00:59:56.410Z
Thank you Shabahat. The problem is that I want to construct my transaction completely offline. I did notice something in the link that you sent about offline signing, but will have to give it another read to understand it better.
craigpastro
CC BY-SA 4.0
2018-05-09T08:14:36.270Z
The transaction remains offline as long as the node you are running does not broadcast the transaction(which has a separate RPC). Go through the link you will understand what I am trying to say.
Shabahat M. Ayubi
CC BY-SA 4.0
2018-05-08T10:31:24.357Z
2018-05-08T10:31:24.357Z
Shabahat M. Ayubi
<p>I am trying to construct a raw transaction for bitcoin cash testnet. I have made a lot of use out of this <a href="https://bitcoin.stackexchange.com/a/5241/82868">very nice answer</a> to how to construct a raw transaction, but am still unable to construct a correct one myself. I would like to spend the the first output <a href="https://www.blocktrail.com/tBCC/tx/7098b2735336c85077e9704cb8ba521f5c2d92f768081345c5ded26c52fcc1a9" rel="nofollow noreferrer">7098b2735336c85077e9704cb8ba521f5c2d92f768081345c5ded26c52fcc1a9</a>.</p>

<p>Here is what I do:</p>

<ol>
<li><p>Create a transaction using the previous transaction hash and index with the scriptSig filled in with the scriptPubKey of the output I want to redeem. The raw transaction looks like this:</p>

<pre><code>01000000
01
a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870
00000000
19
76a914edc822bff914a255b819156c70e19dd09e8c0f6a88ac
ffffffff
01
80d6e34c00000000
19
76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac
00000000
</code></pre></li>
<li><p>To this I append a four-byte hash code type 41000000.</p>

<pre><code>01000000
01
a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870
00000000
19
76a914edc822bff914a255b819156c70e19dd09e8c0f6a88ac
ffffffff
01
80d6e34c00000000
19
76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac
00000000
41000000
</code></pre></li>
<li><p>Now I compute the sha256 hash of this two times which yields:</p>

<pre><code>f6b38f93c859db4920f22a1cd07d6c615a503ce89a7b2a3d90babad521b26062
</code></pre></li>
<li><p>Now I sign the result in 3. This could be where I am doing something wrong. The signature results in:</p>

<pre><code>r = 767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa6131
s = 6dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136
</code></pre>

<p>and then I encode it into DER format as</p>

<pre><code>30440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136
</code></pre>

<p>Does this look right?</p></li>
<li><p>To this signature I append the one-byte hash code type 41:</p>

<pre><code>30440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e64613641
</code></pre></li>
<li><p>Then I prepend the length of the result of 5:</p>

<pre><code>4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e64613641
</code></pre></li>
<li><p>Then I append <code>2103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72</code> (the length of the public key + the actual public key) to the result of 6 yielding:</p>

<pre><code>4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136412103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72
</code></pre></li>
<li><p>The result of 7 is my signature, its length is 0x6a, so now I replace the scriptSig_length and scriptSig in 1 with this data yielding:</p>

<pre><code>01000000
01
a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870
00000000
6a
4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136412103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72
ffffffff
01
80d6e34c00000000
19
76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac
00000000
</code></pre></li>
</ol>

<p>This is my raw transaction, but when I try to send it I get the following error:</p>

<pre><code>16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)
</code></pre>

<p>Can anyone tell me what I did wrong? Thank you very much!</p>

<bitcoin-cash><raw-transaction><ecdsa>
Unable to construct a correct raw transaction for bitcoin cash testnet
craigpastro
# Unable to construct a correct raw transaction for bitcoin cash testnet

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

I am trying to construct a raw transaction for bitcoin cash testnet. I have made a lot of use out of this [very nice answer](https://bitcoin.stackexchange.com/a/5241/82868) to how to construct a raw transaction, but am still unable to construct a correct one myself. I would like to spend the the first output [7098b2735336c85077e9704cb8ba521f5c2d92f768081345c5ded26c52fcc1a9](https://www.blocktrail.com/tBCC/tx/7098b2735336c85077e9704cb8ba521f5c2d92f768081345c5ded26c52fcc1a9).

Here is what I do:

1. Create a transaction using the previous transaction hash and index with the scriptSig filled in with the scriptPubKey of the output I want to redeem. The raw transaction looks like this:

   ```
   01000000
   01
   a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870
   00000000
   19
   76a914edc822bff914a255b819156c70e19dd09e8c0f6a88ac
   ffffffff
   01
   80d6e34c00000000
   19
   76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac
   00000000

   ```
2. To this I append a four-byte hash code type 41000000.

   ```
   01000000
   01
   a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870
   00000000
   19
   76a914edc822bff914a255b819156c70e19dd09e8c0f6a88ac
   ffffffff
   01
   80d6e34c00000000
   19
   76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac
   00000000
   41000000

   ```
3. Now I compute the sha256 hash of this two times which yields:

   ```
   f6b38f93c859db4920f22a1cd07d6c615a503ce89a7b2a3d90babad521b26062

   ```
4. Now I sign the result in 3. This could be where I am doing something wrong. The signature results in:

   ```
   r = 767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa6131
   s = 6dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136

   ```

   and then I encode it into DER format as

   ```
   30440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136

   ```

   Does this look right?
5. To this signature I append the one-byte hash code type 41:

   ```
   30440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e64613641

   ```
6. Then I prepend the length of the result of 5:

   ```
   4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e64613641

   ```
7. Then I append `2103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72` (the length of the public key + the actual public key) to the result of 6 yielding:

   ```
   4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136412103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72

   ```
8. The result of 7 is my signature, its length is 0x6a, so now I replace the scriptSig\_length and scriptSig in 1 with this data yielding:

   ```
   01000000
   01
   a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870
   00000000
   6a
   4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136412103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72
   ffffffff
   01
   80d6e34c00000000
   19
   76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac
   00000000

   ```

This is my raw transaction, but when I try to send it I get the following error:

```
16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)

```

Can anyone tell me what I did wrong? Thank you very much!

**Tags:** bitcoin-cash, raw-transaction, ecdsa


---

## 2 answers

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

Bitcoin Cash does not use the original Bitcoin sighash scheme (which looks like what you're trying to do here).

Instead, it uses a variant of the sighash scheme that was proposed in BIP143 (which is used for Segregated Witness transactions in Bitcoin).

> **Comments:**
> **username_0**: Thank you username_1! I hadn't realized that. I will get to reading that BIP!  

---

### Answer by username_2 on 2018-05-08. Score: 1

Although you have followed the steps correctly, you are trying to "reinvent the wheel" by trying to sign it yourself via a python script.
Bitcoin Cash software, just like bitcoin will provide you with the JSON RPC API's. These API's follow the reference implementation and already provide you with all the steps in built which you just performed.
As an example/tutorial you can go through the [tutorial](https://bitcoin.org/en/developer-examples#transaction-tutorial) available on bitcoin.org which is very concise and explanatory.

Do note that you need a running node for you to be able to access the API's

> **Comments:**
> **username_0**: Thank you username_2. The problem is that I want to construct my transaction completely offline. I did notice something in the link that you sent about offline signing, but will have to give it another read to understand it better.  
> **username_2**: The transaction remains offline as long as the node you are running does not broadcast the transaction(which has a separate RPC). Go through the link you will understand what I am trying to say.  

---

