<blockquote>
  <p>I understand that: in "asm" i have RedeemScript. In txinwitness i have my signature and the pubblic key. Correct?</p>
</blockquote>

<p>Correct. in a P2SH-P2WPKH the witness contains the signature and the public key, and the script signature contains the redeem script.</p>

<blockquote>
  <p>is it correct to say that, as an additional condition, to unlock a UTXO P2SH-P2WPKH, compared to a P2PKH, you need the HASH of an additional public key?</p>
</blockquote>

<p>This is where you are kind of wrong. </p>

<p>The address of a P2SH-P2WPKH is the hash160 of the serialized redeem script of the P2WPKH, which has to be <code>OP_0 &lt;hash160_of_public_key&gt;</code> according to BIP0141. So it looks like you need an extra public key, but in reality you are just using the same public key twice. One that goes inside a redeem script to the script_signature, and another for the witness field that goes <em>naked</em> alongside the signature. </p>

<p>In other words, there is no extra public key. It's the same public key that is wrapped inside a smart contract (redeem script) defined by BIP0141, serialized, and then hashed. Now, this hash is the same hash used for the public keys (hash160), therefore the outputs of these smart-contract hashes have the same length of those public-key hashes, and that's why they can be confused with public keys when in reality they are smart contracts. This is done on purpose to make P2SH backward compatible .</p>

CC BY-SA 4.0
2020-05-05T06:51:34.960Z
Therefore, the correct interpretation of the ScriptPubkey for a P2SH-P2WPKH address is "OP_HASH160         RedeemScript  (= HASH160 Public Key)   .   Correct?
MaXbeMan
CC BY-SA 4.0
2020-05-05T07:47:50.053Z
Almost. The script_pubkey would be "OP_HASH160 < HASH160 RedeemScript(= OP_0 <HASH160 Public Key>) > OP_EQUAL". You are omitting the "OP_0" in the redeem script (besides the OP_EQUAL in the script_pubkey). This OP_0 is very important because that  is the pattern that will trigger the special rule in the nodes to process this as a segregated witness transaction, and not just as a regular P2SH transaction. This will allow the node to extract the signature and the public key from the witness field and to put it in the stack for final evaluation.
Oscar Serna
CC BY-SA 4.0
2020-05-04T23:57:57.757Z
2020-05-05T07:54:07.203Z
2020-05-05T07:54:07.203Z
Oscar Serna
<p>I'm not sure I understand the fields of my input. I have a P2SH address (Testnet). This is the input of my transaction:</p>

<pre><code>  "vin": [
    {
      "txid": "a61389ff9af670770182198a984a4c1785625c60795bd57645ecc78765b23679",
      "vout": 1,
      "scriptSig": {
        "asm": "0014778daf651b21684b572644b47d551fce0ff71c74",
        "hex": "160014778daf651b21684b572644b47d551fce0ff71c74"
      },
      "txinwitness": [
        "3044022034a07f088dd88396cd378912a4ed6a156701b075f0facbbae810677048c390e602205df07e13bf4d56feece791a1b5e4d94bec9c93aae14bb87a1b4c6c54ae9bf2f701",
        "032c27921f2d15e7eca768c0e8a8b1aeb863328c74d6c194d41dde2888d4b0e75b"
      ],
      "sequence": 4294967295
    }
  ],
</code></pre>

<blockquote>
  <p>I understand that: in "asm" i have RedeemScript. In txinwitness i have
  my signature and the pubblic key. Correct?</p>
</blockquote>

<p>Now, from the utxo used:</p>

<pre><code>"txid": "a61389ff9af670770182198a984a4c1785625c60795bd57645ecc78765b23679",
    "vout": 1,
    "address": "2N7MaihNErbNH8XTkuZWtJYPzMQAWMPVKHd",
    "label": "",
    "redeemScript": "0014778daf651b21684b572644b47d551fce0ff71c74",
    "scriptPubKey": "a9149ac58ff47e27214b158b38da04bc8cbf72def14387",
    "amount": 0.01944571,
    "confirmations": 116,
    "spendable": true,
    "solvable": true,
    "desc": "sh(wpkh([7de694b7/0'/0'/7']032c27921f2d15e7eca768c0e8a8b1aeb863328c74d6c194d41dde2888d4b0e75b))#6z7uwusp",
    "safe": true

</code></pre>

<p>I deduce that I have used a P2SH-P2WPKH output. So in the ScriptPubkey I find the unlock condition (HASH160 of the public key of my address). In the redeem script I find HASH160 of the compressed public key <code>032c27921f2d15...</code> </p>

