先说一下环境,我用的是Ubuntu16.04,bitcoin源码0.12.2
一、脚本源码
- import hashlib, binascii, struct, array, os, time, sys, optparse
- import scrypt
- from construct import *
- def main():
- options = get_args()
- algorithm = get_algorithm(options)
- # https://en.bitcoin.it/wiki/Difficulty
- bits, target = get_difficulty(algorithm)
- input_script = create_input_script(options.timestamp)
- output_script = create_output_script(options.pubkey)
- # hash merkle root is the double sha256 hash of the transaction(s)
- tx = create_transaction(input_script, output_script,options)
- hash_merkle_root = hashlib.sha256(hashlib.sha256(tx).digest()).digest()
- print_block_info(options, hash_merkle_root, bits)
- block_header = create_block_header(hash_merkle_root, options.time, bits, options.nonce)
- genesis_hash, nonce = generate_hash(block_header, algorithm, options.nonce, target)
- announce_found_genesis(genesis_hash, nonce)
- def get_args():
- parser = optparse.OptionParser()
- parser.add_option("-t", "--time", dest="time", default=int(time.time()),
- type="int", help="the (unix) time when the genesisblock is created")
- parser.add_option("-z", "--timestamp", dest="timestamp", default="The Times 03/Jan/2009 Chancellor on brink of second bailout for banks",
- type="string", help="the pszTimestamp found in the coinbase of the genesisblock")
- parser.add_option("-n", "--nonce", dest="nonce", default=0,
- type="int", help="the first value of the nonce that will be incremented when searching the genesis hash")
- parser.add_option("-a", "--algorithm", dest="algorithm", default="SHA256",
- help="the PoW algorithm: [SHA256|scrypt|X11|X13|X15]")
- parser.add_option("-p", "--pubkey", dest="pubkey", default="04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f",
- type="string", help="the pubkey found in the output script")
- parser.add_option("-v", "--value", dest="value", default=5000000000,
- type="int", help="the value in coins for the output, full value (exp. in bitcoin 5000000000 - To get other coins value: Block Value * 100000000)")
- (options, args) = parser.parse_args()
- return options
- def get_algorithm(options):
- supported_algorithms = ["SHA256", "scrypt", "X11", "X13",