<blockquote>
  <p>if I've P2SH-P2WPKH address, does it mean that all outputs to this
  address (UTXOs) are by definition segwit UTXOs ?</p>
</blockquote>

<p>Yes, Bitcoin sent to a <code>P2SH-P2WPKH</code> address will produce a segwit <code>UTXO</code>.</p>

<blockquote>
  <p>Why they wrote this "For spending of non-segwit UTXO, the signature
  generation algorithm is unchanged" part, what case it covers? If
  you've P2SH-P2WPKH address but you are taking some foreign UTXOs to
  spend(that were directed not to your address)?</p>
</blockquote>

<p>I think you're right, it sounds like it's just saying if you want to send to a segwit address, you don't need to sign a non-segwit input any differently than before.</p>

<h2>Spending P2SH(P2WPKH)</h2>

<p>Here is a good reference example: <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh-nested-in-bip16-p2sh" rel="nofollow noreferrer">BIP 141: P2SH(P2WPKH)</a>. In order to spend Bitcoin sent to your address <code>3MxYPuvEcKDdaEG1xoGyddqir6C4b66MJi</code>, you have to provide the following:</p>

<pre><code>witness:      &lt;signature&gt; &lt;pubkey&gt;
scriptSig:    &lt;0 &lt;20-byte-key-hash&gt;&gt;
              (0x160014{389f26d8d616cb96df1cfd6d9989248a30b933b9})
</code></pre>

<p>Perhaps you are forgetting the push ops <code>0x16</code> and/or <code>0x14</code> in <code>scriptSig</code>?</p>

CC BY-SA 4.0
2018-10-11T15:15:37.727Z
`Yes, Bitcoin sent to a P2SH-P2WPKH address will produce a segwit UTXO.` Technically, the utxo type is still p2sh. It's only revealed as segwit when it is spent. You could totally spend it as a p2sh if you can brute force a redeem script that satisfies the hash and is solvable by you, although that's rather impossible. Regardless, the output itself is p2sh.
Raghav Sood
CC BY-SA 4.0
2018-10-11T15:18:35.343Z
@RaghavSood True, there would be no way to detect that it was a segwit UTXO, because it's the redeem script that is segwit.
JBaczuk
CC BY-SA 4.0
2018-10-11T16:25:01.133Z
Thanks for the answer and comments, now it's more clear what is what :)
I don't have 0x16, here is mine full script:

     "script": "0014389f26d8d616cb96df1cfd6d9989248a30b933b9", 

Is 0x16 required also?
Jaroslav Gorjatsev
CC BY-SA 4.0
2018-10-11T16:25:06.337Z
Basically here is serialized tx I've

