Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. import hashlib, binascii, struct, array, os, time, sys, optparse
  2. import scrypt
  3.  
  4. from construct import *
  5.  
  6.  
  7. def main():
  8. options = get_args()
  9.  
  10. algorithm = get_algorithm(options)
  11.  
  12. input_script = create_input_script(options.timestamp)
  13. output_script = create_output_script(options.pubkey)
  14. # hash merkle root is the double sha256 hash of the transaction(s)
  15. tx = create_transaction(input_script, output_script,options)
  16. hash_merkle_root = hashlib.sha256(hashlib.sha256(tx).digest()).digest()
  17. print_block_info(options, hash_merkle_root)
  18.  
  19. #block_header = create_block_header(hash_merkle_root, options.time, options.bits, options.nonce)
  20. genesis_hash, nonce, block_time = generate_hash(hash_merkle_root, algorithm, options.time, options.bits, options.nonce)
  21. #generate_hash(block_header, algorithm, options.nonce, options.bits)
  22. announce_found_genesis(genesis_hash, nonce, block_time)
  23.  
  24.  
  25. def get_args():
  26. parser = optparse.OptionParser()
  27. parser.add_option("-t", "--time", dest="time", default=int(time.time()),
  28. type="int", help="the (unix) time when the genesisblock is created")
  29. parser.add_option("-z", "--timestamp", dest="timestamp", default="The Times 03/Jan/2009 Chancellor on brink of second bailout for banks",
  30. type="string", help="the pszTimestamp found in the coinbase of the genesisblock")
  31. parser.add_option("-n", "--nonce", dest="nonce", default=0,
  32. type="int", help="the first value of the nonce that will be incremented when searching the genesis hash")
  33. parser.add_option("-a", "--algorithm", dest="algorithm", default="SHA256",
  34. help="the PoW algorithm: [SHA256|scrypt|X11|X13|X15]")
  35. parser.add_option("-p", "--pubkey", dest="pubkey", default="04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f",
  36. type="string", help="the pubkey found in the output script")
  37. parser.add_option("-v", "--value", dest="value", default=5000000000,
  38. type="int", help="the value in coins for the output, full value (exp. in bitcoin 5000000000 - To get other coins value: Block Value * 100000000)")
  39. parser.add_option("-b", "--bits", dest="bits",
  40. type="int", help="the target in compact representation, associated to a difficulty of 1")
  41.  
  42. (options, args) = parser.parse_args()
  43. if not options.bits:
  44. if options.algorithm == "scrypt" or options.algorithm == "X11" or options.algorithm == "X13" or options.algorithm == "X15":
  45. options.bits = 0x1e0ffff0
  46. else:
  47. options.bits = 0x1d00ffff
  48. return options
  49.  
  50. def get_algorithm(options):
  51. supported_algorithms = ["SHA256", "scrypt", "X11", "X13", "X15"]
  52. if options.algorithm in supported_algorithms:
  53. return options.algorithm
  54. else:
  55. sys.exit("Error: Given algorithm must be one of: " + str(supported_algorithms))
  56.  
  57. def create_input_script(psz_timestamp):
  58. psz_prefix = ""
  59. #use OP_PUSHDATA1 if required
  60. if len(psz_timestamp) > 76: psz_prefix = '4c'
  61.  
  62. script_prefix = '04ffff001d0104' + psz_prefix + chr(len(psz_timestamp)).encode('hex')
  63. print (script_prefix + psz_timestamp.encode('hex'))
  64. return (script_prefix + psz_timestamp.encode('hex')).decode('hex')
  65.  
  66.  
  67. def create_output_script(pubkey):
  68. script_len = '41'
  69. OP_CHECKSIG = 'ac'
  70. return (script_len + pubkey + OP_CHECKSIG).decode('hex')
  71.  
  72.  
  73. def create_transaction(input_script, output_script,options):
  74. transaction = Struct("transaction",
  75. Bytes("version", 4),
  76. Byte("num_inputs"),
  77. StaticField("prev_output", 32),
  78. UBInt32('prev_out_idx'),
  79. Byte('input_script_len'),
  80. Bytes('input_script', len(input_script)),
  81. UBInt32('sequence'),
  82. Byte('num_outputs'),
  83. Bytes('out_value', 8),
  84. Byte('output_script_len'),
  85. Bytes('output_script', 0x43),
  86. UBInt32('locktime'))
  87.  
  88. tx = transaction.parse('\x00'*(127 + len(input_script)))
  89. tx.version = struct.pack('<I', 1)
  90. tx.num_inputs = 1
  91. tx.prev_output = struct.pack('<qqqq', 0,0,0,0)
  92. tx.prev_out_idx = 0xFFFFFFFF
  93. tx.input_script_len = len(input_script)
  94. tx.input_script = input_script
  95. tx.sequence = 0xFFFFFFFF
  96. tx.num_outputs = 1
  97. tx.out_value = struct.pack('<q' ,options.value)#0x000005f5e100)#012a05f200) #50 coins
  98. #tx.out_value = struct.pack('<q' ,0x000000012a05f200) #50 coins
  99. tx.output_script_len = 0x43
  100. tx.output_script = output_script
  101. tx.locktime = 0
  102. return transaction.build(tx)
  103.  
  104.  
  105. def create_block_header(hash_merkle_root, time, bits, nonce):
  106. block_header = Struct("block_header",
  107. Bytes("version",4),
  108. Bytes("hash_prev_block", 32),
  109. Bytes("hash_merkle_root", 32),
  110. Bytes("time", 4),
  111. Bytes("bits", 4),
  112. Bytes("nonce", 4))
  113.  
  114. genesisblock = block_header.parse('\x00'*80)
  115. genesisblock.version = struct.pack('<I', 1)
  116. genesisblock.hash_prev_block = struct.pack('<qqqq', 0,0,0,0)
  117. genesisblock.hash_merkle_root = hash_merkle_root
  118. genesisblock.time = struct.pack('<I', time)
  119. genesisblock.bits = struct.pack('<I', bits)
  120. genesisblock.nonce = struct.pack('<I', nonce)
  121. return block_header.build(genesisblock)
  122.  
  123.  
  124. # https://en.bitcoin.it/wiki/Block_hashing_algorithm
  125. #def generate_hash(data_block, algorithm, start_nonce, bits):
  126. #genesis_hash, nonce, time = generate_hash(hash_merkle_root, algorithm, options.time, options.bits, options.nonce)
  127. def generate_hash(hash_merkle_root, algorithm, start_time, bits, start_nonce):
  128.  
  129. print 'Searching for genesis hash..'
  130. nonce = start_nonce
  131. block_time = start_time
  132. data_block = create_block_header(hash_merkle_root, block_time, bits, nonce)
  133.  
  134. last_updated = time.time()
  135. # https://en.bitcoin.it/wiki/Difficulty
  136. target = (bits & 0xffffff) * 2**(8*((bits >> 24) - 3))
  137.  
  138. while True:
  139. sha256_hash, header_hash = generate_hashes_from_block(data_block, algorithm)
  140. last_updated = calculate_hashrate(nonce, last_updated)
  141. if is_genesis_hash(header_hash, target):
  142. if algorithm == "X11" or algorithm == "X13" or algorithm == "X15":
  143. return (header_hash, nonce, block_time)
  144. return (sha256_hash, nonce, block_time)
  145. else:
  146. nonce = nonce + 1
  147. if (nonce > 4294967295):
  148. nonce = 0
  149. block_time = block_time + 1
  150. print "Nonce wrapped, resetting it and increasing block header time to: " + str(block_time)
  151. data_block = create_block_header(hash_merkle_root, block_time, bits, nonce)
  152. else:
  153. data_block = data_block[0:len(data_block) - 4] + struct.pack('<I', nonce)
  154.  
  155.  
  156. def generate_hashes_from_block(data_block, algorithm):
  157. sha256_hash = hashlib.sha256(hashlib.sha256(data_block).digest()).digest()[::-1]
  158. header_hash = ""
  159. if algorithm == 'scrypt':
  160. header_hash = scrypt.hash(data_block,data_block,1024,1,1,32)[::-1]
  161. elif algorithm == 'SHA256':
  162. header_hash = sha256_hash
  163. elif algorithm == 'X11':
  164. try:
  165. exec('import %s' % "xcoin_hash")
  166. except ImportError:
  167. sys.exit("Cannot run X11 algorithm: module xcoin_hash not found")
  168. header_hash = xcoin_hash.getPoWHash(data_block)[::-1]
  169. elif algorithm == 'X13':
  170. try:
  171. exec('import %s' % "x13_hash")
  172. except ImportError:
  173. sys.exit("Cannot run X13 algorithm: module x13_hash not found")
  174. header_hash = x13_hash.getPoWHash(data_block)[::-1]
  175. elif algorithm == 'X15':
  176. try:
  177. exec('import %s' % "x15_hash")
  178. except ImportError:
  179. sys.exit("Cannot run X15 algorithm: module x15_hash not found")
  180. header_hash = x15_hash.getPoWHash(data_block)[::-1]
  181. return sha256_hash, header_hash
  182.  
  183.  
  184. def is_genesis_hash(header_hash, target):
  185. return int(header_hash.encode('hex_codec'), 16) < target
  186.  
  187.  
  188. def calculate_hashrate(nonce, last_updated):
  189. if nonce % 1000000 == 999999:
  190. now = time.time()
  191. hashrate = round(1000000/(now - last_updated))
  192. generation_time = round(pow(2, 32) / hashrate / 3600, 1)
  193. sys.stdout.write("\r%s hash/s, estimate: %s h"%(str(hashrate), str(generation_time)))
  194. sys.stdout.flush()
  195. return now
  196. else:
  197. return last_updated
  198.  
  199.  
  200. def print_block_info(options, hash_merkle_root):
  201. print "algorithm: " + (options.algorithm)
  202. print "merkle hash: " + hash_merkle_root[::-1].encode('hex_codec')
  203. print "pszTimestamp: " + options.timestamp
  204. print "pubkey: " + options.pubkey
  205. print "time: " + str(options.time)
  206. print "bits: " + str(hex(options.bits))
  207.  
  208.  
  209. def announce_found_genesis(genesis_hash, nonce, block_time):
  210. print "genesis hash found!"
  211. print "block header nonce: " + str(nonce)
  212. print "block header time: " + str(block_time)
  213. print "genesis hash: " + genesis_hash.encode('hex_codec')
  214.  
  215.  
  216. # GOGOGO!
  217. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement