diff --git a/lib/accounts.dart b/lib/accounts.dart index d166766..0322fbb 100644 --- a/lib/accounts.dart +++ b/lib/accounts.dart @@ -142,7 +142,8 @@ abstract class _ActiveAccount with Store { final prefs = await SharedPreferences.getInstance(); final coin = prefs.getInt('coin') ?? 0; var id = prefs.getInt('account') ?? 0; - setActiveAccount(coin, id); + if (WarpApi.checkAccount(coin, id)) + setActiveAccount(coin, id); checkAndUpdate(); } diff --git a/lib/main.dart b/lib/main.dart index ac8db33..fbbb385 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -388,14 +388,17 @@ class ZWalletAppState extends State { WarpApi.mempoolRun(unconfirmedBalancePort.sendPort.nativePort); final c = coins.first; - if (!isMobile() && File(c.dbFullPath).existsSync()) { - if (!WarpApi.decryptDb(c.dbFullPath, '')) { - final passwd = await getDbPasswd(context, c.dbFullPath); - if (passwd != null) { - settings.dbPasswd = passwd; - } + do { + if (isMobile()) break; // db encryption is only for desktop + if (!File(c.dbFullPath).existsSync()) break; // fresh install + if (WarpApi.decryptDb(c.dbFullPath, '')) break; // not encrypted + + final reset = await getDbPasswd(context, c.dbFullPath); + if (reset) { // user didn't input the passwd and wants to reset + await clearApp(context); } - } + else break; + } while(true); for (var c in coins) { _setProgress(0.2 * (c.coin+1), 'Initializing ${c.ticker}'); @@ -977,8 +980,9 @@ void resetApp() { WarpApi.truncateData(); } -Future getDbPasswd(BuildContext context, String dbPath) async { +Future getDbPasswd(BuildContext context, String dbPath) async { final s = S.of(context); + final navigator = navigatorKey.currentState!; final passwdController = TextEditingController(); final checkPasswd = (String? v) { final valid = WarpApi.decryptDb(dbPath, passwdController.text); @@ -987,7 +991,7 @@ Future getDbPasswd(BuildContext context, String dbPath) async { }; final formKey = GlobalKey(); - final confirmed = await showDialog( + final reset = await showDialog( context: context, barrierColor: Colors.black, barrierDismissible: false, @@ -1000,14 +1004,28 @@ Future getDbPasswd(BuildContext context, String dbPath) async { decoration: InputDecoration(labelText: s.databasePassword), controller: passwdController, validator: checkPasswd, + onSaved: (v) { settings.dbPasswd = v!; }, obscureText: true, ), ])))), - actions: confirmButtons(context, () { - if (formKey.currentState!.validate()) - Navigator.of(context).pop(true); - }, okLabel: s.ok), - ); + actions: + [ + ElevatedButton.icon( + icon: Icon(Icons.lock_reset), + label: Text(s.reset), + onPressed: () => navigator.pop(true)), + ElevatedButton.icon( + icon: Icon(Icons.done), + label: Text(s.ok), + onPressed: () { + final fs = formKey.currentState!; + if (fs.validate()) { + fs.save(); + navigator.pop(false); + } + }) + ]); }) ?? false; - return confirmed ? passwdController.text : null; + + return reset; } diff --git a/lib/reset.dart b/lib/reset.dart index 7a4f996..4ca8aa1 100644 --- a/lib/reset.dart +++ b/lib/reset.dart @@ -5,6 +5,7 @@ import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:warp_api/types.dart'; import 'package:warp_api/warp_api.dart'; +import 'coin/coins.dart'; import 'generated/l10n.dart'; import 'main.dart'; import 'package:path/path.dart' as p; @@ -230,3 +231,16 @@ Future showRestartMessage() async { ) ); } + +Future clearApp(BuildContext context) async { + final reset = await showDialog(context: context, barrierDismissible: false, builder: + (context) => AlertDialog( + title: Text('Reset Database'), + content: Text('Are you sure you want to DELETE ALL your data?'), + actions: confirmButtons(context, () => Navigator.of(context).pop(true)), + )) ?? false; + if (reset) { + final c = coins.first; + File(c.dbDir).deleteSync(recursive: true); + } +} diff --git a/native/zcash-sync b/native/zcash-sync index 2940d8f..56b8113 160000 --- a/native/zcash-sync +++ b/native/zcash-sync @@ -1 +1 @@ -Subproject commit 2940d8f8dac0c365f97e06adaab1c88174cff805 +Subproject commit 56b8113084bda7ea367a76e0e10efab1e467efe3 diff --git a/packages/warp_api_ffi/lib/warp_api.dart b/packages/warp_api_ffi/lib/warp_api.dart index fc574d7..d0354bb 100644 --- a/packages/warp_api_ffi/lib/warp_api.dart +++ b/packages/warp_api_ffi/lib/warp_api.dart @@ -190,6 +190,10 @@ class WarpApi { return unwrapResultString(address); } + static bool checkAccount(int coin, int account) { + return account != 0 && warp_api_lib.check_account(coin, account) != 0; + } + static void setActiveAccount(int coin, int account) { warp_api_lib.set_active(coin); warp_api_lib.set_active_account(coin, account); diff --git a/packages/warp_api_ffi/lib/warp_api_generated.dart b/packages/warp_api_ffi/lib/warp_api_generated.dart index 43f0424..deafd43 100644 --- a/packages/warp_api_ffi/lib/warp_api_generated.dart +++ b/packages/warp_api_ffi/lib/warp_api_generated.dart @@ -761,6 +761,21 @@ class NativeLibrary { late final _dart_truncate_sync_data _truncate_sync_data = _truncate_sync_data_ptr.asFunction<_dart_truncate_sync_data>(); + int check_account( + int coin, + int account, + ) { + return _check_account( + coin, + account, + ); + } + + late final _check_account_ptr = + _lookup>('check_account'); + late final _dart_check_account _check_account = + _check_account_ptr.asFunction<_dart_check_account>(); + void delete_account( int coin, int account, @@ -2008,6 +2023,16 @@ typedef _c_truncate_sync_data = ffi.Void Function(); typedef _dart_truncate_sync_data = void Function(); +typedef _c_check_account = ffi.Int8 Function( + ffi.Uint8 coin, + ffi.Uint32 account, +); + +typedef _dart_check_account = int Function( + int coin, + int account, +); + typedef _c_delete_account = ffi.Void Function( ffi.Uint8 coin, ffi.Uint32 account, diff --git a/pubspec.yaml b/pubspec.yaml index 3278426..afa84db 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.3.5+397 +version: 1.3.5+398 environment: sdk: ">=2.12.0 <3.0.0"