Esempio n. 1
0
def lndhub_getuserinvoices():
    delete_expired_invoices()
    for invoice in g.wallet.get_payments(complete=False,
                                         pending=True,
                                         outgoing=False,
                                         incoming=True,
                                         exclude_uncheckable=True):
        invoice.set_pending(
            WALLET.get_invoice_status(invoice.checking_id).pending)

    limit = int(request.args.get("limit", 200))
    return jsonify([{
        "r_hash": to_buffer(invoice.payment_hash),
        "payment_request": invoice.bolt11,
        "add_index": "500",
        "description": invoice.memo,
        "payment_hash": invoice.payment_hash,
        "ispaid": not invoice.pending,
        "amt": int(invoice.amount / 1000),
        "expire_time": int(time.time() + 1800),
        "timestamp": invoice.time,
        "type": "user_invoice",
    } for invoice in reversed(
        g.wallet.get_payments(
            pending=True, complete=True, incoming=True, outgoing=False)
        [:limit])])
Esempio n. 2
0
async def api_payment(payment_hash):
    payment = g.wallet.get_payment(payment_hash)

    if not payment:
        return jsonify({"message":
                        "Payment does not exist."}), HTTPStatus.NOT_FOUND
    elif not payment.pending:
        return jsonify({"paid": True}), HTTPStatus.OK

    try:
        if payment.is_uncheckable:
            pass
        elif payment.is_out:
            is_paid = not WALLET.get_payment_status(
                payment.checking_id).pending
        elif payment.is_in:
            is_paid = not WALLET.get_invoice_status(
                payment.checking_id).pending
    except Exception:
        return jsonify({"paid": False}), HTTPStatus.OK

    if is_paid:
        payment.set_pending(False)
        return jsonify({"paid": True}), HTTPStatus.OK

    return jsonify({"paid": False}), HTTPStatus.OK
Esempio n. 3
0
async def check_invoice_status(wallet_id: str,
                               payment_hash: str) -> PaymentStatus:
    payment = await get_wallet_payment(wallet_id, payment_hash)
    if not payment:
        return PaymentStatus(None)

    return WALLET.get_invoice_status(payment.checking_id)
Esempio n. 4
0
def lnurlwallet():
    memo = "LNbits LNURL funding"

    try:
        withdraw_res = handle_lnurl(request.args.get("lightning"))
        if not withdraw_res.ok:
            abort(
                HTTPStatus.BAD_REQUEST,
                f"Could not process LNURL-withdraw: {withdraw_res.error_msg}")
        if not isinstance(withdraw_res, LnurlWithdrawResponse):
            abort(HTTPStatus.BAD_REQUEST, "Not a valid LNURL-withdraw.")
    except LnurlException:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR,
              "Could not process LNURL-withdraw.")

    try:
        ok, checking_id, payment_request, error_message = WALLET.create_invoice(
            withdraw_res.max_sats, memo)
    except Exception as e:
        ok, error_message = False, str(e)

    if not ok:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR, error_message)

    r = requests.get(
        withdraw_res.callback.base,
        params={
            **withdraw_res.callback.query_params,
            **{
                "k1": withdraw_res.k1,
                "pr": payment_request
            }
        },
    )

    if not r.ok:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR,
              "Could not process LNURL-withdraw.")

    for i in range(10):
        invoice_status = WALLET.get_invoice_status(checking_id)
        sleep(i)
        if not invoice_status.paid:
            continue
        break

    user = get_user(create_account().id)
    wallet = create_wallet(user_id=user.id)
    create_payment(
        wallet_id=wallet.id,
        checking_id=checking_id,
        amount=withdraw_res.max_sats * 1000,
        memo=memo,
        pending=invoice_status.pending,
    )

    return redirect(url_for("core.wallet", usr=user.id, wal=wallet.id))
Esempio n. 5
0
    def check_pending(self) -> None:
        if self.is_uncheckable:
            return

        if self.is_out:
            pending = WALLET.get_payment_status(self.checking_id)
        else:
            pending = WALLET.get_invoice_status(self.checking_id)

        self.set_pending(pending.pending)
Esempio n. 6
0
def api_payments():
    if "check_pending" in request.args:
        g.wallet.delete_expired_payments()

        for payment in g.wallet.get_payments(include_all_pending=True):
            if payment.is_out:
                payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
            else:
                payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending)

    return jsonify(g.wallet.get_payments()), Status.OK
Esempio n. 7
0
async def api_payments():
    if "check_pending" in request.args:
        delete_expired_invoices()

        for payment in g.wallet.get_payments(complete=False,
                                             pending=True,
                                             exclude_uncheckable=True):
            if payment.is_out:
                payment.set_pending(
                    WALLET.get_payment_status(payment.checking_id).pending)
            else:
                payment.set_pending(
                    WALLET.get_invoice_status(payment.checking_id).pending)

    return jsonify(g.wallet.get_payments(pending=True)), HTTPStatus.OK
