Skip to content

Implement transaction decoding and TxID calculation#3400

Open
d3adlast45-commits wants to merge 1 commit into
github:mainfrom
d3adlast45-commits:patch-1
Open

Implement transaction decoding and TxID calculation#3400
d3adlast45-commits wants to merge 1 commit into
github:mainfrom
d3adlast45-commits:patch-1

Conversation

@d3adlast45-commits
Copy link
Copy Markdown

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)}")

Added functions to decode variable integers and transactions from hex strings, including legacy serialization for TxID calculation.  gh pr checkout 2587
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant