Пример #1
0
def rescan_all():
    logging.info("Starting a rescan of all addresses in database...")
    bitcoin_addresses = BitcoinAddresses.query.filter_by(
        user_id=current_user.username)
    logging.info("Found a total of " + str(bitcoin_addresses.count()) +
                 " addresses to rescan")
    # Get token
    at = dojo_get_settings()["token"]
    address_string = ""
    # send a pipe separated list to Dojo
    for address in bitcoin_addresses:
        address_string += address.address_hash + "|"
    address_string = address_string[:-1]
    reg = dojo_multiaddr(address_string, "active", at)
    if "error" in reg.json():
        flash(
            f"Something went wrong while rescanning addresses." +
            "Error: {reg.json()['error']}",
            "danger",
        )
    else:
        flash(
            "Address rescan finished for a total of " +
            str(bitcoin_addresses.count()) + " addresses", "success")
    return (json.dumps("OK"))
Пример #2
0
def bitcoin_address():
    form = AddressForm()
    title = form_title = "Bitcoin Address"
    # address_list = BitcoinAddresses.query.filter_by(user_id=current_user.username)
    if form.validate_on_submit():
        id = request.args.get("id")
        if id:
            bitcoin_address = BitcoinAddresses.query.filter_by(user_id=current_user.username).filter_by(address_id=id).first()
            if bitcoin_address is None:
                flash("Address id not found", "danger")
                return redirect(url_for("node.bitcoin_monitor"))
                
            bitcoin_address.user_id = current_user.username
            bitcoin_address.address_hash = form.address.data
            bitcoin_address.check_method = form.check_method.data
            bitcoin_address.account_id = form.account.data
            bitcoin_address.auto_check = form.auto_check.data
            bitcoin_address.imported_from_hdaddress = form.hd_parent.data
            bitcoin_address.notes = form.notes.data
            db.session.commit()
            # Make sure address is registered with Dojo
            at = dojo_get_settings()["token"]
            reg = dojo_multiaddr(bitcoin_address.address_hash, "active", at)
            if "error" in reg:
                flash(
                    f"Something went wrong when registering this address to your Dojo. \
                    It was added to the database but you may want to check your connection. Error: {reg}",
                    "danger",
                )
                return redirect(url_for("node.bitcoin_monitor"))

            flash("Address edited", "success")
            return redirect(url_for("node.bitcoin_monitor"))

        bitcoin_address = BitcoinAddresses(
            user_id=current_user.username,
            address_hash=form.address.data,
            check_method=form.check_method.data,
            account_id=form.account.data,
            auto_check=form.auto_check.data,
            imported_from_hdaddress=form.hd_parent.data,
            notes=form.notes.data,
        )

        try:
            db.session.add(bitcoin_address)
            db.session.commit()
        except Exception as e:
            flash(
                f"Address not included in database. Something went wrong. Try again. | Error Message: {e}",
                "danger",
            )
        try:
            # Import this address into the Dojo database
            at = dojo_get_settings()["token"]
            # Register this new address
            # dojo_multiaddr(bitcoin_address.address_hash, "new", at)
            dojo_multiaddr(bitcoin_address.address_hash, "active", at)
            flash(f"Address included.", "success")
        except Exception as e:
            flash(
                f"Address included in database but something went wrong while trying to register this address at the Dojo. Check if your Dojo is connected.",
                "warning",
            )

        return redirect(url_for("node.bitcoin_monitor"))

    elif request.method == "GET":
        title = form_title = "Register New Bitcoin Address"
        form.auto_check.data = True
        id = request.args.get("id")
        if id:
            title = form_title = "Edit Bitcoin Address"
            bitcoinaddress = (
                BitcoinAddresses.query.filter_by(user_id=current_user.username)
                .filter_by(address_id=id)
                .first()
            )
            if bitcoinaddress is None:
                flash("Address id not found", "danger")
                return redirect(url_for("node.bitcoin_monitor"))
            form.address.data = bitcoinaddress.address_hash
            form.check_method.data = bitcoinaddress.check_method
            form.account.data = bitcoinaddress.account_id
            form.hd_parent.data = bitcoinaddress.imported_from_hdaddress
            form.notes.data = bitcoinaddress.notes

    return render_template(
        "new_address.html", form=form, form_title=form_title, title=title
    )
