def getInitialUser(): private_key = ed25519.Ed25519PrivateKey.from_private_bytes( bytes.fromhex( "1d82897e5881368cac9eb99126cdfca1e0317629dbeaa7280484c5dae81e932b") ) public_key = ed25519.Ed25519PublicKey.from_public_bytes( bytes.fromhex( "75efa6f1fdf1393a5ea815b2b3690293d079df187944f22ec79f3380ef7bd743") ) txns = [ Transaction.build_signed_txn( "75efa6f1fdf1393a5ea815b2b3690293d079df187944f22ec79f3380ef7bd743", [], [ OutFlow( 10000, "75efa6f1fdf1393a5ea815b2b3690293d079df187944f22ec79f3380ef7bd743" ) ], "1d82897e5881368cac9eb99126cdfca1e0317629dbeaa7280484c5dae81e932b") ] # Note that we need two hexadecimal digits for the bytes.fromhex( str ) method to work, # hence we include two zeros block_obj = Block("00", txns, 0) success = False while not success: magic_num = os.urandom(32).hex() block_obj.magic_num = magic_num if int(block_obj.get_hash(), 16) & 0xFFFF == 0xCCCC: success = True print(json.dumps(block_obj.serialize()))
mt1 = Transaction() mt1.add_output(TransOutput(Btc(MINING_BTCS), key1.get_address())) b1.set_head_transaction(mt1) b1.set_index(1) # 挖矿 b1.find_randnum() # 添加区块 bc.add_block(b1) # key1向key2转账 t2 = Transaction() t2.add_input(TransInput(1, 1, 1)) t2.add_output(TransOutput(Btc("23.567"), key2.get_address())) t2.sign_transaction(key1) if not Verify.verify_transaction(t2): print("交易有问题") b2 = Block(pre_hash=b1.get_hash()) b2.add_transaction(t2) mt2 = Transaction() mt2.add_output(TransOutput(Btc(MINING_BTCS), key2.get_address())) # 计算交易费 fee = bc.compute_block_fee(b2) mt2.add_output(TransOutput(fee, key2.get_address())) b2.set_head_transaction(mt2) b2.set_index(2) b2.find_randnum() if not Verify.verify_block_depth(b2): print("区块有问题") bc.add_block(b2) if not Verify.verify_blockchain_depth(): print("区块链有问题")