Exemple #1
0
def create_new_user(
    username: str,
    password: str,
    is_admin: bool = False,
    first_name: str = "",
    last_name: str = "",
) -> int:
    """
    Attempts to add a new user by username, password hash, and salt, throwing
    types.UsernameExistsError if the username exists already
    """

    if get_user(username=username):
        raise UsernameExistsError(f"username {username} already exists!")

    registration_status = RegistrationStatus.Registered

    if is_admin:
        registration_status = RegistrationStatus.Approved

    password_hash, salt = _generate_password_hash_and_salt(password)

    return add_user(
        username=username,
        password_hash=bytes.hex(password_hash),
        salt=bytes.hex(salt),
        is_admin=is_admin,
        registration_status=registration_status,
        first_name=first_name,
        last_name=last_name,
    )
Exemple #2
0
def verify_kyc(user_id: int) -> None:
    user = get_user(user_id)
    if _is_verified(user):
        user_service.update_user(
            user_id=user_id, registration_status=RegistrationStatus.Approved)

    if user.first_name == "doctor" and user.last_name == "evil":
        user_service.update_user(
            user_id=user_id, registration_status=RegistrationStatus.Rejected)
    else:
        user_service.update_user(
            user_id=user_id, registration_status=RegistrationStatus.Approved)
        account_service.create_account(account_name=f"{user.username}-account",
                                       user_id=user.id)
def test_create_user() -> None:
    username = "******"
    password_hash = "fake_hash"
    salt = "fake_salt"
    user_id = add_user(
        username=username,
        password_hash=password_hash,
        salt=salt,
        registration_status=RegistrationStatus.Pending,
    )

    storage_user = get_user(username=username)
    assert storage_user
    assert storage_user.id == user_id
Exemple #4
0
def get_user_kyc_info(user_id):
    user = get_user(user_id)
    return {
        "payload_version": 1,
        "type": "individual",
        "given_name": xstr(user.first_name),
        "surname": xstr(user.last_name),
        "dob": xstr(user.dob),
        "address": {
            "city": xstr(user.city),
            "country": xstr(user.country),
            "line1": xstr(user.address_1),
            "line2": xstr(user.address_2),
            "postal_code": xstr(user.zip),
            "state": xstr(user.state),
        },
    }
Exemple #5
0
def execute_convert(order: Order) -> ConvertResult:
    inventory_account = get_account(account_name=INVENTORY_ACCOUNT_NAME).id
    user_account = get_user(order.user_id).account.id
    order_id = typing.cast(OrderId, order.id)

    from_amount = order.amount
    from_diem_currency = DiemCurrency[order.base_currency]
    to_amount = order.exchange_amount
    to_diem_currency = DiemCurrency[order.quote_currency]

    if not validate_balance(sender_id=user_account,
                            amount=from_amount,
                            currency=from_diem_currency):
        return ConvertResult.InsufficientBalance

    if not validate_balance(sender_id=inventory_account,
                            amount=to_amount,
                            currency=to_diem_currency):
        return ConvertResult.InsufficientInventoryBalance

    try:
        to_inventory_tx = internal_transaction(
            sender_id=user_account,
            receiver_id=inventory_account,
            amount=from_amount,
            currency=from_diem_currency,
            payment_type=TransactionType.INTERNAL,
        )
        from_inventory_tx = internal_transaction(
            sender_id=inventory_account,
            receiver_id=user_account,
            amount=to_amount,
            currency=to_diem_currency,
            payment_type=TransactionType.INTERNAL,
        )
        update_order(
            order_id=order_id,
            internal_ledger_tx=to_inventory_tx.id,
            correlated_tx=from_inventory_tx.id,
            order_status=OrderStatus.Executed,
        )
        return ConvertResult.Success
    except Exception:
        logging.exception("execute convert")
        update_order(order_id=order_id, order_status=OrderStatus.FailedExecute)
        return ConvertResult.TransferFailure
Exemple #6
0
def authorize(password: str,
              user: Optional[User] = None,
              username: Optional[str] = None) -> LoginError:
    if not user:
        user = get_user(username=username)

    if not user:
        return LoginError.USER_NOT_FOUND

    if user.is_blocked:
        return LoginError.UNAUTHORIZED

    if user.is_admin and not ADMIN_LOGIN_ENABLED:
        return LoginError.ADMIN_DISABLED

    if is_correct_password(user, password):
        return LoginError.SUCCESS
    else:
        return LoginError.WRONG_PASSWORD
Exemple #7
0
def execute_trade(order: Order):
    inventory_account_id = get_account(account_name=INVENTORY_ACCOUNT_NAME).id
    user_account_id = get_user(order.user_id).account.id
    order_id = typing.cast(OrderId, order.id)

    base_diem_currency = DiemCurrency[order.base_currency]

    if Direction[order.direction] == Direction.Buy:
        sender_id = inventory_account_id
        receiver_id = user_account_id

        if not validate_balance(sender_id, order.amount, base_diem_currency):
            buy_funds(CurrencyPairs[
                f"{base_diem_currency}_{INVENTORY_COVER_CURRENCY}"])
    else:
        sender_id = user_account_id
        receiver_id = inventory_account_id

    try:
        transaction = internal_transaction(
            sender_id=sender_id,
            receiver_id=receiver_id,
            amount=order.amount,
            currency=base_diem_currency,
            payment_type=TransactionType.INTERNAL,
        )
        update_order(
            order_id=order_id,
            internal_ledger_tx=transaction.id,
            order_status=OrderStatus.Executed,
        )
        return True
    except BalanceError:
        logging.exception("execute trade")
        update_order(order_id=order_id, order_status=OrderStatus.FailedExecute)
        return False
Exemple #8
0
def get_user_by_token(token_id: str) -> User:
    token = get_token(token_id)
    return get_user(token.user_id)
Exemple #9
0
def is_verified(user_id: int) -> bool:
    user = get_user(user_id)
    return _is_verified(user)