1

I am trying to learn more about the bitcoin protocol and I am writing some python software to make a light wallet. I have advanced quite far and I have come across something that I dont really understand while reading about segregated witness protocol changes in terms of transactions.

https://bitcoincore.org/en/segwit_wallet_dev/

If you go here and read the part with a heading "Transaction Serialization" the text reads like so:

If a txin is not associated with any witness data, its corresponding witness field is an exact 0x00, indicating that the number of witness stack items is zero.

How do I know if an input I am using in my script is associated with witness data? I have tried using public API's to look at the outputs I am using and I cant understand what it means. I see witness fields associated with the inputs of the transaction that produced the outputs I am trying to include in my transaction but not the other way around..

13
  • Are you trying to create a transaction, or trying to parse an existing one? Commented Nov 5, 2018 at 18:11
  • Trying to create a transaction. I would use the addresses to determine it but I know that it is impossible to tell a P2SH from a P2SH-P2WPKH Commented Nov 5, 2018 at 18:15
  • You need to know what you're signing. Obviously you can't create a signature without the private key; equally you need to know whether it's a multisig or P2SH or P2WPKH or whatever script you're signing. The address is never enough. Commented Nov 5, 2018 at 18:24
  • Understood. So if I have a bech32 or a p2sh-p2wpkh the input I possess will always be classed as being associated with witness data? But with a legacy address it will be never associated? Commented Nov 5, 2018 at 18:27
  • 1
    Spending a segwit output always requires a witness. Spending a non-segwit output never requires a witness. But I don't see how this information is helpful; you still need to know exactly how to construct the scriptSig and witness anyway, which depends on the type of script you're spending. It just so happens that all segwit scripts need a non-empty witness while others don't. Commented Nov 5, 2018 at 18:30

1 Answer 1

1

How do I know if an input I am using in my script is associated with witness data? I have tried using public API's to look at the outputs I am using and I cant understand what it means. I see witness fields associated with the inputs of the transaction that produced the outputs I am trying to include in my transaction but not the other way around.

I believe your confusion is due to this line

If a txin is not associated with any witness data, its corresponding witness field is an exact 0x00, indicating that the number of witness stack items is zero.

not applying to the serialization format, but to the contents.

A witness is always serialized as:

  • A variable-length integer indicating the number of witness stack items n.
  • For each of the n stack items:
    • A variable-length integer indicating the size of the stack element b
    • A b-byte array with the contents of that stack element.

A corollary is that an empty witness is always serialized as just a 0x00 byte (indicating that there are zero stack items).

So your serialization and parsing code does not need to know whether a particular txin has associated witness data. It just serializes/parses the witness, which may be empty.

It so happens that the validity rules require that the witness for a non-segwit spend always has to be empty, and as a result, the serialization of such a witness will always be just 0x00.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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