def new_message_request(data):
    """Verifies that the given dict represents a valid message."""
    success = True
    errors = []
    dkeys = data.keys()
    req_keys = ['receiver_id', 'sender_id', 'receiver_key_id', 'sender_key_id',
            'message', 'signature']

    # First thing is to ensure that the keys in the given data dict match the
    # expected keys exactly.
    if set(dkeys) != set(req_keys):
        success = False
        errors.append({
            'type': 'FATAL',
            'cause':
            "Keys in request (%s) do not match required keys (%s)." % \
                    (str(dkeys), str(req_keys))})
    else:
        # Helper function to verify subset of keys are uuids.
        success = merge_errors(success, errors, keys_are_uuids(data,
            req_keys[:4]))
        if success:
            # All uuid fields are valid.
            for k in req_keys[:2]:
                if not model.user_exists(data[k]):
                    success = False
                    errors.append({
                        'type': 'FATAL',
                        'cause': "The user (%s) does not exist." % data[k]})
            if success:
                # All users are valid
                for k in req_keys[2:4]:
                    # Check that given keys exist
                    user_key_id = data[k]
                    key = model.get_key(user_key_id)
                    if not key:
                        success = False
                        errors.append({
                            'type': 'FATAL',
                            'cause': "Invalid key_id (%s)." % (user_key_id)})
                    elif key['user_id'] != data[k[:-6] + 'id']:
                        success = False
                        errors.append({
                            'type': 'FATAL',
                            'cause': ("Key's user_id ({0}) does not match " \
                                "given user_id ({1}).").format(key['user_id'],
                                    data[k[:-6] + 'id'])})
                if success:
                    if not science.verify_message(data['message'],
                            data['signature'],
                            model.get_key(
                                data['sender_key_id'])['key']):
                        success = False
                        errors.append({
                            'type': 'FATAL',
                            'cause': ("Given key ({0}) could not verify " \
                                "signature.").format(key['key'])})

    return (success, errors)
def get_key(key_id):
    """Get's the key with the given key_id."""
    try:
        key = model.get_key(UUID(key_id))
    except ValueError:
        key = model.get_key(key_id)
    if key != None:
        del key['_id']
    if key:
        result = {
                'success': True,
                'key': key}
    else:
        result = {
                'success': False,
                'errors': [{
                    'type': 'FATAL',
                    'cause': 'Could not find a key with the given id (%s).' %
                    (key_id)}],
                'key': None}

    return result