<blockquote>
  <p>The question is Where and When I push OP_DUP e OP_CHECKSIG?</p>
</blockquote>

<p>You don't. Neither the transaction creating the output or the transaction spending it explicitly contain these instructions.</p>

<p>When verifying a P2WPKH spend (which means either the scriptPubKey or the P2SH redeemScript is exactly <code>OP_0 &lt;20-byte hash&gt;</code>), special logic is triggered which verifies the witness stack against the script <code>OP_DUP OP_HASH160 &lt;20-byte-hash&gt; OP_EQUALVERIFY OP_CHECKSIG</code>.</p>

<p>See the examples in <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="nofollow noreferrer">BIP 141</a> for more information.</p>

CC BY-SA 4.0
2020-04-11T23:25:02.523Z
In that case I get only VOUT Amount and Txid from UTXO. Why I need to store informations in asm (and hex) of UTXO scriptPubKey?
monkeyUser
CC BY-SA 4.0
2020-04-11T23:28:15.440Z
I don't understand your question. What are you trying to accomplish?
Pieter Wuille
CC BY-SA 4.0
2020-04-12T09:55:36.410Z
In P2SH and P2PKH, I need to get informations from ScriptPubKey to unlock the transaction. In P2SH-P2WPKH I don't care about scriptPubKey `a9141a83d46422201593cb8f42f44a22a29e86ad652f87` to unlock the transaction
monkeyUser
CC BY-SA 4.0
2020-04-12T10:33:50.803Z
I know that `1a83d46422201593cb8f42f44a22a29e86ad652f` is hash160 of redeem script. Maybe first of all check if redeem script hash160 is equal to scriptPubKey to satisfy backwards? And "duplicate" the stack like "classic" P2SH, where older node check only this condition and "see" the transaction ANYONE_CAN_SPEND?
monkeyUser
CC BY-SA 4.0
2020-04-12T18:11:08.373Z
I have no idea what you're talking about. In all of P2SH, P2WSH, and P2SH-P2WSH, no actual locking code is in the scriptPubKey (and in P2SH-P2WSH not in redeemScript either). They all just have some fixed magical formula in scriptPubKey that says "look elsewhere for the script".
Pieter Wuille
CC BY-SA 4.0
2020-10-06T08:18:32.087Z
As noted above, this confusion is my fault. I've amended btcdeb to now print a notice when it does this.
Kalle
CC BY-SA 4.0
2020-04-11T18:58:04.803Z
2020-04-11T18:58:04.803Z
Pieter Wuille
<p>I'm studying P2SH-P2WPKH and when I create the signed transaction I can see this stack (using <a href="https://github.com/kallewoof/btcdeb" rel="nofollow noreferrer">btcdeb</a>)</p>

<pre><code>script                                   |                                                             stack 
-----------------------------------------+-------------------------------------------------------------------
OP_DUP                                   | Public Key
OP_HASH160                               | Signature
3ecfc6b4....a49                          | 
OP_EQUALVERIFY                           | 
OP_CHECKSIG                              | 
</code></pre>

<p>The question is
Where and When I push OP_DUP e OP_CHECKSIG?
I don't find anything inside Bitcoin code in GitHub, maybe during <code>signrawtransactionwithkey</code>?</p>

<p>I understand that the stack contains the</p>

<pre><code>OP_DUP OP_HASH160 &lt;PubKHash&gt; OP_EQUALVERIFY OP_CHECKSIG
</code></pre>

<p>like P2PKH, but I don't understand why are inside the stack.</p>

<p>It's my UTXO and I don't find <code>1a83d46422201593cb8f42f44a22a29e86ad652f</code> inside my stack.</p>

<pre><code>{
  "asm": "OP_HASH160 1a83d46422201593cb8f42f44a22a29e86ad652f OP_EQUAL",
  "hex": "a9141a83d46422201593cb8f42f44a22a29e86ad652f87",
  "reqSigs": 1,
  "type": "scripthash",
  "addresses": [
    "2MufRUxRcGnwbdL4PWKzKb7uSFzBT1wQL7u"
  ]
}
</code></pre>

<p><strong>UPDATE</strong>
I don't find the code inside Github, but maybe thanks to the <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh-nested-in-bip16-p2sh" rel="nofollow noreferrer">witness program</a> (&lt;0 &lt;20-byte-key-hash>>) the software is triggered and create the stack with P2PKH. For the older node is <code>ANYONE_CAN_SPEND</code>, for newer node check if <code>hash160 (pubkey) == pubkeyhash</code> and then check the signature. </p>

<p>In that case When the UTXO is involve only for TXID and Amount, not for validation (scriptPubKey) like P2SH and P2PKH.</p>

CC BY-SA 4.0
2020-04-11T17:11:48.710Z
**I think!!** Your script a ScriptPubKey (unlock script) and the operator `OP_DUP` create a dump of the stack whet the ScriptSig (locked script) was execute and copy on a different stack. Why this? because in past the bitcoin worked on the single stack and it was corrupted. the `OP_DUP` is used because now bitcoin uses more stack to verify a script. I'm only a bitcoin student, not take my answer how true, but test this or wait for a possible answer from bitcoin-dev.
vincenzopalazzo
CC BY-SA 4.0
2020-04-11T17:13:34.240Z
Thanks, Do you have some documentation about it?
monkeyUser
CC BY-SA 4.0
2020-04-11T17:15:04.417Z
OP_DUP Duplicates the top stack item, not the entire stack
monkeyUser
CC BY-SA 4.0
2020-04-11T17:17:53.817Z
I don't know where you can found the documentation. about the operator `OP_DUMP` if you think, the entry stack is the ScriptSig. Maybe I have the wrong information. I only added an idea.
vincenzopalazzo
CC BY-SA 4.0
2020-04-11T17:19:54.553Z
no, it's the public key
monkeyUser
CC BY-SA 4.0
2020-04-11T21:50:00.370Z
OP_DUP only duplicates the top stack element.
Pieter Wuille
CC BY-SA 4.0
2020-10-06T08:17:40.680Z
I believe I may be to blame for the confusion here. Btcdeb creates a virtual script to emulate the process of verifying the program. The latest btcdeb will now output a short note regarding this. Try running with --dataset=p2sh-p2wpkh for example of this.
Kalle
<transactions><segregated-witness><utxo>
P2SH-P2WPKH stack
monkeyUser
# P2SH-P2WPKH stack

