<p>From BIP143:</p>

<blockquote>
  <p>If the ANYONECANPAY flag is not set, hashPrevouts is the double SHA256
  of the serialization of all input outpoints;</p>
  
  <p>Otherwise, hashPrevouts is a uint256 of 0x0000......0000.</p>
</blockquote>

<p>All the details you need are in the BIP</p>

CC BY-SA 4.0
2020-01-08T10:06:09.653Z
Thanks. It feels like I've tried every combination of setting hashPrevouts, hashSequence, hashOutputs to 0x0000......0000, as well as the sighash type itself, but I keep getting the error message:
error message:
non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 64)
Darius
CC BY-SA 4.0
2020-01-08T19:53:10.170Z
Have you looked at the code? You might find it more helpful: https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp#L1238-L1282 or in bcoin: https://github.com/bcoin-org/bcoin/blob/master/lib/primitives/tx.js#L637-L720
pinhead
CC BY-SA 4.0
2020-01-10T18:23:59.153Z
Unfortunately I don't know cpp so it's a bit hard to follow
Darius
CC BY-SA 4.0
2020-01-10T18:26:33.300Z
@Darius bcoin is JavaScript, if that helps. What language are you using to compute this sighash?
pinhead
CC BY-SA 4.0
2020-01-10T18:39:03.447Z
I'm not familiar with JavaScript either. I am using the ecsda and hashlib libraries in python. I am creating a script based off this tutorial: https://github.com/zeltsi/segwit_tutorial/tree/master/transactions
In case you were curious this is what the (non-working) script looks like:
https://github.com/DariusParvin/temp/blob/master/P2WPKH_to_P2WPKH_anyonecanpay.py
Darius
CC BY-SA 4.0
2020-01-10T19:57:33.173Z
Just took a quick look - you may need check that your txid is little-endian...
pinhead
CC BY-SA 4.0
2020-01-10T20:14:54.487Z
thanks a lot. I know everything else is okay because I used it to create a valid sighash_all tx. From that all I did was change the sighash flag to 0x83, set hashPrevouts, hashSequence, hashOutputs to 0s in the digest, but btc core is telling me the signature is invalid.
Darius
CC BY-SA 4.0
2020-01-12T02:00:24.617Z
glad to say I finally solved it. It was a stupid mistake. When converting from string to hex, I put 32 zeros which of course only corresponded to 16 bytes of zeros. So yes, all the info was clearly in the BIP and it was my bad!
Darius
CC BY-SA 4.0
2020-01-07T17:21:23.543Z
2020-01-07T17:21:23.543Z
pinhead
<p>I have script to create a P2WPKH to P2WPKH transaction (1 input, 1 output) and sign it with the sighash type 0x01 SIGHASH_ALL.</p>

<p>Which fields would I need to change in the tx digest preimage to turn the signature into one with a sighash type of 0x83 (SINGLE|ANYONECANPAY)? I'm having trouble understanding the documentation in bip_143.</p>

<p>For reference, the tx digest preimage:</p>

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

<signature><sighash-flags><bip143>
What goes in the tx hash preimage when signing with 0x83 (SINGLE|ANYONECANPAY)?
Darius
# What goes in the tx hash preimage when signing with 0x83 (SINGLE|ANYONECANPAY)?

(Asked by: username_0 on 2020-01-07)

I have script to create a P2WPKH to P2WPKH transaction (1 input, 1 output) and sign it with the sighash type 0x01 SIGHASH\_ALL.

Which fields would I need to change in the tx digest preimage to turn the signature into one with a sighash type of 0x83 (SINGLE|ANYONECANPAY)? I'm having trouble understanding the documentation in bip\_143.

For reference, the tx digest preimage:

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

**Tags:** signature, sighash-flags, bip143


---

## 1 answers

### Answer by username_1 on 2020-01-07. Score: 2 | Accepted answer

From BIP143:

> If the ANYONECANPAY flag is not set, hashPrevouts is the double SHA256
> of the serialization of all input outpoints;
>
> Otherwise, hashPrevouts is a uint256 of 0x0000......0000.

All the details you need are in the BIP

> **Comments:**
> **username_0**: Thanks. It feels like I've tried every combination of setting hashPrevouts, hashSequence, hashOutputs to 0x0000......0000, as well as the sighash type itself, but I keep getting the error message: error message: non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 64)  
> **username_1**: Have you looked at the code? You might find it more helpful: https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp#L1238-L1282 or in bcoin: https://github.com/bcoin-org/bcoin/blob/master/lib/primitives/tx.js#L637-L720  
> **username_0**: Unfortunately I don't know cpp so it's a bit hard to follow  
> **username_1**: @username_0 bcoin is JavaScript, if that helps. What language are you using to compute this sighash?  
> **username_0**: I'm not familiar with JavaScript either. I am using the ecsda and hashlib libraries in python. I am creating a script based off this tutorial: https://github.com/zeltsi/segwit_tutorial/tree/master/transactions In case you were curious this is what the (non-working) script looks like: https://github.com/username_0Parvin/temp/blob/master/P2WPKH_to_P2WPKH_anyonecanpay.py  
> **username_1**: Just took a quick look - you may need check that your txid is little-endian...  
> **username_0**: thanks a lot. I know everything else is okay because I used it to create a valid sighash_all tx. From that all I did was change the sighash flag to 0x83, set hashPrevouts, hashSequence, hashOutputs to 0s in the digest, but btc core is telling me the signature is invalid.  
> **username_0**: glad to say I finally solved it. It was a stupid mistake. When converting from string to hex, I put 32 zeros which of course only corresponded to 16 bytes of zeros. So yes, all the info was clearly in the BIP and it was my bad!  

---

