Bitcoin Forum
November 24, 2017, 08:52:03 PM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Python Bitcoin - Calculating Block-Header  (Read 1403 times)
Rambo123
Newbie
*
Offline Offline

Activity: 2


View Profile
September 20, 2015, 12:05:55 PM
 #1

Hello everyone,

I want to calculate the block hash from this block by my own with the following python script:

https://blockchain.info/rawblock/00000000000000000be983a81043933c38008010b849fd6a35d5dd2d57f929bd

Code:
hash: 00000000000000000be983a81043933c38008010b849fd6a35d5dd2d57f929bd
ver: 3
prev_block: 0000000000000000051f5de334085b92ce27c03888c726c9b2bb78069e55aeb6
mrkl_root: f4db18d3ecab87eeb23a56490d5b0b514848d510d409b43f6bbf2b82f55da8db
time: 1442663985
bits: 403867578
nonce: 3548193207

My conversion looks like this:

Code:
ver --> HextoBinary --> 3 --> 11 --> 00000011
prev_block --> No conversion necessary (Hex)
mrkl_root --> No conversion necessary (Hex)
time --> toHex --> 1442663985 --> 55fd4e31 (Hex)
bits --> toHex --> 403867578 --> 181287ba
nonce --> toHex --> 3548193207 --> d37d21b7

Script:

Code:
import hashlib
header_hex = ("000000110000000000000000051f5de334085b92ce27c03888c726c9b2bb78069e55aeb6f4db18d3ecab87eeb23a56490d5b0b514848d510d409b43f6bbf2b82f55da8db55fd4e31181287bad37d21b7")
header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
hash.encode('hex_codec')
print hash[::-1].encode('hex_codec')

Unfortunately, the result looks like this:

7012fc1c69b4b5d0c0df1b732c5ea58752e96bd8f53f7c09d2f5b57bcc0186d1

but it should be

00000000000000000be983a81043933c38008010b849fd6a35d5dd2d57f929bd

Maybe I do something wrong with the version or prev_block field?
Thank you for your support Smiley

Sincerely,
Rambo123
1511556723
Hero Member
*
Offline Offline

Posts: 1511556723

View Profile Personal Message (Offline)

Ignore
1511556723
Reply with quote  #2

1511556723
Report to moderator
Join ICO Now Coinlancer is Disrupting the Freelance marketplace!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1511556723
Hero Member
*
Offline Offline

Posts: 1511556723

View Profile Personal Message (Offline)

Ignore
1511556723
Reply with quote  #2

1511556723
Report to moderator
1511556723
Hero Member
*
Offline Offline

Posts: 1511556723

View Profile Personal Message (Offline)

Ignore
1511556723
Reply with quote  #2

1511556723
Report to moderator
1511556723
Hero Member
*
Offline Offline

Posts: 1511556723

View Profile Personal Message (Offline)

Ignore
1511556723
Reply with quote  #2

1511556723
Report to moderator
Nancarrow
Hero Member
*****
Offline Offline

Activity: 494


View Profile
September 20, 2015, 12:53:50 PM
 #2

You need to remember all of the bytearrays are little-endian. Or big-endian, I can never remember which is which. Point is you need to reverse the byte order for each field.

So header_hex starts with "11000000...". The next part will be the byte-reversed previous block hash - so it starts with "b6ae55" and ends with the string of zeroes.
Similarly for all the remaining fields.

ETA I see from your "hash[::-1]" construction that you know about the endianness. Well that applies to each field rather than the entire block header. At least "[::-1]" gives you a very efficient way to do it.

If I've said anything amusing and/or informative and you're feeling generous:
1GNJq39NYtf7cn2QFZZuP5vmC1mTs63rEW
Kefkius
Member
**
Offline Offline

Activity: 64


View Profile
September 25, 2015, 05:52:00 PM
 #3

In addition to the above answer, line 4 of the below code - hash.encode('hex_codec') - is an expression that returns a value, but doesn't change hash, so it's not really doing anything.

<snip>

Script:

Code:
import hashlib
header_hex = ("000000110000000000000000051f5de334085b92ce27c03888c726c9b2bb78069e55aeb6f4db18d3ecab87eeb23a56490d5b0b514848d510d409b43f6bbf2b82f55da8db55fd4e31181287bad37d21b7")
header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
hash.encode('hex_codec')
print hash[::-1].encode('hex_codec')

<snip>

Developer of FreeBitcoins.com Clamcoin faucet.
teukon
Legendary
*
Offline Offline

Activity: 1246



View Profile
September 27, 2015, 10:56:11 PM
 #4

For block 00000000000000000be983a81043933c38008010b849fd6a35d5dd2d57f929bd:
  • version = 3, encoded as '03000000' (4-byte little-endian);
  • previous_hash = 'b6ae559e0678bbb2c926c78838c027ce925b0834e35d1f050000000000000000';
  • merkle_root = 'dba85df5822bbf6b3fb409d410d54848510b5b0d49563ab2ee87abecd318dbf4';
  • time = 1442663985, encoded as '314efd55' (4-byte little-endian);
  • bits = '181287ba', stored as 'ba871218';
  • nonce = 3548193207, encoded as 'b7217dd3' (4-byte little-endian).

With these changes, your Python 2 code becomes:
Code:
import hashlib
header_hex = '03000000b6ae559e0678bbb2c926c78838c027ce925b0834e35d1f050000000000000000dba85df5822bbf6b3fb409d410d54848510b5b0d49563ab2ee87abecd318dbf4314efd55ba871218b7217dd3'
header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
print hash[::-1].encode('hex_codec')
which yields 00000000000000000be983a81043933c38008010b849fd6a35d5dd2d57f929bd as required.
vlajce
Member
**
Offline Offline

Activity: 98

i wanna tool picture


View Profile
October 02, 2015, 01:33:09 AM
 #5

OP this one is good. Can you send us code.py?

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!