10

How does one calculate the hash of a Tx? Which fields need to be double SHA hashed and in what order?

ThePiachu
  • 43,091
  • 25
  • 139
  • 348
  • Do you mean the transaction ID hash that the JSON-RPC send* methods return? It can be confusing because hashes for transactions are calculated and signed to transmit bitcoins, but the hashes that are signed are NOT the same as the transaction ID hashes.... – gavinandresen Dec 08 '11 at 03:32
  • I mean the hashes calculated for the merkle tree, but I suppose both would be useful. – ThePiachu Dec 08 '11 at 11:16

3 Answers3

9

As explained by Gavin Andersen on the forum - in order to calculate the ID hash of the Tx that is used in the Merkle Tree, one needs to SHA hash the whole Tx message as defined in the Protocol Specification wiki page twice.

For example, for the only Tx from the Genesis Block one would double hash the following array of bytes:

01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000

And if double hashed correctly, it would give the following hash:

4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B

ThePiachu
  • 43,091
  • 25
  • 139
  • 348
  • 2
    Little endian! In most implementations that double-hash would yield 3b a3 ed fd 7a 7b 12 b2 7a c7 2c 3e 67 76 8f 61 7f c8 1b c3 88 8a 51 32 3a 9f b8 aa 4b 1e 5e 4a, but you'll want to reverse that: 4a 5e 1e 4b aa b8 9f 3a 32 51 8a 88 c3 1b c8 7f 61 8f 76 67 3e 2c c7 7a b2 12 7b 7a fd ed a3 3b, resulting in the actual hash that is actually used. – 19h Mar 07 '16 at 00:42
  • Thanks, here's my php implementation: function getTransactionHash($transaction_in_hex) { $bin = hex2bin($transaction_in_hex); $hash = hex2bin(hash('sha256', hex2bin(hash('sha256', $bin)))); return bin2hex(strrev($hash)); } – felix021 Feb 11 '18 at 15:40
1

Here's a python implementation to find the doubleSHA256 hash for genesis block:

01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000

import codecs

//switch the endianness of a given string
def revEndian(string):
    return ''.join(reversed([string[i:i+2] for i in range(0, len(string), 2)]))

//convert a bytebuffer into a string
def hashStr(bytebuffer):
    return str(codecs.encode(bytebuffer, 'hex'))[2:-1]

//find the double sha256 hash for a given hex string
def doubleSha256(hex): 
    bin = codecs.decode(hex, 'hex')
    hash = hashlib.sha256(bin).digest()
    hash2 = hashlib.sha256(hash).digest()
    return revEndian(hashStr(hash2))
Ava Chow
  • 70,382
  • 5
  • 81
  • 161
-3

There's a diagram explaining the process of hashing a transaction when creating or verifying the signature of a bitcoin transaction on the forum, and a higher resolution version of the same image on dropbox.

Chris Moore
  • 14,825
  • 6
  • 66
  • 87