fix for multiple wallets

This commit is contained in:
Czarek Nakamoto 2024-05-08 17:36:39 +02:00
parent a745c59af4
commit 0e902b6f63

View file

@ -1,4 +1,3 @@
import 'dart:ffi'; import 'dart:ffi';
import 'package:cw_monero/api/account_list.dart'; 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_keys_exception.dart';
import 'package:cw_monero/api/exceptions/wallet_restore_from_seed_exception.dart'; import 'package:cw_monero/api/exceptions/wallet_restore_from_seed_exception.dart';
import 'package:cw_monero/api/wallet.dart'; import 'package:cw_monero/api/wallet.dart';
import 'package:flutter/foundation.dart';
import 'package:monero/monero.dart' as monero; import 'package:monero/monero.dart' as monero;
monero.WalletManager? _wmPtr; monero.WalletManager? _wmPtr;
final monero.WalletManager wmPtr = Pointer.fromAddress((() { final monero.WalletManager wmPtr = Pointer.fromAddress((() {
try { 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(); _wmPtr ??= monero.WalletManagerFactory_getWalletManager();
print("ptr: $_wmPtr"); print("ptr: $_wmPtr");
} catch (e) { } catch (e) {
@ -23,16 +26,18 @@ final monero.WalletManager wmPtr = Pointer.fromAddress((() {
void createWalletSync( void createWalletSync(
{required String path, {required String path,
required String password, required String password,
required String language, required String language,
int nettype = 0}) { int nettype = 0}) {
wptr = monero.WalletManager_createWallet(wmPtr, path: path, password: password, language: language, networkType: 0); wptr = monero.WalletManager_createWallet(wmPtr,
path: path, password: password, language: language, networkType: 0);
final status = monero.Wallet_status(wptr!); final status = monero.Wallet_status(wptr!);
if (status != 0) { if (status != 0) {
throw WalletCreationException(message: monero.Wallet_errorString(wptr!)); throw WalletCreationException(message: monero.Wallet_errorString(wptr!));
} }
monero.Wallet_store(wptr!, path: path); monero.Wallet_store(wptr!, path: path);
openedWalletsByPath[path] = wptr!;
// is the line below needed? // is the line below needed?
// setupNodeSync(address: "node.moneroworld.com:18089"); // setupNodeSync(address: "node.moneroworld.com:18089");
@ -48,7 +53,6 @@ void restoreWalletFromSeedSync(
required String seed, required String seed,
int nettype = 0, int nettype = 0,
int restoreHeight = 0}) { int restoreHeight = 0}) {
wptr = monero.WalletManager_recoveryWallet( wptr = monero.WalletManager_recoveryWallet(
wmPtr, wmPtr,
path: path, path: path,
@ -65,6 +69,8 @@ void restoreWalletFromSeedSync(
final error = monero.Wallet_errorString(wptr!); final error = monero.Wallet_errorString(wptr!);
throw WalletRestoreFromSeedException(message: error); throw WalletRestoreFromSeedException(message: error);
} }
openedWalletsByPath[path] = wptr!;
} }
void restoreWalletFromKeysSync( void restoreWalletFromKeysSync(
@ -76,7 +82,6 @@ void restoreWalletFromKeysSync(
required String spendKey, required String spendKey,
int nettype = 0, int nettype = 0,
int restoreHeight = 0}) { int restoreHeight = 0}) {
wptr = monero.WalletManager_createWalletFromKeys( wptr = monero.WalletManager_createWalletFromKeys(
wmPtr, wmPtr,
path: path, path: path,
@ -90,19 +95,21 @@ void restoreWalletFromKeysSync(
final status = monero.Wallet_status(wptr!); final status = monero.Wallet_status(wptr!);
if (status != 0) { if (status != 0) {
throw WalletRestoreFromKeysException(message: monero.Wallet_errorString(wptr!)); throw WalletRestoreFromKeysException(
message: monero.Wallet_errorString(wptr!));
} }
openedWalletsByPath[path] = wptr!;
} }
void restoreWalletFromSpendKeySync( void restoreWalletFromSpendKeySync(
{required String path, {required String path,
required String password, required String password,
required String seed, required String seed,
required String language, required String language,
required String spendKey, required String spendKey,
int nettype = 0, int nettype = 0,
int restoreHeight = 0}) { int restoreHeight = 0}) {
// wptr = monero.WalletManager_createWalletFromKeys( // wptr = monero.WalletManager_createWalletFromKeys(
// wmPtr, // wmPtr,
// path: path, // path: path,
@ -135,6 +142,8 @@ void restoreWalletFromSpendKeySync(
monero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed); monero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed);
storeSync(); storeSync();
openedWalletsByPath[path] = wptr!;
} }
String _lastOpenedWallet = ""; String _lastOpenedWallet = "";
@ -170,17 +179,22 @@ String _lastOpenedWallet = "";
// } // }
// } // }
Map<String, monero.wallet> openedWalletsByPath = {};
void loadWallet({ void loadWallet(
required String path, {required String path, required String password, int nettype = 0}) {
required String password, if (openedWalletsByPath[path] != null) {
int nettype = 0}) { wptr = openedWalletsByPath[path]!;
return;
}
try { try {
if (wptr == null || path != _lastOpenedWallet) { if (wptr == null || path != _lastOpenedWallet) {
if (wptr != null) { if (wptr != null) {
monero.Wallet_store(wptr!); 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; _lastOpenedWallet = path;
} }
} catch (e) { } catch (e) {
@ -248,12 +262,15 @@ void _restoreFromSpendKey(Map<String, dynamic> args) {
spendKey: spendKey); spendKey: spendKey);
} }
Future<void> _openWallet(Map<String, String> args) async => Future<void> _openWallet(Map<String, String> args) async => loadWallet(
loadWallet(path: args['path'] as String, password: args['password'] as String); path: args['path'] as String, password: args['password'] as String);
Future<bool> _isWalletExist(String path) async => isWalletExistSync(path: path); Future<bool> _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); loadWallet(path: path, password: password, nettype: nettype);
Future<void> openWalletAsync(Map<String, String> args) async => Future<void> openWalletAsync(Map<String, String> args) async =>
@ -306,13 +323,13 @@ Future<void> restoreFromKeys(
}); });
Future<void> restoreFromSpendKey( Future<void> restoreFromSpendKey(
{required String path, {required String path,
required String password, required String password,
required String seed, required String seed,
required String language, required String language,
required String spendKey, required String spendKey,
int nettype = 0, int nettype = 0,
int restoreHeight = 0}) async => int restoreHeight = 0}) async =>
_restoreFromSpendKey({ _restoreFromSpendKey({
'path': path, 'path': path,
'password': password, 'password': password,