Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Martin P. Hellwig on June 12, 2011, 12:27:33 PM



Title: How is the hash of a transaction calculated? (Move to dev discussion please?)
Post by: Martin P. Hellwig on June 12, 2011, 12:27:33 PM
Hi all,

I was wondering how the hash was calculated of a transaction, so I can replicate it in my python code.
I have read up on:
https://en.bitcoin.it/wiki/Protocol_specification#tx
and
https://en.bitcoin.it/wiki/Dump_format

But I think I am missing something.

For example the block http://blockexplorer.com/b/130264
hash a transaction with hash "230cf0853ffdd82e99fbf1eef37cc3e97de310812d9e2a3ba79fe1ffa9518109"

How is this hash calculated with the given context?
---cut---
Code:
    {
      "hash":"230cf0853ffdd82e99fbf1eef37cc3e97de310812d9e2a3ba79fe1ffa9518109",
      "ver":1,
      "vin_sz":1,
      "vout_sz":2,
      "lock_time":0,
      "size":257,
      "in":[
        {
          "prev_out":{
            "hash":"e65af16c23e007da9b15ddad8b9ea8e27fd6181ac71c197b1e96a8aa53e32e70",
            "n":0
          },
          "scriptSig":"304402204076ee83b670eed45880d63de03b1c0416516c78d6ae3cd89896c5ab14e2f8700220170c0558f5f55f6c278017a493ba26e0ca6d7611130da747184bce7ac8e5381601 042563b05ef6870d0aaaec0365d0cf2b9d8264b7ed1b86e38c8a57166e2d04a3e6176f434ec93ac7c6dceafcdb4c8b633859580b50555e6020594298ac7be14d93"
        }
      ],
      "out":[
        {
          "value":"3.47000000",
          "scriptPubKey":"OP_DUP OP_HASH160 ef7af3db798daeb7f8206e2ecfbfc2465524d9e1 OP_EQUALVERIFY OP_CHECKSIG"
        },
        {
          "value":"129.50000000",
          "scriptPubKey":"OP_DUP OP_HASH160 2008f9f46db9ccd1fc8eda2fdcae57e258be0865 OP_EQUALVERIFY OP_CHECKSIG"
        }
      ]
    },
---cut---

Thanks for any suggestions,

Martin


Title: Re: How is the hash of a transaction calculated? (Move to dev discussion please?)
Post by: theymos on June 12, 2011, 12:59:30 PM
You need to put that data into the binary format before hashing it.


Title: Re: How is the hash of a transaction calculated? (Move to dev discussion please?)
Post by: Martin P. Hellwig on June 12, 2011, 02:09:08 PM
Ah Cool thanks,

One thing I noticed is that https://en.bitcoin.it/wiki/Protocol_specification#tx says that both [tx_in count, tx_out count] are a byte or more size.
But is that actually true? As far as I can see the parser does not know in advance what size that count field is.

Is the spec wrong in saying this field is variable size and should be one byte and as such a transaction can have a maximum of 256 inputs and outputs?


Title: Re: How is the hash of a transaction calculated? (Move to dev discussion please?)
Post by: joan on June 12, 2011, 03:58:36 PM
Ah Cool thanks,

One thing I noticed is that https://en.bitcoin.it/wiki/Protocol_specification#tx says that both [tx_in count, tx_out count] are a byte or more size.
But is that actually true? As far as I can see the parser does not know in advance what size that count field is.

Is the spec wrong in saying this field is variable size and should be one byte and as such a transaction can have a maximum of 256 inputs and outputs?
They are variable integers, check the page you linked further up.
If the byte value is 252 or less (which is almost always the case for tx_in_count and tx_out_count), it's coded on one byte only and the count is the byte value. If it's 253, the count is coded on the next 2 bytes, etc.


Title: Re: How is the hash of a transaction calculated? (Move to dev discussion please?)
Post by: Martin P. Hellwig on June 12, 2011, 04:42:24 PM
Ah yes, under common structures, variable length integers. Thanks for the pointer!