1

I'm trying to write a parsing function to find the input and output fields in a serialised transaction.

My problem is that I can find the number of inputs and outputs using the varint byte(s), but not the length of each serialised input and output, since the scriptsig and scriptpubkey fields are not fixed sizes.

I can't find any other way to find the sizes in the serialised bytes, help is much appreciated.

1 Answer 1

3

Inputs and outputs are self descriptive in length. They contain some fixed length fields, and have a variable length script which will tell you its length. Each script is always prepended by its length in the form of a compact size unsigned integer (the same kind used to indicate the number of inputs and outputs).

For an input, your parser should read 32 bytes as the txid, the next 4 bytes as the output index, then a compact size unsigned integer for the length of the script, then the script itself using the length previously read, and then the sequence number.

For an output, your parser should read 8 bytes for the output amount, then a compact size unsigned integer for the length of the script, then the script itself using the length previously read.

1
  • Thank you this was very helpful!
    – Chiru
    Aug 4, 2021 at 8:51

Your Answer

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

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