<p>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).</p>
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2021-08-04T08:51:17.393Z
Thank you this was very helpful!
Chiru
CC BY-SA 4.0
2021-08-03T17:37:35.740Z
2021-08-03T17:37:35.740Z
Ava Chow
<p>I'm trying to write a parsing function to find the input and output fields in a serialised transaction.</p>
<p>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.</p>
<p>I can't find any other way to find the sizes in the serialised bytes, help is much appreciated.</p>

<output><transaction-input>
How is the input and output fields serialised in a transaction?
Chiru
# How is the input and output fields serialised in a transaction?

(Asked by: username_0 on 2021-08-03)

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.

**Tags:** output, transaction-input


---

## 1 answers

### Answer by username_1 on 2021-08-03. Score: 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.

> **Comments:**
> **username_0**: Thank you this was very helpful!  

---

