Close the wallet when the wallet gets changed

This commit is contained in:
Konstantin Ullrich 2024-05-22 13:14:23 +02:00
parent 5ca5c545b4
commit cf8a20f8db
No known key found for this signature in database
GPG key ID: E9562A013280F5DB
2 changed files with 24 additions and 36 deletions

View file

@ -147,10 +147,27 @@ abstract class MoneroWalletBase
Future<void>? updateBalance() => null;
@override
void close() {
void close() async {
_listener?.stop();
_onAccountChangeReaction?.reaction.dispose();
_autoSaveTimer?.cancel();
final currentWalletDirPath = await pathForWalletDir(name: name, type: type);
if (openedWalletsByPath["$currentWalletDirPath/$name"] != null) {
// NOTE: this is realistically only required on windows.
print("closing wallet");
final wmaddr = wmPtr.address;
final waddr = openedWalletsByPath["$currentWalletDirPath/$name"]!.address;
await Isolate.run(() {
monero.WalletManager_closeWallet(
Pointer.fromAddress(wmaddr),
Pointer.fromAddress(waddr),
true
);
});
openedWalletsByPath.remove("$currentWalletDirPath/$name");
print("wallet closed");
}
}
@override
@ -328,28 +345,18 @@ abstract class MoneroWalletBase
}
await walletAddresses.updateAddressesInBox();
await backupWalletFiles(name);
await monero_wallet.store();
try {
await backupWalletFiles(name);
} catch (e) {
print("¯\\_(ツ)_/¯");
print(e);
}
}
@override
Future<void> renameWalletFiles(String newWalletName) async {
final currentWalletDirPath = await pathForWalletDir(name: name, type: type);
if (openedWalletsByPath[currentWalletDirPath] != null) {
// NOTE: this is realistically only required on windows.
print("closing wallet");
final wmaddr = wmPtr!.address;
final waddr = openedWalletsByPath[currentWalletDirPath]!.address;
await Isolate.run(() {
monero.WalletManager_closeWallet(
Pointer.fromAddress(wmaddr),
Pointer.fromAddress(waddr),
true
);
});
openedWalletsByPath.remove(currentWalletDirPath);
print("wallet closed");
}
try {
// -- rename the waller folder --
final currentWalletDir = Directory(await pathForWalletDir(name: name, type: type));

View file

@ -1,6 +1,4 @@
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:cw_core/monero_wallet_utils.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/unspent_coins_info.dart';
@ -16,8 +14,6 @@ import 'package:cw_monero/monero_wallet.dart';
import 'package:flutter/widgets.dart';
import 'package:hive/hive.dart';
import 'package:polyseed/polyseed.dart';
import 'package:cw_monero/api/wallet_manager.dart';
import 'package:monero/monero.dart' as monero;
class MoneroNewWalletCredentials extends WalletCredentials {
MoneroNewWalletCredentials(
@ -178,21 +174,6 @@ class MoneroWalletService extends WalletService<MoneroNewWalletCredentials,
@override
Future<void> remove(String wallet) async {
final path = await pathForWalletDir(name: wallet, type: getType());
if (openedWalletsByPath[path] != null) {
// NOTE: this is realistically only required on windows.
print("closing wallet");
final wmaddr = wmPtr!.address;
final waddr = openedWalletsByPath[path]!.address;
await Isolate.run(() {
monero.WalletManager_closeWallet(
Pointer.fromAddress(wmaddr),
Pointer.fromAddress(waddr),
false
);
});
openedWalletsByPath.remove(path);
print("wallet closed");
}
final file = Directory(path);
final isExist = file.existsSync();