Implement transaction decoding and TxID calculation#3400
Open
d3adlast45-commits wants to merge 1 commit into
Open
Implement transaction decoding and TxID calculation#3400d3adlast45-commits wants to merge 1 commit into
d3adlast45-commits wants to merge 1 commit into
Conversation
Added functions to decode variable integers and transactions from hex strings, including legacy serialization for TxID calculation. gh pr checkout 2587
This was referenced May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added functions to decode variable integers and transactions from hex strings, including legacy serialization for TxID calculation. gh pr checkout 2587import binascii
import struct
raw_tx = "020000000257ded1c7a09c2965a12a486b4ad37c45da485a94ab96347e0400294d445380510900000000fdffffff57ded1c7a09c2965a12a486b4ad37c45da485a94ab96347e0400294d445380510b00000000fdffffff010000000000000000296a2765766572797468696e6727732066696e65207c204f6e207369676e6574207765206c6561726e2e32a70400"
bytes_tx = binascii.unhexlify(raw_tx)
Decode
version = bytes_tx[0:4]
inputs
num_inputs = bytes_tx[4]
Input 1
in1_txid = bytes_tx[5:37]
in1_vout = bytes_tx[37:41]
... variable length script sig ...
Wait, the provided hex has "00" scriptSig length?
Let's parse dynamically.
offset = 4
num_inputs = bytes_tx[offset]
offset += 1
inputs = []
for i in range(num_inputs):
txid = bytes_tx[offset:offset+32]
offset += 32
vout = bytes_tx[offset:offset+4]
offset += 4
script_len = bytes_tx[offset]
offset += 1
script = bytes_tx[offset:offset+script_len]
offset += script_len
sequence = bytes_tx[offset:offset+4]
offset += 4
inputs.append({"txid": binascii.hexlify(txid[::-1]), "vout": struct.unpack("<I", vout)[0], "seq": binascii.hexlify(sequence)})
num_outputs = bytes_tx[offset]
offset += 1
outputs = []
for i in range(num_outputs):
value = bytes_tx[offset:offset+8]
offset += 8
script_len = bytes_tx[offset]
offset += 1
script = bytes_tx[offset:offset+script_len]
offset += script_len
outputs.append({"value": struct.unpack("<Q", value)[0], "script": binascii.hexlify(script)})
locktime = bytes_tx[offset:offset+4]
print(f"Inputs: {inputs}")
print(f"Outputs: {outputs}")
print(f"Locktime: {binascii.hexlify(locktime)}")
message
msg_hex = outputs[0]['script'][4:] # Skip 6a (OP_RETURN) and 27 (len)
print(f"Message: {binascii.unhexlify(msg_hex)}")