def add_transaction(): try: transaction_json = request.get_json(force=True) private_key = transaction_json['privateKey'] public_key = transaction_json['publicKey'] value = float(transaction_json['value']) if not re.match(r'[\da-f]{66}$', public_key): return jsonify({'message': 'Invalid address'}), 400 if value < 1: return jsonify({'message': 'Invalid amount. Minimum allowed amount is 1'}), 400 wallet = KeyPair(private_key) if(public_key == wallet.public_key): return jsonify({'message': 'Destination same as source'}), 400 balance = blockchain.get_balance(wallet.public_key) balance_discount = blockchain.get_balance_discount(wallet.public_key) if balance + balance_discount < value: return jsonify({'message': 'Insuficient amount of coins'}), 400 transaction = wallet.create_transaction(public_key, value) send_transaction = json.dumps({ 'source': transaction.source, 'destination': transaction.destination, 'amount': transaction.amount, 'fee': transaction.fee, 'timestamp': transaction.timestamp, 'tx_hash': transaction.tx_hash, 'signature': transaction.signature }) blockchain.add_transaction(transaction) announce_new_transaction(send_transaction) return jsonify({'message': f'Pending transaction {transaction.tx_hash} added to the Blockchain'}), 201 except BlockchainException as bce: return jsonify({'message': f'Transaction rejected: {bce}'}), 400
def add_transaction2(private_key, public_key, value): try: wallet = KeyPair(private_key) transaction = wallet.create_transaction(public_key, float(value)) blockchain.add_transaction(transaction) return jsonify({'message': f'Pending transaction {transaction.tx_hash} added to the Blockchain'}), 201 except BlockchainException as bce: return jsonify({'message': f'Transaction rejected: {bce}'}), 400
def generatePublicKey(): json = request.get_json(force=True) wallet = KeyPair(json["private_key"]) data = { 'public_key':wallet.public_key } return jsonify(data), 200
def generateWallet(): wallet = KeyPair() data = { 'private_key':wallet.private_key, 'public_key':wallet.public_key } return jsonify(data), 200
def generate_key(): wallet = KeyPair() rs = [{ 'private_key': f'{wallet.private_key}' }, { 'public_key': f'{wallet.public_key}' }] return jsonify(rs), 200
def minerarBloco(self, address): wallet = KeyPair(address) r = requests.get('https://uclcriptocoin.herokuapp.com/block/minable/' + wallet.public_key) print(r.text) last_block = json.loads(r.text) block = Block.from_dict(last_block["block"]) difficulty = last_block["difficulty"] while block.current_hash[:difficulty].count('0') < difficulty: block.nonce += 1 block.recalculate_hash() data = json.dumps(block, default=lambda x: x.__dict__) r = requests.post('https://uclcriptocoin.herokuapp.com/block', data, json=True) print(r.text)
def generatePublicKey(address): wallet = KeyPair(address) data = { 'public_key':wallet.public_key } return jsonify(data), 200