Home Bitcoin The best way to compute a TXID of any bitcoin transaction in Python

The best way to compute a TXID of any bitcoin transaction in Python

0
The best way to compute a TXID of any bitcoin transaction in Python

[ad_1]

That is a SegWit transaction, not all of it’s a part of the transaction hash preimage (see right here for particulars). You need to exclude the SegWit elements. This Bash one-liner returns the proper TXID:

echo '0100000001189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c200000000000ffffffff0240420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf500000000' | tr -d 'n' | xxd -r -p | sha256sum | grep -oE '^[0-9a-f]+' | xxd -r -p | sha256sum | grep -oE '^[0-9a-f]+' | tac -rs '..'; echo

Output

9d2d91b764ba2559b098cdd30525e2160bb15c6e27d6869e7b121a9037d8bd86

Clarification, your TX might be disassembled as follows, and it’s a must to hash every part besides the daring elements:

TX model: 01000000
Witness flag: 0001
Enter rely: 01
Prevout reference: 189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c2000000000
Unlocking script size: 00
Unlocking script: EMPTY (observe: non-SegWit inputs could have script size >0 and have the unlocking script right here as a substitute of within the witness, so for non-SegWit this won’t be EMPTY)
Sequence: ffffffff
Output rely: 02
Output 01: 40420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac
Output 02: 6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf5
Witness rely: 02
Witness 01: 473044022001609cd43eb8e9b8f8438eded9f6b10bad32efd7620724ccd2ed5277c0c6a3ae02200f0c1c3f4c409ada536d2363a2d8bdad418df67fed9b36bfa4482bd9985bf39601
Witness 02: 2102ee3c98964dd1bfe13bee16c0b95fcf8281f12c5885d1fcb7b59fc2cb01ca7632
TX locktime: 00000000

Concatenating solely the non-SegWit elements offers us the TX hash preimage: 0100000001189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c200000000000ffffffff0240420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf500000000

[ad_2]

Supply hyperlink

LEAVE A REPLY

Please enter your comment!
Please enter your name here