Author

Topic: Python Bitcoin - Calculating Block-Header (Read 1652 times)

member
Activity: 112
Merit: 10
welcome
October 01, 2015, 08:33:09 PM
#5
OP this one is good. Can you send us code.py?
legendary
Activity: 1246
Merit: 1011
September 27, 2015, 05: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.
member
Activity: 64
Merit: 21
September 25, 2015, 12: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.



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')


hero member
Activity: 492
Merit: 503
September 20, 2015, 07:53:50 AM
#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.
newbie
Activity: 2
Merit: 0
September 20, 2015, 07:05:55 AM
#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
Jump to: