Ejemplo n.º 1
0
def resetPassword():

    if request.method == 'POST':

        password = request.form.get('password')

        studentQuery = db.Students.find({'Email': dbEmail})
        ownerQuery = db.Owners.find({'Email': dbEmail})
        authorityQuery = db.Authorities.find({'Email': dbEmail})

        if studentQuery.count() > 0:
            db.Students.update_one({'Email': dbEmail}, {
                '$set': {
                    'Password': str(hashpw(password.encode('utf-8'), salt))
                }
            })
        elif ownerQuery.count() > 0:
            db.Owners.update_one({'Email': dbEmail}, {
                '$set': {
                    'Password': str(hashpw(password.encode('utf-8'), salt))
                }
            })
        elif authorityQuery.count() > 0:
            db.Authorities.update_one({'Email': dbEmail}, {
                '$set': {
                    'Password': str(hashpw(password.encode('utf-8'), salt))
                }
            })
        else:
            return jsonify(Error='User does not exist')
        return jsonify(Success='Password changed successfully')
Ejemplo n.º 2
0
def new_transaction():
    values = imports.request.form

    # Check that the required fields are in the POST'ed data
    required = ['sender_address', 'recipient_address', 'amount', 'signature','miningReward']
    if not all(k in values for k in required):
        return 'Missing values', 400
    # Create a new Transaction
    transaction_result = blockchain.submit_transaction(values['sender_address'], values['recipient_address'], values['amount'], values['signature'],values['miningReward'])

    if transaction_result == False:
        response = {'message': 'Invalid Transaction!'}
        return imports.jsonify(response), 404
    else:
        response = {'message': 'Transaction will be added to Block '+ str(transaction_result)}
        return imports.jsonify(response), 201
Ejemplo n.º 3
0
def generate_transaction():
    sender_address = imports.request.form['sender_address']
    sender_private_key = imports.request.form['sender_private_key']
    recipient_address = imports.request.form['recipient_address']
    value = imports.request.form['amount']
    MReward = imports.request.form['miningReward']

    transaction = trans.Transaction(sender_address, sender_private_key,
                                    recipient_address, value, MReward)
    response = {
        'transaction': transaction.to_dict(),
        'signature': transaction.sign_transaction()
    }
    return imports.jsonify(response), 200
Ejemplo n.º 4
0
def new_wallet():
    random_gen = imports.Crypto.Random.new().read
    private_key = imports.RSA.generate(1024, random_gen)
    public_key = private_key.publickey()
    response = {
        'private_key':
        imports.binascii.hexlify(
            private_key.exportKey(format='DER')).decode('ascii'),
        'public_key':
        imports.binascii.hexlify(
            public_key.exportKey(format='DER')).decode('ascii')
    }

    return imports.jsonify(response), 200
Ejemplo n.º 5
0
def consensus():
    replaced = blockchain.resolve_conflicts()

    if replaced:
        response = {
            'message': 'Our chain was replaced',
            'new_chain': blockchain.chain
        }
    else:
        response = {
            'message': 'Our chain is authoritative',
            'chain': blockchain.chain
        }
    return imports.jsonify(response), 200
Ejemplo n.º 6
0
def register_nodes():
    values = imports.request.form
    nodes = values.get('nodes').replace(" ", "").split(',')

    if nodes is None:
        return "Error: Please supply a valid list of nodes", 400

    for node in nodes:
        blockchain.register_node(node)

    response = {
        'message': 'New nodes have been added',
        'total_nodes': [node for node in blockchain.nodes],
    }
    return imports.jsonify(response), 201
Ejemplo n.º 7
0
def mine():
    # We run the proof of work algorithm to get the next proof...
    last_block = blockchain.chain[-1]
    nonce = blockchain.proof_of_work()

    # We must receive a reward for finding the proof.
    blockchain.submit_transaction(sender_address=BlockC.MINING_SENDER, recipient_address=blockchain.node_id, value=0, signature="",MReward=1)

    # Forge the new Block by adding it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.create_block(nonce, previous_hash)

    response = {
        'message': "New Block Forged",
        'block_number': block['block_number'],
        'transactions': block['transactions'],
        'nonce': block['nonce'],
        'previous_hash': block['previous_hash'],
    }
    return imports.jsonify(response), 200
Ejemplo n.º 8
0
def doLogin(email, password):
    dbPassword, error = (None, '')

    if db.Students.find({'Email': email}).count() > 0:
        query = db.Students.find(
            {'Email': email}, {'_id': '0', 'Password': '******'})

        for i in query:
            dbPassword = i['Password']

        if str(hashpw(password.encode('utf-8'), salt)) == dbPassword:
            return jsonify(Message='Logged In', error=error)
        else:
            return jsonify(error='Invalid credentials')

    elif db.Owners.find({'Email': email}).count() > 0:
        query = db.Owners.find(
            {'Email': email}, {'_id': '0', 'Password': '******'})

        for i in query:
            dbPassword = i['Password']

        if str(hashpw(password.encode('utf-8'), salt)) == dbPassword:
            return jsonify(Message='Logged In', error=error)
        else:
            return jsonify(error='Invalid credentials')

    elif db.Authorities.find({'Email': email}).count() > 0:
        query = db.Authorities.find(
            {'Email': email}, {'_id': '0', 'Password': '******'})

        for i in query:
            dbPassword = i['Password']

        if str(hashpw(password.encode('utf-8'), salt)) == dbPassword:
            return jsonify(Message='Logged In', error=error)
        else:
            return jsonify(error='Invalid credentials')

    else:
        return jsonify(Message='Not Found')
Ejemplo n.º 9
0
def full_chain():
    response = {
        'chain': blockchain.chain,
        'length': len(blockchain.chain),
    }
    return imports.jsonify(response), 200
Ejemplo n.º 10
0
def get_transactions():
    #Get transactions from transactions pool
    transactions = blockchain.transactions

    response = {'transactions': transactions}
    return imports.jsonify(response), 200
Ejemplo n.º 11
0
def get_nodes():
    nodes = list(blockchain.nodes)
    response = {'nodes': nodes}
    return imports.jsonify(response), 200