Skip to content

Commit

Permalink
fix: return 200 status for lnurl errors and fix uniques (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni authored Jul 23, 2024
1 parent 00064f6 commit 4e6d61f
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions views_lnurl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from datetime import datetime
from http import HTTPStatus
from typing import Callable
from typing import Callable, Optional
from urllib.parse import urlparse

import httpx
import shortuuid
from fastapi import APIRouter, HTTPException, Query, Request, Response
from fastapi import APIRouter, HTTPException, Request, Response
from fastapi.responses import JSONResponse
from fastapi.routing import APIRoute
from lnbits.core.crud import update_payment_extra
Expand All @@ -30,16 +30,14 @@ def get_route_handler(self) -> Callable:
async def custom_route_handler(request: Request) -> Response:
try:
response = await original_route_handler(request)
return response
except HTTPException as exc:
logger.debug(f"HTTPException: {exc}")
response = JSONResponse(
status_code=exc.status_code,
status_code=200,
content={"status": "ERROR", "reason": f"{exc.detail}"},
)
except Exception as exc:
raise exc

return response
return response

return custom_route_handler

Expand All @@ -65,6 +63,13 @@ async def api_lnurl_response(request: Request, unique_hash: str):
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw is spent."
)

if link.is_unique:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="This link requires an id_unique_hash.",
)

url = str(
request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash)
)
Expand Down Expand Up @@ -105,10 +110,10 @@ async def api_lnurl_response(request: Request, unique_hash: str):
},
)
async def api_lnurl_callback(
unique_hash,
k1: str = Query(...),
pr: str = Query(...),
id_unique_hash=None,
unique_hash: str,
k1: str,
pr: str,
id_unique_hash: Optional[str] = None,
):

link = await get_withdraw_link_by_hash(unique_hash)
Expand All @@ -133,6 +138,12 @@ async def api_lnurl_callback(
detail=f"wait link open_time {link.open_time - now} seconds.",
)

if not id_unique_hash and link.is_unique:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="id_unique_hash is required for this link.",
)

if id_unique_hash:
if check_unique_link(link, id_unique_hash):
await remove_unique_withdraw_link(link, id_unique_hash)
Expand Down

0 comments on commit 4e6d61f

Please sign in to comment.