Beispiel #1
0
 def __init__(self, session: Session, request_args):
     if 'Session-Id' in request_args:
         # TODO: Respect session's timeout
         user_session = session.query(UserSession).filter_by(
             session_id=request_args['Session-Id']).first()
         self.account = session.query(Account).filter_by(
             address=user_session.address).first()
         self.public_key = self.account.public_key
         self.address = self.account.address
         self.asset_id = user_session.currency
     else:
         self.asset_id = request_args['Asset-Id']
         self.auth_hash = b58decode(request_args['AuthHash'])
         self.auth_nonce = b58decode(request_args['AuthNonce'])
         self.public_key = bytes(request_args['Public-Key'], 'latin-1')
         self.address = request_args['Address']
         if self.address != public_key_to_account(
                 request_args['Public-Key']):
             raise PermissionError("Public key is and address differ")
         self.account = Account.get_or_create(session, self.address,
                                              self.public_key)
         if not self._is_auth_hash_valid(self.public_key, self.auth_hash,
                                         self.auth_nonce, self.asset_id):
             raise PermissionError("WAC header is invalid")
         user_session = UserSession(self.address, self.asset_id)
         session.add(user_session)
     self.session_id = user_session.session_id
Beispiel #2
0
    def _handle_deposits(self):
        session = Session()

        plugin_name = __name__.split('.')[1]
        my_currency_ids = [
            c for c, p in currencies.items() if p == plugin_name
        ]

        # TODO: List unconfirmed deposits
        for transaction in get_last_transactions():
            if transaction['category'] == 'receive' and transaction[
                    'confirmations'] >= dogecoin_confirmations:
                account = session.query(Account).filter_by(
                    dogecoin_deposit_address=transaction['address']).first()
                if account is not None:
                    deposit = session.query(Deposit).filter_by(
                        dogecoin_txid=transaction['txid']).first()
                    if deposit is None:
                        real_amount = transaction['amount']
                        if real_amount <= 0:
                            amount = 0
                        amount = int(real_amount *
                                     (10**self.DOGECOIN_DECIMAL_PLACES))
                        deposit = Deposit(account.address, my_currency_ids[0],
                                          amount)
                        deposit.dogecoin_txid = transaction['txid']
                        deposit.accepted = True
                        logger.info("New deposit: %s %s" % (account, deposit))
                        session.add(deposit)
                        session.commit()
                        session.flush()

        session.commit()
 def _scan_block(self, session: Session, block_number: int):
     transactions = get_transactions_for_block(block_number)
     for tx in transactions:
         try:
             if tx["type"] == 4:  # Asset transfer transaction
                 account = session.query(Account).filter_by(
                     deposit_address=tx["recipient"]).first()
                 if account and session.query(BlockchainTransaction).get(
                         tx["id"]) is None:
                     attachment = ''.join(
                         chr(x) for x in b58decode(tx["attachment"]))
                     logging.info(
                         "\t❤❤❤❤ A new withdrawal transaction received. - %s"
                         % tx["id"])
                     logging.info("\tFrom %s" % account.address)
                     logging.info("\tTo %s" % tx["recipient"])
                     logging.info("\tAsset %s" % tx["assetId"])
                     logging.info("\tAmount %d" % tx["amount"])
                     logging.info("\tAttachment %s" % attachment)
                     # TODO: Check if currency is defined
                     blockchain_transaction = BlockchainTransaction(
                         tx["id"], account.address, tx["type"],
                         tx["timestamp"], attachment, tx["assetId"],
                         tx["amount"])
                     session.add(blockchain_transaction)
         except KeyError:
             pass  # Unknown transaction type
 def send_money(self, session: Session, account: str, amount: int):
     acc = session.query(BankSimBalances).filter_by(
         bank_account=account).first()
     if acc is None:
         acc = BankSimBalances(account, amount)
         session.add(acc)
     else:
         acc.balance += amount
Beispiel #5
0
def fetch_airline_companies():
    session = Session()
    # crete airlines
    gol_airline = Airline('GLO', 'Gol Transportes Aéreos', 'Brazil')
    avianca_airline = Airline('ONE', 'Avianca Brazil', 'Brazil')
    tam_airline = Airline('TAM', 'TAM', 'Brazil')
    azul_airline = Airline('AZU', 'Azul Linhas Aéreas Brasileiras', 'Brazil')
    # persist data
    session.add(gol_airline)
    session.add(avianca_airline)
    session.add(tam_airline)
    session.add(azul_airline)
    # commit and close session
    session.commit()
    session.close()
def index():
    session = Session()
    if "account" in request.form and "amount" in request.form:
        deposit_iban = request.form["account"]
        amount = int(
            float(request.form["amount"]) *
            (10**list(currencies.values())[0].decimals))

        account = session.query(Account).filter_by(
            deposit_iban=deposit_iban).first()
        if account is None:
            return "There is no account with IBAN %s" % (deposit_iban)

        deposit = Deposit(account.address, list(currencies.keys())[0], amount)
        # We are accepting banking transactions immediately.
        # We are waiting for confirmations only on blockchain transactions.
        deposit.accepted = True
        session.add(deposit)
        session.commit()
        return """Your deposit to %s for %d.%.2d USD was made""" % (
            deposit_iban, amount /
            (10.**list(currencies.values())[0].decimals), amount %
            (10.**list(currencies.values())[0].decimals))
    session.commit()
    msg = """Welcome to the Bank simulator!<br/>Make a deposit:
    <form method='POST' action='/'>
    Account [IBAN]:<input name='account'/><br/>
    Amount [USD]:<input name='amount'/><br/>
    <input type='submit'/></form><br/>
    <b>Account balances:</b><br/>"""

    for balance in session.query(BankSimBalances).all():
        msg += "%s - %d.%.2d<br/>" % (balance.bank_account, balance.balance /
                                      100, balance.balance % 100)

    return msg