From 10fb16467bedadc17cea94ab1b29f69f29e5ed97 Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Wed, 10 Nov 2021 11:15:33 +0000 Subject: [PATCH 1/4] url_for not working --- lnbits/extensions/livestream/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lnbits/extensions/livestream/views.py b/lnbits/extensions/livestream/views.py index a56fadb17..ecb80a18b 100644 --- a/lnbits/extensions/livestream/views.py +++ b/lnbits/extensions/livestream/views.py @@ -21,8 +21,9 @@ async def index(request: Request, user: User = Depends(check_user_exists)): @livestream_ext.get("/track/{track_id}", name="livestream.track_redirect_download") -async def track_redirect_download(track_id, request: Request): - payment_hash = request.path_params["p"] +async def track_redirect_download(track_id, p: str = Query(...)): + print("BOO", track_id, p) + payment_hash = p track = await get_track(track_id) ls = await get_livestream_by_track(track_id) payment: Payment = await get_wallet_payment(ls.wallet, payment_hash) From 1537b06e00a8ae114d2b981e6f33881d5e31faf5 Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Wed, 10 Nov 2021 17:00:53 +0000 Subject: [PATCH 2/4] reset form --- lnbits/extensions/satspay/templates/satspay/index.html | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lnbits/extensions/satspay/templates/satspay/index.html b/lnbits/extensions/satspay/templates/satspay/index.html index d941e90b7..7c5b21140 100644 --- a/lnbits/extensions/satspay/templates/satspay/index.html +++ b/lnbits/extensions/satspay/templates/satspay/index.html @@ -509,6 +509,13 @@ .then(function (response) { self.ChargeLinks.push(mapCharge(response.data)) self.formDialogCharge.show = false + self.formDialogCharge.data = { + onchain: false, + lnbits: false, + description: '', + time: null, + amount: null + } }) .catch(function (error) { LNbits.utils.notifyApiError(error) From 119404b5353e6e7c71d2f0c153a36418f38391e1 Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Wed, 10 Nov 2021 21:12:47 +0000 Subject: [PATCH 3/4] updated withdraw lnurl --- lnbits/extensions/withdraw/lnurl.py | 98 +++++++++++++++++------------ 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index a5a071bd7..fe5a1820a 100644 --- a/lnbits/extensions/withdraw/lnurl.py +++ b/lnbits/extensions/withdraw/lnurl.py @@ -2,9 +2,11 @@ from datetime import datetime from http import HTTPStatus import shortuuid # type: ignore +import json from fastapi import HTTPException from fastapi.param_functions import Query from starlette.requests import Request +from starlette.responses import HTMLResponse # type: ignore from lnbits.core.services import pay_invoice @@ -16,7 +18,7 @@ from .crud import get_withdraw_link_by_hash, update_withdraw_link @withdraw_ext.get( "/api/v1/lnurl/{unique_hash}", - status_code=HTTPStatus.OK, + response_class=HTMLResponse, name="withdraw.api_lnurl_response", ) async def api_lnurl_response(request: Request, unique_hash): @@ -35,17 +37,67 @@ async def api_lnurl_response(request: Request, unique_hash): # WHAT STATUS_CODE TO USE?? detail="Withdraw is spent." ) - # return ({"status": "ERROR", "reason": "Withdraw is spent."}, - # HTTPStatus.OK, - # ) + url = request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash) + withdrawResponse = { + "tag": "withdrawRequest", + "callback": url, + "k1": link.k1, + "minWithdrawable": link.min_withdrawable * 1000, + "maxWithdrawable": link.max_withdrawable * 1000, + "defaultDescription": link.title, + } + return json.dumps(withdrawResponse) - return link.lnurl_response(request).dict() + +# FOR LNURLs WHICH ARE UNIQUE + + +@withdraw_ext.get( + "/api/v1/lnurl/{unique_hash}/{id_unique_hash}", + response_class=HTMLResponse, + name="withdraw.api_lnurl_multi_response", +) +async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash): + link = await get_withdraw_link_by_hash(unique_hash) + + if not link: + raise HTTPException( + status_code=HTTPStatus.OK, detail="LNURL-withdraw not found." + ) + + if link.is_spent: + raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.") + + useslist = link.usescsv.split(",") + found = False + for x in useslist: + tohash = link.id + link.unique_hash + str(x) + if id_unique_hash == shortuuid.uuid(name=tohash): + found = True + if not found: + raise HTTPException( + status_code=HTTPStatus.OK, detail="LNURL-withdraw not found." + ) + + url = request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash) + withdrawResponse = { + "tag": "withdrawRequest", + "callback": url, + "k1": link.k1, + "minWithdrawable": link.min_withdrawable * 1000, + "maxWithdrawable": link.max_withdrawable * 1000, + "defaultDescription": link.title, + } + return json.dumps(withdrawResponse) # CALLBACK - -@withdraw_ext.get("/api/v1/lnurl/cb/{unique_hash}", name="withdraw.api_lnurl_callback") +@withdraw_ext.get( + "/api/v1/lnurl/cb/{unique_hash}", + status_code=HTTPStatus.OK, + name="withdraw.api_lnurl_callback" + ) async def api_lnurl_callback( request: Request, unique_hash: str = Query(...), @@ -102,35 +154,3 @@ async def api_lnurl_callback( await update_withdraw_link(link.id, **changesback) return {"status": "ERROR", "reason": "Link not working"} - -# FOR LNURLs WHICH ARE UNIQUE - - -@withdraw_ext.get( - "/api/v1/lnurl/{unique_hash}/{id_unique_hash}", - status_code=HTTPStatus.OK, - name="withdraw.api_lnurl_multi_response", -) -async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash): - link = await get_withdraw_link_by_hash(unique_hash) - - if not link: - raise HTTPException( - status_code=HTTPStatus.OK, detail="LNURL-withdraw not found." - ) - - if link.is_spent: - raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.") - - useslist = link.usescsv.split(",") - found = False - for x in useslist: - tohash = link.id + link.unique_hash + str(x) - if id_unique_hash == shortuuid.uuid(name=tohash): - found = True - if not found: - raise HTTPException( - status_code=HTTPStatus.OK, detail="LNURL-withdraw not found." - ) - - return link.lnurl_response(request).dict() From 860709f48caf836337789119494cea242f2b6374 Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Wed, 10 Nov 2021 21:14:47 +0000 Subject: [PATCH 4/4] async wrap --- lnbits/wallets/clightning.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lnbits/wallets/clightning.py b/lnbits/wallets/clightning.py index bb9543896..8f21f3c88 100644 --- a/lnbits/wallets/clightning.py +++ b/lnbits/wallets/clightning.py @@ -8,6 +8,7 @@ import random from functools import partial, wraps from os import getenv from typing import AsyncGenerator, Optional +import time from .base import ( InvoiceResponse, @@ -30,6 +31,10 @@ def async_wrap(func): return run +def _pay_invoice(ln, bolt11): + return ln.pay(bolt11) + + def _paid_invoices_stream(ln, last_pay_index): return ln.waitanyinvoice(last_pay_index) @@ -99,7 +104,8 @@ class CLightningWallet(Wallet): async def pay_invoice(self, bolt11: str) -> PaymentResponse: try: - r = self.ln.pay(bolt11) + wrapped = async_wrap(_pay_invoice) + r = await wrapped(self.ln, bolt11) except RpcError as exc: return PaymentResponse(False, None, 0, None, str(exc)) @@ -133,4 +139,4 @@ class CLightningWallet(Wallet): wrapped = async_wrap(_paid_invoices_stream) paid = await wrapped(self.ln, self.last_pay_index) self.last_pay_index = paid["pay_index"] - yield paid["label"] + yield paid["label"] \ No newline at end of file