Skip to content

Commit

Permalink
check if input is NodeURI
Browse files Browse the repository at this point in the history
  • Loading branch information
ubbabeck committed Jul 6, 2023
1 parent 8c5c246 commit 7cfbfaf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/bloc/input/input_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:c_breez/bloc/input/input_state.dart';
import 'package:c_breez/models/invoice.dart';
import 'package:c_breez/services/device.dart';
import 'package:c_breez/services/lightning_links.dart';
import 'package:c_breez/utils/node_id.dart';
import 'package:fimber/fimber.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:rxdart/rxdart.dart';
Expand Down Expand Up @@ -55,6 +56,10 @@ class InputBloc extends Cubit<InputState> {
// Emit an empty InputState with isLoading to display a loader on UI layer
emit(InputState(isLoading: true));
try {
String? nodeID = parseNodeId(input);
if (nodeID != null) {
input = nodeID;
}
final parsedInput = await parseInput(input: input);
// Todo: Merge these functions w/o sacrificing readability
_logParsedInput(parsedInput);
Expand Down
21 changes: 18 additions & 3 deletions lib/utils/node_id.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import 'package:hex/hex.dart';

const NODE_URI_SEPARATOR = "@";
const NODE_ID_LENGTH = 66;

String? parseNodeId(String? nodeID) {
if (nodeID == null) {
return null;
}
if (nodeID.length == NODE_ID_LENGTH) {
nodeID = nodeID.trim();

if (nodeID.length == NODE_ID_LENGTH && _isParsable(nodeID)) {
return nodeID;
}

if (nodeID.length > NODE_ID_LENGTH &&
nodeID.substring(NODE_ID_LENGTH, NODE_ID_LENGTH + 1) == NODE_URI_SEPARATOR) {
return nodeID.substring(0, 66);
nodeID = nodeID.substring(0, NODE_ID_LENGTH);
if (_isParsable(nodeID)) {
return nodeID;
}
}

return null;
}

bool _isParsable(String nodeID) {
try {
HEX.decode(nodeID);
} on FormatException catch (_) {
return false;
}
return true;
}

0 comments on commit 7cfbfaf

Please sign in to comment.