def load_keys(): if wallet.load_keys(): global blockchain blockchain = Blockchain(wallet.public_key) response = { 'public_key': wallet.public_key, 'private_key': wallet.private_key, 'funds': blockchain.get_balance() } return jsonify(response), 201 else: response = { 'message': 'Loading keys failed' } return jsonify(response), 500
def listen_for_inputs(self): waiting_for_input = True while waiting_for_input: print('1: transaction') print('2: mine block') print('3: history') print('4: validity') print('5: create wallet') print('6: load wallet') print('7: save wallet') print('q: exit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("added transaction") else: print('transaction failed') elif user_choice == '2': if not self.blockchain.mine_block(): print("Mining failed") elif user_choice == '3': self.print_blockchain_history() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('all true') else: print("invalid exist") elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print("input invalid") if not Verification.verify_chain(self.blockchain.get_chain()): self.print_blockchain_history() print("invalid blockchain") break
def create_keys(): wallet.create_keys() if wallet.save_keys(): global blockchain blockchain = Blockchain(wallet.public_key) response = { 'public_key': wallet.public_key, 'private_key': wallet.private_key } return jsonify(response), 201 else: response = { 'message': 'Saving keys failed' } return jsonify(response), 500
def __init__(self): self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key)
class Node: def __init__(self): self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): tx_recipient = input('Enter sender of txn:') tx_amount = float(input('Enter txn amount')) return (tx_recipient, tx_amount) def get_user_choice(self): user_input = input("User choice: ") def print_blockchain_history(self): for block in self.blockchain.get_chain: print("Outputting Block") print(block) def listen_for_inputs(self): waiting_for_input = True while waiting_for_input: print('1: transaction') print('2: mine block') print('3: history') print('4: validity') print('5: create wallet') print('6: load wallet') print('7: save wallet') print('q: exit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("added transaction") else: print('transaction failed') elif user_choice == '2': if not self.blockchain.mine_block(): print("Mining failed") elif user_choice == '3': self.print_blockchain_history() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('all true') else: print("invalid exist") elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print("input invalid") if not Verification.verify_chain(self.blockchain.get_chain()): self.print_blockchain_history() print("invalid blockchain") break
from flask import Flask, jsonify, request from uuid import uuid4 from bc import Blockchain import json # instantiate the app app = Flask(__name__) # generate a unique address for this node node_address = str(uuid4()).replace('-','') # instantiate the blockchain blockchain = Blockchain() # basic blockchain functionalities # - mine a block # - add new transactions # - to view the whole blockchain # API endpoint to mine new blocks @app.route('/mine', methods=['GET']) def mine(): # get the last block of the blockchain last_block = blockchain.last_block # get the proof of the last block last_proof = last_block['proof'] current_proof = blockchain.proof_of_work(last_proof) # Node recieves 1 amount of currency for mining one block
requests.post(url=validated_url, data=json.dumps({'validated': 'True'})) else: requests.post(url=validated_url, data=json.dumps({'validated': 'False'})) last_transaction = current_transaction['id'] await asyncio.sleep(1) sync() asyncio.Task(listener()) client = discord.Client() bc = Blockchain() bc.add_advertiser('corpinc', 2000, "myad") listener() if (bc.blocks == []): bc.root() @client.event async def on_message(message): # we do not want the bot to reply to itself if message.author == client.user: return if message.content.startswith('!AdBot give'): content = message.content[11:] status = bc.add_transaction('server', str(message.author),
} return jsonify(response), 200 @app.route('/node', methods=["GET"]) def get_nodes(): nodes = blockchain.get_peer_nodes() response = { "all_nodes": nodes } return jsonify(response), 200 @app.route('/chain', methods=["GET"]) def get_chain(): chain_snapshot = blockchain.chain dict_chain = [block.__dict__.copy() for block in chain_snapshot] for dict_block in dict_chain: dict_block['transactions'] = [ tx.__dict__ for tx in dict_block['transactions']] return jsonify(dict_chain), 200 if __name__ == '__main__': from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument('-p', '--port', type=int, default = 5000) args = parser.parse_args() port = args.port wallet = Wallet(port) blockchain = Blockchain(wallet.public_key, port) app.run(host="0.0.0.0", port=port)