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

88 lines
2.8 KiB
Python
Raw Normal View History

2021-10-12 19:54:35 +01:00
import hashlib
2021-10-18 11:51:51 +01:00
import json
from http import HTTPStatus
2021-10-18 11:51:51 +01:00
from fastapi import Request
from fastapi.param_functions import Query
2021-10-12 19:54:35 +01:00
from lnurl.types import LnurlPayMetadata
2021-10-18 11:51:51 +01:00
from starlette.exceptions import HTTPException
2021-10-18 11:58:21 +01:00
from starlette.responses import HTMLResponse # type: ignore
2021-10-18 11:51:51 +01:00
2021-10-12 19:54:35 +01:00
from lnbits.core.services import create_invoice
2021-10-18 11:51:51 +01:00
2021-10-12 19:54:35 +01:00
from . import copilot_ext
from .crud import get_copilot
2021-10-17 23:19:42 +01:00
@copilot_ext.get(
"/lnurl/{cp_id}", response_class=HTMLResponse, name="copilot.lnurl_response"
)
async def lnurl_response(req: Request, cp_id: str = Query(None)):
2021-10-12 19:54:35 +01:00
cp = await get_copilot(cp_id)
if not cp:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
2021-10-12 19:54:35 +01:00
payResponse = {
"tag": "payRequest",
"callback": req.url_for("copilot.lnurl_callback", cp_id=cp_id),
"metadata": LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]])),
"maxSendable": 50000000,
"minSendable": 10000,
}
2021-10-12 19:54:35 +01:00
if cp.show_message:
payResponse["commentAllowed"] = 300
return json.dumps(payResponse)
2021-10-12 19:54:35 +01:00
@copilot_ext.get(
"/lnurl/cb/{cp_id}", response_class=HTMLResponse, name="copilot.lnurl_callback"
)
async def lnurl_callback(
cp_id: str = Query(None), amount: str = Query(None), comment: str = Query(None)
):
2021-10-12 19:54:35 +01:00
cp = await get_copilot(cp_id)
if not cp:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
amount_received = int(amount)
2021-10-18 11:51:51 +01:00
2021-10-12 19:54:35 +01:00
if amount_received < 10000:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Amount {round(amount_received / 1000)} is smaller than minimum 10 sats.",
)
2021-10-12 19:54:35 +01:00
elif amount_received / 1000 > 10000000:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Amount {round(amount_received / 1000)} is greater than maximum 50000.",
)
2021-10-12 19:54:35 +01:00
comment = ""
if comment:
2021-10-12 19:54:35 +01:00
if len(comment or "") > 300:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Got a comment with {len(comment)} characters, but can only accept 300",
)
2021-10-12 19:54:35 +01:00
if len(comment) < 1:
comment = "none"
payment_hash, payment_request = await create_invoice(
wallet_id=cp.wallet,
amount=int(amount_received / 1000),
2021-10-18 11:51:51 +01:00
memo=cp.lnurl_title or "",
2021-10-12 19:54:35 +01:00
description_hash=hashlib.sha256(
(
LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]]))
).encode("utf-8")
).digest(),
extra={"tag": "copilot", "copilotid": cp.id, "comment": comment},
2021-10-12 19:54:35 +01:00
)
payResponse = {
"pr": payment_request,
"routes": [],
}
return json.dumps(payResponse)