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:
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
To this I append a four-byte hash code type 41000000.
01000000 01 a9c1fc526cd2dec545130868f7922d5c1f52bab84c70e97750c8365373b29870 00000000 19 76a914edc822bff914a255b819156c70e19dd09e8c0f6a88ac ffffffff 01 80d6e34c00000000 19 76a9147971da00aa2aae27e2ff5d801a602014453c4c2c88ac 00000000 41000000
Now I compute the sha256 hash of this two times which yields:
f6b38f93c859db4920f22a1cd07d6c615a503ce89a7b2a3d90babad521b26062
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?
To this signature I append the one-byte hash code type 41:
30440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e64613641
Then I prepend the length of the result of 5:
4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e64613641
Then I append
2103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72
(the length of the public key + the actual public key) to the result of 6 yielding:4730440220767fb3ae5c9cab9a5afae16289e672a9723bcea919c429fc9cc6449d0baa613102206dcaab24e992565473c0e080d934df239c3bbb5f7869ac92d93f598a9e646136412103ce9f14cccee963def0f3a50978daa2dd91905eb11958da2c83e44c0ebb585c72
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!