"""Lightning backend factory. Returns a mock or real LND backend based on ``settings.lightning_backend``. """ from __future__ import annotations import hashlib import logging import secrets from dataclasses import dataclass from config import settings logger = logging.getLogger(__name__) @dataclass class Invoice: """Minimal Lightning invoice representation.""" payment_hash: str payment_request: str amount_sats: int memo: str class MockBackend: """In-memory mock Lightning backend for development and testing.""" def create_invoice(self, amount_sats: int, memo: str = "") -> Invoice: """Create a fake invoice with a random payment hash.""" raw = secrets.token_bytes(32) payment_hash = hashlib.sha256(raw).hexdigest() payment_request = f"lnbc{amount_sats}mock{payment_hash[:20]}" logger.debug("Mock invoice: %s sats — %s", amount_sats, payment_hash[:12]) return Invoice( payment_hash=payment_hash, payment_request=payment_request, amount_sats=amount_sats, memo=memo, ) # Singleton — lazily created _backend: MockBackend | None = None def get_backend() -> MockBackend: """Return the configured Lightning backend (currently mock-only). Raises ``ValueError`` if an unsupported backend is requested. """ global _backend # noqa: PLW0603 if _backend is not None: return _backend kind = settings.lightning_backend if kind == "mock": _backend = MockBackend() elif kind == "lnd": # LND gRPC integration is on the roadmap — for now fall back to mock. logger.warning("LND backend not yet implemented — using mock") _backend = MockBackend() else: raise ValueError(f"Unknown lightning_backend: {kind!r}") logger.info("Lightning backend: %s", kind) return _backend