Skip to content

Commit

Permalink
Merge pull request #38 from lnbits/lnurlfix
Browse files Browse the repository at this point in the history
Added comment and variable amounts
  • Loading branch information
arcbtc authored Apr 18, 2024
2 parents fddf840 + 70a43ff commit b064e86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
34 changes: 16 additions & 18 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
15 changes: 7 additions & 8 deletions lnurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -285,6 +284,9 @@ 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),
Expand All @@ -293,27 +295,24 @@ 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
)
resp = JSONResponse(
{
resp = {
"pr": payment_request,
"successAction": {
"tag": "text",
"description": f"{int(amount / 1000)}sats sent"
"tag": "message",
"message": f"{int(amount / 1000)}sats sent"
},
"routes": [],
}
)

return resp

Expand Down
7 changes: 4 additions & 3 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}",
)

0 comments on commit b064e86

Please sign in to comment.