#!/usr/bin/env python # This python script is used in the YouTube Mobilefish.com Blockchain tutorial 27: Bitcoin Raw TX & TXID # Source: https://bitcoin.stackexchange.com/questions/32765/how-do-i-calculate-the-txid-of-this-raw-transaction # TXIDs have two forms: # - Internal-Byte-Order Hash (big endian byte order) # The form used internally for outpoints and merkle leaves (internal byte order) # - RPC-Byte-Order Hash (little endian byte order) # The form used in RPC calls and block explorers (RPC byte order). from sys import byteorder from hashlib import sha256 # You can put in $data an 80-byte block header to get its header hash, # or a raw transaction to get its txid data = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000".decode("hex") hash = sha256(sha256(data).digest()).digest() print "Warning: this code only tested on a little-endian x86_64 arch" print print "System byte order:", byteorder print "Internal-Byte-Order Hash: ", hash.encode('hex_codec') print "RPC-Byte-Order Hash: ", hash[::-1].encode('hex_codec') print print "===========================================" print "Data in unicode", data