Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return 200 status for lnurl errors and fix uniques #43

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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