How do I generate the Bitcoin Cash hash preimage? I am attempting to manually generate a signed transaction which is to be sent from my address 1GiJZtN7wjUuL4V5i7ZYYDAvFtqi5AhCDr which is bitcoincash:qzk9nxh6qpd9yt3hvv6vvun78s834f3escdhu2vdjp in the new address format. I am using an uncompressed public key in this transaction.

I have the following unsigned Bitcoin Cash transaction. How do I find its hash preimage which is the hash used in the signing of this transaction?


  0100000001fb4a8e5c7ac5311f32fbe127f031134ee3e7490f3308ca19c567f78d6aa96d770000000000ffffffff0100a00700000000001976a914a8e181e0847b495df439066ca8fb36ce093692be88ac00000000


Breaking it up, it looks like this:

01000000 - nVersion

01 - num of inputs

fb4a8e5c7ac5311f32fbe127f031134ee3e7490f3308ca19c567f78d6aa96d77 - txid (little-endian)

00000000 - vout

00 - size of sigScript

ffffffff - sequence number

01 - num of outputs

00a0070000000000 - amount (hex, little-endian)

1976a914a8e181e0847b495df439066ca8fb36ce093692be88ac - scriptPubKey

00000000 - timeLock

I am attempting to consume output 0 from the transaction with txid: 776da96a8df767c519ca08330f49e7e34e1331f027e1fb321f31c57a5c8e4afb,
The scriptPubKey for that transaction's output 0 is:

1976a914ac599afa005a522e376334c6727e3c0f1aa6398688ac


The value of the received output is 0.005 BCH

The transaction I am attempting to consume can be found here:
https://bch.btc.com/776da96a8df767c519ca08330f49e7e34e1331f027e1fb321f31c57a5c8e4afb
And the rawtx for the transaction I want to consume is here:
https://bch.btc.com/776da96a8df767c519ca08330f49e7e34e1331f027e1fb321f31c57a5c8e4afb.rawhex

Can anybody help me?
Bitcoin Cash uses BIP 143 for signature hash creation.

From the BIP:


  Double SHA256 of the serialization of:
     1. nVersion of the transaction (4-byte little endian)
     2. hashPrevouts (32-byte hash)
     3. hashSequence (32-byte hash)
     4. outpoint (32-byte hash + 4-byte little endian) 
     5. scriptCode of the input (serialized as scripts inside CTxOuts)
     6. value of the output spent by this input (8-byte little endian)
     7. nSequence of the input (4-byte little endian)
     8. hashOutputs (32-byte hash)
     9. nLocktime of the transaction (4-byte little endian)
    10. sighash type of the signature (4-byte little endian)



So the hash preimage will look like this:

01000000be16f25831b40c713fed6627e39aa7717170f5cdd62c765bd4882abbefdaa50e3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044fb4a8e5c7ac5311f32fbe127f031134ee3e7490f3308ca19c567f78d6aa96d77000000001976a914ac599afa005a522e376334c6727e3c0f1aa6398688ac20a1070000000000ffffffffc8e2e71fe663fc91d7aa109feffee0f1497862af35525f68f500cbd55f6ed6620000000041000000


And here is its breakdown:

nVersion: 01000000
hashPrevouts: be16f25831b40c713fed6627e39aa7717170f5cdd62c765bd4882abbefdaa50e
hashSequence: 3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044
outpoint: fb4a8e5c7ac5311f32fbe127f031134ee3e7490f3308ca19c567f78d6aa96d7700000000
scriptCode: 1976a914ac599afa005a522e376334c6727e3c0f1aa6398688ac
value: 20a1070000000000
nSequence: ffffffff
hashOutputs: c8e2e71fe663fc91d7aa109feffee0f1497862af35525f68f500cbd55f6ed662
nLocktime: 00000000
sighash: 41000000