diff --git a/lnbits/app.py b/lnbits/app.py index e432155d7..da8b058c8 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -16,7 +16,7 @@ from fastapi.staticfiles import StaticFiles from loguru import logger from lnbits.core.tasks import register_task_listeners -from lnbits.settings import WALLET, check_admin_settings, settings +from lnbits.settings import check_admin_settings, get_wallet_class, settings from .commands import migrate_databases from .core import core_app @@ -78,12 +78,15 @@ def create_app() -> FastAPI: async def check_funding_source() -> None: + # original_sigint_handler = signal.getsignal(signal.SIGINT) # def signal_handler(signal, frame): # logger.debug(f"SIGINT received, terminating LNbits.") # sys.exit(1) # signal.signal(signal.SIGINT, signal_handler) + + WALLET = get_wallet_class() while True: try: error_message, balance = await WALLET.status() diff --git a/lnbits/core/models.py b/lnbits/core/models.py index 216acafd8..d6a20a2a2 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -11,7 +11,7 @@ from pydantic import BaseModel from lnbits.db import Connection from lnbits.helpers import url_for -from lnbits.settings import WALLET +from lnbits.settings import get_wallet_class from lnbits.wallets.base import PaymentStatus @@ -163,6 +163,7 @@ class Payment(BaseModel): f"Checking {'outgoing' if self.is_out else 'incoming'} pending payment {self.checking_id}" ) + WALLET = get_wallet_class() if self.is_out: status = await WALLET.get_payment_status(self.checking_id) else: diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 23a5b315c..eeadcfcce 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -21,7 +21,7 @@ from lnbits.decorators import ( ) from lnbits.helpers import url_for, urlsafe_short_hash from lnbits.requestvars import g -from lnbits.settings import FAKE_WALLET, WALLET, settings +from lnbits.settings import FAKE_WALLET, get_wallet_class, settings from lnbits.wallets.base import PaymentResponse, PaymentStatus from . import db @@ -65,7 +65,7 @@ async def create_invoice( invoice_memo = None if description_hash else memo # use the fake wallet if the invoice is for internal use only - wallet = FAKE_WALLET if internal else WALLET + wallet = FAKE_WALLET if internal else get_wallet_class() ok, checking_id, payment_request, error_message = await wallet.create_invoice( amount=amount, @@ -193,6 +193,7 @@ async def pay_invoice( else: logger.debug(f"backend: sending payment {temp_id}") # actually pay the external invoice + WALLET = get_wallet_class() payment: PaymentResponse = await WALLET.pay_invoice( payment_request, fee_reserve_msat ) diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index c45c39769..f1a528d06 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -30,7 +30,7 @@ from lnbits.decorators import ( require_invoice_key, ) from lnbits.helpers import url_for, urlsafe_short_hash -from lnbits.settings import WALLET, settings +from lnbits.settings import get_wallet_class, settings from lnbits.utils.exchange_rates import ( currencies, fiat_amount_as_satoshis, @@ -682,7 +682,7 @@ async def api_auditor(wallet: WalletTypeInfo = Depends(get_key_type)): raise HTTPException( status_code=HTTPStatus.FORBIDDEN, detail="Not an admin user" ) - + WALLET = get_wallet_class() total_balance = await get_total_balance() error_message, node_balance = await WALLET.status() diff --git a/lnbits/extensions/admin/views.py b/lnbits/extensions/admin/views.py index 884aeb7e9..aa9330179 100644 --- a/lnbits/extensions/admin/views.py +++ b/lnbits/extensions/admin/views.py @@ -9,7 +9,7 @@ from starlette.responses import HTMLResponse from lnbits.core.models import User from lnbits.decorators import check_admin from lnbits.requestvars import g -from lnbits.settings import WALLET, settings +from lnbits.settings import get_wallet_class, settings from . import admin_ext, admin_renderer @@ -18,6 +18,7 @@ templates = Jinja2Templates(directory="templates") @admin_ext.get("/", response_class=HTMLResponse) async def index(request: Request, user: User = Depends(check_admin)): + WALLET = get_wallet_class() error, balance = await WALLET.status() return admin_renderer().TemplateResponse( diff --git a/lnbits/extensions/lndhub/views_api.py b/lnbits/extensions/lndhub/views_api.py index b2328c392..4503b53b2 100644 --- a/lnbits/extensions/lndhub/views_api.py +++ b/lnbits/extensions/lndhub/views_api.py @@ -12,7 +12,7 @@ from lnbits import bolt11 from lnbits.core.crud import delete_expired_invoices, get_payments from lnbits.core.services import create_invoice, pay_invoice from lnbits.decorators import WalletTypeInfo -from lnbits.settings import WALLET, settings +from lnbits.settings import get_wallet_class, settings from . import lndhub_ext from .decorators import check_wallet, require_admin_key @@ -175,6 +175,7 @@ async def lndhub_getuserinvoices( offset=offset, exclude_uncheckable=True, ): + WALLET = get_wallet_class() await invoice.set_pending( (await WALLET.get_invoice_status(invoice.checking_id)).pending ) diff --git a/lnbits/settings.py b/lnbits/settings.py index a0b1fa688..1ccf60a7b 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -229,6 +229,9 @@ async def check_admin_settings(): wallets_module = importlib.import_module("lnbits.wallets") -wallet_class = getattr(wallets_module, settings.lnbits_backend_wallet_class) -WALLET = wallet_class() FAKE_WALLET = getattr(wallets_module, "FakeWallet")() + + +def get_wallet_class(): + wallet_class = getattr(wallets_module, settings.lnbits_backend_wallet_class) + return wallet_class() diff --git a/lnbits/tasks.py b/lnbits/tasks.py index 94e43dcfd..c68b6f0b1 100644 --- a/lnbits/tasks.py +++ b/lnbits/tasks.py @@ -15,7 +15,7 @@ from lnbits.core.crud import ( get_standalone_payment, ) from lnbits.core.services import redeem_lnurl_withdraw -from lnbits.settings import WALLET +from lnbits.settings import get_wallet_class from .core import db @@ -79,6 +79,7 @@ async def webhook_handler(): """ Returns the webhook_handler for the selected wallet if present. Used by API. """ + WALLET = get_wallet_class() handler = getattr(WALLET, "webhook_listener", None) if handler: return await handler() @@ -108,6 +109,7 @@ async def invoice_listener(): Called by the app startup sequence. """ + WALLET = get_wallet_class() async for checking_id in WALLET.paid_invoices_stream(): logger.info("> got a payment notification", checking_id) asyncio.create_task(invoice_callback_dispatcher(checking_id))