From 0e902b6f6318713859149e5358a1e82d71148c0a Mon Sep 17 00:00:00 2001 From: Czarek Nakamoto Date: Wed, 8 May 2024 17:36:39 +0200 Subject: [PATCH] fix for multiple wallets --- cw_monero/lib/api/wallet_manager.dart | 79 ++++++++++++++++----------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/cw_monero/lib/api/wallet_manager.dart b/cw_monero/lib/api/wallet_manager.dart index a3e9ff33f..3d5d81c6d 100644 --- a/cw_monero/lib/api/wallet_manager.dart +++ b/cw_monero/lib/api/wallet_manager.dart @@ -1,4 +1,3 @@ - import 'dart:ffi'; import 'package:cw_monero/api/account_list.dart'; @@ -7,12 +6,16 @@ import 'package:cw_monero/api/exceptions/wallet_opening_exception.dart'; import 'package:cw_monero/api/exceptions/wallet_restore_from_keys_exception.dart'; import 'package:cw_monero/api/exceptions/wallet_restore_from_seed_exception.dart'; import 'package:cw_monero/api/wallet.dart'; +import 'package:flutter/foundation.dart'; import 'package:monero/monero.dart' as monero; monero.WalletManager? _wmPtr; final monero.WalletManager wmPtr = Pointer.fromAddress((() { try { - monero.printStarts = true; + // Problems with the wallet? Crashes? Lags? this will print all calls to xmr + // codebase, so it will be easier to debug what happens. At least easier + // than plugging gdb in. Especially on windows/android. + monero.printStarts = kDebugMode; _wmPtr ??= monero.WalletManagerFactory_getWalletManager(); print("ptr: $_wmPtr"); } catch (e) { @@ -23,16 +26,18 @@ final monero.WalletManager wmPtr = Pointer.fromAddress((() { void createWalletSync( {required String path, - required String password, - required String language, - int nettype = 0}) { - wptr = monero.WalletManager_createWallet(wmPtr, path: path, password: password, language: language, networkType: 0); + required String password, + required String language, + int nettype = 0}) { + wptr = monero.WalletManager_createWallet(wmPtr, + path: path, password: password, language: language, networkType: 0); final status = monero.Wallet_status(wptr!); if (status != 0) { throw WalletCreationException(message: monero.Wallet_errorString(wptr!)); } monero.Wallet_store(wptr!, path: path); + openedWalletsByPath[path] = wptr!; // is the line below needed? // setupNodeSync(address: "node.moneroworld.com:18089"); @@ -48,7 +53,6 @@ void restoreWalletFromSeedSync( required String seed, int nettype = 0, int restoreHeight = 0}) { - wptr = monero.WalletManager_recoveryWallet( wmPtr, path: path, @@ -65,6 +69,8 @@ void restoreWalletFromSeedSync( final error = monero.Wallet_errorString(wptr!); throw WalletRestoreFromSeedException(message: error); } + + openedWalletsByPath[path] = wptr!; } void restoreWalletFromKeysSync( @@ -76,7 +82,6 @@ void restoreWalletFromKeysSync( required String spendKey, int nettype = 0, int restoreHeight = 0}) { - wptr = monero.WalletManager_createWalletFromKeys( wmPtr, path: path, @@ -90,19 +95,21 @@ void restoreWalletFromKeysSync( final status = monero.Wallet_status(wptr!); if (status != 0) { - throw WalletRestoreFromKeysException(message: monero.Wallet_errorString(wptr!)); + throw WalletRestoreFromKeysException( + message: monero.Wallet_errorString(wptr!)); } + + openedWalletsByPath[path] = wptr!; } void restoreWalletFromSpendKeySync( {required String path, - required String password, - required String seed, - required String language, - required String spendKey, - int nettype = 0, - int restoreHeight = 0}) { - + required String password, + required String seed, + required String language, + required String spendKey, + int nettype = 0, + int restoreHeight = 0}) { // wptr = monero.WalletManager_createWalletFromKeys( // wmPtr, // path: path, @@ -135,6 +142,8 @@ void restoreWalletFromSpendKeySync( monero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed); storeSync(); + + openedWalletsByPath[path] = wptr!; } String _lastOpenedWallet = ""; @@ -170,17 +179,22 @@ String _lastOpenedWallet = ""; // } // } +Map openedWalletsByPath = {}; -void loadWallet({ - required String path, - required String password, - int nettype = 0}) { +void loadWallet( + {required String path, required String password, int nettype = 0}) { + if (openedWalletsByPath[path] != null) { + wptr = openedWalletsByPath[path]!; + return; + } try { if (wptr == null || path != _lastOpenedWallet) { if (wptr != null) { monero.Wallet_store(wptr!); } - wptr = monero.WalletManager_openWallet(wmPtr, path: path, password: password); + wptr = monero.WalletManager_openWallet(wmPtr, + path: path, password: password); + openedWalletsByPath[path] = wptr!; _lastOpenedWallet = path; } } catch (e) { @@ -248,12 +262,15 @@ void _restoreFromSpendKey(Map args) { spendKey: spendKey); } -Future _openWallet(Map args) async => - loadWallet(path: args['path'] as String, password: args['password'] as String); +Future _openWallet(Map args) async => loadWallet( + path: args['path'] as String, password: args['password'] as String); Future _isWalletExist(String path) async => isWalletExistSync(path: path); -void openWallet({required String path, required String password, int nettype = 0}) async => +void openWallet( + {required String path, + required String password, + int nettype = 0}) async => loadWallet(path: path, password: password, nettype: nettype); Future openWalletAsync(Map args) async => @@ -306,13 +323,13 @@ Future restoreFromKeys( }); Future restoreFromSpendKey( - {required String path, - required String password, - required String seed, - required String language, - required String spendKey, - int nettype = 0, - int restoreHeight = 0}) async => + {required String path, + required String password, + required String seed, + required String language, + required String spendKey, + int nettype = 0, + int restoreHeight = 0}) async => _restoreFromSpendKey({ 'path': path, 'password': password,