From 6687e478f493a0ef77ac7c2ae2c141b44da76098 Mon Sep 17 00:00:00 2001 From: cyan Date: Sat, 12 Sep 2026 22:57:19 +0800 Subject: [PATCH] fix: monero silently ignoring wallet opens (#3621) --- cw_monero/lib/api/wallet_manager.dart | 131 ++++++++++++-------------- 1 file changed, 61 insertions(+), 70 deletions(-) diff --git a/cw_monero/lib/api/wallet_manager.dart b/cw_monero/lib/api/wallet_manager.dart index 85bc77291..9de58c686 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 (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; - } - - 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; + if (currentWallet != null) { + final addr = currentWallet!.ffiAddress(); + Isolate.run(() { + monero.Wallet_store(Pointer.fromAddress(addr)); }); - - final newW = MoneroWallet(Pointer.fromAddress(newWptrAddr)); - - 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; - _lastOpenedWallet = path; - openedWalletsByPath[path] = currentWallet!; } + txhistory = null; + + /// 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; + } + + 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)); + + 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) {