diff --git a/cw_monero/lib/api/wallet_manager.dart b/cw_monero/lib/api/wallet_manager.dart index 85bc772910..9de58c686e 100644 --- a/cw_monero/lib/api/wallet_manager.dart +++ b/cw_monero/lib/api/wallet_manager.dart @@ -101,7 +101,6 @@ void createWallet( currentWallet!.setCacheAttribute(key: "cakewallet.passphrase", value: passphrase); currentWallet!.store(path: path); openedWalletsByPath[path] = currentWallet!; - _lastOpenedWallet = path; } bool isWalletExist({required String path}) { @@ -147,7 +146,6 @@ void restoreWalletFromSeedSync( openedWalletsByPath[path] = currentWallet!; currentWallet!.store(path: path); - _lastOpenedWallet = path; } void storePassphrase({required String path, required String passphrase}) { @@ -220,7 +218,6 @@ void restoreWalletFromKeys( currentWallet = newW; openedWalletsByPath[path] = currentWallet!; - _lastOpenedWallet = path; } // English only, because normalization. @@ -310,11 +307,8 @@ void restoreWalletFromSpendKeySync( setupBackgroundSync(password, currentWallet!); openedWalletsByPath[path] = currentWallet!; - _lastOpenedWallet = path; } -String _lastOpenedWallet = ""; - Future restoreWalletFromHardwareWallet( {required String path, required String password, @@ -339,7 +333,6 @@ Future restoreWalletFromHardwareWallet( currentWallet = newW; currentWallet!.store(path: path); - _lastOpenedWallet = path; openedWalletsByPath[path] = currentWallet!; } @@ -351,72 +344,70 @@ Future loadWallet({required String path, required String password, int net currentWallet = openedWalletsByPath[path]!; return; } - if (currentWallet == null || path != _lastOpenedWallet) { - if (currentWallet != null) { - final addr = currentWallet!.ffiAddress(); - Isolate.run(() { - monero.Wallet_store(Pointer.fromAddress(addr)); - }); - } - txhistory = null; - /// Get the device type - /// 0: Software Wallet - /// 1: Ledger - /// 2: Trezor - var deviceType = 0; + if (currentWallet != null) { + final addr = currentWallet!.ffiAddress(); + Isolate.run(() { + monero.Wallet_store(Pointer.fromAddress(addr)); + }); + } + txhistory = null; - if (Platform.isAndroid || Platform.isIOS) { - deviceType = wmPtr.queryWalletDevice( - keysFileName: "$path.keys", - password: password, - kdfRounds: 1, - ); - final status = wmPtr.errorString(); - if (status != "") { - printV("loadWallet:" + status); - // This is most likely closeWallet call leaking error. This is fine. - if (status.contains("failed to save file")) { - printV("loadWallet: error leaked: $status"); - deviceType = 0; - } else { - throw WalletOpeningException(message: status); - } + /// Get the device type + /// 0: Software Wallet + /// 1: Ledger + /// 2: Trezor + var deviceType = 0; + + if (Platform.isAndroid || Platform.isIOS) { + deviceType = wmPtr.queryWalletDevice( + keysFileName: "$path.keys", + password: password, + kdfRounds: 1, + ); + final status = wmPtr.errorString(); + if (status != "") { + printV("loadWallet:" + status); + // This is most likely closeWallet call leaking error. This is fine. + if (status.contains("failed to save file")) { + printV("loadWallet: error leaked: $status"); + deviceType = 0; + } else { + throw WalletOpeningException(message: status); } - } else { - deviceType = 0; } + } else { + deviceType = 0; + } - if (deviceType == 1) { - if (gLedger == null) { - throw Exception("Tried to open a ledger wallet with no ledger connected"); - } - enableLedgerExchange(gLedger!); + if (deviceType == 1) { + if (gLedger == null) { + throw Exception("Tried to open a ledger wallet with no ledger connected"); } + enableLedgerExchange(gLedger!); + } - final addr = wmPtr.ffiAddress(); - final newWptrAddr = await Isolate.run(() { - return monero.WalletManager_openWallet(Pointer.fromAddress(addr), - path: path, password: password) - .address; - }); - - final newW = MoneroWallet(Pointer.fromAddress(newWptrAddr)); + final addr = wmPtr.ffiAddress(); + final newWptrAddr = await Isolate.run(() { + return monero.WalletManager_openWallet(Pointer.fromAddress(addr), + path: path, password: password) + .address; + }); - int status = newW.status(); - if (status != 0) { - final err = newW.errorString(); - printV("loadWallet:" + err); - throw WalletOpeningException(message: err); - } - if (deviceType == 0) { - setupBackgroundSync(password, newW); - } + final newW = MoneroWallet(Pointer.fromAddress(newWptrAddr)); - currentWallet = newW; - _lastOpenedWallet = path; - openedWalletsByPath[path] = currentWallet!; + int status = newW.status(); + if (status != 0) { + final err = newW.errorString(); + printV("loadWallet:" + err); + throw WalletOpeningException(message: err); + } + if (deviceType == 0) { + setupBackgroundSync(password, newW); } + + currentWallet = newW; + openedWalletsByPath[path] = currentWallet!; } void setupBackgroundSync(String password, Wallet2Wallet wallet) {