Skip to content

Commit

Permalink
Handle LNURL-Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Jul 12, 2024
1 parent 3e82107 commit f20b322
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/bloc/account/account_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,14 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
}
}

Future<liquid_sdk.LnUrlCallbackStatus> lnurlAuth({
required liquid_sdk.LnUrlAuthRequestData reqData,
}) async {
try {
return await _liquidSdk.instance!.lnurlAuth(reqData: reqData);
} catch (e) {
_log.severe("lnurlAuth error", e);
rethrow;
}
}
}
3 changes: 3 additions & 0 deletions lib/handlers/input_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:l_breez/bloc/input/input_state.dart';
import 'package:l_breez/handlers/handler.dart';
import 'package:l_breez/handlers/handler_context_provider.dart';
import 'package:l_breez/models/invoice.dart';
import 'package:l_breez/routes/lnurl/auth/lnurl_auth_handler.dart';
import 'package:l_breez/routes/lnurl/payment/lnurl_payment_handler.dart';
import 'package:l_breez/routes/lnurl/withdraw/lnurl_withdraw_handler.dart';
import 'package:l_breez/utils/exceptions.dart';
Expand Down Expand Up @@ -91,6 +92,8 @@ class InputHandler extends Handler {
handlePayRequest(context, firstPaymentItemKey, inputState.data);
} else if (inputState is LnUrlWithdrawInputState) {
handleWithdrawRequest(context, inputState.data);
} else if (inputState is LnUrlAuthInputState) {
handleAuthRequest(context, inputState.data);
} else if (inputState is LnUrlErrorInputState) {
throw inputState.data.reason;
} else if (inputState is BitcoinAddressInputState) {
Expand Down
68 changes: 68 additions & 0 deletions lib/routes/lnurl/auth/lnurl_auth_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:breez_translations/breez_translations_locales.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';
import 'package:l_breez/bloc/account/account_bloc.dart';
import 'package:l_breez/routes/lnurl/auth/login_text.dart';
import 'package:l_breez/routes/lnurl/widgets/lnurl_page_result.dart';
import 'package:l_breez/widgets/error_dialog.dart';
import 'package:l_breez/widgets/loader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:logging/logging.dart';

final _log = Logger("HandleLNURLAuthRequest");

Future<LNURLPageResult?> handleAuthRequest(
BuildContext context,
LnUrlAuthRequestData reqData,
) async {
return promptAreYouSure(context, null, LoginText(domain: reqData.domain)).then(
(permitted) async {
if (permitted == true) {
final texts = context.texts();
final navigator = Navigator.of(context);
final loaderRoute = createLoaderRoute(context);
navigator.push(loaderRoute);
try {
final resp = await context.read<AccountBloc>().lnurlAuth(reqData: reqData);
if (resp is LnUrlCallbackStatus_Ok) {
_log.info("LNURL auth success");
return const LNURLPageResult(protocol: LnUrlProtocol.Auth);
} else if (resp is LnUrlCallbackStatus_ErrorStatus) {
_log.info("LNURL auth failed: ${resp.data.reason}");
return LNURLPageResult(protocol: LnUrlProtocol.Auth, error: resp.data.reason);
} else {
_log.warning("Unknown response from lnurlAuth: $resp");
return LNURLPageResult(
protocol: LnUrlProtocol.Auth,
error: texts.lnurl_payment_page_unknown_error,
);
}
} catch (e) {
_log.warning("Error authenticating LNURL auth", e);
if (loaderRoute.isActive) {
navigator.removeRoute(loaderRoute);
}
return LNURLPageResult(protocol: LnUrlProtocol.Auth, error: e);
} finally {
if (loaderRoute.isActive) {
navigator.removeRoute(loaderRoute);
}
}
}
return Future.value();
},
);
}

void handleLNURLAuthPageResult(BuildContext context, LNURLPageResult result) {
if (result.hasError) {
_log.info("Handle LNURL auth page result with error '${result.error}'");
promptError(
context,
context.texts().lnurl_webview_error_title,
Text(result.errorMessage),
okFunc: () => Navigator.of(context).pop(),
);
throw result.error!;
}
}
30 changes: 30 additions & 0 deletions lib/routes/lnurl/auth/login_text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:breez_translations/breez_translations_locales.dart';
import 'package:flutter/material.dart';

class LoginText extends StatelessWidget {
final String domain;

const LoginText({super.key, required this.domain});

@override
Widget build(BuildContext context) {
final texts = context.texts();
final themeData = Theme.of(context);
return RichText(
text: TextSpan(
style: themeData.dialogTheme.contentTextStyle,
text: texts.handler_lnurl_login_anonymously,
children: [
TextSpan(
text: domain,
style: themeData.dialogTheme.contentTextStyle!.copyWith(fontWeight: FontWeight.bold),
),
TextSpan(
text: "?",
style: themeData.dialogTheme.contentTextStyle,
),
],
),
);
}
}
4 changes: 4 additions & 0 deletions lib/routes/lnurl/lnurl_invoice_delegate.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:l_breez/routes/lnurl/auth/lnurl_auth_handler.dart';
import 'package:l_breez/routes/lnurl/payment/lnurl_payment_handler.dart';
import 'package:l_breez/routes/lnurl/withdraw/lnurl_withdraw_handler.dart';
import 'package:flutter/material.dart';
Expand All @@ -16,6 +17,9 @@ void handleLNURLPageResult(BuildContext context, LNURLPageResult result) {
case LnUrlProtocol.Withdraw:
handleLNURLWithdrawPageResult(context, result);
break;
case LnUrlProtocol.Auth:
handleLNURLAuthPageResult(context, result);
break;
default:
break;
}
Expand Down

0 comments on commit f20b322

Please sign in to comment.