import 'dart:developer'; import 'package:nip01/nip01.dart' as nip01; import 'package:nip47/nip47.dart'; class CreateWalletConnectionUseCase { final WalletConnectionRepository _walletConnectionRepository; final InfoEventRepository _infoEventRepository; final RequestRepository _requestRepository; CreateWalletConnectionUseCase({ required WalletConnectionRepository walletConnectionRepository, required InfoEventRepository infoEventRepository, required RequestRepository requestRepository, }) : _walletConnectionRepository = walletConnectionRepository, _infoEventRepository = infoEventRepository, _requestRepository = requestRepository; Future execute({ required nip01.KeyPair walletServiceKeyPair, required List relays, BudgetRenewal budgetRenewal = BudgetRenewal.never, String? name, int? maxAmountSat, int? expiresAt, List? methods, List? notifications, List? customMethods, List? customNotifications, bool? isolated, List? categories, String? lud16, }) async { try { final relayUrls = relays.map((e) => e.toString()).toList(); final connectionUri = await _walletConnectionRepository.createConnection( walletServicePubkey: walletServiceKeyPair.publicKey, relays: relayUrls, budgetRenewal: budgetRenewal, name: name, maxAmountSat: maxAmountSat, expiresAt: expiresAt, methods: methods, notifications: notifications, customMethods: customMethods, customNotifications: customNotifications, isolated: isolated, categories: categories, lud16: lud16, ); await _requestRepository.subscribeToRequests( clientPubkey: connectionUri.clientPubkey, walletServiceKeyPair: walletServiceKeyPair, relayUrls: relayUrls, ); final infoEvent = InfoEvent( walletServicePubkey: walletServiceKeyPair.publicKey, methods: methods, notifications: notifications, customMethods: customMethods ?? [], customNotifications: customNotifications ?? [], ); try { await _infoEventRepository.publish( infoEvent, walletServicePrivateKey: walletServiceKeyPair.privateKey, relays: relayUrls, ); } catch (e) { log('Error publishing info event for walletServicePubkey ${connectionUri.walletServicePubkey}: $e'); } return connectionUri; } catch (e) { throw CreateWalletConnectionUseCaseException('$e'); } } } class CreateWalletConnectionUseCaseException implements Exception { final String message; CreateWalletConnectionUseCaseException(this.message); @override String toString() { return '[CreateWalletConnectionUseCase]: $message'; } }