# What is Witness and what data does it contain?

I've learned about the Segregated Witness but couldn't find info on braking it apart.

I know usually when legacy addresses are used the public keys for example will be under Scriptig. With P2SH, I can see that the ScriptSig contains the hash of the redeemScript which must match the hash of the address in the Output ( I think). But I am not sure about the witness data.

Let's take this TX:

TX

What info is in the witness, can someone brake it down please?

This structure contains data required to check transaction validity but not required to determine transaction effects. In particular, scripts and signatures are moved into this new structure.

The witness is a serialization of all witness data of the transaction. Each txin is associated with a witness field. A witness field starts with a var_int to indicate the number of stack items for the txin. It is followed by stack items, with each item starts with a var_int to indicate the length. Witness data is NOT script. See BIP 141.

Witness data depends on the type of transaction.

## Input 3

Looking at transaction input #3 in the raw json:
script = a914771962306e72e479245d48e879dd2a1862225b4c87

This has the structure of a Pay to Script Hash (P2SH) transaction. Because of the witness data, this is probably a multisig P2WSH nested in BIP16 P2SH:

witness:      0 <signature1> <1 <pubkey1> <pubkey2> 2 CHECKMULTISIG>
scriptSig:    <0 <32-byte-hash>>
(0x220020{32-byte-hash})
scriptPubKey: HASH160 <20-byte-hash> EQUAL
(0xA914{20-byte-hash}87)


witness:
04 (4 stack elements)

00 (1st stack element, 0 Bytes)

47 (2nd stack element, first signature, hex: 0x47 decimal: 71 Bytes) 304402202c3f94e5daf4057377d9f16d45b57e962de42fb42cb7e95a0382b7c66624980a02204098f6acd43b0391ea1b4a8102797e78895848fb7e883f98d207d14d45945a6901

47 (3rd stack element, second signature, hex: 0x47 decimal: 71 Bytes) 30440220448460edd5291a548c571ccf3a72caf47b02364035dc84f420d311e3a0c5494802205bb1cc89f20dc1e2c1f6eadb74898f8eecc46fbf488b676636b45fafaeb96e0f01

69 (4th stack element, 2-of-3 multisig script, hex: 0x47 decimal: 105 Bytes)
<52 OP_2>
21 (33 bytes)
<021e6617e06bb90f621c3800e8c37ab081a445ae5527f6c5f68a022e7133f9b5fe pubkey1>
21 (33 bytes)
<03bea1a8ce6369435bb74ff1584a136a7efeebfe4bc320b4d59113c92acd869f38 pubkey2>
21 (33 bytes)
<0280631b27700baf7d472483fadfe1c4a7340a458f28bf6bae5d3234312d684c65 pubkey3> <53 OP_3><ae OP_CHECKMULTISIG>

Note that the OP_2 and OP_3 indicate this is a 2-of-3 multisig transaction script. See Pay-to-Script-Hash | Transaction

• Thanks! I understand witness data much better now. One final clarification please. So the command OP_1 says take these two signatures and add them to the stack. OP_2 instructs to add the following three pubKeys, and OP_3 says to validate this TX, 2 of 3 signatures corresponding to OP_1 are needed? Or 2 of 3 signatures will be needed to complete the TX in next INPUT? Decoding the scriptSig for Input 3 is not resulting in multiSig. 002044c55c1da36a576217259c3bc21b0c3943f7eb3ff4e3c381d9fd3502434b9e87 gives type: scripthash. Thanks again. – RobertH Jul 14 '18 at 12:31
• Someone will have to correct me if I'm wrong, but I believe the 0x04 tells you that there are 4 stack elements, there is no OP_1 (there is an empty stack element, not sure why), the format for a multisig P2SH is: m-of-n multi-signature transaction: scriptSig: 0 <sig1> ... <script> script: OP_m <pubKey1> ... OP_n OP_CHECKMULTISIG See en.bitcoin.it/wiki/Transaction#Pay-to-Script-Hash – JBaczuk Jul 14 '18 at 13:42

For the segwit variants of an output (P2PKH becomes P2WPKH and P2SH becomes P2WSH), the witness contains the same data that would be found in the scriptSig. For P2PKH, in the scriptSig, you would have a signature and a pubkey. The same is in the witness for a P2WPKH.

For P2SH, you would have a redeemScript, signatures, and other stuff in the scriptSig. For P2WSH, those are in the witness and the redeemScript in the witness is referred to as the witnessScript.

It is important to note that the witness will look different than the scriptSig. This is because it is not actually a script but rather a stack of elements. A standard scriptSig is one which only pushes elements to the stack. The witness takes this a step farther by providing a stack to use instead of having to execute a script which produces the same stack.

additional to JBaczuk and Andrew Chow, here is a detailed tx decoding. This is a mixed transaction, with three "normal" inputs, and a 4th segwit type input (TX_IN[3]). Therefor after the version field of the tx we see bytes "0001", with 0x01 indicating a segwit data structure is included in the tx. In the input section, there are three following "standard" elements, also with multisig signatures. The fourth input element (TX_IN[3]) is the segwit part. The scripts is filled with no signatures, only the 0x0020{32-byte scripthash} structure.

