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

View file

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