Example #1
0
def admin_view_all_tokens(token_id, session_token):
    db = database.Database(logger=current_app.logger)
    user_id = db.validate_session(session_token)
    sc_info = db.get_smart_contract_info(token_id)
    if sc_info["owner_id"] == user_id:
        sc = SmartContract(smart_token_id=token_id)
        all_tokens = sc.list_all_issued_tokens(token_id)
        return render_template("admin/view_all_tokens.jinja2",{"all_tokens":all_tokens})
    abort(403)
Example #2
0
def admin_view_source(token_id, session_token):
    token_id = int(token_id)
    db = database.Database(logger=current_app.logger)
    user_id = db.validate_session(session_token)
    if user_id and token_id > 0:
        token_info = db.get_smart_contract_info(token_id)
        if token_info["owner_id"] == user_id:
            sc = SmartContract(smart_token_id=token_id)
            return render_template("view_smart_contract.html",
                                   new_solidity_contract=sc.solidity_code)
    abort(403)
Example #3
0
def get_owned_tokens(user_id, db, logger=None):
    smart_contracts = db.get_smart_contracts(user_id)
    owned_tokens = []
    for each in smart_contracts:
        pending = False
        if each["published"] is not None and each["eth_address"] is None:
            pending = True
        token_data = {"token_id": each["token_id"],
                      "token_name": each["token_name"],
                      "ico_tokens": each["tokens"],
                      "token_symbol": each["token_symbol"],
                      "eth_address": each["eth_address"],
                      "created": each["created"],
                      "published": each["published"],
                      "pending": pending,
                      "max_priority": each["max_priority"]}
        if not pending:
            sc = SmartContract(each["token_id"], logger=logger)
            token_data["issued_tokens"] = sc.get_issued_token_count()
            token_data["issued_not_confirmed"] = 0
            token_data["confirmed_not_assigned"] = 0
        owned_tokens.append(token_data)
    owned_tokens = sorted(owned_tokens, key=lambda token: token['created'], reverse=True)
    return owned_tokens
Example #4
0
def erc20_publish():
    session_token = request.form["session_token"]
    token_id_form_field = request.form["token_id"]
    confirmation = request.form["confirmation"]
    db = database.Database()
    user_id = db.validate_session(session_token)
    if user_id:
        token_id = int(token_id_form_field)
        if token_id < 1:
            raise ValueError
        sc = SmartContract(smart_token_id=token_id)
        if sc.smart_contract_id < 1:
            abort(404)
        if confirmation == "true":
            credits = Credits(user_id, db, current_app.logger)
            current_balance = credits.get_credit_balance()
            if current_balance < credits.erc20_publish_price:
                message = "Your credit balance of <span class=\"credit_balance\">"
                message += str(current_balance) + "</span> is less than the <span class=\"credit_price\">"
                message += str(credits.erc20_publish_price) + "</span> required to publish an ERC20 token."
                message += "<p>[ <a class=\"login_anchor\" href=\"/admin/credits/purchase/"
                message += session_token + "\">purchase credits</a> ]</p>"
                return render_template("admin/admin_confirmation.jinja2",
                                       title="Unable to publice ERC20 contract",
                                       session_token=session_token,
                                       confirmation_value=token_id,
                                       confirmation_title="Insufficient Credits",
                                       confirmation_type="insufficient_credits",
                                       confirmation_message=message,
                                       default_choice="Cancel")
            message = "Are you sure you want to publish <em>" + sc.token_name + "</em> permanently to the Ethereum "
            message += "blockchain, costing <span class=\"credit_price\">"
            message += str(credits.erc20_publish_price) + "</span> credits?"
            return render_template("admin/admin_confirmation.jinja2",
                                   title="Confirm",
                                   session_token=session_token,
                                   confirmation_value=token_id,
                                   confirmation_title="Publish ERC20 contract?",
                                   confirmation_message=message,
                                   confirmation_type="erc20_publish",
                                   choices=["Cancel"],
                                   default_choice="Publish")
Example #5
0
def admin_tokens_issue():
    db = database.Database(logger=current_app.logger)
    session_token = request.form['session_token']
    token_id = int(request.form['token_id'])
    user_id = db.validate_session(session_token)
    if user_id:
        sc_info = db.get_smart_contract_info(token_id)

        if sc_info['owner_id'] == user_id:
            sc = SmartContract(smart_token_id=token_id)
            if request.form['generate'] == "random":
                tokens = int(request.form['random_token_count'])
                sc.issue_tokens(tokens)
            elif request.form['generate'] == "hash":
                hash_string = request.form['hash']
                if ETH_ADDRESS_REGEX.match(hash_string):
                    sc.issue_token(hash_string)
            return redirect(url_for("admin.admin_tokens", session_token=session_token))
    abort(403)
