Modernize the entire customer-merchant communication layer from deprecated NIP-04 (kind 4, AES-256-CBC) to NIP-17 private direct messages using NIP-44 v2 encryption (ChaCha20 + HMAC-SHA256) and NIP-59 gift wrapping (rumor/seal/gift-wrap protocol). No backwards compatibility retained. New modules: - nostr/nip44.py: NIP-44 v2 encryption verified against official spec vectors - nostr/nip59.py: NIP-59 gift wrap with wrap/unwrap convenience functions - tests/: 44 unit tests for NIP-44 and NIP-59 Key changes: - Subscription filters: kind 4 → kind 1059 gift wraps - Message handler: _handle_nip04_message → _handle_gift_wrap (unwrap + route) - send_dm/reply_to_structured_dm: NIP-59 gift wrap to recipient + self-archive - Merchant model: removed NIP-04 crypto methods (decrypt/encrypt/build_dm_event) - helpers.py: removed NIP-04 functions, kept Schnorr signing + key normalization - views_api.py: consolidated DM sending through send_dm() service function Reliability improvements: - Event deduplication via bounded LRU set in NostrClient - Subscription health monitor (resubscribes after 120s of silence) - Preserved 5-minute lenient time window from prior work Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
import asyncio
|
|
|
|
from fastapi import APIRouter
|
|
from lnbits.db import Database
|
|
from lnbits.helpers import template_renderer
|
|
from lnbits.tasks import create_permanent_unique_task
|
|
from loguru import logger
|
|
|
|
from .nostr.nostr_client import NostrClient
|
|
|
|
db = Database("ext_nostrmarket")
|
|
|
|
nostrmarket_ext: APIRouter = APIRouter(prefix="/nostrmarket", tags=["nostrmarket"])
|
|
|
|
nostrmarket_static_files = [
|
|
{
|
|
"path": "/nostrmarket/static",
|
|
"name": "nostrmarket_static",
|
|
}
|
|
]
|
|
|
|
|
|
def nostrmarket_renderer():
|
|
return template_renderer(["nostrmarket/templates"])
|
|
|
|
|
|
nostr_client: NostrClient = NostrClient()
|
|
|
|
|
|
from .tasks import ( # noqa
|
|
subscription_health_monitor,
|
|
wait_for_nostr_events,
|
|
wait_for_paid_invoices,
|
|
)
|
|
from .views import * # noqa
|
|
from .views_api import * # noqa
|
|
|
|
scheduled_tasks: list[asyncio.Task] = []
|
|
|
|
|
|
async def nostrmarket_stop():
|
|
for task in scheduled_tasks:
|
|
try:
|
|
task.cancel()
|
|
except Exception as ex:
|
|
logger.warning(ex)
|
|
|
|
await nostr_client.stop()
|
|
|
|
|
|
def nostrmarket_start():
|
|
|
|
async def _subscribe_to_nostr_client():
|
|
# wait for 'nostrclient' extension to initialize
|
|
await asyncio.sleep(10)
|
|
await nostr_client.run_forever()
|
|
|
|
async def _wait_for_nostr_events():
|
|
# wait for this extension to initialize
|
|
await asyncio.sleep(15)
|
|
await wait_for_nostr_events(nostr_client)
|
|
|
|
task1 = create_permanent_unique_task(
|
|
"ext_nostrmarket_paid_invoices", wait_for_paid_invoices
|
|
)
|
|
task2 = create_permanent_unique_task(
|
|
"ext_nostrmarket_subscribe_to_nostr_client", _subscribe_to_nostr_client
|
|
)
|
|
task3 = create_permanent_unique_task(
|
|
"ext_nostrmarket_wait_for_events", _wait_for_nostr_events
|
|
)
|
|
|
|
async def _health_monitor():
|
|
# start after the subscription is active
|
|
await asyncio.sleep(20)
|
|
await subscription_health_monitor(nostr_client)
|
|
|
|
task4 = create_permanent_unique_task(
|
|
"ext_nostrmarket_health_monitor", _health_monitor
|
|
)
|
|
scheduled_tasks.extend([task1, task2, task3, task4])
|