From 078a2c724a0179f3b43a277d7e0109140e90ca44 Mon Sep 17 00:00:00 2001 From: blackcoffeexbt <87530449+blackcoffeexbt@users.noreply.github.com> Date: Tue, 28 Jul 2026 10:55:14 +0100 Subject: [PATCH] Fix LNURLp backwards compatibility without importing extension code --- lnbits/core/services/lightning_address.py | 21 ++++++++++++++------- lnbits/middleware.py | 20 ++++++++------------ tests/api/test_wallet_api.py | 4 ++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lnbits/core/services/lightning_address.py b/lnbits/core/services/lightning_address.py index 87d424d7e..38ec77a5c 100644 --- a/lnbits/core/services/lightning_address.py +++ b/lnbits/core/services/lightning_address.py @@ -18,7 +18,7 @@ from sqlalchemy.exc import OperationalError from lnbits.core.db import db from lnbits.core.models.wallets import Wallet -from lnbits.db import Connection +from lnbits.db import Connection, Database from lnbits.exceptions import PaymentError from lnbits.settings import settings @@ -26,6 +26,7 @@ MAX_SENDABLE_MSAT = 2_100_000_000_000_000_000 COMMENT_ALLOWED = 799 LIGHTNING_ADDRESS_REGEX = re.compile(r"^[a-z0-9_.-]{1,210}$") _RANDOM = SystemRandom() +_LEGACY_LNURLP_DB = Database("ext_lnurlp") _PARTICIPLES = [ "asking", @@ -115,11 +116,17 @@ async def _core_address_exists_for_other_wallet( return bool(row) -async def _pay_links_address_exists(local_part: str) -> bool: +async def legacy_lnurlp_address_exists(local_part: str) -> bool: try: - from lnbits.extensions.lnurlp.crud import get_pay_link_by_username - - return await get_pay_link_by_username(local_part) is not None + row: Any = await _LEGACY_LNURLP_DB.fetchone( + """ + SELECT 1 FROM lnurlp.pay_links + WHERE username = :username + LIMIT 1 + """, + {"username": local_part}, + ) + return row is not None except OperationalError: return False except Exception: @@ -133,7 +140,7 @@ async def generate_lightning_address_local_part( local_part = _generate_local_part() if await _core_address_exists(local_part, conn): continue - if await _pay_links_address_exists(local_part): + if await legacy_lnurlp_address_exists(local_part): continue return local_part raise ValueError("Could not generate a unique wallet lightning address.") @@ -182,7 +189,7 @@ async def validate_lightning_address_local_part( raise ValueError("Lightning Address contains a reserved word.") if await _core_address_exists_for_other_wallet(local_part, wallet.id, conn): raise ValueError("Lightning Address is already taken.") - if await _pay_links_address_exists(local_part): + if await legacy_lnurlp_address_exists(local_part): raise ValueError("Lightning Address is already taken.") return local_part diff --git a/lnbits/middleware.py b/lnbits/middleware.py index 66c1699db..bae246070 100644 --- a/lnbits/middleware.py +++ b/lnbits/middleware.py @@ -123,22 +123,18 @@ class ExtensionsRedirectMiddleware: username = path_parts[2].lower() base_username = username.partition("+")[0] - try: - from lnbits.extensions.lnurlp.crud import get_pay_link_by_username - - if await get_pay_link_by_username(username): - return False - if base_username != username and await get_pay_link_by_username( - base_username - ): - return False - except Exception: - return False - from lnbits.core.services.lightning_address import ( get_wallet_by_lightning_address, + legacy_lnurlp_address_exists, ) + if await legacy_lnurlp_address_exists(username): + return False + if base_username != username and await legacy_lnurlp_address_exists( + base_username + ): + return False + return await get_wallet_by_lightning_address(base_username) is not None diff --git a/tests/api/test_wallet_api.py b/tests/api/test_wallet_api.py index 221a546cc..d17f0cc29 100644 --- a/tests/api/test_wallet_api.py +++ b/tests/api/test_wallet_api.py @@ -208,7 +208,7 @@ async def test_wallet_api_custom_lightning_address_owner_rules( assert invalid.status_code == 400 mocker.patch( - "lnbits.core.services.lightning_address._pay_links_address_exists", + "lnbits.core.services.lightning_address.legacy_lnurlp_address_exists", mocker.AsyncMock(return_value=True), ) conflict = await http_client.patch( @@ -219,7 +219,7 @@ async def test_wallet_api_custom_lightning_address_owner_rules( assert conflict.status_code == 400 mocker.patch( - "lnbits.core.services.lightning_address._pay_links_address_exists", + "lnbits.core.services.lightning_address.legacy_lnurlp_address_exists", mocker.AsyncMock(return_value=False), ) updated = await http_client.patch(