<p>For signatures in P2WPKH and P2WSH spends, the sighash algorithm is described in BIP143.</p>

<p>It is very different from the one used in legacy spends.</p>

CC BY-SA 4.0
2019-12-28T08:52:49.950Z
2019-12-28T08:52:49.950Z
Pieter Wuille
<p>(Assume there's no OP_CODESEPERATOR)</p>

<hr>

<p>For standard P2PKH, the signature digest is generated this way:</p>

<p>NewTransaction</p>

<ol>
<li>settle the TxOuts well</li>
<li>set all TxIn scripts to empty</li>
<li>insert the <strong>Previous Locking Script</strong> into the TxIn you are signing</li>
<li>append hash type code to the end of the transaction</li>
<li>SHA256 twice this transaction</li>
<li>Be a digest.</li>
</ol>

<hr>

<p>For standard P2SH, the signature digest is generated this way:</p>

<p>NewTransaction</p>

<ol>
<li>settle the TxOuts well</li>
<li>set all TxIn scripts to empty</li>
<li>insert the Previous Unlocking Script's <strong>Redeem Script</strong> into the TxIn you are signing</li>
<li>append hash type code to the end of the transaction</li>
<li>SHA256 twice this transaction</li>
<li>Be a digest.</li>
</ol>

<hr>

<p>I learned those above from <a href="https://en.bitcoin.it/wiki/File:Bitcoin_OpCheckSig_InDetail.png" rel="nofollow noreferrer">here</a>. As for those above, I have written programs and successfully signed and submitted Bitcoin transactions to the blockchain.</p>

<p>But when it comes to <strong>Segwit</strong>, I don't know what the digest should be. For P2WPKH output, I tried to use the same method to get the digest as P2PKH, but it failed. Blockchain told me my signature was wrong.</p>

<segregated-witness><signature><nbitcoin><p2wpkh><p2wsh>
What is the signature digest for Segwit outputs
gudako
# What is the signature digest for Segwit outputs

(Asked by: username_0 on 2019-12-28)

(Assume there's no OP\_CODESEPERATOR)

---

For standard P2PKH, the signature digest is generated this way:

NewTransaction

1. settle the TxOuts well
2. set all TxIn scripts to empty
3. insert the **Previous Locking Script** into the TxIn you are signing
4. append hash type code to the end of the transaction
5. SHA256 twice this transaction
6. Be a digest.

---

For standard P2SH, the signature digest is generated this way:

NewTransaction

1. settle the TxOuts well
2. set all TxIn scripts to empty
3. insert the Previous Unlocking Script's **Redeem Script** into the TxIn you are signing
4. append hash type code to the end of the transaction
5. SHA256 twice this transaction
6. Be a digest.

---

I learned those above from [here](https://en.bitcoin.it/wiki/File:Bitcoin_OpCheckSig_InDetail.png). As for those above, I have written programs and successfully signed and submitted Bitcoin transactions to the blockchain.

But when it comes to **Segwit**, I don't know what the digest should be. For P2WPKH output, I tried to use the same method to get the digest as P2PKH, but it failed. Blockchain told me my signature was wrong.

**Tags:** segregated-witness, signature, nbitcoin, p2wpkh, p2wsh


---

## 1 answers

### Answer by username_1 on 2019-12-28. Score: 2 | Accepted answer

For signatures in P2WPKH and P2WSH spends, the sighash algorithm is described in BIP143.

It is very different from the one used in legacy spends.


---

