-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_lnurl.py
68 lines (61 loc) · 1.99 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
import random
from http import HTTPStatus
from fastapi import APIRouter, Query, Request
from lnbits.core.services import create_invoice
from .crud import get_eightball
eightball_lnurl_router = APIRouter()
@eightball_lnurl_router.get(
"/api/v1/lnurl/pay/{eightball_id}",
status_code=HTTPStatus.OK,
name="eightball.api_lnurl_pay",
)
async def api_lnurl_pay(
request: Request,
eightball_id: str,
):
eightball = await get_eightball(eightball_id)
if not eightball:
return {"status": "ERROR", "reason": "No eightball found"}
if not eightball.lnurlpayamount:
return {"status": "ERROR", "reason": "Eightball has no lnurlpayamount set"}
return {
"callback": str(
request.url_for(
"eightball.api_lnurl_pay_callback", eightball_id=eightball_id
)
),
"maxSendable": eightball.lnurlpayamount * 1000,
"minSendable": eightball.lnurlpayamount * 1000,
"metadata": f"""[["text/plain", "{eightball.name}"]]""",
"tag": "payRequest",
}
@eightball_lnurl_router.get(
"/api/v1/lnurl/paycb/{eightball_id}",
status_code=HTTPStatus.OK,
name="eightball.api_lnurl_pay_callback",
)
async def api_lnurl_pay_cb(
request: Request,
eightball_id: str,
amount: int = Query(...),
):
eightball = await get_eightball(eightball_id)
if not eightball:
return {"status": "ERROR", "reason": "No eightball found"}
payment = await create_invoice(
wallet_id=eightball.wallet,
amount=int(amount / 1000),
memo=eightball.name,
unhashed_description=f'[["text/plain", "{eightball.name}"]]'.encode(),
extra={
"tag": "EightBall",
"eightballId": eightball_id,
"extra": request.query_params.get("amount"),
},
)
random_word = random.choice(eightball.wordlist.split("\n"))
return {
"pr": payment.bolt11,
"routes": [],
"successAction": {"tag": "message", "message": random_word},
}