快速制作创世区块

这篇博客介绍如何在Ubuntu 16.04上使用Python脚本创建比特币的创世区块,包括设置环境、编写脚本、安装依赖库和运行脚本的步骤。脚本涉及SHA256、scrypt等加密算法,并提供了参数选项以自定义区块信息。
摘要由CSDN通过智能技术生成

先说一下环境,我用的是Ubuntu16.04,bitcoin源码0.12.2

一、脚本源码

  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.   # https://en.bitcoin.it/wiki/Difficulty  
  13.   bits, target   = get_difficulty(algorithm)  
  14.   
  15.   input_script  = create_input_script(options.timestamp)  
  16.   output_script = create_output_script(options.pubkey)  
  17.   # hash merkle root is the double sha256 hash of the transaction(s)   
  18.   tx = create_transaction(input_script, output_script,options)  
  19.   hash_merkle_root = hashlib.sha256(hashlib.sha256(tx).digest()).digest()  
  20.   print_block_info(options, hash_merkle_root, bits)  
  21.   
  22.   block_header        = create_block_header(hash_merkle_root, options.time, bits, options.nonce)  
  23.   genesis_hash, nonce = generate_hash(block_header, algorithm, options.nonce, target)  
  24.   announce_found_genesis(genesis_hash, nonce)  
  25.   
  26.   
  27. def get_args():  
  28.   parser = optparse.OptionParser()  
  29.   parser.add_option("-t""--time", dest="time", default=int(time.time()),   
  30.                    type="int", help="the (unix) time when the genesisblock is created")  
  31.   parser.add_option("-z""--timestamp", dest="timestamp", default="The Times 03/Jan/2009 Chancellor on brink of second bailout for banks",  
  32.                    type="string", help="the pszTimestamp found in the coinbase of the genesisblock")  
  33.   parser.add_option("-n""--nonce", dest="nonce", default=0,  
  34.                    type="int", help="the first value of the nonce that will be incremented when searching the genesis hash")  
  35.   parser.add_option("-a""--algorithm", dest="algorithm", default="SHA256",  
  36.                     help="the PoW algorithm: [SHA256|scrypt|X11|X13|X15]")  
  37.   parser.add_option("-p""--pubkey", dest="pubkey", default="04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f",  
  38.                    type="string", help="the pubkey found in the output script")  
  39.   parser.add_option("-v""--value", dest="value", default=5000000000,  
  40.                    type="int", help="the value in coins for the output, full value (exp. in bitcoin 5000000000 - To get other coins value: Block Value * 100000000)")  
  41.   
  42.   (options, args) = parser.parse_args()  
  43.   return options  
  44.   
  45. def get_algorithm(options):  
  46.   supported_algorithms = ["SHA256""scrypt""X11""X13"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值