Merge branch 'FastAPI' of https://github.com/arcbtc/lnbits into FastAPI

This commit is contained in:
Tiago vasconcelos 2021-10-18 10:58:20 +01:00
commit 59de66f00b
2 changed files with 34 additions and 34 deletions

View file

@ -33,21 +33,22 @@ async def lnurl_response(req: Request, cp_id: str = Query(None)):
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
resp = LnurlPayResponse(
callback=req.url_for("copilot.lnurl_callback", cp_id=cp_id, _external=True),
min_sendable=10000,
max_sendable=50000000,
metadata=LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]])),
)
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,
}
params = resp.dict()
if cp.show_message:
params["commentAllowed"] = 300
return params
payResponse["commentAllowed"] = 300
return json.dumps(payResponse)
@copilot_ext.get("/lnurl/cb/{cp_id}", response_class=HTMLResponse)
@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)
):
@ -56,26 +57,27 @@ async def lnurl_callback(
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
)
amount_received = int(amount)
if amount_received < 10000:
return LnurlErrorResponse(
reason=f"Amount {round(amount_received / 1000)} is smaller than minimum 10 sats."
).dict()
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Amount {round(amount_received / 1000)} is smaller than minimum 10 sats.",
)
elif amount_received / 1000 > 10000000:
return LnurlErrorResponse(
reason=f"Amount {round(amount_received / 1000)} is greater than maximum 50000."
).dict()
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:
return LnurlErrorResponse(
reason=f"Got a comment with {len(comment)} characters, but can only accept 300"
).dict()
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"
payment_hash, payment_request = await create_invoice(
wallet_id=cp.wallet,
amount=int(amount_received / 1000),
@ -85,9 +87,10 @@ async def lnurl_callback(
LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]]))
).encode("utf-8")
).digest(),
extra={"tag": "copilot", "copilot": cp.id, "comment": comment},
extra={"tag": "copilot", "copilotid": cp.id, "comment": comment},
)
resp = LnurlPayActionResponse(
pr=payment_request, success_action=None, disposable=False, routes=[]
)
return resp.dict()
payResponse = {
"pr": payment_request,
"routes": [],
}
return json.dumps(payResponse)

View file

@ -29,12 +29,9 @@ async def on_invoice_paid(payment: Payment) -> None:
if "copilot" != payment.extra.get("tag"):
# not an copilot invoice
return
print("cunt")
if payment.extra.get("wh_status"):
# this webhook has already been sent
return
copilot = await get_copilot(payment.extra.get("copilot", -1))
copilot = await get_copilot(payment.extra.get("copilotid", -1))
if not copilot:
raise HTTPException(
@ -70,8 +67,8 @@ async def on_invoice_paid(payment: Payment) -> None:
await mark_webhook_sent(payment, -1)
if payment.extra.get("comment"):
await updater(copilot.id, data, payment.extra.get("comment"))
else:
await updater(copilot.id, data, "none")
await updater(copilot.id, data, "none")
async def mark_webhook_sent(payment: Payment, status: int) -> None: