I'm trying to go through the transactions in a block and determine how many segwit inputs or outputs there are.

I've noticed in getrawtransaction that segwit inputs seem to have a 'txinwitness' field. Is that a good way to classify them?

For outputs I know I can't just look at the 3* address because not all p2sh are segwit. So what's the best way there?

EDIT: I just found this page about 3* addresses. Difference between a SegWit address and a P2SH address

Am I correct in interpreting this to say that you can't determine whether an output is segwit? Only an input?

1 Answer 1


I'm trying to go through the transactions in a block and determine how many segwit inputs or outputs there are.

BIP141 defines a segwit tx marker and a flag at the beginning of a transaction. So if you decompose such a tx, you would see with the flag, if there is SegWit:

raw tx is: ### raw tx is this:


SEGWIT (BIP141): this is a segwit tx, marker=00
       (BIP141): flag=01

TX_IN COUNT [var_int]: hex=02, decimal=2
  TX_IN[0]       9F96ADE4B41D5433F4EDA31E1738EC2B36F6E7D1420D94A6AF99801A88F7F7FF
  TX_IN[0] hex=00000000, reversed=00000000, decimal=0
  TX_IN[0] Script Length hex=49, decimal=73
  TX_IN[0] Script Sig 4830450221008B9D1DC26BA6A9CB62127B02742FA9D754CD3BEBF337F7A55D114C8E5CDD30BE022040529B19
  TX_IN[0] Sequence (uint32_t)
  TX_IN[1] 8AC60EB9575DB5B2D987E29F301B5B819EA83A5C6579D282D189CC04B8E151EF
  TX_IN[1] OutPoint index hex=01000000, reversed=00000001, decimal=1
  TX_IN[1] Script Length hex=00, decimal=0
  TX_IN[1] Sequence FFFFFFFF

TX_OUT COUNT, hex=02, decimal=2

WITNESS TXIN[0] stack elements: hex=00, decimal=0
WITNESS TXIN[1] stack elements: hex=02, decimal=2
 WITNESS data[0]:
 WITNESS data[1]:


As this is a tx with two inputs, the first input is non segwit (hence a signature in the V_IN part), the second input is segwit (Script Length = 0), and the witness for second input is added at the end of the tx.

Am I correct in interpreting this to say that you can't determine whether an output is segwit? Only an input?

With P2SH, you take an original set of data, and run it through a hash function. As the hash function is not reversible (to today's knowledge), you cannot know, what is inside a P2SH. Usual use cases are for example multisig transactions. But it can be other things as well, and only if the spending transaction is in the blockchain, you can see, what was previously inside. So this adds a certain layer of privacy for the time between funding the tx and spending the tx.

  • Just to clarify, the segwit tx flag indicates whether there is >= 1 segwit inputs in the tx correct? How would I go about counting the exact number of segwit inputs? You mention a signature in the V_IN part and script length = 0, but are either of those definitive and exhaustive ways? The "txinwitness" field in getrawtransaction seems to indicate a segwit input but I don't know if that's just be a subset of them. Jan 13, 2018 at 23:09
  • From here: bitcoincore.org/en/segwit_wallet_dev/…: „The witness is a serialization of all witness data of the transaction. Each txin is associated with a witness field. As a result, there is no indication of number of witness fields, as it is implied by the number of txins“ Jan 14, 2018 at 7:45

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.