Пример #3
0
def bitcoin_transactions(address):
    logging.info(f"Started Bitcoin Transaction method for {address}")
    meta = {}
    transactions = {}
    # Check if HD Address
    hd_address_list = ("xpub", "ypub", "zpub")
    if address.lower().startswith(hd_address_list):
        hd_address = True
        # Get address data from DB
        bitcoin_address = (
            AccountInfo.query.filter_by(user_id=current_user.username)
            .filter_by(account_blockchain_id=address)
            .first()
        )
    else:
        hd_address = False
        # Get address data from DB
        # Check first if this address is in database
        bitcoin_address = (
            BitcoinAddresses.query.filter_by(user_id=current_user.username)
            .filter_by(address_hash=address)
            .first()
        )
    transactions["error"] = ""
    meta["hd"] = hd_address
    meta["success"] = False
    meta["n_txs"] = 0
    if bitcoin_address:
        logging.info("Address Found in Database")
        meta["found"] = True  # Found in database
        meta["account"] = bitcoin_address.account_id
        # Let's check if there's a balance in this address
        at = dojo_get_settings()["token"]  # Get Dojo Authent Token
        try:
            derivation = "pubkey"
            if hd_address:
                derivation = bitcoin_address.xpub_derivation
                if not derivation:
                    derivation = "pubkey"
            balance = dojo_multiaddr(address, derivation, at).json()
        except AttributeError:
            logging.warn("Did not receive a json back from multi_add")

            # balance = dojo_multiaddr(address, derivation, at)
        # Check if there's a balance in this address
        # {'wallet': {'final_balance': 0}, 'info':
        # {'latest_block': {'height': 586366, 'hash': '00000000000000000015ea0990b12ea4c04161203a305a0ceb5c67a678468f20',
        # 'time': 1563702071}}, 'addresses': [], 'txs': []}
        try:
            if balance["wallet"]["final_balance"] >= 0:
                meta["balance"] = balance["wallet"]["final_balance"]
                meta["success"] = True
        except (KeyError, UnboundLocalError):
            transactions[
                "error"
            ] += "Could not retrieve a balance for this address. Check the address."
            logging.warn("No balance found on this address")
        except TypeError:
            try:
                balance = balance.json()
                transactions["error"] += balance["error"]
            except TypeError:
                transactions[
                    "error"
                ] += "An unknown error occured. Check connection settings and address info."
        # Check if there are transactions in this address
        try:
            # the [0] here is needed since we're using multiaddr but only returning the 1st (and only) address
            if balance["addresses"][0]["n_tx"] > 0:
                meta["n_txs"] = balance["addresses"][0]["n_tx"]
        except (KeyError, IndexError):
            logging.info("No txs found for this address")
            transactions["error"] += "Could not retrieve any transactions."
            meta["n_txs"] = 0

        # Transactions, at this stage, can only be imported using Dojo
        if "n_txs" in meta:
            meta["check_method"] = "Dojo"
            transactions = dojo_get_txs(address, at)
            if ("balance" in meta) and (meta["balance"] >= 0):
                meta["success"] = True
                logging.info("Success: Address data gathered")

    # OK, this address is not in Database, so do nothing
    else:
        logging.warn("Address not found in database - returning an error message")
        meta["found"] = False
        transactions[
            "error"
        ] = "This address was not found in your list of addresses. Please include."

    return render_template(
        "bitcoin_transactions.html",
        title="Address Transactions",
        meta=meta,
        address=address,
        transactions=transactions,
    )