At the end follows 0x00000004, each byte is the number of segwit elements per input. So the first three bytes are "0", meaning no segwit structure follows for these inputs, and then the fourth byte is 0x04, indicating four witness data elements: a "0x00" to compensate for the removal of the stack element during script evaluation, a signature, another signature and the redeem script.

VERSION 01000000
SEGWIT (BIP141):       this is a segwit tx, marker=00
(BIP141):       flag=01
TX_IN COUNT [var_int]: hex=04, decimal=4
TX_IN[0]
TX_IN[0] OutPoint hash  08A1266CED5EF064741BD4BC51C1202456F22509AE030231860D6E9BEF4ACD5E
TX_IN[0] OutPoint index hex=62000000, reversed=00000062, decimal=98
TX_IN[0] Script Length  hex=FC, decimal=252
TX_IN[0] Script Sig     0047304402207E38831ECA394E472E...555C0E2D7A9D9915D6986BFC200453AE
TX_IN[0] Sequence       FFFFFFFF
TX_IN[1]
TX_IN[1] OutPoint index hex=7D000000, reversed=0000007D, decimal=125
TX_IN[1] Script Length  hex=FC, decimal=252
TX_IN[1] Script Sig     0047304402203299B925B1F2C87282...ABDC12E55B0F545FFF14667A515F53AE
TX_IN[1] Sequence       FFFFFFFF
TX_IN[2]
TX_IN[2] OutPoint hash  C8066B798384B502F225BD89F7EB405265357CB0BDC0C169FE96B013310629B2
TX_IN[2] OutPoint index hex=A3000000, reversed=000000A3, decimal=163
TX_IN[2] Script Length  hex=00FD, decimal=253
TX_IN[2] Sequence       FFFFFFFF
TX_IN[3]
TX_IN[3] OutPoint hash  D80FF02D0D9EB2DA8C8A1C47AB099901F447DD197E34220EA13ECA72D7D6D21D
TX_IN[3] OutPoint index hex=9A000000, reversed=0000009A, decimal=154
TX_IN[3] Script Length  hex=23, decimal=35
TX_IN[3] Script Sig     22002044C55C1DA36A576217259C3BC21B0C3943F7EB3FF4E3C381D9FD3502434B9E87
TX_IN[3] Sequence       FFFFFFFF
TX_OUT COUNT, hex=05, decimal=5
TX_OUT[0]
TX_OUT[0] Value         hex=C0D4010000000000, reversed_hex=000000000001D4C0, dec=120000
TX_OUT[0] PK_Script Len hex=17, dec=23
TX_OUT[1]
TX_OUT[1] Value         hex=A086010000000000, reversed_hex=00000000000186A0, dec=100000
TX_OUT[1] PK_Script Len hex=17, dec=23
TX_OUT[2]
TX_OUT[2] Value         hex=3A4A000000000000, reversed_hex=0000000000004A3A, dec=19002
TX_OUT[2] PK_Script Len hex=17, dec=23
TX_OUT[2] pk_script     A914EC4C73145428ABBE0B1C40FBF58C59F0EF3C29F487
TX_OUT[3]
TX_OUT[3] Value         hex=382C050000000000, reversed_hex=0000000000052C38, dec=339000
TX_OUT[3] PK_Script Len hex=17, dec=23
TX_OUT[3] pk_script     A914ABB18A298E5B629BF5652F341D2CD8207CCC214A87
TX_OUT[4]
TX_OUT[4] Value         hex=8010020000000000, reversed_hex=0000000000021080, dec=135296
TX_OUT[4] PK_Script Len hex=19, dec=25
TX_OUT[4] pk_script     76A91438D769CF2899983022B5611AB4D35BF7907DAE2088AC

WITNESS TXIN[0] stack elements: hex=00, decimal=0
WITNESS TXIN[1] stack elements: hex=00, decimal=0
WITNESS TXIN[2] stack elements: hex=00, decimal=0
WITNESS TXIN[3] stack elements: hex=04, decimal=4
WITNESS data[0]: 00
WITNESS data[1]: 4730440220...D207D14D45945A6901
WITNESS data[2]: 4730440220...36B45FAFAEB96E0F01
WITNESS data[3]: 695221021E...3234312D684C6553AE

LOCK_TIME 00000000


A good summary of all the segwit details is here.

• Thank you for you reply. May I ask what do you use to decode a TX with such detail? I am using chainquery.com – RobertH Jul 14 '18 at 12:36
• I learned through the book of Andreas and the forum here, so I wrote some unixoide shell scripts. And if self marketing is allowed (shamelessly), then the code can be found here: github.com/pebwindkraft/trx_cl_suite. The transaction suite has the tool (shell script) „tcls_tx2txt.sh“, to decode in the way shown above (and then I beautified it a bit, somit better fits in the forum here). – pebwindkraft Jul 14 '18 at 12:42
• Excellent. I'll play with the script on bash soon, thank you! This is useful for anyone interested in the community so I don't think it is like advertising :) – RobertH Jul 14 '18 at 12:54