3

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 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.

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!

3

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).

  • Thank you Pieter! I hadn't realized that. I will get to reading that BIP! – iamcap7 May 9 '18 at 1:04
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 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

  • 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. – iamcap7 May 9 '18 at 0:59
  • 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 May 9 '18 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.