Esempio n. 8
0
def api_ticket_send_ticket(checking_id):
    theticket = get_ticket(checking_id)
    try:
        is_paid = not WALLET.get_invoice_status(checking_id).pending
    except Exception:
        return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND

    if is_paid:
        wallet = get_wallet(theticket.wallet)
        payment = wallet.get_payment(checking_id)
        payment.set_pending(False)
        ticket = update_ticket(paid=True, checking_id=checking_id)

        return jsonify({"paid": True, "ticket_id": ticket.id}), HTTPStatus.OK

    return jsonify({"paid": False}), HTTPStatus.OK
Esempio n. 9
0
def api_amilkit(amilk_id):
    milk = get_amilk(amilk_id)
    memo = milk.id

    try:
        withdraw_res = handle_lnurl(milk.lnurl,
                                    response_class=LnurlWithdrawResponse)
    except LnurlException:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR,
              "Could not process withdraw LNURL.")
    print(withdraw_res.max_sats)

    try:
        checking_id, payment_request = create_invoice(
            wallet_id=milk.wallet, amount=withdraw_res.max_sats, memo=memo)
        #print(payment_request)
    except Exception as e:
        error_message = False, str(e)

    r = requests.get(
        withdraw_res.callback.base,
        params={
            **withdraw_res.callback.query_params,
            **{
                "k1": withdraw_res.k1,
                "pr": payment_request
            }
        },
    )

    if not r.ok:

        abort(HTTPStatus.INTERNAL_SERVER_ERROR,
              "Could not process withdraw LNURL.")

    for i in range(10):
        invoice_status = WALLET.get_invoice_status(checking_id)
        sleep(i)
        if not invoice_status.paid:
            continue
        else:
            return jsonify({"paid": False}), HTTPStatus.OK
        break

    return jsonify({"paid": True}), HTTPStatus.OK
Esempio n. 10
0
def api_paywal_check_invoice(paywall_id):
    paywall = get_paywall(paywall_id)

    if not paywall:
        return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND

    try:
        is_paid = not WALLET.get_invoice_status(g.data["checking_id"]).pending
    except Exception:
        return jsonify({"paid": False}), HTTPStatus.OK

    if is_paid:
        wallet = get_wallet(paywall.wallet)
        payment = wallet.get_payment(g.data["checking_id"])
        payment.set_pending(False)

        return jsonify({"paid": True, "url": paywall.url, "remembers": paywall.remembers}), HTTPStatus.OK

    return jsonify({"paid": False}), HTTPStatus.OK
Esempio n. 11
0
def api_tpos_check_invoice(tpos_id, checking_id):
    tpos = get_tpos(tpos_id)

    if not tpos:
        return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND

    try:
        is_paid = not WALLET.get_invoice_status(checking_id).pending
    except Exception:
        return jsonify({"paid": False}), Status.OK

    if is_paid:
        wallet = get_wallet(tpos.wallet)
        payment = wallet.get_payment(checking_id)
        payment.set_pending(False)

        return jsonify({"paid": True}), Status.OK

    return jsonify({"paid": False}), Status.OK
Esempio n. 12
0
def api_ticket_send_ticket(checking_id):

    form = get_form(g.data['form'])
    if not form:
        return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND
    try:
        is_paid = not WALLET.get_invoice_status(checking_id).pending
    except Exception:
        return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND

    if is_paid:
        wallet = get_wallet(form.wallet)
        payment = wallet.get_payment(checking_id)
        payment.set_pending(False)
        create_ticket(wallet=form.wallet, **g.data)

        return jsonify({"paid": True}), HTTPStatus.OK

    return jsonify({"paid": False}), HTTPStatus.OK
Esempio n. 13
0
def get_diagonalleys_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
    if isinstance(wallet_ids, str):
        wallet_ids = [wallet_ids]

    with open_ext_db("diagonalley") as db:
        q = ",".join(["?"] * len(wallet_ids))
        rows = db.fetchall(f"SELECT * FROM orders WHERE wallet IN ({q})",
                           (*wallet_ids, ))
    for r in rows:
        PAID = WALLET.get_invoice_status(r["invoiceid"]).paid
        if PAID:
            with open_ext_db("diagonalley") as db:
                db.execute("UPDATE orders SET paid = ? WHERE id = ?", (
                    True,
                    r["id"],
                ))
                rows = db.fetchall(
                    f"SELECT * FROM orders WHERE wallet IN ({q})",
                    (*wallet_ids, ))
    return [Orders(**row) for row in rows]