0100000000010142decebdb14e8f102fe0bd2f01b337af4ba8b4603fe463033fdb5c7d5806286c00000000160014389f26d8d616cb96df1cfd6d9989248a30b933b9ffffffff0210270000000000001976a914eeba84c4b62a6b6db8f1e0a1daffa0eccde33adf88acbe2100000000000017a914de523116b281c96ff1e204ea2ff435b75d48f0de8702483045022100fc44e10e8ab2e344c7e90f04f4f21a313850cd8eaebb0320edbc5e2d34c749b70220369da1b85125babd90a067d47e4034bc15ca1b9631ed5c8554148be3a4349d19012102ed991ef73f82488def8031fcccc4d51de840e7f17e3ad5d820781df0b4ee6e0600000000
Jaroslav Gorjatsev
CC BY-SA 4.0
2018-10-11T16:30:29.453Z
You need the `0x16` because it tells the interpreter to push the following 22 bytes (redeem script) to the stack.
JBaczuk
CC BY-SA 4.0
2018-10-11T15:10:31.277Z
2018-10-11T15:10:31.277Z
JBaczuk
<p>I've been reading a lot of docs on BTC transaction structure, and find it really confusing when it comes to SegWit story.</p>
<p>So, the very first thing is, if I've P2SH-P2WPKH address, does it mean that all outputs to this address (UTXOs) are by definition segwit UTXOs ?</p>
<p>In the docs (<a href="https://bitcoincore.org/en/segwit_wallet_dev/" rel="nofollow noreferrer">https://bitcoincore.org/en/segwit_wallet_dev/</a>) it says:</p>
<blockquote>
<p><strong>Signature Generation and Verification for P2SH-P2WPKH</strong></p>
<p>For spending of non-segwit UTXO, the signature generation algorithm is  unchanged.</p>
<p>For spending of P2SH-P2WPKH:</p>
<ul>
<li>The scriptSig MUST ONLY contain a push of the redeemScript</li>
</ul>
<p>...</p>
</blockquote>
<p>So, either all UTXOs for P2SH-P2WPKH address are segwit ones, or they are SegWit if SegWit transaction was made to this address (which does little sense actually for me, as then arises chicken-egg problem).</p>
<p>Why they wrote this <em>&quot;For spending of non-segwit UTXO, the signature generation algorithm is  unchanged&quot;</em> part, what case it covers? If you've P2SH-P2WPKH address but you are taking some foreign UTXOs to spend(that were directed not to your address)?</p>
<p>Another not clear part are scripts for P2SH-P2WPKH address.
So here is my example address:
3MxYPuvEcKDdaEG1xoGyddqir6C4b66MJi</p>
<p>And here is transaction that was made to it:
<a href="https://blockchain.info/rawtx/6c2806587d5cdb3f0363e43f60b4a84baf37b3012fbde02f108f4eb1bdcede42" rel="nofollow noreferrer">https://blockchain.info/rawtx/6c2806587d5cdb3f0363e43f60b4a84baf37b3012fbde02f108f4eb1bdcede42</a></p>
<p>Based on it pubKeyScript of output is <strong>a914de523116b281c96ff1e204ea2ff435b75d48f0de87</strong> which translates to:</p>
<pre><code>OP_HASH160 de523116b281c96ff1e204ea2ff435b75d48f0de OP_EQUAL
</code></pre>
<p>So, <strong>de523116b281c96ff1e204ea2ff435b75d48f0de</strong> is hash160 of redeem script.
The only redeem script that matches this hash I've found is following:</p>
<pre><code>0 389f26d8d616cb96df1cfd6d9989248a30b933b9
</code></pre>
<p>Which is witness v0 program script(0 sha160sha256(pubkey), so it means I've to create SegWit transactions, right?
I tried making P2SH ones and it says that I've non-nominal pushes in the redeem script, that's cause of &quot;0&quot; push there I believe.</p>
<p>I feel that doing something wrong in this part, and as a result signing either wrong thing or in a wrong way...</p>
<p>Very appreciate any explanations.</p>

<raw-transaction><p2sh><p2wpkh><p2sh-p2wpkh>
P2SH-P2WPKH - has always segwit UTXOs?
Jaroslav Gorjatsev
# P2SH-P2WPKH - has always segwit UTXOs?

(Asked by: username_0 on 2018-10-11)

I've been reading a lot of docs on BTC transaction structure, and find it really confusing when it comes to SegWit story.
So, the very first thing is, if I've P2SH-P2WPKH address, does it mean that all outputs to this address (UTXOs) are by definition segwit UTXOs ?
In the docs (https://bitcoincore.org/en/segwit_wallet_dev/) it says:

Signature Generation and Verification for P2SH-P2WPKH
For spending of non-segwit UTXO, the signature generation algorithm is  unchanged.
For spending of P2SH-P2WPKH:

The scriptSig MUST ONLY contain a push of the redeemScript

...

So, either all UTXOs for P2SH-P2WPKH address are segwit ones, or they are SegWit if SegWit transaction was made to this address (which does little sense actually for me, as then arises chicken-egg problem).
Why they wrote this "For spending of non-segwit UTXO, the signature generation algorithm is  unchanged" part, what case it covers? If you've P2SH-P2WPKH address but you are taking some foreign UTXOs to spend(that were directed not to your address)?
Another not clear part are scripts for P2SH-P2WPKH address.
So here is my example address:
3MxYPuvEcKDdaEG1xoGyddqir6C4b66MJi
And here is transaction that was made to it:
https://blockchain.info/rawtx/6c2806587d5cdb3f0363e43f60b4a84baf37b3012fbde02f108f4eb1bdcede42
Based on it pubKeyScript of output is a914de523116b281c96ff1e204ea2ff435b75d48f0de87 which translates to:
OP_HASH160 de523116b281c96ff1e204ea2ff435b75d48f0de OP_EQUAL

So, de523116b281c96ff1e204ea2ff435b75d48f0de is hash160 of redeem script.
The only redeem script that matches this hash I've found is following:
0 389f26d8d616cb96df1cfd6d9989248a30b933b9

Which is witness v0 program script(0 sha160sha256(pubkey), so it means I've to create SegWit transactions, right?
I tried making P2SH ones and it says that I've non-nominal pushes in the redeem script, that's cause of "0" push there I believe.
I feel that doing something wrong in this part, and as a result signing either wrong thing or in a wrong way...
Very appreciate any explanations.

**Tags:** raw-transaction, p2sh, p2wpkh, p2sh-p2wpkh


---

## 1 answers

### Answer by username_1 on 2018-10-11. Score: 1 | Accepted answer

if I've P2SH-P2WPKH address, does it mean that all outputs to this
  address (UTXOs) are by definition segwit UTXOs ?

Yes, Bitcoin sent to a P2SH-P2WPKH address will produce a segwit UTXO.

Why they wrote this "For spending of non-segwit UTXO, the signature
  generation algorithm is unchanged" part, what case it covers? If
  you've P2SH-P2WPKH address but you are taking some foreign UTXOs to
  spend(that were directed not to your address)?

I think you're right, it sounds like it's just saying if you want to send to a segwit address, you don't need to sign a non-segwit input any differently than before.
Spending P2SH(P2WPKH)
Here is a good reference example: BIP 141: P2SH(P2WPKH). In order to spend Bitcoin sent to your address 3MxYPuvEcKDdaEG1xoGyddqir6C4b66MJi, you have to provide the following:
witness:       
scriptSig:    <0 <20-byte-key-hash>>
              (0x160014{389f26d8d616cb96df1cfd6d9989248a30b933b9})

Perhaps you are forgetting the push ops 0x16 and/or 0x14 in scriptSig?

> **Comments:**
> **username_2**: `Yes, Bitcoin sent to a P2SH-P2WPKH address will produce a segwit UTXO.` Technically, the utxo type is still p2sh. It's only revealed as segwit when it is spent. You could totally spend it as a p2sh if you can brute force a redeem script that satisfies the hash and is solvable by you, although that's rather impossible. Regardless, the output itself is p2sh.  
> **username_1**: @RaghavSood True, there would be no way to detect that it was a segwit UTXO, because it's the redeem script that is segwit.  
> **username_0**: Thanks for the answer and comments, now it's more clear what is what :) I don't have 0x16, here is mine full script:       "script": "0014389f26d8d616cb96df1cfd6d9989248a30b933b9",   Is 0x16 required also?  
> **username_0**: Basically here is serialized tx I've  0100000000010142decebdb14e8f102fe0bd2f01b337af4ba8b4603fe463033fdb5c7d5806286c00000000160014389f26d8d616cb96df1cfd6d9989248a30b933b9ffffffff0210270000000000001976a914eeba84c4b62a6b6db8f1e0a1daffa0eccde33adf88acbe2100000000000017a914de523116b281c96ff1e204ea2ff435b75d48f0de8702483045022100fc44e10e8ab2e344c7e90f04f4f21a313850cd8eaebb0320edbc5e2d34c749b70220369da1b85125babd90a067d47e4034bc15ca1b9631ed5c8554148be3a4349d19012102ed991ef73f82488def8031fcccc4d51de840e7f17e3ad5d820781df0b4ee6e0600000000  
> **username_1**: You need the `0x16` because it tells the interpreter to push the following 22 bytes (redeem script) to the stack.  

---

