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?

2 Answers 2


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

The internal byte order used by the Bitcoin is in fact Little-Endian, while the RPC byte order is Big-Endian. For details explaining how this works refer this link.


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 agree to our terms of service and acknowledge you have read our privacy policy.

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