diff --git a/lnbits/extensions/nostrnip5/crud.py b/lnbits/extensions/nostrnip5/crud.py index b445e9912..12adc05a6 100644 --- a/lnbits/extensions/nostrnip5/crud.py +++ b/lnbits/extensions/nostrnip5/crud.py @@ -71,7 +71,7 @@ async def get_all_addresses(wallet_ids: Union[str, List[str]]) -> List[Address]: q = ",".join(["?"] * len(wallet_ids)) rows = await db.fetchall( f""" - SELECT a.* + SELECT a.* FROM nostrnip5.addresses a JOIN nostrnip5.domains d ON d.id = a.domain_id WHERE d.wallet IN ({q}) @@ -139,7 +139,7 @@ async def delete_domain(domain_id) -> bool: return True -async def delete_address(address_id) -> bool: +async def delete_address(address_id): await db.execute( """ DELETE FROM nostrnip5.addresses WHERE id = ? diff --git a/lnbits/extensions/nostrnip5/tasks.py b/lnbits/extensions/nostrnip5/tasks.py index 3cedc9f5b..30e8cec61 100644 --- a/lnbits/extensions/nostrnip5/tasks.py +++ b/lnbits/extensions/nostrnip5/tasks.py @@ -1,9 +1,9 @@ import asyncio -import json + +from loguru import logger from lnbits.core.models import Payment -from lnbits.helpers import urlsafe_short_hash -from lnbits.tasks import internal_invoice_queue, register_invoice_listener +from lnbits.tasks import register_invoice_listener from .crud import activate_address @@ -18,17 +18,18 @@ async def wait_for_paid_invoices(): async def on_invoice_paid(payment: Payment) -> None: + if not payment.extra: + return if payment.extra.get("tag") != "nostrnip5": - # not relevant return domain_id = payment.extra.get("domain_id") address_id = payment.extra.get("address_id") - print("Activating NOSTR NIP-05") - print(domain_id) - print(address_id) - - active = await activate_address(domain_id, address_id) + if domain_id and address_id: + logger.info("Activating NOSTR NIP-05") + logger.info(domain_id) + logger.info(address_id) + await activate_address(domain_id, address_id) return diff --git a/lnbits/extensions/nostrnip5/views.py b/lnbits/extensions/nostrnip5/views.py index 8e49a9f53..fc897d5da 100644 --- a/lnbits/extensions/nostrnip5/views.py +++ b/lnbits/extensions/nostrnip5/views.py @@ -1,8 +1,7 @@ from datetime import datetime from http import HTTPStatus -from fastapi import FastAPI, Request -from fastapi.params import Depends +from fastapi import Depends, Request from fastapi.templating import Jinja2Templates from starlette.exceptions import HTTPException from starlette.responses import HTMLResponse @@ -24,7 +23,7 @@ async def index(request: Request, user: User = Depends(check_user_exists)): @nostrnip5_ext.get("/signup/{domain_id}", response_class=HTMLResponse) -async def index(request: Request, domain_id: str): +async def signup(request: Request, domain_id: str): domain = await get_domain(domain_id) if not domain: @@ -43,7 +42,7 @@ async def index(request: Request, domain_id: str): @nostrnip5_ext.get("/rotate/{domain_id}/{address_id}", response_class=HTMLResponse) -async def index(request: Request, domain_id: str, address_id: str): +async def rotate(request: Request, domain_id: str, address_id: str): domain = await get_domain(domain_id) address = await get_address(domain_id, address_id) diff --git a/lnbits/extensions/nostrnip5/views_api.py b/lnbits/extensions/nostrnip5/views_api.py index bc4f72c7c..704a98c4b 100644 --- a/lnbits/extensions/nostrnip5/views_api.py +++ b/lnbits/extensions/nostrnip5/views_api.py @@ -1,10 +1,8 @@ import re from http import HTTPStatus -from typing import Optional from bech32 import bech32_decode, convertbits -from fastapi import Query, Request, Response -from fastapi.params import Depends +from fastapi import Depends, Query, Response from loguru import logger from starlette.exceptions import HTTPException @@ -38,7 +36,10 @@ async def api_domains( ): wallet_ids = [wallet.wallet.id] if all_wallets: - wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids + user = await get_user(wallet.wallet.user) + if not user: + return [] + wallet_ids = user.wallet_ids return [domain.dict() for domain in await get_domains(wallet_ids)] @@ -49,13 +50,20 @@ async def api_addresses( ): wallet_ids = [wallet.wallet.id] if all_wallets: - wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids + user = await get_user(wallet.wallet.user) + if not user: + return [] + wallet_ids = user.wallet_ids return [address.dict() for address in await get_all_addresses(wallet_ids)] -@nostrnip5_ext.get("/api/v1/domain/{domain_id}", status_code=HTTPStatus.OK) -async def api_invoice(domain_id: str, wallet: WalletTypeInfo = Depends(get_key_type)): +@nostrnip5_ext.get( + "/api/v1/domain/{domain_id}", + status_code=HTTPStatus.OK, + dependencies=[Depends(get_key_type)], +) +async def api_invoice(domain_id: str): domain = await get_domain(domain_id) if not domain: raise HTTPException( @@ -104,11 +112,11 @@ async def api_address_delete( @nostrnip5_ext.post( "/api/v1/domain/{domain_id}/address/{address_id}/activate", status_code=HTTPStatus.OK, + dependencies=[Depends(require_admin_key)], ) async def api_address_activate( domain_id: str, address_id: str, - wallet: WalletTypeInfo = Depends(require_admin_key), ): await activate_address(domain_id, address_id) @@ -126,9 +134,11 @@ async def api_address_rotate( ): if post_data.pubkey.startswith("npub"): - hrp, data = bech32_decode(post_data.pubkey) - decoded_data = convertbits(data, 5, 8, False) - post_data.pubkey = bytes(decoded_data).hex() + _, data = bech32_decode(post_data.pubkey) + if data: + decoded_data = convertbits(data, 5, 8, False) + if decoded_data: + post_data.pubkey = bytes(decoded_data).hex() if len(bytes.fromhex(post_data.pubkey)) != 32: raise HTTPException( @@ -173,10 +183,12 @@ async def api_address_create( status_code=HTTPStatus.NOT_FOUND, detail="Local part already exists." ) - if post_data.pubkey.startswith("npub"): - hrp, data = bech32_decode(post_data.pubkey) - decoded_data = convertbits(data, 5, 8, False) - post_data.pubkey = bytes(decoded_data).hex() + if post_data and post_data.pubkey.startswith("npub"): + _, data = bech32_decode(post_data.pubkey) + if data: + decoded_data = convertbits(data, 5, 8, False) + if decoded_data: + post_data.pubkey = bytes(decoded_data).hex() if len(bytes.fromhex(post_data.pubkey)) != 32: raise HTTPException( @@ -233,15 +245,17 @@ async def api_get_nostr_json( output = {} for address in addresses: - local_part = address.get("local_part").lower() + local_part = address.get("local_part") + if not local_part: + continue if address.get("active") == False: continue - if name and name.lower() != local_part: + if name and name.lower() != local_part.lower(): continue - output[local_part] = address.get("pubkey") + output[local_part.lower()] = address.get("pubkey") response.headers["Access-Control-Allow-Origin"] = "*" response.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS" diff --git a/pyproject.toml b/pyproject.toml index 573eef1be..96a066e53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,7 +103,6 @@ exclude = """(?x)( | ^lnbits/extensions/lnurldevice. | ^lnbits/extensions/lnurlp. | ^lnbits/extensions/lnurlpayout. - | ^lnbits/extensions/nostrnip5. | ^lnbits/extensions/offlineshop. | ^lnbits/extensions/paywall. | ^lnbits/extensions/satspay.