(Asked by: username_0 on 2020-04-11)

I'm studying P2SH-P2WPKH and when I create the signed transaction I can see this stack (using [btcdeb](https://github.com/kallewoof/btcdeb))

```
script                                   |                                                             stack 
-----------------------------------------+-------------------------------------------------------------------
OP_DUP                                   | Public Key
OP_HASH160                               | Signature
3ecfc6b4....a49                          | 
OP_EQUALVERIFY                           | 
OP_CHECKSIG                              | 

```

The question is
Where and When I push OP\_DUP e OP\_CHECKSIG?
I don't find anything inside Bitcoin code in GitHub, maybe during `signrawtransactionwithkey`?

I understand that the stack contains the

```
OP_DUP OP_HASH160 <PubKHash> OP_EQUALVERIFY OP_CHECKSIG

```

like P2PKH, but I don't understand why are inside the stack.

It's my UTXO and I don't find `1a83d46422201593cb8f42f44a22a29e86ad652f` inside my stack.

```
{
  "asm": "OP_HASH160 1a83d46422201593cb8f42f44a22a29e86ad652f OP_EQUAL",
  "hex": "a9141a83d46422201593cb8f42f44a22a29e86ad652f87",
  "reqSigs": 1,
  "type": "scripthash",
  "addresses": [
    "2MufRUxRcGnwbdL4PWKzKb7uSFzBT1wQL7u"
  ]
}

```

**UPDATE**
I don't find the code inside Github, but maybe thanks to the [witness program](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh-nested-in-bip16-p2sh) (<0 <20-byte-key-hash>>) the software is triggered and create the stack with P2PKH. For the older node is `ANYONE_CAN_SPEND`, for newer node check if `hash160 (pubkey) == pubkeyhash` and then check the signature.

In that case When the UTXO is involve only for TXID and Amount, not for validation (scriptPubKey) like P2SH and P2PKH.

**Tags:** transactions, segregated-witness, utxo

> **Comments:**
> **username_1**: **I think!!** Your script a ScriptPubKey (unlock script) and the operator `OP_DUP` create a dump of the stack whet the ScriptSig (locked script) was execute and copy on a different stack. Why this? because in past the bitcoin worked on the single stack and it was corrupted. the `OP_DUP` is used because now bitcoin uses more stack to verify a script. I'm only a bitcoin student, not take my answer how true, but test this or wait for a possible answer from bitcoin-dev.  
> **username_0**: Thanks, Do you have some documentation about it?  
> **username_0**: OP_DUP Duplicates the top stack item, not the entire stack  
> **username_1**: I don't know where you can found the documentation. about the operator `OP_DUMP` if you think, the entry stack is the ScriptSig. Maybe I have the wrong information. I only added an idea.  
> **username_0**: no, it's the public key  
> **username_2**: OP_DUP only duplicates the top stack element.  
> **username_3**: I believe I may be to blame for the confusion here. Btcdeb creates a virtual script to emulate the process of verifying the program. The latest btcdeb will now output a short note regarding this. Try running with --dataset=p2sh-p2wpkh for example of this.  

---

## 1 answers

### Answer by username_2 on 2020-04-11. Score: 1 | Accepted answer

> The question is Where and When I push OP\_DUP e OP\_CHECKSIG?

You don't. Neither the transaction creating the output or the transaction spending it explicitly contain these instructions.

When verifying a P2WPKH spend (which means either the scriptPubKey or the P2SH redeemScript is exactly `OP_0 <20-byte hash>`), special logic is triggered which verifies the witness stack against the script `OP_DUP OP_HASH160 <20-byte-hash> OP_EQUALVERIFY OP_CHECKSIG`.

See the examples in [BIP 141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) for more information.

> **Comments:**
> **username_0**: In that case I get only VOUT Amount and Txid from UTXO. Why I need to store informations in asm (and hex) of UTXO scriptPubKey?  
> **username_2**: I don't understand your question. What are you trying to accomplish?  
> **username_0**: In P2SH and P2PKH, I need to get informations from ScriptPubKey to unlock the transaction. In P2SH-P2WPKH I don't care about scriptPubKey `a9141a83d46422201593cb8f42f44a22a29e86ad652f87` to unlock the transaction  
> **username_0**: I know that `1a83d46422201593cb8f42f44a22a29e86ad652f` is hash160 of redeem script. Maybe first of all check if redeem script hash160 is equal to scriptPubKey to satisfy backwards? And "duplicate" the stack like "classic" P2SH, where older node check only this condition and "see" the transaction ANYONE_CAN_SPEND?  
> **username_2**: I have no idea what you're talking about. In all of P2SH, P2WSH, and P2SH-P2WSH, no actual locking code is in the scriptPubKey (and in P2SH-P2WSH not in redeemScript either). They all just have some fixed magical formula in scriptPubKey that says "look elsewhere for the script".  
> **username_3**: As noted above, this confusion is my fault. I've amended btcdeb to now print a notice when it does this.  

---

