Skip to content

Commit

Permalink
CW-782: Show error report popup without cooldown (#1739)
Browse files Browse the repository at this point in the history
* improve exception throwing on broken wallets
- put _lastOpenedWallet to avoid issues on windows (file is currently open by)
- don't throw corruptedWalletsSeed - instead store it inside of secureStorage
- await ExceptionHandler.onError calls where possible to makse sure that popup won't be canceled by some UI element
- adjust BaseAlertDialog to be scrollable if the text is too long
- add ExceptionHandler.resetLastPopupDate - that can be called when we want to show error report screen (bypassing cooldown)

* fix: HiveError: Box has already been closed.

* await the alerts to be sure that each one of them is being shown
fix typo in secure storage

* Update lib/core/backup_service.dart

Co-authored-by: Omar Hatem <[email protected]>

* address comments on github

* don't store seeds in secure storage

* fix wallet password

* update monero_c
update corrupted seeds UI
prevent app from crashing when wallet is corrupted

* show alert with seeds

* Update corrupted wallet UI
Fix wallet opening cache

* remove unused code

---------

Co-authored-by: Omar Hatem <[email protected]>
  • Loading branch information
MrCyjaneK and OmarHatem28 authored Nov 28, 2024
1 parent d8d4190 commit 17d34be
Show file tree
Hide file tree
Showing 51 changed files with 206 additions and 88 deletions.
15 changes: 12 additions & 3 deletions cw_monero/lib/api/wallet_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void createWalletSync(
wptr = newWptr;
monero.Wallet_store(wptr!, path: path);
openedWalletsByPath[path] = wptr!;
_lastOpenedWallet = path;

// is the line below needed?
// setupNodeSync(address: "node.moneroworld.com:18089");
Expand Down Expand Up @@ -116,6 +117,7 @@ void restoreWalletFromSeedSync(
wptr = newWptr;

openedWalletsByPath[path] = wptr!;
_lastOpenedWallet = path;
}

void restoreWalletFromKeysSync(
Expand Down Expand Up @@ -183,6 +185,7 @@ void restoreWalletFromKeysSync(
wptr = newWptr;

openedWalletsByPath[path] = wptr!;
_lastOpenedWallet = path;
}

void restoreWalletFromSpendKeySync(
Expand Down Expand Up @@ -231,6 +234,7 @@ void restoreWalletFromSpendKeySync(
storeSync();

openedWalletsByPath[path] = wptr!;
_lastOpenedWallet = path;
}

String _lastOpenedWallet = "";
Expand Down Expand Up @@ -260,7 +264,7 @@ Future<void> restoreWalletFromHardwareWallet(
throw WalletRestoreFromSeedException(message: error);
}
wptr = newWptr;

_lastOpenedWallet = path;
openedWalletsByPath[path] = wptr!;
}

Expand Down Expand Up @@ -295,6 +299,11 @@ Future<void> loadWallet(
password: password,
kdfRounds: 1,
);
final status = monero.WalletManager_errorString(wmPtr);
if (status != "") {
print("loadWallet:"+status);
throw WalletOpeningException(message: status);
}
} else {
deviceType = 0;
}
Expand All @@ -314,15 +323,15 @@ Future<void> loadWallet(

final newWptr = Pointer<Void>.fromAddress(newWptrAddr);

_lastOpenedWallet = path;
final status = monero.Wallet_status(newWptr);
if (status != 0) {
final err = monero.Wallet_errorString(newWptr);
print(err);
print("loadWallet:"+err);
throw WalletOpeningException(message: err);
}

wptr = newWptr;
_lastOpenedWallet = path;
openedWalletsByPath[path] = wptr!;
}
}
Expand Down
2 changes: 1 addition & 1 deletion cw_monero/lib/monero_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class MoneroWalletService extends WalletService<
}

