bitcointx

need help understanding bitcoin raw transaction


I'm trying to write some python code to digest bitcoin rawTX's and I'm pretty much stuck at step two.

Everywhere I look it says that there is a 4 byte field for the version and then a varint for the number of tx inputs.

BIP69 says that there can be a version 2 but I can't see anything there that extends or changes the field sizes in any way since it wants to keep backwards compatibility. My example however is a tx version 1.

The number for the tx inputs can't be 0x00 and it can only be bigger than 1 byte when it starts with 0xfd 0xfe 0xff.

As an example I have this non-Coinbase rawtx with 2 inputs an A LOT of outputs from the address 111qT5kHBkZ2q96iUxTY4DGUW2APSmCnz

transaction ID txid: 9ea0df029f138e92f50379df6fde0ade9cf101cb8c975825c74188305cac6eed

https://sochain.com/tx/BTC/9ea0df029f138e92f50379df6fde0ade9cf101cb8c975825c74188305cac6eed

and the raw transaction which if I'm not mistaken is the "tx_hex" field at the end if I press on RAW TX link in the link above

it starts with: 0100000000010264066fbd45afd2cedaf3638a9886f80c863e6fe083c797ac0c64c93998a7b10ffd03000023220020bdd68a4cff85348e9300087f455fbad025c1e7fb54bdbf17a8c044876309069dffffffff9cf4f90ec1d0c0eae485724c4dce55e4278a1f78016d224531552c20bb94f74c0000000023220020bdd68a4cff85348e9300087f455fbad025c1e7fb54bdbf17a8c044876309069dfffffffffd4f0544620000..................

Even if I interpret this backwards from the ffffffff then I get:

01000000 -> 4 byte version

00010264 -> ???????????? how can this start with 0x00? the first 02 I see is two bytes down the line and then what is the 0x64?

066fbd45afd2cedaf3638a9886f80c863e6fe083c797ac0c64c93998a7b10ffd -> 32 byte hash of input to redeem according to https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx

03000023 -> output index

22 -> varint 1 byte for script length

0020bdd68a4cff85348e9300087f455fbad025c1e7fb54bdbf17a8c044876309069d -> script of length 0x22

ffffffff 9cf4f90ec.............

I'm missing some information obviously.. can someone at least point me to that and or tell my where my thinking had gone wrong?


Solution

  • This is a segwit transaction, so the 4 version bytes are followed by a marker and flag byte.

    So for the transaction in your question:

     01000000 - version
     00 - marker byte, always 0
     01 - flag byte
     02 - number of inputs, etc
    

    This is specified in BIP0144, see https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki