2

When I ask the google what is the purpose of P2WPKH nested in P2SH, it says the following:

This allows the user to send funds to a SegWit (Segregated Witness) address using a non-SegWit transaction, which can be useful in cases where the user's wallet or software does not support SegWit transactions.

But I do not get it, if a wallet does not support segwit, how will a wallet then create a transaction with witness data to spends it.

So my question: What is the purpose of P2WPKH-P2SH? What do we get with sending SegWit type to non-segwit address (starting with 3 base58check for P2SH)?

Vojtěch Strnad
  • 8,292
  • 2
  • 12
  • 40
dassd
  • 1,069
  • 1
  • 4
  • 21

1 Answers1

5

P2SH-P2WPKH addresses can only be created by segwit-compatible wallet software. However, sending to a P2SH-P2WPKH works with any P2SH-compatible wallet, as the address is indistinguishable from (and effectively is) a P2SH address.

It was useful in a time before most wallets supported segwit, as even those that did not usually supported P2SH.

Pieter Wuille
  • 105,497
  • 9
  • 194
  • 308
  • So there is no need (real purpose) anymore for P2SH-P2WPKH? Btw is it correct to say P2SH-P2WPKH or P2WPKH-P2SH for P2WPKH nested in P2SH? – dassd Sep 03 '23 at 19:27
  • You can say what you want, but I think P2SH-P2WPKH is a lot more common. Most (but still not all) wallets support BIP173 (native segwit v0 addresses) now, so it is certainly less useful than a few years ago. – Pieter Wuille Sep 03 '23 at 19:32
  • Okay, but what do you mean by saying "P2SH-P2WPKH addresses can only be created by segwit-compatible wallet software."? I do not get this. If that is a P2SH then it will follow classic P2SH scheme and address will start with 3, it won't even know if the hash is from some random redeem script or from witness program (0 for version then 20 byte for hash of public key)? How does wallet wont be able to create address when its literally classic P2SH? – dassd Sep 03 '23 at 19:35
  • By "creating" an address I mean generating a private key, computing the corresponding script, and converting it to an address, so it can be given out to senders, while being able to spend the coins sent to that address. While it's true that software can compute a P2SH address for any P2SH script, that's not the same as a receiving wallet creating such an address to actually use it. – Pieter Wuille Sep 03 '23 at 19:37
  • Okay, but if P2SH-P2WPKH can only be created by segwit compatible wallet, how will that help, that is how will that change anything? I do not get it. If you send P2SH-P2WPKH to non-segwit wallet then that wallet can't spend it because it does not know to work with segwit? Is the point that the user will know in this way that funds have been sent to him because it is P2SH (P2WPKH nested), but he will not be able to spend them because his wallet is not segwit compatible? Otherwise, if it is only P2WPKH he wouldn't even be able to see that the funds arrived? In this way he can at least see that. – dassd Sep 03 '23 at 19:47
  • 3
    I think you're confusing sender and receiver. The receiver creates the address, and they only create addresses they know how to spend from. So P2SH-P2WPKH can be used by receivers who support segwit, but with senders that only support P2SH (but do not know about segwit, as they don't care). When the funds arrive at the receiver, and they want to send those funds onward, they need a segwit spend, but they know how to construct that, as they are a segwit wallet. – Pieter Wuille Sep 03 '23 at 19:55
  • "If you send P2SH-P2WPKH to non-segwit wallet then that wallet can't spend it because it does not know to work with segwit?" - This cannot happen, because the non-segwit wallet will not give out a P2WPKH address; it'll give a P2PKH address for example, which it does know how to spend. – Pieter Wuille Sep 03 '23 at 20:12
  • So, if I (my wallet) know how to work with SegWit (as a result of wanting to work with unconfirmed transactions, to avoid malleability, lower fee, etc.) I can actually generate P2SH address that will contain hash of P2WPKH (0 20byte-hash) instead of some random redeem script as in "traditional" P2SH, and give it to some sender that does not need to understand the P2WPKH. In this way, he will "pay" to classic P2SH (cause he does not know how to create P2WPKH locking script, just P2SH locking script) and I can get all the benefit from working with P2WPKH (which is actually "packed" in P2SH)? – dassd Sep 03 '23 at 21:14
  • 1
    @maximus Exactly. You get the benefits of segwit (with a small penalty, the P2SH wrapping has a cost), but you get to receive coins from senders who do not support native segwit addresses. – Pieter Wuille Sep 03 '23 at 21:17
  • What kind of cost? – dassd Sep 03 '23 at 21:18
  • The P2SH wrapper increases the size of transaction inputs that spend outputs sending to P2SH-P2WPKH, compared to spending P2WPKH outputs. As transaction fees generally scale with the size (weight) of the transaction, the wrapper increases the fees for the receiver. – Pieter Wuille Sep 03 '23 at 21:24
  • So with P2SH-P2WPKH you have to pay bigger fee than for classic P2WPKH but at least you will get the other benefits that P2WPKH provides (for example resistance to malleability) comparing to "traditional" P2PKH? – dassd Sep 03 '23 at 21:27
  • Yes, but P2SH-P2WPKH is also cheaper (=smaller input) than P2PKH, just not quite as cheap as native P2WPKH. P2TR key path spends are even cheaper. – Pieter Wuille Sep 03 '23 at 21:31
  • So in context of fee (from the biggest to lowest fee): P2SH/P2PKH -> P2SH-P2WPKH/P2SH-P2WSH -> P2WPKH/P2WSH -> P2TR. Additionally, SegWit (v0 - P2WSH/P2WPKH, v1 - P2TR) has other benefits, with the fact that v1 (P2TR) has provides even greater benefits compared to v0 (P2WSH/P2WPKH)? – dassd Sep 03 '23 at 21:50
  • In general yes. It's more complicated for the script variants (P2SH, P2WSH, P2TR script path), as the cost of spending is also a function of the complexity of the script/spending policy involved. There are several answers on this site with comparisons of script type costs, though. – Pieter Wuille Sep 03 '23 at 22:11
  • Can you give me a link to some such posts that compare it in detail? Maybe some that you wrote? It would be help a lot. – dassd Sep 03 '23 at 22:19
  • 3
    @maximus https://bitcoin.stackexchange.com/a/84006/140802 – dassd Sep 04 '23 at 09:28