await restoreOrResetWalletFiles(name);
return openWallet(name, password, retryOnFailure: false);
return await openWallet(name, password, retryOnFailure: false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cw_monero/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ packages:
dependency: "direct main"
description:
path: "impls/monero.dart"
ref: d72c15f4339791a7bbdf17e9d827b7b56ca144e4
resolved-ref: d72c15f4339791a7bbdf17e9d827b7b56ca144e4
ref: c41c4dad9aa5003a914cfb2c528c76386f952665
resolved-ref: c41c4dad9aa5003a914cfb2c528c76386f952665
url: "https://github.com/mrcyjanek/monero_c"
source: git
version: "0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion cw_monero/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies:
monero:
git:
url: https://github.com/mrcyjanek/monero_c
ref: d72c15f4339791a7bbdf17e9d827b7b56ca144e4
ref: c41c4dad9aa5003a914cfb2c528c76386f952665
# ref: 6eb571ea498ed7b854934785f00fabfd0dadf75b # monero_c hash
path: impls/monero.dart
mutex: ^3.1.0
Expand Down
4 changes: 2 additions & 2 deletions cw_wownero/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ packages:
dependency: "direct main"
description:
path: "impls/monero.dart"
ref: d72c15f4339791a7bbdf17e9d827b7b56ca144e4
resolved-ref: d72c15f4339791a7bbdf17e9d827b7b56ca144e4
ref: c41c4dad9aa5003a914cfb2c528c76386f952665
resolved-ref: c41c4dad9aa5003a914cfb2c528c76386f952665
url: "https://github.com/mrcyjanek/monero_c"
source: git
version: "0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion cw_wownero/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies:
monero:
git:
url: https://github.com/mrcyjanek/monero_c
ref: d72c15f4339791a7bbdf17e9d827b7b56ca144e4
ref: c41c4dad9aa5003a914cfb2c528c76386f952665
# ref: 6eb571ea498ed7b854934785f00fabfd0dadf75b # monero_c hash
path: impls/monero.dart
mutex: ^3.1.0
Expand Down
2 changes: 1 addition & 1 deletion lib/anypay/anypay_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AnyPayApi {
final response = await post(url, headers: headers, body: utf8.encode(json.encode(body)));

if (response.statusCode != 200) {
ExceptionHandler.onError(FlutterErrorDetails(exception: response));
await ExceptionHandler.onError(FlutterErrorDetails(exception: response));
throw Exception('Unexpected response http code: ${response.statusCode}');
}

Expand Down
18 changes: 8 additions & 10 deletions lib/core/backup_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,15 @@ class BackupService {
json.decode(transactionDescriptionFile.readAsStringSync()) as Map<String, dynamic>;
final descriptionsMap = jsonData.map((key, value) =>
MapEntry(key, TransactionDescription.fromJson(value as Map<String, dynamic>)));

if (!_transactionDescriptionBox.isOpen) {
final transactionDescriptionsBoxKey = await getEncryptionKey(secureStorage: secureStorageShared, forKey: TransactionDescription.boxKey);
final transactionDescriptionBox = await CakeHive.openBox<TransactionDescription>(
var box = _transactionDescriptionBox;
if (!box.isOpen) {
final transactionDescriptionsBoxKey =
await getEncryptionKey(secureStorage: _secureStorage, forKey: TransactionDescription.boxKey);
box = await CakeHive.openBox<TransactionDescription>(
TransactionDescription.boxName,
encryptionKey: transactionDescriptionsBoxKey,
);
await transactionDescriptionBox.putAll(descriptionsMap);
return;
}
await _transactionDescriptionBox.putAll(descriptionsMap);
encryptionKey: transactionDescriptionsBoxKey);
}
await box.putAll(descriptionsMap);
}

Future<void> _importPreferencesDump() async {
Expand Down
64 changes: 55 additions & 9 deletions lib/core/wallet_loading_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ import 'dart:async';

import 'package:cake_wallet/core/generate_wallet_password.dart';
import 'package:cake_wallet/core/key_service.dart';
import 'package:cake_wallet/core/secure_storage.dart';
import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/main.dart';
import 'package:cake_wallet/reactions/on_authentication_state_change.dart';
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/utils/exception_handler.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

class WalletLoadingService {
Expand Down Expand Up @@ -58,24 +65,25 @@ class WalletLoadingService {

return wallet;
} catch (error, stack) {
ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stack));
await ExceptionHandler.resetLastPopupDate();
await ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stack));

// try fetching the seeds of the corrupted wallet to show it to the user
String corruptedWalletsSeeds = "Corrupted wallets seeds (if retrievable, empty otherwise):";
try {
corruptedWalletsSeeds += await _getCorruptedWalletSeeds(name, type);
} catch (e) {
corruptedWalletsSeeds += "\nFailed to fetch $name seeds: $e";
corruptedWalletsSeeds += "\nFailed to fetch $name seeds: $e";
}

// try opening another wallet that is not corrupted to give user access to the app
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);

WalletBase? wallet;
for (var walletInfo in walletInfoSource.values) {
try {
final walletService = walletServiceFactory.call(walletInfo.type);
final walletPassword = password ?? (await keyService.getWalletPassword(walletName: name));
final wallet = await walletService.openWallet(walletInfo.name, walletPassword);
final walletPassword = await keyService.getWalletPassword(walletName: walletInfo.name);
wallet = await walletService.openWallet(walletInfo.name, walletPassword);

if (walletInfo.type == WalletType.monero) {
await updateMoneroWalletPassword(wallet);
Expand All @@ -88,8 +96,6 @@ class WalletLoadingService {

// if found a wallet that is not corrupted, then still display the seeds of the corrupted ones
authenticatedErrorStreamController.add(corruptedWalletsSeeds);

return wallet;
} catch (e) {
print(e);
// save seeds and show corrupted wallets' seeds to the user
Expand All @@ -99,16 +105,56 @@ class WalletLoadingService {
corruptedWalletsSeeds += seeds;
}
} catch (e) {
corruptedWalletsSeeds += "\nFailed to fetch $name seeds: $e";
corruptedWalletsSeeds += "\nFailed to fetch $name seeds: $e";
}
}
}

// if all user's wallets are corrupted throw exception
throw error.toString() + "\n\n" + corruptedWalletsSeeds;
final msg = error.toString() + "\n" + corruptedWalletsSeeds;
if (navigatorKey.currentContext != null) {
await showPopUp<void>(
context: navigatorKey.currentContext!,
builder: (BuildContext context) {
return AlertWithTwoActions(
alertTitle: "Corrupted seeds",
alertContent: S.of(context).corrupted_seed_notice,
leftButtonText: S.of(context).cancel,
rightButtonText: S.of(context).show_seed,
actionLeftButton: () => Navigator.of(context).pop(),
actionRightButton: () => showSeedsPopup(context, msg),
);
});
} else {
throw msg;
}
if (wallet == null) {
throw Exception("Wallet is null");
}
return wallet;
}
}

Future<void> showSeedsPopup(BuildContext context, String message) async {
Navigator.of(context).pop();
await showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithTwoActions(
alertTitle: "Corrupted seeds",
alertContent: message,
leftButtonText: S.of(context).copy,
rightButtonText: S.of(context).ok,
actionLeftButton: () async {
await Clipboard.setData(ClipboardData(text: message));
},
actionRightButton: () async {
Navigator.of(context).pop();
},
);
});
}

Future<void> updateMoneroWalletPassword(WalletBase wallet) async {
final key = PreferencesKey.moneroWalletUpdateV1Key(wallet.name);
var isPasswordUpdated = sharedPreferences.getBool(key) ?? false;
Expand Down
6 changes: 3 additions & 3 deletions lib/di.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ Future<void> setup({
totpAuthPageState.changeProcessText('Loading the wallet');

if (loginError != null) {
totpAuthPageState.changeProcessText('ERROR: ${loginError.toString()}');
totpAuthPageState.changeProcessText('ERROR: ${loginError.toString()}'.trim());
}

ReactionDisposer? _reaction;
Expand Down Expand Up @@ -604,7 +604,7 @@ Future<void> setup({
authPageState.changeProcessText('Loading the wallet');

if (loginError != null) {
authPageState.changeProcessText('ERROR: ${loginError.toString()}');
authPageState.changeProcessText('ERROR: ${loginError.toString()}'.trim());
loginError = null;
}

Expand All @@ -624,7 +624,7 @@ Future<void> setup({
}

if (loginError != null) {
authPageState.changeProcessText('ERROR: ${loginError.toString()}');
authPageState.changeProcessText('ERROR: ${loginError.toString()}'.trim());
timer.cancel();
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Future<void> runAppWithZone({Key? topLevelKey}) async {
);
}

ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stackTrace));
await ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stackTrace));
});
}

Expand Down
6 changes: 3 additions & 3 deletions lib/reactions/on_authentication_state_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ void startAuthenticationStateChange(
if (!requireHardwareWalletConnection()) await loadCurrentWallet();
} catch (error, stack) {
loginError = error;
ExceptionHandler.onError(
FlutterErrorDetails(exception: error, stack: stack));
await ExceptionHandler.resetLastPopupDate();
await ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stack));
}
return;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ void startAuthenticationStateChange(
.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false);
}
if (!(await authenticatedErrorStreamController.stream.isEmpty)) {
ExceptionHandler.showError(
await ExceptionHandler.showError(
(await authenticatedErrorStreamController.stream.first).toString());
authenticatedErrorStreamController.stream.drain();
}
Expand Down
23 changes: 21 additions & 2 deletions lib/src/screens/auth/auth_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:another_flushbar/flushbar.dart';
import 'package:cake_wallet/utils/show_bar.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:mobx/mobx.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
Expand All @@ -9,6 +10,8 @@ import 'package:cake_wallet/view_model/auth_view_model.dart';
import 'package:cake_wallet/src/screens/pin_code/pin_code.dart';
import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:flutter/services.dart';

typedef OnAuthenticationFinished = void Function(bool, AuthPageState);

Expand Down Expand Up @@ -66,7 +69,6 @@ class AuthPagePinCodeStateImpl extends AuthPageState<AuthPage> {
dismissFlushBar(_authBar);
showBar<void>(
context, S.of(context).failed_authentication(state.error));

widget.onAuthenticationFinished(false, this);
});
}
Expand All @@ -77,12 +79,12 @@ class AuthPagePinCodeStateImpl extends AuthPageState<AuthPage> {
dismissFlushBar(_authBar);
showBar<void>(
context, S.of(context).failed_authentication(state.error));

widget.onAuthenticationFinished(false, this);
});
}
});


if (widget.authViewModel.isBiometricalAuthenticationAllowed) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await Future<void>.delayed(Duration(milliseconds: 100));
Expand All @@ -93,6 +95,23 @@ class AuthPagePinCodeStateImpl extends AuthPageState<AuthPage> {
super.initState();
}

Future<void> _showSeedsPopup(BuildContext context, String message) async {
await showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithTwoActions(
alertTitle: "Corrupted seeds",
alertContent: message,
leftButtonText: S.of(context).copy,
rightButtonText: S.of(context).ok,
actionLeftButton: () async {
await Clipboard.setData(ClipboardData(text: message));
},
actionRightButton: () => Navigator.of(context).pop(),
);
});
}

@override
void dispose() {
_reaction?.reaction.dispose();
Expand Down
Loading

0 comments on commit 17d34be

Please sign in to comment.