Example #6
0
def command_output(api_key):
    db = database.Database(current_app.logger)
    ip_addr = request.access_route[-1]
    node_id = db.validate_api_key(api_key)
    if node_id:
        json_data = request.get_json(force=True)
        if json_data:
            event_data = {}
            if json_data["success"] is True:
                event_data["error"] = False
                event_data["command_id"] = json_data["command_id"]
                event_data["input"] = json_data["input"]
                event_data["ip_address"] = ip_addr
                command_output = json.loads(event_data["input"])
                if "block_number" in command_output:
                    block_data = BlockData(json_data=event_data["input"])
                    manager = BlockDataManager(db, current_app.logger)
                    manager.put_block(block_data)
                elif "erc20_function" in command_output:
                    erc20_function = command_output["erc20_function"]
                    if erc20_function == "publish":
                        event_data["contract_address"] = command_output[
                            "new_contract_address"]
                        event_data["token_id"] = command_output["token_id"]
                        sc = SmartContract(
                            smart_token_id=event_data["token_id"],
                            logger=current_app.logger)
                        if sc.smart_contract_id > -1 and sc.set_contract_address(
                                event_data["contract_address"]):
                            publish_event = events.Event(
                                "ERC20 Token Published", db,
                                current_app.logger)
                            publish_event.log_event(node_id, event_data)
                    elif erc20_function == "burn":
                        event_data["contract_address"] = command_output[
                            "contract_address"]
                        event_data["token_id"] = command_output["token_id"]
                        event_data["tokens"] = command_output["tokens"]
                        event_data["gas_price"] = command_output["gas_price"]
                        burn_function = events.Event("ERC20 Token Burned", db,
                                                     current_app.logger)
                        burn_function.log_event(node_id, event_data)
                    elif erc20_function == "transfer":
                        event_data["contract_address"] = command_output[
                            "contract_address"]
                        event_data["token_id"] = command_output["token_id"]
                        event_data["tokens"] = command_output["tokens"]
                        event_data["gas_price"] = command_output["gas_price"]
                        event_data["address"] = command_output["address"]
                        transfer_function = events.Event(
                            "ERC20 Token External Transfer", db,
                            current_app.logger)
                        transfer_function.log_event(node_id, event_data)
                    elif erc20_function == "total_supply":
                        event_data["total_supply"] = json_data["total_supply"]
                new_event = events.Event("Ethereum Node Command Output", db,
                                         current_app.logger)
                new_event.log_event(node_id, event_data)
            else:
                event_data["error"] = True
                event_data["command_id"] = json_data["command_id"]
                event_data["error_message"] = json_data["error_message"]
                event_data["ip_address"] = ip_addr
                # mark command undispatched
                # db.clear_dispatch_event_id(event_data["command_id"])
                # log error event
                new_event = events.Event("Ethereum Node Command Failed", db,
                                         current_app.logger)
                new_event.log_event(node_id, event_data)
            return Response(json.dumps({"success": event_data["error"]}))
        abort(500)
    abort(403)
Example #7
0
def create_tokens_form():
    session_token = request.form["session_token"]
    if session_token:
        db = database.Database(logger=current_app.logger)
        logger = current_app.logger
        user_id = db.validate_session(session_token)
        ctx = UserContext(user_id, db, logger)
        auth = ctx.check_acl("launch-ico")
        token_name = request.form['token_name']
        if not TOKEN_NAME_REGEX.match(token_name):
            create_token_error = "Invalid token name, must consist of 4-36 alphanumeric characters only."
            return render_template("admin/admin_confirmation.jinja2",
                                   session_token=session_token,
                                   confirmation_title="Invalid ERC20 parameter(s)",
                                   confirmation_message=create_token_error,
                                   confirmation_type="create_erc20_failed",
                                   default_choice="OK")
        token_symbol = request.form['token_symbol']
        if len(token_symbol) > 0 and not TOKEN_SYMBOL_REGEX.match(token_symbol):
            create_token_error = "Invalid token symbol, must consist of between 1-5 uppercase letters or numbers. This field is optional."
            return render_template("admin/admin_confirmation.jinja2",
                                   session_token=session_token,
                                   confirmation_title="Invalid ERC20 parameter(s)",
                                   confirmation_message=create_token_error,
                                   confirmation_type="create_erc20_failed",
                                   default_choice="OK")
        elif len(token_symbol) == 0:
            token_symbol = None
        token_count = request.form['token_count']
        if not TOKEN_COUNT_REGEX.match(token_count):
            return render_template("admin/admin_confirmation.jinja2",
                                   session_token=session_token,
                                   confirmation_title="Invalid ERC20 parameter(s)",
                                   confirmation_message="Invalid initial token count value, must be a positive integer.",
                                   confirmation_type="create_erc20_failed",
                                   default_choice="OK")
        token_count = int(token_count)
        if auth:
            sc = SmartContract(token_name=token_name,
                               token_symbol=token_symbol,
                               token_count=token_count,
                               logger=current_app.logger,
                               owner_id=user_id)
            if sc.smart_contract_id > 0:
                new_event = Event("ERC20 Token Created", db, current_app.logger)
                new_event.log_event(user_id, {"ip_address": request.access_route[-1],
                                              "token_name": token_name,
                                              "token_symbol": token_symbol,
                                              "token_count": token_count,
                                              "token_id": sc.smart_contract_id,
                                              })
                return redirect(url_for("admin.admin_tokens", session_token=session_token))
            else:
                create_token_error = "Token with this name already managed by ERC20Master, to make things less "
                create_token_error += "confusing, please use a unique token name."
                return render_template("admin/admin_confirmation.jinja2",
                                       session_token=session_token,
                                       title="Error",
                                       confirmation_title="Token Name Exists",
                                       confirmation_message=create_token_error,
                                       confirmation_type="create_erc20_failed",
                                       default_choice="OK")
    abort(403)
