lnbits-legend/lnbits/extensions/lnurlp/lnurl.py

108 lines
3.3 KiB
Python
Raw Normal View History

import hashlib
import math
from http import HTTPStatus
2021-10-20 12:03:11 +01:00
from fastapi import Request
from lnurl import ( # type: ignore
2021-10-17 18:33:29 +01:00
LnurlErrorResponse,
2021-10-20 12:03:11 +01:00
LnurlPayActionResponse,
LnurlPayResponse,
)
from starlette.exceptions import HTTPException
from lnbits.core.services import create_invoice
from lnbits.utils.exchange_rates import get_fiat_rate_satoshis
from . import lnurlp_ext
from .crud import increment_pay_link
2021-10-17 18:33:29 +01:00
@lnurlp_ext.get(
"/api/v1/lnurl/{link_id}",
status_code=HTTPStatus.OK,
name="lnurlp.api_lnurl_response",
)
2021-09-30 14:42:04 +01:00
async def api_lnurl_response(request: Request, link_id):
link = await increment_pay_link(link_id, served_meta=1)
if not link:
2021-09-30 14:42:04 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Pay link does not exist."
)
2022-06-15 12:48:13 +02:00
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
2021-10-08 12:10:43 +01:00
resp = LnurlPayResponse(
2021-10-08 12:10:43 +01:00
callback=request.url_for("lnurlp.api_lnurl_callback", link_id=link.id),
min_sendable=round(link.min * rate) * 1000,
max_sendable=round(link.max * rate) * 1000,
metadata=link.lnurlpay_metadata,
)
params = resp.dict()
if link.comment_chars > 0:
params["commentAllowed"] = link.comment_chars
2021-09-30 14:42:04 +01:00
return params
2021-10-17 18:33:29 +01:00
@lnurlp_ext.get(
"/api/v1/lnurl/cb/{link_id}",
status_code=HTTPStatus.OK,
name="lnurlp.api_lnurl_callback",
)
2021-09-30 14:42:04 +01:00
async def api_lnurl_callback(request: Request, link_id):
link = await increment_pay_link(link_id, served_pr=1)
if not link:
2021-09-30 14:42:04 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Pay link does not exist."
)
min, max = link.min, link.max
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
if link.currency:
# allow some fluctuation (as the fiat price may have changed between the calls)
min = rate * 995 * link.min
max = rate * 1010 * link.max
else:
min = link.min * 1000
max = link.max * 1000
2021-10-17 18:33:29 +01:00
amount_received = int(request.query_params.get("amount") or 0)
if amount_received < min:
2021-09-30 14:42:04 +01:00
return LnurlErrorResponse(
2021-10-17 18:33:29 +01:00
reason=f"Amount {amount_received} is smaller than minimum {min}."
).dict()
2021-09-30 14:42:04 +01:00
elif amount_received > max:
2021-09-30 14:42:04 +01:00
return LnurlErrorResponse(
2021-10-17 18:33:29 +01:00
reason=f"Amount {amount_received} is greater than maximum {max}."
).dict()
2021-09-30 14:42:04 +01:00
2021-10-08 12:10:43 +01:00
comment = request.query_params.get("comment")
if len(comment or "") > link.comment_chars:
2021-09-30 14:42:04 +01:00
return LnurlErrorResponse(
2021-10-17 18:33:29 +01:00
reason=f"Got a comment with {len(comment)} characters, but can only accept {link.comment_chars}"
).dict()
2021-11-03 11:49:13 +00:00
payment_hash, payment_request = await create_invoice(
wallet_id=link.wallet,
amount=int(amount_received / 1000),
memo=link.description,
description_hash=link.lnurlpay_metadata.encode("utf-8"),
2021-10-17 18:33:29 +01:00
extra={
"tag": "lnurlp",
"link": link.id,
"comment": comment,
"extra": request.query_params.get("amount"),
},
)
success_action = link.success_action(payment_hash)
if success_action:
resp = LnurlPayActionResponse(
2021-10-17 18:33:29 +01:00
pr=payment_request, success_action=success_action, routes=[]
)
else:
2021-10-17 18:33:29 +01:00
resp = LnurlPayActionResponse(pr=payment_request, routes=[])
2021-10-08 12:10:43 +01:00
return resp.dict()