ext: withdraw ported + lnurlp fixes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import hashlib
|
||||
import math
|
||||
from http import HTTPStatus
|
||||
from quart import jsonify, url_for, request
|
||||
from fastapi import FastAPI, Request
|
||||
from lnurl import LnurlPayResponse, LnurlPayActionResponse, LnurlErrorResponse # type: ignore
|
||||
|
||||
from lnbits.core.services import create_invoice
|
||||
@@ -11,18 +11,18 @@ from . import lnurlp_ext
|
||||
from .crud import increment_pay_link
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/lnurl/<link_id>", methods=["GET"])
|
||||
async def api_lnurl_response(link_id):
|
||||
@lnurlp_ext.get("/api/v1/lnurl/{link_id}", status_code=HTTPStatus.OK)
|
||||
async def api_lnurl_response(request: Request, link_id):
|
||||
link = await increment_pay_link(link_id, served_meta=1)
|
||||
if not link:
|
||||
return (
|
||||
jsonify({"status": "ERROR", "reason": "LNURL-pay not found."}),
|
||||
HTTPStatus.OK,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Pay link does not exist."
|
||||
)
|
||||
|
||||
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
|
||||
resp = LnurlPayResponse(
|
||||
callback=url_for("lnurlp.api_lnurl_callback", link_id=link.id, extra=request.args.get('extra'), _external=True),
|
||||
callback=url_for("lnurlp.api_lnurl_callback", link_id=link.id, extra=request.path_params['extra'], _external=True),
|
||||
min_sendable=math.ceil(link.min * rate) * 1000,
|
||||
max_sendable=round(link.max * rate) * 1000,
|
||||
metadata=link.lnurlpay_metadata,
|
||||
@@ -32,16 +32,16 @@ async def api_lnurl_response(link_id):
|
||||
if link.comment_chars > 0:
|
||||
params["commentAllowed"] = link.comment_chars
|
||||
|
||||
return jsonify(params), HTTPStatus.OK
|
||||
return params
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/lnurl/cb/<link_id>", methods=["GET"])
|
||||
async def api_lnurl_callback(link_id):
|
||||
@lnurlp_ext.get("/api/v1/lnurl/cb/{link_id}", status_code=HTTPStatus.OK)
|
||||
async def api_lnurl_callback(request: Request, link_id):
|
||||
link = await increment_pay_link(link_id, served_pr=1)
|
||||
if not link:
|
||||
return (
|
||||
jsonify({"status": "ERROR", "reason": "LNURL-pay not found."}),
|
||||
HTTPStatus.OK,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Pay link does not exist."
|
||||
)
|
||||
|
||||
min, max = link.min, link.max
|
||||
@@ -54,36 +54,23 @@ async def api_lnurl_callback(link_id):
|
||||
min = link.min * 1000
|
||||
max = link.max * 1000
|
||||
|
||||
amount_received = int(request.args.get("amount") or 0)
|
||||
amount_received = int(request.path_params['amount'] or 0)
|
||||
if amount_received < min:
|
||||
return (
|
||||
jsonify(
|
||||
LnurlErrorResponse(
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Amount {amount_received} is smaller than minimum {min}."
|
||||
).dict()
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
|
||||
elif amount_received > max:
|
||||
return (
|
||||
jsonify(
|
||||
LnurlErrorResponse(
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Amount {amount_received} is greater than maximum {max}."
|
||||
).dict()
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
|
||||
comment = request.args.get("comment")
|
||||
|
||||
comment = request.path_params["comment"]
|
||||
if len(comment or "") > link.comment_chars:
|
||||
return (
|
||||
jsonify(
|
||||
LnurlErrorResponse(
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Got a comment with {len(comment)} characters, but can only accept {link.comment_chars}"
|
||||
).dict()
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
|
||||
payment_hash, payment_request = await create_invoice(
|
||||
wallet_id=link.wallet,
|
||||
@@ -92,7 +79,7 @@ async def api_lnurl_callback(link_id):
|
||||
description_hash=hashlib.sha256(
|
||||
link.lnurlpay_metadata.encode("utf-8")
|
||||
).digest(),
|
||||
extra={"tag": "lnurlp", "link": link.id, "comment": comment, 'extra': request.args.get('extra')},
|
||||
extra={"tag": "lnurlp", "link": link.id, "comment": comment, 'extra': request.path_params['amount']},
|
||||
)
|
||||
|
||||
success_action = link.success_action(payment_hash)
|
||||
@@ -108,4 +95,4 @@ async def api_lnurl_callback(link_id):
|
||||
routes=[],
|
||||
)
|
||||
|
||||
return jsonify(resp.dict()), HTTPStatus.OK
|
||||
return resp.dict())
|
||||
|
||||
@@ -8,13 +8,17 @@ from fastapi import FastAPI, Request
|
||||
from fastapi.params import Depends
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import HTMLResponse
|
||||
from lnbits.core.models import User
|
||||
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
@lnurlp_ext.get("/", response_class=HTMLResponse)
|
||||
@validate_uuids(["usr"], required=True)
|
||||
# @check_user_exists()
|
||||
async def index(request: Request, user: User = Depends(check_user_exists)):
|
||||
return lnurlp_renderer().TemplateResponse("lnurlp/index.html", {"request": request, "user": user})
|
||||
return lnurlp_renderer().TemplateResponse("lnurlp/index.html", {"request": request, "user": user.dict()})
|
||||
|
||||
|
||||
@lnurlp_ext.get("/{link_id}", response_class=HTMLResponse)
|
||||
@@ -27,7 +31,7 @@ async def display(request: Request,link_id):
|
||||
)
|
||||
# abort(HTTPStatus.NOT_FOUND, "Pay link does not exist.")
|
||||
|
||||
return await lnurlp_renderer().TemplateResponse("lnurlp/display.html", {"request": request, "link":link})
|
||||
return lnurlp_renderer().TemplateResponse("lnurlp/display.html", {"request": request, "link":link})
|
||||
|
||||
|
||||
@lnurlp_ext.get("/print/{link_id}", response_class=HTMLResponse)
|
||||
@@ -40,4 +44,4 @@ async def print_qr(request: Request,link_id):
|
||||
)
|
||||
# abort(HTTPStatus.NOT_FOUND, "Pay link does not exist.")
|
||||
|
||||
return await lnurlp_renderer().TemplateResponse("lnurlp/print_qr.html", {"request": request, "link":link})
|
||||
return lnurlp_renderer().TemplateResponse("lnurlp/print_qr.html", {"request": request, "link":link})
|
||||
|
||||
Reference in New Issue
Block a user