Example #8
0
def admin_confirm():
    session_token = request.form["session_token"]
    confirmation_type = request.form["confirmation_type"]
    confirmation_val = request.form["confirmation_value"]
    choice = request.form["choice"]
    if confirmation_type == "recover_email":
        if choice == "Send E-mail":
            email_address = request.form['email_address']
            mailer = Mailer(email_address, request.access_route[-1], current_app.logger)
            mailer.recover_password()
            return render_template("admin/admin_login.jinja2", error="""
            If the e-mail address is in the database, instructions have been sent on how to recover 
            your password. Please check your spam/junk mail folder.
            """)
        return redirect(url_for('homepage'))
    elif confirmation_type == "no_erc20_tokens":
        return redirect(url_for('admin.admin_tokens', session_token=session_token))
    elif confirmation_type == "erc20_publish" and choice == "Cancel":
        return redirect(url_for('admin.admin_tokens', session_token=session_token))
    elif confirmation_type == "create_erc20_failed" and choice == "OK":
        return redirect(url_for('admin.admin_tokens', session_token=session_token))
    elif confirmation_type == "onboarded_new_user":
        if choice == "Administration":
            return redirect(url_for('admin.admin_main', session_token=session_token))
        else:
            return redirect(url_for('admin.create_user', session_token=session_token))
    elif confirmation_type == "reset-password":
        if choice == "Cancel":
            return redirect(url_for("admin.view_users", session_token=session_token, limit=PAGE_LIMIT, offset=0))
    elif confirmation_type == "acl_updated":
        if choice == "OK":
            return redirect(url_for("admin.view_users", session_token=session_token, limit=PAGE_LIMIT, offset=0))
    db = database.Database(logger=current_app.logger)
    user_id = db.validate_session(session_token)
    if user_id:
        user_ctx = UserContext(user_id, db, current_app.logger)
        if confirmation_type == "erc20_publish":
            token_id = int(confirmation_val)
            sc = SmartContract(smart_token_id=token_id)
            credits = Credits(user_id, db, logger=current_app.logger)
            if sc.smart_contract_id > 0:
                event_data = {"token_name": sc.token_name,
                              "token_symbol": sc.token_symbol,
                              "token_count": sc.tokens,
                              "token_id": sc.smart_contract_id,
                              "ip_address": request.access_route[-1]}
                if user_ctx.check_acl("launch-ico"):
                    credits_balance = credits.get_credit_balance()
                    if credits_balance >= credits.erc20_publish_price:
                        new_event = Event("ERC20 Token Mined", db, logger=current_app.logger)
                        event_id = new_event.log_event(user_id, event_data)
                        event_data["event_id"] = event_id
                        credits.debit(credits.erc20_publish_price, event_data)
                        command_id = db.post_command(json.dumps({"erc20_function":"publish",
                                                                 "token_name":sc.token_name,
                                                                 "token_symbol":sc.token_symbol,
                                                                 "token_count":sc.tokens,
                                                                 "token_id":sc.smart_contract_id}), 100)
                        if command_id:
                            return redirect(url_for("admin.admin_tokens", session_token=session_token))
                        else:
                            abort(500)
                    else:
                        credits.logger.error("Insufficient credits for ERC20 Publish: "
                                             + user_ctx.user_info["email_address"])
                abort(403)
        elif confirmation_type == "reset-password":
            user_id = int(confirmation_val)
            if request.form["password"] != request.form["repeat_password"]:
                return render_template("admin/admin_confirmation.jinja2",
                                       confirmation_type="reset-password",
                                       confirmation_value=user_id,
                                       title="Reset Password",
                                       confirmation_title="Reset Password",
                                       confirmation_message="Passwords must match both times.",
                                       new_password=True,
                                       choices=["Cancel"],
                                       default_choice="Reset Password",
                                       session_token=session_token)
            if db.reset_password(int(confirmation_val), request.form["password"]):
                return redirect(url_for("admin.view_users", session_token=session_token, limit=PAGE_LIMIT, offset=0))
        elif confirmation_type == "issue-credits":
            if choice == "Issue Credits" and user_ctx.check_acl("issue-credits"):
                user_credits = Credits(confirmation_val, db, current_app.logger)
                amount = int(request.form["credits"])
                # max issued credits 10,000
                if 0 < amount < 100000:
                    user_credits.issue_credits(amount, {"ip_addr": request.access_route[-1], "admin": user_id})
                    return redirect(
                        url_for("admin.view_users", session_token=session_token, limit=PAGE_LIMIT, offset=0))
                else:
                    raise ValueError

    abort(403)