Skip to content

Commit

Permalink
docs: Add docstrings for LnAddressCubit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Jan 29, 2025
1 parent 89df12f commit 72701f4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/cubit/ln_address/ln_address_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class LnAddressCubit extends Cubit<LnAddressState> {
required this.webhookService,
}) : super(const LnAddressState());

/// Sets up or updates the Lightning Address.
///
/// - If [baseUsername] is provided, the function updates the Lightning Address username.
/// - Otherwise, it initializes a new Lightning Address or refreshes an existing one.
Future<void> setupLightningAddress({String? baseUsername}) async {
final bool isUpdating = baseUsername != null;
final String actionMessage =
Expand Down Expand Up @@ -97,6 +101,11 @@ class LnAddressCubit extends Cubit<LnAddressState> {
errorMessage: errorMessage,
);
}

/// Registers or updates an LNURL webhook for a Lightning Address.
///
/// - If [baseUsername] is provided, it updates the existing registration.
/// - Otherwise, it determines a suitable username and registers a new webhook.
Future<RegisterLnurlPayResponse> _setupAndRegisterLnAddress({String? baseUsername}) async {
final WalletInfo walletInfo = await _getWalletInfo();
final String webhookUrl = await _setupWebhook(walletInfo.pubkey);
Expand All @@ -123,6 +132,9 @@ class LnAddressCubit extends Cubit<LnAddressState> {
return walletInfo;
}

/// Sets up a webhook for the given public key.
/// - Generates a new webhook URL, unregisters any existing webhook if needed,
/// - Registers the new webhook, and stores the webhook URL in preferences.
Future<String> _setupWebhook(String pubKey) async {
_logger.info('Setting up webhook');
final String webhookUrl = await webhookService.generateWebhookUrl();
Expand All @@ -132,6 +144,7 @@ class LnAddressCubit extends Cubit<LnAddressState> {
return webhookUrl;
}

/// Checks if there is an existing webhook URL and unregisters it if different from the provided one.
Future<void> _unregisterExistingWebhookIfNeeded({
required String pubKey,
required String webhookUrl,
Expand All @@ -144,8 +157,10 @@ class LnAddressCubit extends Cubit<LnAddressState> {
}
}

/// Unregisters a webhook for a given public key.
Future<void> _unregisterWebhook(String webhookUrl, String pubKey) async {
final int time = DateTime.now().millisecondsSinceEpoch ~/ 1000;

final String message = '$time-$webhookUrl';
final String signature = await _signMessage(message);

Expand All @@ -158,6 +173,7 @@ class LnAddressCubit extends Cubit<LnAddressState> {
await lnAddressService.unregister(pubKey, invalidateWebhookRequest);
}

/// Signs the given message with the private key.
Future<String> _signMessage(String message) async {
_logger.info('Signing message: $message');

Expand All @@ -173,6 +189,10 @@ class LnAddressCubit extends Cubit<LnAddressState> {
return signMessageRes.signature;
}

/// Resolves the appropriate username for LNURL registration.
///
/// - If the webhook is not yet registered, it utilizes default profile name as username.
/// - If the webhook is already registered, it retrieves the stored username from [BreezPreferences].
Future<String?> _resolveUsername() async {
final bool isLnUrlWebhookRegistered = await breezPreferences.isLnUrlWebhookRegistered;

Expand All @@ -189,6 +209,7 @@ class LnAddressCubit extends Cubit<LnAddressState> {
return storedUsername;
}

/// Signs a webhook request message for authentication and validation purposes.
Future<String> _generateWebhookSignature(int time, String webhookUrl, String? username) async {
_logger.info('Generating webhook signature');
final String usernameComponent = username?.isNotEmpty == true ? '-$username' : '';
Expand All @@ -198,6 +219,10 @@ class LnAddressCubit extends Cubit<LnAddressState> {
return signature;
}

/// Registers an LNURL webhook with the provided public key and request.
///
/// - Saves the username to [BreezPreferences] if present and
/// - Sets webhook as registered on [BreezPreferences] if succeeds
Future<RegisterLnurlPayResponse> _registerLnurlWebhook({
required String pubKey,
required RegisterLnurlPayRequest request,
Expand Down

0 comments on commit 72701f4

Please sign in to comment.