Skip to content

Commit

Permalink
Integrate input parser
Browse files Browse the repository at this point in the history
closes #22
  • Loading branch information
erdemyerebasmaz committed Jun 27, 2024
1 parent 75b3a65 commit 4f8cba6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
36 changes: 6 additions & 30 deletions lib/bloc/input/input_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart' as liquid_sdk;
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';
import 'package:l_breez/bloc/input/input_data.dart';
import 'package:l_breez/bloc/input/input_printer.dart';
import 'package:l_breez/bloc/input/input_source.dart';
Expand Down Expand Up @@ -70,28 +70,10 @@ class InputBloc extends Cubit<InputState> {
// Emit an empty InputState with isLoading to display a loader on UI layer
emit(const InputState.loading());
try {
/*
final parsedInput = await parseInput(input: input.data);
final parsedInput = await parse(input: input.data);
return await _handleParsedInput(parsedInput, input.source);
*/
final parsedInput = parseInvoice(input: input.data);
final req = liquid_sdk.PrepareSendRequest(invoice: parsedInput.bolt11);
final resp = await ServiceInjector().liquidSDK.wallet!.prepareSendPayment(req: req);
// TODO: Liquid/FRB - Address BigInt & Int changes
return InputState.invoice(
Invoice(
bolt11: resp.invoice,
paymentHash: parsedInput.paymentHash,
description: parsedInput.description ?? "",
amountMsat: parsedInput.amountMsat ?? BigInt.zero,
expiry: parsedInput.expiry,
lspFee: resp.feesSat.toInt(),
),
input.source,
);
} catch (e) {
// TODO: Liquid - Revert back to "Failed to parse input" once InputParser is fully integrated into Liquid SDK
_log.severe("Failed to prepare Send Payment", e);
_log.severe("Failed to parse input", e);
return const InputState.empty();
}
});
Expand All @@ -101,10 +83,6 @@ class InputBloc extends Cubit<InputState> {
_log.info("handlePaymentRequest: $inputData source: $source");
final LNInvoice lnInvoice = inputData.invoice;

/*NodeState? nodeState = await _breezSDK.nodeInfo();
if (nodeState == null || nodeState.id == lnInvoice.payeePubkey) {
return const InputState.empty();
}*/
final invoice = Invoice(
bolt11: lnInvoice.bolt11,
paymentHash: lnInvoice.paymentHash,
Expand All @@ -115,8 +93,6 @@ class InputBloc extends Cubit<InputState> {
return InputState.invoice(invoice, source);
}

// TODO: Liquid - Implement input parser to parse bolt11 invoice - https://github.com/breez/breez-liquid-sdk/issues/232
// ignore: unused_element
Future<InputState> _handleParsedInput(InputType parsedInput, InputSource source) async {
_log.info("handleParsedInput: $source => ${inputTypeToString(parsedInput)}");
InputState result;
Expand All @@ -143,8 +119,8 @@ class InputBloc extends Cubit<InputState> {
return result;
}

liquid_sdk.LNInvoice parseInvoice({required String input}) {
_log.info("parseInvoice: $input");
return liquid_sdk.parseInvoice(input: input);
Future<InputType> parseInput({required String input}) async {
_log.info("parseInput: $input");
return await parse(input: input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ class EnterPaymentInfoDialogState extends State<EnterPaymentInfoDialog> {
final texts = context.texts();
try {
_setValidatorErrorMessage("");
// TODO: Liquid - Previously parseInput, parseInvoice only parses LNInvoice's.
final inputType = context.read<InputBloc>().parseInvoice(input: input);
final inputType = context.read<InputBloc>().parseInput(input: input);
_log.info("Parsed input type: '${inputType.runtimeType.toString()}");
// Can't compare against a list of InputType as runtime type comparison is a bit tricky with binding generated enums
// TODO: Liquid - Add other supported InputType's once parse_invoice has evolved into parse_input.
// ignore: unnecessary_type_check
if (inputType is! LNInvoice) {
if (!(inputType is InputType_Bolt11 ||
inputType is InputType_LnUrlPay ||
inputType is InputType_LnUrlWithdraw ||
inputType is InputType_LnUrlAuth ||
inputType is InputType_LnUrlError ||
inputType is InputType_NodeId)) {
_setValidatorErrorMessage(texts.payment_info_dialog_error_unsupported_input);
}
} catch (e) {
Expand Down

0 comments on commit 4f8cba6

Please sign in to comment.