From 3aee76be39ec48398ddf7ffa93623f7d7c48f58f Mon Sep 17 00:00:00 2001 From: Gene Takavic Date: Fri, 16 Dec 2022 11:09:10 +0100 Subject: [PATCH] reducing the PR --- lnbits/core/services.py | 32 +------------------ lnbits/extensions/boltcards/crud.py | 12 ++----- lnbits/extensions/boltcards/lnurl.py | 16 ---------- lnbits/extensions/boltcards/migrations.py | 8 ----- lnbits/extensions/boltcards/models.py | 2 -- .../extensions/boltcards/static/js/index.js | 4 +-- lnbits/extensions/boltcards/tasks.py | 21 +----------- .../boltcards/templates/boltcards/index.html | 10 ------ lnbits/extensions/boltcards/views_api.py | 16 ---------- lnbits/extensions/withdraw/lnurl.py | 30 ++++++++++------- 10 files changed, 23 insertions(+), 128 deletions(-) diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 4b6cc8e4..5d993b4c 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -2,7 +2,7 @@ import asyncio import json from binascii import unhexlify from io import BytesIO -from typing import Dict, Optional, Tuple, Union +from typing import Dict, Optional, Tuple from urllib.parse import parse_qs, urlparse import httpx @@ -102,7 +102,6 @@ async def pay_invoice( extra: Optional[Dict] = None, description: str = "", conn: Optional[Connection] = None, - webhook: Optional[Union[str, tuple]] = None, ) -> str: """ Pay a Lightning invoice. @@ -232,35 +231,6 @@ async def pay_invoice( f"didn't receive checking_id from backend, payment may be stuck in database: {temp_id}" ) - additionals = None - if type(webhook) is str: - webhook_url = webhook - elif type(webhook) is tuple: - webhook_url = webhook[0] - additionals = webhook[1] - else: - webhook_url = None - - if webhook_url: - async with httpx.AsyncClient() as client: - try: - json = { - "payment_hash": invoice.payment_hash, - "payment_request": payment_request, - "amount": int(invoice.amount_msat / 1000), - } - if type(additionals) is dict: - json.update(additionals) - - r = await client.post( - webhook_url, - json=json, - timeout=40, - ) - except Exception as exc: - # webhook fails shouldn't cause the lnurlw to fail since invoice is already paid - logger.error("Caught exception when dispatching webhook url:", exc) - return invoice.payment_hash diff --git a/lnbits/extensions/boltcards/crud.py b/lnbits/extensions/boltcards/crud.py index 346b5522..f15deb69 100644 --- a/lnbits/extensions/boltcards/crud.py +++ b/lnbits/extensions/boltcards/crud.py @@ -27,10 +27,9 @@ async def create_card(data: CreateCardData, wallet_id: str) -> Card: k0, k1, k2, - otp, - webhook_url + otp ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( card_id, @@ -46,7 +45,6 @@ async def create_card(data: CreateCardData, wallet_id: str) -> Card: data.k1, data.k2, secrets.token_hex(16), - data.webhook_url, ), ) card = await get_card(card_id) @@ -179,9 +177,6 @@ async def get_hit(hit_id: str) -> Optional[Hit]: async def get_hits(cards_ids: Union[str, List[str]]) -> List[Hit]: - if len(cards_ids) == 0: - return [] - q = ",".join(["?"] * len(cards_ids)) rows = await db.fetchall( f"SELECT * FROM boltcards.hits WHERE card_id IN ({q})", (*cards_ids,) @@ -276,9 +271,6 @@ async def get_refund(refund_id: str) -> Optional[Refund]: async def get_refunds(hits_ids: Union[str, List[str]]) -> List[Refund]: - if len(hits_ids) == 0: - return [] - q = ",".join(["?"] * len(hits_ids)) rows = await db.fetchall( f"SELECT * FROM boltcards.refunds WHERE hit_id IN ({q})", (*hits_ids,) diff --git a/lnbits/extensions/boltcards/lnurl.py b/lnbits/extensions/boltcards/lnurl.py index cd4c6ba4..7588f56e 100644 --- a/lnbits/extensions/boltcards/lnurl.py +++ b/lnbits/extensions/boltcards/lnurl.py @@ -112,27 +112,11 @@ async def lnurl_callback( invoice = bolt11.decode(pr) hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000)) try: - webhook = ( - ( - card.webhook_url, - { - "notification": "card_payment", - "card_external_id": card.external_id, - "card_name": card.card_name, - "card_otp": card.otp[ - -16: - ], # actually only half of the OTP is sent (full otp reveals the keys) - }, - ) - if card.webhook_url - else None - ) await pay_invoice( wallet_id=card.wallet, payment_request=pr, max_sat=card.tx_limit, extra={"tag": "boltcard", "tag": hit.id}, - webhook=webhook, ) return {"status": "OK"} diff --git a/lnbits/extensions/boltcards/migrations.py b/lnbits/extensions/boltcards/migrations.py index 64bb2dc9..9609e0c3 100644 --- a/lnbits/extensions/boltcards/migrations.py +++ b/lnbits/extensions/boltcards/migrations.py @@ -58,11 +58,3 @@ async def m001_initial(db): ); """ ) - - -async def m002_add_webhook(db): - await db.execute( - """ - ALTER TABLE boltcards.cards ADD COLUMN webhook_url TEXT NOT NULL DEFAULT ''; - """ - ) diff --git a/lnbits/extensions/boltcards/models.py b/lnbits/extensions/boltcards/models.py index 8e6f77c9..47ca1df0 100644 --- a/lnbits/extensions/boltcards/models.py +++ b/lnbits/extensions/boltcards/models.py @@ -30,7 +30,6 @@ class Card(BaseModel): prev_k1: str prev_k2: str otp: str - webhook_url: str time: int def from_row(cls, row: Row) -> "Card": @@ -57,7 +56,6 @@ class CreateCardData(BaseModel): prev_k0: str = Query(ZERO_KEY) prev_k1: str = Query(ZERO_KEY) prev_k2: str = Query(ZERO_KEY) - webhook_url: str = Query(...) class Hit(BaseModel): diff --git a/lnbits/extensions/boltcards/static/js/index.js b/lnbits/extensions/boltcards/static/js/index.js index 1f32ad44..880a555e 100644 --- a/lnbits/extensions/boltcards/static/js/index.js +++ b/lnbits/extensions/boltcards/static/js/index.js @@ -23,7 +23,6 @@ new Vue({ cardDialog: { show: false, data: { - webhook_url: '', counter: 1, k0: '', k1: '', @@ -273,8 +272,7 @@ new Vue({ k1: card.k1, k2: card.k2, k3: card.k1, - k4: card.k2, - webhook_url: card.webhook_url + k4: card.k2 } this.qrCodeDialog.data_wipe = JSON.stringify({ action: 'wipe', diff --git a/lnbits/extensions/boltcards/tasks.py b/lnbits/extensions/boltcards/tasks.py index c5a9ac9c..7d5d7407 100644 --- a/lnbits/extensions/boltcards/tasks.py +++ b/lnbits/extensions/boltcards/tasks.py @@ -9,7 +9,7 @@ from lnbits.core.models import Payment from lnbits.helpers import get_current_extension_name from lnbits.tasks import register_invoice_listener -from .crud import create_refund, get_card, get_hit +from .crud import create_refund, get_hit async def wait_for_paid_invoices(): @@ -36,25 +36,6 @@ async def on_invoice_paid(payment: Payment) -> None: ) await mark_webhook_sent(payment, 1) - card = await get_card(hit.card_id) - if card.webhook_url: - async with httpx.AsyncClient() as client: - try: - r = await client.post( - card.webhook_url, - json={ - "notification": "card_refund", - "payment_hash": payment.payment_hash, - "payment_request": payment.bolt11, - "card_external_id": card.external_id, - "card_name": card.card_name, - "amount": int(payment.amount / 1000), - }, - timeout=40, - ) - except Exception as exc: - logger.error("Caught exception when dispatching webhook url:", exc) - async def mark_webhook_sent(payment: Payment, status: int) -> None: payment.extra["wh_status"] = status diff --git a/lnbits/extensions/boltcards/templates/boltcards/index.html b/lnbits/extensions/boltcards/templates/boltcards/index.html index aefb22c6..61aa6fec 100644 --- a/lnbits/extensions/boltcards/templates/boltcards/index.html +++ b/lnbits/extensions/boltcards/templates/boltcards/index.html @@ -324,14 +324,6 @@ >Zero if you don't know. - - Lock key: {{ qrCodeDialog.data.k0 }}
Meta key: {{ qrCodeDialog.data.k1 }}
File key: {{ qrCodeDialog.data.k2 }}
- Notification webhook: {{ qrCodeDialog.data.webhook_url - }}

Always backup all keys that you're trying to write on the card. Without diff --git a/lnbits/extensions/boltcards/views_api.py b/lnbits/extensions/boltcards/views_api.py index c2d5bff3..80679556 100644 --- a/lnbits/extensions/boltcards/views_api.py +++ b/lnbits/extensions/boltcards/views_api.py @@ -113,22 +113,6 @@ async def enable_card( return card.dict() -@boltcards_ext.post("/api/v1/disablecard") -async def disble_card_with_otp(a): - if len(a) < 16: - raise HTTPException(detail="Invalid OTP.", status_code=HTTPStatus.BAD_REQUEST) - card = await get_card_by_otp(a, half=True) - if not card: - raise HTTPException(detail="No card found.", status_code=HTTPStatus.NOT_FOUND) - - new_otp = secrets.token_hex(16) - await update_card_otp(new_otp, card.id) - - card = await enable_disable_card(enable=False, id=card.id) - - return {"status": "OK"} - - @boltcards_ext.delete("/api/v1/cards/{card_id}") async def api_card_delete(card_id, wallet: WalletTypeInfo = Depends(require_admin_key)): card = await get_card(card_id) diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index 2ae47f6b..5737e54f 100644 --- a/lnbits/extensions/withdraw/lnurl.py +++ b/lnbits/extensions/withdraw/lnurl.py @@ -3,6 +3,7 @@ import traceback from datetime import datetime from http import HTTPStatus +import httpx import shortuuid # type: ignore from fastapi import HTTPException from fastapi.param_functions import Query @@ -132,24 +133,29 @@ async def api_lnurl_callback( payment_request = pr - webhook = ( - ( - link.webhook_url, - { - "lnurlw": link.id, - }, - ) - if link.webhook_url - else None - ) - await pay_invoice( + payment_hash = await pay_invoice( wallet_id=link.wallet, payment_request=payment_request, max_sat=link.max_withdrawable, extra={"tag": "withdraw"}, - webhook=webhook, ) + if link.webhook_url: + async with httpx.AsyncClient() as client: + try: + r = await client.post( + link.webhook_url, + json={ + "payment_hash": payment_hash, + "payment_request": payment_request, + "lnurlw": link.id, + }, + timeout=40, + ) + except Exception as exc: + # webhook fails shouldn't cause the lnurlw to fail since invoice is already paid + logger.error("Caught exception when dispatching webhook url:", exc) + return {"status": "OK"} except Exception as e: