diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index dadc52e05..3b5f42fc5 100644 --- a/lnbits/extensions/withdraw/lnurl.py +++ b/lnbits/extensions/withdraw/lnurl.py @@ -1,11 +1,11 @@ +from fastapi.param_functions import Query +from fastapi import HTTPException import shortuuid # type: ignore from http import HTTPStatus from datetime import datetime from lnbits.core.services import pay_invoice -from fastapi.param_functions import Query from starlette.requests import Request -from starlette.exceptions import HTTPException from . import withdraw_ext from .crud import get_withdraw_link_by_hash, update_withdraw_link @@ -39,60 +39,19 @@ async def api_lnurl_response(request: Request, unique_hash): return link.lnurl_response(request).dict() -# FOR LNURLs WHICH ARE UNIQUE - - -@withdraw_ext.get("/api/v1/lnurl/{unique_hash}/{id_unique_hash}", status_code=HTTPStatus.OK, name="withdraw.api_lnurl_multi_response") -async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash): - link = await get_withdraw_link_by_hash(unique_hash) - - if not link: - raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, - detail="LNURL-withdraw not found." - ) - # return ( - # {"status": "ERROR", "reason": "LNURL-withdraw not found."}, - # HTTPStatus.OK, - # ) - - if link.is_spent: - raise HTTPException( - # WHAT STATUS_CODE TO USE?? - detail="Withdraw is spent." - ) - # return ( - # {"status": "ERROR", "reason": "Withdraw is spent."}, - # HTTPStatus.OK, - # ) - - useslist = link.usescsv.split(",") - found = False - for x in useslist: - tohash = link.id + link.unique_hash + str(x) - if id_unique_hash == shortuuid.uuid(name=tohash): - found = True - if not found: - raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, - detail="LNURL-withdraw not found." - ) - # return ( - # {"status": "ERROR", "reason": "LNURL-withdraw not found."}, - # HTTPStatus.OK, - # ) - - return link.lnurl_response(req=request).dict() - # CALLBACK -@withdraw_ext.get("/api/v1/lnurl/cb/{unique_hash}", status_code=HTTPStatus.OK, name="withdraw.api_lnurl_callback") -async def api_lnurl_callback(unique_hash, k1: str = Query(...), pr: str = Query(...)): +@withdraw_ext.get("/api/v1/lnurl/cb/{unique_hash}", name="withdraw.api_lnurl_callback") +async def api_lnurl_callback(request: Request, + unique_hash: str=Query(...), + k1: str = Query(...), + payment_request: str = Query(..., alias="pr") + ): link = await get_withdraw_link_by_hash(unique_hash) - payment_request = pr now = int(datetime.now().timestamp()) + if not link: raise HTTPException( status_code=HTTPStatus.NOT_FOUND, @@ -163,3 +122,48 @@ async def api_lnurl_callback(unique_hash, k1: str = Query(...), pr: str = Query( return {"status": "ERROR", "reason": str(e)} return {"status": "OK"} + +# FOR LNURLs WHICH ARE UNIQUE + + +@withdraw_ext.get("/api/v1/lnurl/{unique_hash}/{id_unique_hash}", status_code=HTTPStatus.OK, name="withdraw.api_lnurl_multi_response") +async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash): + link = await get_withdraw_link_by_hash(unique_hash) + + if not link: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="LNURL-withdraw not found." + ) + # return ( + # {"status": "ERROR", "reason": "LNURL-withdraw not found."}, + # HTTPStatus.OK, + # ) + + if link.is_spent: + raise HTTPException( + # WHAT STATUS_CODE TO USE?? + detail="Withdraw is spent." + ) + # return ( + # {"status": "ERROR", "reason": "Withdraw is spent."}, + # HTTPStatus.OK, + # ) + + useslist = link.usescsv.split(",") + found = False + for x in useslist: + tohash = link.id + link.unique_hash + str(x) + if id_unique_hash == shortuuid.uuid(name=tohash): + found = True + if not found: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="LNURL-withdraw not found." + ) + # return ( + # {"status": "ERROR", "reason": "LNURL-withdraw not found."}, + # HTTPStatus.OK, + # ) + + return link.lnurl_response(request).dict()