I'm trying to parse a bitcoin block using Python.

I called getrawtransaction over JSON-RPC. I got this:


How do I calculate the txid of this raw transaction?

up vote 3 down vote accepted

TXIDs have two forms; the form used internally for outpoints and merkle leaves (internal byte order), and the form used in RPC calls and block explorers (RPC byte order). These are often called, respectivey, little-endian byte order and big-endian byte order (although those descriptions aren't quite accurate).

Here's python code from the Bitcoin.org Developer Documentation generating both forms for the TXID for the transaction in your question:

#!/usr/bin/env python

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 = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000".decode("hex")
hash = sha256(sha256(data).digest()).digest()

print "Warning: this code only tested on a little-endian x86_64 arch"
print "System byte order:", byteorder
print "Internal-Byte-Order Hash: ", hash.encode('hex_codec')
print "RPC-Byte-Order Hash:      ", hash[::-1].encode('hex_codec')

And here's the output:

Warning: this code only tested on a little-endian x86_64 arch

System byte order: little
Internal-Byte-Order Hash:  660802c98f18fd34fd16d61c63cf447568370124ac5f3be626c2e1c3c9f0052d
RPC-Byte-Order Hash:       2d05f0c9c3e1c226e63b5fac240137687544cf631cd616fd34fd188fc9020866
  • I honestly don't know which of these are big-endian/little-endian. Is the RPC big-endian and the txid forms used in raw transactions little-endian, or is it the other way around? – Thorkil Værge Mar 21 at 9:05

Call: bitcoin-cli decoderawtransaction 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000 from your bitcoin client, you'll get:

    "txid" : "2d05f0c9c3e1c226e63b5fac240137687544cf631cd616fd34fd188fc9020866",
    "version" : 1,
    "locktime" : 0,
    "vin" : [
            "coinbase" : "04ffff001d014d",
            "sequence" : 4294967295
    "vout" : [
            "value" : 50.00000000,
            "n" : 0,
            "scriptPubKey" : {
                "asm" : "04e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93 OP_CHECKSIG",
                "hex" : "4104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac",
                "reqSigs" : 1,
                "type" : "pubkey",
                "addresses" : [

Alternatively you can use Gavin Andresen's deserializing script written in Python: https://github.com/gavinandresen/bitcointools/blob/master/deserialize.py

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.