From c2e58fa1b438afcc9b4f9fa92b9ac7e666d2e7e6 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 22 Mar 2023 17:49:43 +0100 Subject: [PATCH] generate new keys --- __init__.py | 20 ++++++++++++++++---- lnurl.py | 14 ++++++++++---- tasks.py | 21 ++++++++++----------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/__init__.py b/__init__.py index 4ef4c7d..7a466ba 100644 --- a/__init__.py +++ b/__init__.py @@ -7,6 +7,18 @@ from fastapi.staticfiles import StaticFiles from lnbits.db import Database from lnbits.helpers import template_renderer from lnbits.tasks import catch_everything_and_restart +from loguru import logger + +try: + from ..nostrclient.nostr.event import Event + from ..nostrclient.nostr.key import PrivateKey, PublicKey + + nostrclient_present = True + nostr_privatekey = PrivateKey() + nostr_publickey: PublicKey = nostr_privatekey.public_key + logger.debug(f"LNURLP Zaps Nostr pubkey: {nostr_publickey.hex()}") +except ImportError: + nostrclient_present = False db = Database("ext_lnurlp") @@ -19,10 +31,10 @@ lnurlp_static_files = [ ] lnurlp_redirect_paths = [ - { - "from_path": "/.well-known/lnurlp", - "redirect_to_path": "/api/v1/well-known", - } + { + "from_path": "/.well-known/lnurlp", + "redirect_to_path": "/api/v1/well-known", + } ] scheduled_tasks: List[asyncio.Task] = [] diff --git a/lnurl.py b/lnurl.py index 3175a85..00ca97d 100644 --- a/lnurl.py +++ b/lnurl.py @@ -12,6 +12,13 @@ from .crud import increment_pay_link, get_pay_link, get_address_data from loguru import logger from urllib.parse import urlparse import json +from . import nostrclient_present, nostr_publickey + +if nostrclient_present: + try: + from ..nostrclient.nostr.key import PrivateKey, PublicKey + except ImportError: + nostrclient_present = False @lnurlp_ext.get( @@ -144,8 +151,7 @@ async def api_lnurl_response(request: Request, link_id, lnaddress=False): if link.comment_chars > 0: params["commentAllowed"] = link.comment_chars - params["allowsNostr"] = True - params[ - "nostrPubkey" - ] = "749b4d4dfc6b00a5e6c9a88d8a220c46c069ff8f027dcf312f040475e059554a" # private: de1af06647137d49b2277faa86f96effc94257a7b7efd6f5dcc52bea08a4746b + if nostrclient_present: + params["allowsNostr"] = True + params["nostrPubkey"] = nostr_publickey.hex() return params diff --git a/tasks.py b/tasks.py index 0526d62..e6abbeb 100644 --- a/tasks.py +++ b/tasks.py @@ -12,6 +12,14 @@ from websocket import WebSocketApp from lnbits.settings import settings from .crud import get_pay_link from threading import Thread +from . import nostrclient_present, nostr_privatekey + +if nostrclient_present: + try: + from ..nostrclient.nostr.event import Event + from ..nostrclient.nostr.key import PrivateKey, PublicKey + except ImportError: + nostrclient_present = False async def wait_for_paid_invoices(): @@ -67,9 +75,7 @@ async def on_invoice_paid(payment: Payment): # NIP-57 nostr = payment.extra.get("nostr") - if nostr: - from ..nostrclient.nostr.event import Event - from ..nostrclient.nostr.key import PrivateKey, PublicKey + if nostrclient_present and nostr: event_json = json.loads(nostr) @@ -79,13 +85,6 @@ async def on_invoice_paid(payment: Payment): ] return res[0] if res else None - private_key = PrivateKey( - bytes.fromhex( - "de1af06647137d49b2277faa86f96effc94257a7b7efd6f5dcc52bea08a4746b" - ) - ) - - p_tag = get_tag(event_json, "p") tags = [] for t in ["p", "e"]: tag = get_tag(event_json, t) @@ -96,7 +95,7 @@ async def on_invoice_paid(payment: Payment): zap_receipt = Event( kind=9735, tags=tags, content=payment.extra.get("comment") or "" ) - private_key.sign_event(zap_receipt) + nostr_privatekey.sign_event(zap_receipt) def send_event(_): logger.debug(f"Sending zap: {zap_receipt.to_message()}")