To understand why digital signatures are integral to Bitcoin transactions, you'll need to learn a bit about the structure of Bitcoin transactions. This chapter will introduce you to what's happening "Behind the Scenes" in these transactions. We'll discuss this more in Unit 5.
Transaction Outputs and Inputs
Transaction serialization – inputs
When transactions are serialized for transmission on the network, their inputs are encoded into a byte stream as shown in Transaction input serialization.
Table 2. Transaction input serialization
Size | Field | Description |
---|---|---|
32 bytes |
Transaction Hash |
Pointer to the transaction containing the UTXO to be spent |
4 bytes |
Output Index |
The index number of the UTXO to be spent; first one is 0 |
1–9 bytes (VarInt) |
Unlocking-Script Size |
Unlocking-Script length in bytes, to follow |
Variable |
Unlocking-Script |
A script that fulfills the conditions of the UTXO locking script |
4 bytes |
Sequence Number |
Used for locktime or disabled (0xFFFFFFFF) |
As with the outputs, let's see if we can find the inputs from Alice's transaction in the serialized format. First, the inputs decoded:
"vin": [ { "txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18", "vout": 0, "scriptSig" : "3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccb b6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c0 9db8f6e3813[ALL] 0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416a b9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf", "sequence": 4294967295 } ],
Now, let's see if we can identify these fields in the serialized hex encoding in Alice's transaction, serialized and presented in hexadecimal notation:
Example 2. Alice’s transaction, serialized and presented in hexadecimal notation
0100000001186f9f998a5aa6f048e51dd8419a14d8a0f1a8a2836dd734d2804fe65fa35779000000008b483045022100884d142d86652a3f47
ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e3813
01410484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc1
7b4a10fa336a8d752adfffffffff0260e31600000000001976a914ab6 8025513c3dbd2f7b92a94e0581f5d50f654e788acd0ef800000000000
1976a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac00000 000
Hints:
- The transaction ID is serialized in reversed byte order, so it starts with (hex) 18 and ends with 79
- The output index is a 4-byte group of zeros, easy to identify
- The length of the scriptSig is 139 bytes, or 8b in hex
- The sequence number is set to FFFFFFFF, again easy to identify
ScriptSig is a specific type of unlocking script that when serialized for transmission on the network, inputs are encoded into a byte stream as shown in ScriptSig input serialization. The serialization of the signature field is detailed in Serialization of signatures (DER). The signature field also includes a Signature Hash Type (SIGHASH), which is detailed in ignature Hash Types (SIGHASH).
Table 3. ScriptSig input serialization
Size | Field | Description |
---|---|---|
1–9 bytes (VarInt) |
Signature Size |
Signature length in bytes, to follow |
Variable |
Signature |
A signature that is produced by the user's wallet from his or her private key, which includes a SIGHASH |
1–9 bytes (VarInt) |
Public Key Size |
Public key length in bytes, to follow |
Variable |
Public Key |
The public key, unhashed |