<blockquote>
  <p>is it correct to say that, as an additional condition, to unlock a
  UTXO P2SH-P2WPKH, compared to a P2PKH, you need the HASH of an
  additional public key?</p>
</blockquote>

<utxo><p2sh><input-script><p2pkh><p2wpkh>
"scriptSig" for P2SH-P2WPKH
MaXbeMan
# "scriptSig" for P2SH-P2WPKH

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

I'm not sure I understand the fields of my input. I have a P2SH address (Testnet). This is the input of my transaction:

```
  "vin": [
    {
      "txid": "a61389ff9af670770182198a984a4c1785625c60795bd57645ecc78765b23679",
      "vout": 1,
      "scriptSig": {
        "asm": "0014778daf651b21684b572644b47d551fce0ff71c74",
        "hex": "160014778daf651b21684b572644b47d551fce0ff71c74"
      },
      "txinwitness": [
        "3044022034a07f088dd88396cd378912a4ed6a156701b075f0facbbae810677048c390e602205df07e13bf4d56feece791a1b5e4d94bec9c93aae14bb87a1b4c6c54ae9bf2f701",
        "032c27921f2d15e7eca768c0e8a8b1aeb863328c74d6c194d41dde2888d4b0e75b"
      ],
      "sequence": 4294967295
    }
  ],

```

> I understand that: in "asm" i have RedeemScript. In txinwitness i have
> my signature and the pubblic key. Correct?

Now, from the utxo used:

```
"txid": "a61389ff9af670770182198a984a4c1785625c60795bd57645ecc78765b23679",
    "vout": 1,
    "address": "2N7MaihNErbNH8XTkuZWtJYPzMQAWMPVKHd",
    "label": "",
    "redeemScript": "0014778daf651b21684b572644b47d551fce0ff71c74",
    "scriptPubKey": "a9149ac58ff47e27214b158b38da04bc8cbf72def14387",
    "amount": 0.01944571,
    "confirmations": 116,
    "spendable": true,
    "solvable": true,
    "desc": "sh(wpkh([7de694b7/0'/0'/7']032c27921f2d15e7eca768c0e8a8b1aeb863328c74d6c194d41dde2888d4b0e75b))#6z7uwusp",
    "safe": true

```

I deduce that I have used a P2SH-P2WPKH output. So in the ScriptPubkey I find the unlock condition (HASH160 of the public key of my address). In the redeem script I find HASH160 of the compressed public key `032c27921f2d15...`

> is it correct to say that, as an additional condition, to unlock a
> UTXO P2SH-P2WPKH, compared to a P2PKH, you need the HASH of an
> additional public key?

**Tags:** utxo, p2sh, input-script, p2pkh, p2wpkh


---

## 1 answers

### Answer by username_1 on 2020-05-04. Score: 1 | Accepted answer

> I understand that: in "asm" i have RedeemScript. In txinwitness i have my signature and the pubblic key. Correct?

Correct. in a P2SH-P2WPKH the witness contains the signature and the public key, and the script signature contains the redeem script.

> is it correct to say that, as an additional condition, to unlock a UTXO P2SH-P2WPKH, compared to a P2PKH, you need the HASH of an additional public key?

This is where you are kind of wrong.

The address of a P2SH-P2WPKH is the hash160 of the serialized redeem script of the P2WPKH, which has to be `OP_0 <hash160_of_public_key>` according to BIP0141. So it looks like you need an extra public key, but in reality you are just using the same public key twice. One that goes inside a redeem script to the script\_signature, and another for the witness field that goes *naked* alongside the signature.

In other words, there is no extra public key. It's the same public key that is wrapped inside a smart contract (redeem script) defined by BIP0141, serialized, and then hashed. Now, this hash is the same hash used for the public keys (hash160), therefore the outputs of these smart-contract hashes have the same length of those public-key hashes, and that's why they can be confused with public keys when in reality they are smart contracts. This is done on purpose to make P2SH backward compatible .

> **Comments:**
> **username_0**: Therefore, the correct interpretation of the ScriptPubkey for a P2SH-P2WPKH address is "OP_HASH160         RedeemScript  (= HASH160 Public Key)   .   Correct?  
> **username_1**: Almost. The script_pubkey would be "OP_HASH160 < HASH160 RedeemScript(= OP_0 <HASH160 Public Key>) > OP_EQUAL". You are omitting the "OP_0" in the redeem script (besides the OP_EQUAL in the script_pubkey). This OP_0 is very important because that  is the pattern that will trigger the special rule in the nodes to process this as a segregated witness transaction, and not just as a regular P2SH transaction. This will allow the node to extract the signature and the public key from the witness field and to put it in the stack for final evaluation.  

---

