Decoding the Transaction
Below is the Python program to decode the transaction:
import struct
def decode_varint(stream):
n = stream[0]
if n < 0xFD:
return n, 1
elif n == 0xFD:
return struct.unpack("<H", stream[1:3])[0], 3
elif n == 0xFE:
return struct.unpack("<I", stream[1:5])[0], 5
elif n == 0xFF:
return struct.unpack("<Q", stream[1:9])[0], 9
def deserialize_tx(serialized_tx):
offset = 0
version = struct.unpack("<L", serialized_tx[offset:offset+4])[0]
offset += 4
vin_count, vin_offset = decode_varint(serialized_tx[offset:])
offset += vin_offset
vin = []
for _ in range(vin_count):
txid = serialized_tx[offset:offset+32][::-1].hex()
offset += 32
vout = struct.unpack("<L", serialized_tx[offset:offset+4])[0]
offset += 4
script_length, script_offset = decode_varint(serialized_tx[offset:])
offset += script_offset
scriptsig = serialized_tx[offset:offset+script_length].hex()
offset += script_length
sequence = struct.unpack("<L", serialized_tx[offset:offset+4])[0]
offset += 4
vin.append({"txid": txid, "vout": vout, "scriptsig": scriptsig,
"sequence": sequence})
vout_count, vout_offset = decode_varint(serialized_tx[offset:])
offset += vout_offset
vout = []
for _ in range(vout_count):
value = struct.unpack("<Q", serialized_tx[offset:offset+8])[0]
offset += 8
script_length, script_offset = decode_varint(serialized_tx[offset:])
offset += script_offset
scriptpubkey = serialized_tx[offset:offset+script_length].hex()
offset += script_length
vout.append({"value": value, "scriptpubkey": scriptpubkey})
locktime = struct.unpack("<L", serialized_tx[offset:offset+4])[0]
return {
"version": version,
"vin": vin,
"vout": vout,
"locktime": locktime
}
serialized_tx = "0100000002b9a2c28ea00905a5d24a172598b9574fbd973fc085df49901208a358d29a233b000000001716001415ff0337937ecadd10ce56ffdfd4674817613223f0ffffff8415e7099fba6d81474912d22eb5113bcdcfe45a4a1ae0c2701549a46326384f010000001716001415ff0337937ecadd10ce56ffdfd4674817613223f0ffffff02007daf01000000001976a91471a3d2f54b0917dc9d2c877b2861ac52967dec7f88ac2073f6010000000017a914423877331b30a905240c7e1f2adee4ebaa47c5f68700000000"
transaction = deserialize_tx(bytes.fromhex(serialized_tx))
print("Version:", transaction["version"])
print("Vin:")
for inp in transaction["vin"]:
print(" TXID:", inp["txid"])
print(" Vout:", inp["vout"])
print(" ScriptSig:", inp["scriptsig"])
print(" Sequence:", inp["sequence"])
print("Vout:")
for out in transaction["vout"]:
print(" Value:", out["value"])
print(" ScriptPubKey:", out["scriptpubkey"])
print("Locktime:", transaction["locktime"])
Output:
How to Decode Input Data from a Transaction?
When transactions are transmitted over the network or exchanged between applications, they are serialized. Serialization is most commonly used for encoding data structures for transmission over a network or for storage in a file. The serialization format of a transaction output is shown in Transaction output serialization. This article focuses on discussing steps to decode input data from a transaction.
Table of Content
- What is Serialization?
- Raw Bitcoin Transaction
- Format of Various Fields in Serialized Transaction
- Decoding the Transaction
- Detailed Explanation
- Verification
- Conclusion