Bitcoin Forum
December 03, 2016, 09:41:43 AM *
News: Latest stable version of Bitcoin Core: 0.13.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: How is the hash of a transaction calculated? (Move to dev discussion please?)  (Read 782 times)
Martin P. Hellwig
Jr. Member
*
Offline Offline

Activity: 33


View Profile
June 12, 2011, 12:27:33 PM
 #1

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
1480758103
Hero Member
*
Offline Offline

Posts: 1480758103

View Profile Personal Message (Offline)

Ignore
1480758103
Reply with quote  #2

1480758103
Report to moderator
1480758103
Hero Member
*
Offline Offline

Posts: 1480758103

View Profile Personal Message (Offline)

Ignore
1480758103
Reply with quote  #2

1480758103
Report to moderator
1480758103
Hero Member
*
Offline Offline

Posts: 1480758103

View Profile Personal Message (Offline)

Ignore
1480758103
Reply with quote  #2

1480758103
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1480758103
Hero Member
*
Offline Offline

Posts: 1480758103

View Profile Personal Message (Offline)

Ignore
1480758103
Reply with quote  #2

1480758103
Report to moderator
1480758103
Hero Member
*
Offline Offline

Posts: 1480758103

View Profile Personal Message (Offline)

Ignore
1480758103
Reply with quote  #2

1480758103
Report to moderator
1480758103
Hero Member
*
Offline Offline

Posts: 1480758103

View Profile Personal Message (Offline)

Ignore
1480758103
Reply with quote  #2

1480758103
Report to moderator
theymos
Administrator
Legendary
*
expert
Offline Offline

Activity: 2492


View Profile
June 12, 2011, 12:59:30 PM
 #2

You need to put that data into the binary format before hashing it.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
Martin P. Hellwig
Jr. Member
*
Offline Offline

Activity: 33


View Profile
June 12, 2011, 02:09:08 PM
 #3

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?
joan
Jr. Member
*
Offline Offline

Activity: 56



View Profile
June 12, 2011, 03:58:36 PM
 #4

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.
Martin P. Hellwig
Jr. Member
*
Offline Offline

Activity: 33


View Profile
June 12, 2011, 04:42:24 PM
 #5

Ah yes, under common structures, variable length integers. Thanks for the pointer!
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!