-
Notifications
You must be signed in to change notification settings - Fork 3
/
views_lnurl.py
87 lines (77 loc) · 2.73 KB
/
views_lnurl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import json
from http import HTTPStatus
from fastapi import APIRouter, Query, Request
from fastapi.exceptions import HTTPException
from fastapi.responses import HTMLResponse
from lnbits.core.services import create_invoice
from lnurl.types import LnurlPayMetadata
from .crud import get_copilot
copilot_lnurl_router = APIRouter()
@copilot_lnurl_router.get(
"/lnurl/{cp_id}", response_class=HTMLResponse, name="copilot.lnurl_response"
)
async def lnurl_response(req: Request, cp_id: str):
cp = await get_copilot(cp_id)
if not cp:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
pay_response = {
"tag": "payRequest",
"callback": str(req.url_for("copilot.lnurl_callback", cp_id=cp_id)),
"metadata": LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]])),
"maxSendable": 50000000,
"minSendable": 10000,
}
if cp.show_message:
pay_response["commentAllowed"] = 300
return json.dumps(pay_response)
@copilot_lnurl_router.get("/lnurl/cb/{cp_id}", name="copilot.lnurl_callback")
async def lnurl_callback(
cp_id: str, amount: str = Query(None), comment: str = Query(None)
):
cp = await get_copilot(cp_id)
if not cp:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
amount_received = int(amount)
if amount_received < 10000:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail=(
"Amount {round(amount_received / 1000)} "
"is smaller than minimum 10 sats."
),
)
elif amount_received / 1000 > 10000000:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail=(
"Amount {round(amount_received / 1000)} "
"is greater than maximum 50000."
),
)
comment = ""
if comment:
if len(comment or "") > 300:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail=(
"Got a comment with {len(comment)} characters, "
"but can only accept 300"
),
)
if len(comment) < 1:
comment = "none"
assert cp.wallet, "Copilot wallet not found"
payment = await create_invoice(
wallet_id=cp.wallet,
amount=int(amount_received / 1000),
memo=cp.lnurl_title or "",
unhashed_description=(
LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]]))
).encode(),
extra={"tag": "copilot", "copilotid": cp.id, "comment": comment},
)
return {"pr": payment.bolt11, "routes": []}