From dc0f7d071220f9fed1861ef199840d6ba2d3461c Mon Sep 17 00:00:00 2001 From: benarc Date: Wed, 17 Apr 2024 10:24:57 +0100 Subject: [PATCH 1/2] fixes lnurl switch --- lnurl.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lnurl.py b/lnurl.py index 068c8db..fc7d328 100644 --- a/lnurl.py +++ b/lnurl.py @@ -285,6 +285,8 @@ async def lnurl_callback( return {"status": "ERROR", "reason": "Payment failed, use a different wallet."} return {"status": "OK"} if device.device == "switch": + if not amount: + return {"status": "ERROR", "reason": "No amount"} payment_hash, payment_request = await create_invoice( wallet_id=device.wallet, amount=int(amount / 1000), @@ -304,8 +306,7 @@ async def lnurl_callback( lnurldevicepayment = await update_lnurldevicepayment( lnurldevicepayment_id=paymentid, payhash=payment_hash ) - resp = JSONResponse( - { + resp = { "pr": payment_request, "successAction": { "tag": "text", @@ -313,7 +314,7 @@ async def lnurl_callback( }, "routes": [], } - ) + logger.debug(resp) return resp From 70a43ff6993668be55552a0d452ea862fc70ad8e Mon Sep 17 00:00:00 2001 From: benarc Date: Thu, 18 Apr 2024 07:59:25 +0100 Subject: [PATCH 2/2] variable and comment added --- crud.py | 34 ++++++++++++++++------------------ lnurl.py | 10 ++++------ tasks.py | 7 ++++--- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/crud.py b/crud.py index ac7462f..2edf006 100644 --- a/crud.py +++ b/crud.py @@ -105,15 +105,14 @@ async def get_lnurldevice(lnurldevice_id: str, req: Request) -> Optional[Lnurlde if device.switches: url = req.url_for("lnurldevice.lnurl_v2_params", device_id=device.id) for _switch in device.switches: - if not _switch.lnurl: - _switch.lnurl = lnurl_encode( - str(url) - + f"?pin={_switch.pin}" - + f"&amount={_switch.amount}" - + f"&duration={_switch.duration}" - + f"&variable={_switch.variable}" - + f"&comment={_switch.comment}" - ) + _switch.lnurl = lnurl_encode( + str(url) + + f"?pin={_switch.pin}" + + f"&amount={_switch.amount}" + + f"&duration={_switch.duration}" + + f"&variable={_switch.variable}" + + f"&comment={_switch.comment}" + ) return device @@ -136,15 +135,14 @@ async def get_lnurldevices(wallet_ids: List[str], req: Request) -> List[Lnurldev if device.switches: url = req.url_for("lnurldevice.lnurl_v2_params", device_id=device.id) for _switch in device.switches: - if not _switch.lnurl: - _switch.lnurl = lnurl_encode( - str(url) - + f"?pin={_switch.pin}" - + f"&amount={_switch.amount}" - + f"&duration={_switch.duration}" - + f"&variable={_switch.variable}" - + f"&comment={_switch.comment}" - ) + _switch.lnurl = lnurl_encode( + str(url) + + f"?pin={_switch.pin}" + + f"&amount={_switch.amount}" + + f"&duration={_switch.duration}" + + f"&variable={_switch.variable}" + + f"&comment={_switch.comment}" + ) return devices diff --git a/lnurl.py b/lnurl.py index fc7d328..7681e52 100644 --- a/lnurl.py +++ b/lnurl.py @@ -159,7 +159,6 @@ async def lnurl_params( resp["commentAllowed"] = 1500 if variable == True: resp["maxSendable"] = price_msat * 360 - logger.debug(resp) return resp if len(p) % 4 > 0: @@ -287,6 +286,7 @@ async def lnurl_callback( if device.device == "switch": if not amount: return {"status": "ERROR", "reason": "No amount"} + payment_hash, payment_request = await create_invoice( wallet_id=device.wallet, amount=int(amount / 1000), @@ -295,13 +295,12 @@ async def lnurl_callback( extra={ "tag": "Switch", "pin": str(lnurldevicepayment.pin), - "amount": amount, + "amount": str(int(amount)), "comment": comment, "variable": variable, "id": paymentid, }, ) - logger.debug(bolt11.decode(payment_request)) lnurldevicepayment = await update_lnurldevicepayment( lnurldevicepayment_id=paymentid, payhash=payment_hash @@ -309,12 +308,11 @@ async def lnurl_callback( resp = { "pr": payment_request, "successAction": { - "tag": "text", - "description": f"{int(amount / 1000)}sats sent" + "tag": "message", + "message": f"{int(amount / 1000)}sats sent" }, "routes": [], } - logger.debug(resp) return resp diff --git a/tasks.py b/tasks.py index 8ad3eb9..ecd6348 100644 --- a/tasks.py +++ b/tasks.py @@ -31,9 +31,10 @@ async def on_invoice_paid(payment: Payment) -> None: lnurldevicepayment_id=payment.extra["id"], payhash="used" ) comment = payment.extra["comment"] + variable = payment.extra["variable"] payload = lnurldevicepayment.payload - if payload: - payload = int(payload) * int(lnurldevicepayment.sats) + if variable: + payload = int((int(payload) / int(lnurldevicepayment.sats)) * int(payment.extra["amount"])) if comment: return await websocket_updater( @@ -42,5 +43,5 @@ async def on_invoice_paid(payment: Payment) -> None: ) return await websocket_updater( lnurldevicepayment.deviceid, - f"{lnurldevicepayment.pin}-{lnurldevicepayment.payload}", + f"{lnurldevicepayment.pin}-{payload}", )