Exemplo n.º 1
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))
Exemplo n.º 2
0
async def api_amilkit(amilk_id):
    milk = await 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.")

    try:
        payment_hash, payment_request = await create_invoice(
            wallet_id=milk.wallet,
            amount=withdraw_res.max_sats,
            memo=memo,
            extra={"tag": "amilk"},
        )
    except Exception as e:
        return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR

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

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

    for i in range(10):
        sleep(i)
        invoice_status = await check_invoice_status(milk.wallet, payment_hash)
        if invoice_status.paid:
            return jsonify({"paid": True}), HTTPStatus.OK
        else:
            continue

    return jsonify({"paid": False}), HTTPStatus.OK
Exemplo n.º 3
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