feat: Introduce QR Scanning to add new node (#1007)

* feat: Introduce QR Scanning to add new node

* fix: Exception handing for scan qr and invalid qr value

* fix: Exception handing for scan qr and invalid qr value
This commit is contained in:
Adegoke David
2023-08-04 14:27:19 +03:00
committed by GitHub
parent 76322a1777
commit e9df03b49b
3 changed files with 64 additions and 2 deletions
@@ -1,4 +1,5 @@
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/entities/qr_scanner.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
@@ -8,10 +9,12 @@ import 'package:collection/collection.dart';
part 'node_create_or_edit_view_model.g.dart';
class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase with _$NodeCreateOrEditViewModel;
class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase
with _$NodeCreateOrEditViewModel;
abstract class NodeCreateOrEditViewModelBase with Store {
NodeCreateOrEditViewModelBase(this._nodeSource, this._walletType, this._settingsStore)
NodeCreateOrEditViewModelBase(
this._nodeSource, this._walletType, this._settingsStore)
: state = InitialExecutionState(),
connectionState = InitialExecutionState(),
useSSL = false,
@@ -170,4 +173,39 @@ abstract class NodeCreateOrEditViewModelBase with Store {
@action
void setAsCurrent(Node node) => _settingsStore.nodes[_walletType] = node;
@action
Future<void> scanQRCodeForNewNode() async {
try {
String code = await presentQRScanner();
if (code.isEmpty) {
throw Exception('Unexpected scan QR code value: value is empty');
}
final uri = Uri.tryParse(code);
if (uri == null) {
throw Exception('Unexpected scan QR code value: Value is invalid');
}
final userInfo = uri.userInfo.split(':');
if (userInfo.length < 2) {
throw Exception('Unexpected scan QR code value: Value is invalid');
}
final rpcUser = userInfo[0];
final rpcPassword = userInfo[1];
final ipAddress = uri.host;
final port = uri.port.toString();
setAddress(ipAddress);
setPassword(rpcPassword);
setLogin(rpcUser);
setPort(port);
} on Exception catch (e) {
connectionState = FailureState(e.toString());
}
}
}