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.

up vote 0 down vote accepted

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:      <signature> <pubkey>
scriptSig:    <0 <20-byte-key-hash>>
              (0x160014{389f26d8d616cb96df1cfd6d9989248a30b933b9})

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

  • 1
    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 Oct 11 at 15:15
  • @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 Oct 11 at 15:18
  • 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 Oct 11 at 16:25
  • Basically here is serialized tx I've 0100000000010142decebdb14e8f102fe0bd2f01b337af4ba8b4603fe463033fdb5c7d5806286c00000000160014389f26d8d616cb96df1cfd6d9989248a30b933b9ffffffff0210270000000000001976a914eeba84c4b62a6b6db8f1e0a1daffa0eccde33adf88acbe2100000000000017a914de523116b281c96ff1e204ea2ff435b75d48f0de8702483045022100fc44e10e8ab2e344c7e90f04f4f21a313850cd8eaebb0320edbc5e2d34c749b70220369da1b85125babd90a067d47e4034bc15ca1b9631ed5c8554148be3a4349d19012102ed991ef73f82488def8031fcccc4d51de840e7f17e3ad5d820781df0b4ee6e0600000000 – Jaroslav Gorjatsev Oct 11 at 16:25
  • You need the 0x16 because it tells the interpreter to push the following 22 bytes (redeem script) to the stack. – JBaczuk Oct 11 at 16:30

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.