From 10e1bbcb66175abedb00a1cf38b21af892a50d41 Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Tue, 21 Jun 2022 10:31:18 +0100 Subject: [PATCH] pay lnurl --- lnbits/extensions/scrub/crud.py | 14 ++++++ lnbits/extensions/scrub/static/js/index.js | 57 ---------------------- lnbits/extensions/scrub/tasks.py | 51 +++++++++++-------- lnbits/extensions/scrub/views_api.py | 2 +- 4 files changed, 46 insertions(+), 78 deletions(-) diff --git a/lnbits/extensions/scrub/crud.py b/lnbits/extensions/scrub/crud.py index bacac4f00..c9880567c 100644 --- a/lnbits/extensions/scrub/crud.py +++ b/lnbits/extensions/scrub/crud.py @@ -65,3 +65,17 @@ async def update_scrub_link(link_id: int, **kwargs) -> Optional[ScrubLink]: async def delete_scrub_link(link_id: int) -> None: await db.execute("DELETE FROM scrub.scrub_links WHERE id = ?", (link_id,)) + +async def get_scrub_by_wallet(wallet_id) -> Optional[ScrubLink]: + row = await db.fetchone( + "SELECT * from scrub.scrub_links WHERE wallet = ?", + (wallet_id,), + ) + return ScrubLink(**row) if row else None + +async def unique_scrubed_wallet(wallet_id): + row, = await db.fetchone( + "SELECT COUNT(wallet) FROM scrub.scrub_links WHERE wallet = ?", + (wallet_id,), + ) + return row diff --git a/lnbits/extensions/scrub/static/js/index.js b/lnbits/extensions/scrub/static/js/index.js index f0381118c..439907921 100644 --- a/lnbits/extensions/scrub/static/js/index.js +++ b/lnbits/extensions/scrub/static/js/index.js @@ -26,8 +26,6 @@ new Vue({ mixins: [windowMixin], data() { return { - currencies: [], - fiatRates: {}, checker: null, payLinks: [], payLinksTable: { @@ -37,7 +35,6 @@ new Vue({ }, formDialog: { show: false, - fixedAmount: true, data: {} }, qrCodeDialog: { @@ -65,42 +62,11 @@ new Vue({ closeFormDialog() { this.resetFormData() }, - openQrCodeDialog(linkId) { - var link = _.findWhere(this.payLinks, {id: linkId}) - if (link.currency) this.updateFiatRate(link.currency) - - this.qrCodeDialog.data = { - id: link.id, - amount: - (link.min === link.max ? link.min : `${link.min} - ${link.max}`) + - ' ' + - (link.currency || 'sat'), - currency: link.currency, - comments: link.comment_chars - ? `${link.comment_chars} characters` - : 'no', - webhook: link.webhook_url || 'nowhere', - success: - link.success_text || link.success_url - ? 'Display message "' + - link.success_text + - '"' + - (link.success_url ? ' and URL "' + link.success_url + '"' : '') - : 'do nothing', - lnurl: link.lnurl, - pay_url: link.pay_url, - print_url: link.print_url - } - this.qrCodeDialog.show = true - }, openUpdateDialog(linkId) { const link = _.findWhere(this.payLinks, {id: linkId}) - if (link.currency) this.updateFiatRate(link.currency) this.formDialog.data = _.clone(link._data) this.formDialog.show = true - this.formDialog.fixedAmount = - this.formDialog.data.min === this.formDialog.data.max }, sendFormData() { const wallet = _.findWhere(this.g.user.wallets, { @@ -166,35 +132,12 @@ new Vue({ LNbits.utils.notifyApiError(err) }) }) - }, - updateFiatRate(currency) { - LNbits.api - .request('GET', '/scrub/api/v1/rate/' + currency, null) - .then(response => { - let rates = _.clone(this.fiatRates) - rates[currency] = response.data.rate - this.fiatRates = rates - }) - .catch(err => { - LNbits.utils.notifyApiError(err) - }) } }, created() { if (this.g.user.wallets.length) { var getScrubLinks = this.getScrubLinks getScrubLinks() - // this.checker = setInterval(() => { - // getScrubLinks() - // }, 20000) } - LNbits.api - .request('GET', '/scrub/api/v1/currencies') - .then(response => { - this.currencies = ['satoshis', ...response.data] - }) - .catch(err => { - LNbits.utils.notifyApiError(err) - }) } }) diff --git a/lnbits/extensions/scrub/tasks.py b/lnbits/extensions/scrub/tasks.py index 9627707c8..4498464e0 100644 --- a/lnbits/extensions/scrub/tasks.py +++ b/lnbits/extensions/scrub/tasks.py @@ -1,12 +1,15 @@ import asyncio import json + import httpx from lnbits.core import db as core_db -from .models import ScrubLink +from lnbits.core.crud import get_wallet +from lnbits.core.models import Payment from lnbits.tasks import register_invoice_listener -from .crud import get_scrub_link +from .crud import get_scrub_by_wallet, get_scrub_link +from .models import ScrubLink async def wait_for_paid_invoices(): @@ -18,26 +21,34 @@ async def wait_for_paid_invoices(): await on_invoice_paid(payment) -async def on_invoice_paid(payment: ScrubLink) -> None: - if "scrub" != payment.extra.get("tag"): - # not an scrub invoice +async def on_invoice_paid(payment: Payment) -> None: + # (avoid loops) + if "scrub" == payment.extra.get("tag"): + # already scrubbed return - if payment.extra.get("wh_status"): - # this webhook has already been sent + scrub_link = await get_scrub_by_wallet(payment.wallet_id) + + if not scrub_link: return - pay_link = await get_pay_link(payment.extra.get("link", -1)) - # PAY LNURLP AND LNADDRESS - - -async def mark_webhook_sent(payment: ScrubLink, status: int) -> None: - payment.extra["wh_status"] = status - - await core_db.execute( - """ - UPDATE apipayments SET extra = ? - WHERE hash = ? - """, - (json.dumps(payment.extra), payment.payment_hash), + from lnbits.core.views.api import ( + CreateLNURLData, + api_lnurlscan, + api_payments_pay_lnurl, ) + + wallet = await get_wallet(wallet_id=payment.wallet_id) + assert wallet + + # PAY LNURLP AND LNADDRESS + invoice = await api_lnurlscan(scrub_link.payoraddress) + invoice_data = CreateLNURLData( + callback = invoice["callback"], + description_hash = invoice["description_hash"], + amount = payment.amount + ) + print("INV", invoice_data, wallet) + + invoice_paid = await api_payments_pay_lnurl(data=invoice_data, wallet=wallet.adminkey) + print("PAID", invoice_paid) diff --git a/lnbits/extensions/scrub/views_api.py b/lnbits/extensions/scrub/views_api.py index 459473dbb..ff5b8e59d 100644 --- a/lnbits/extensions/scrub/views_api.py +++ b/lnbits/extensions/scrub/views_api.py @@ -97,7 +97,7 @@ async def api_scrub_create_or_update( @scrub_ext.delete("/api/v1/links/{link_id}") -async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)): +async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admin_key)): link = await get_scrub_link(link_id) if not link: