Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 79 additions & 50 deletions lib/wallets/wallet/intermediate/lib_monero_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
int get isarTransactionVersion => 2;

LibMoneroWallet(super.currency, this.compatType) {
_attachTorListeners();

// Potentially dangerous hack. See comments in _startInit()
_startInit();
}

void _attachTorListeners() {
final bus = GlobalEventBus.instance;

// Listen for tor status changes.
Expand Down Expand Up @@ -83,10 +90,8 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
) async {
await updateNode();
});

// Potentially dangerous hack. See comments in _startInit()
_startInit();
}

// cw based wallet listener to handle synchronization of utxo frozen states
late final StreamSubscription<List<UTXO>> _streamSub;
Future<void> _startInit() async {
Expand Down Expand Up @@ -193,6 +198,10 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>

@override
Future<void> open() async {
if (_torStatusListener == null || _torPreferenceListener == null) {
_attachTorListeners();
}

bool wasNull = false;

if (wallet == null) {
Expand Down Expand Up @@ -441,7 +450,7 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
);

if (this.wallet != null) {
await exit();
await _exitNative();
}

this.wallet = wallet;
Expand Down Expand Up @@ -504,24 +513,50 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>

@override
Future<void> updateNode() async {
final node = getCurrentNode();

if (await _torNodeMismatchGuard(node)) {
throw Exception("TOR – clearnet mismatch");
if (wallet == null) {
return;
}

final host = node.host.endsWith(".onion")
? node.host
: Uri.parse(node.host).host;
final ({InternetAddress host, int port})? proxy =
AppConfig.hasFeature(AppFeature.tor) && prefs.useTor && !node.forceNoTor
? TorService.sharedInstance.getProxyInfo()
: null;
await _updateNodeMutex.protect(() async {
if (wallet == null) {
return;
}

_setSyncStatus(lib_monero_compat.ConnectingSyncStatus());
try {
if (_requireMutex) {
await _torConnectingLock.protect(() async {
final node = getCurrentNode();

if (await _torNodeMismatchGuard(node)) {
throw Exception("TOR – clearnet mismatch");
}

final host = node.host.endsWith(".onion")
? node.host
: Uri.parse(node.host).host;
final ({InternetAddress host, int port})? proxy =
AppConfig.hasFeature(AppFeature.tor) &&
prefs.useTor &&
!node.forceNoTor
? TorService.sharedInstance.getProxyInfo()
: null;

_setSyncStatus(lib_monero_compat.ConnectingSyncStatus());
try {
if (_requireMutex) {
await _torConnectingLock.protect(() async {
await csMonero.connect(
wallet!,
daemonAddress: "$host:${node.port}",
daemonUsername: node.loginName,
daemonPassword: await node.getPassword(secureStorageInterface),
trusted: node.trusted ?? false,
useSSL: node.useSSL,
socksProxyAddress: node.forceNoTor
? null
: proxy == null
? null
: "${proxy.host.address}:${proxy.port}",
);
});
} else {
await csMonero.connect(
wallet!,
daemonAddress: "$host:${node.port}",
Expand All @@ -535,37 +570,21 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
? null
: "${proxy.host.address}:${proxy.port}",
);
});
} else {
await csMonero.connect(
wallet!,
daemonAddress: "$host:${node.port}",
daemonUsername: node.loginName,
daemonPassword: await node.getPassword(secureStorageInterface),
trusted: node.trusted ?? false,
useSSL: node.useSSL,
socksProxyAddress: node.forceNoTor
? null
: proxy == null
? null
: "${proxy.host.address}:${proxy.port}",
}
await csMonero.startSyncing(wallet!);
await csMonero.startListeners(wallet!);
csMonero.startAutoSaving(wallet!);

_setSyncStatus(lib_monero_compat.ConnectedSyncStatus());
} catch (e, s) {
_setSyncStatus(lib_monero_compat.FailedSyncStatus());
Logging.instance.e(
"Exception caught in $runtimeType.updateNode(): ",
error: e,
stackTrace: s,
);
}
await csMonero.startSyncing(wallet!);
await csMonero.startListeners(wallet!);
csMonero.startAutoSaving(wallet!);

_setSyncStatus(lib_monero_compat.ConnectedSyncStatus());
} catch (e, s) {
_setSyncStatus(lib_monero_compat.FailedSyncStatus());
Logging.instance.e(
"Exception caught in $runtimeType.updateNode(): ",
error: e,
stackTrace: s,
);
}

return;
});
}

@override
Expand Down Expand Up @@ -732,13 +751,21 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
@override
Future<void> exit() async {
Logging.instance.i("exit called on monero $walletId!");
await _exitNative();
await _torStatusListener?.cancel();
await _torPreferenceListener?.cancel();
_torStatusListener = null;
_torPreferenceListener = null;
Logging.instance.i("exit call completed monero $walletId!");
}

Future<void> _exitNative() async {
if (wallet != null) {
csMonero.stopAutoSaving(wallet!);
await csMonero.stopListeners(wallet!);
await csMonero.stopSyncing(wallet!);
await csMonero.save(wallet!);
}
Logging.instance.i("exit call completed monero $walletId!");
}

Future<String> pathForWalletDir({
Expand Down Expand Up @@ -1578,7 +1605,7 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
);

if (this.wallet == null) {
await exit();
await _exitNative();
}
this.wallet = wallet;

Expand Down Expand Up @@ -1629,4 +1656,6 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>

final Mutex _torConnectingLock = Mutex();
bool _requireMutex = false;

final Mutex _updateNodeMutex = Mutex();
}
127 changes: 78 additions & 49 deletions lib/wallets/wallet/intermediate/lib_salvium_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
int get isarTransactionVersion => 2;

LibSalviumWallet(super.currency) {
_attachTorListeners();

// Potentially dangerous hack. See comments in _startInit()
_startInit();
}

void _attachTorListeners() {
final bus = GlobalEventBus.instance;

// Listen for tor status changes.
Expand Down Expand Up @@ -81,10 +88,8 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
) async {
await updateNode();
});

// Potentially dangerous hack. See comments in _startInit()
_startInit();
}

// cw based wallet listener to handle synchronization of utxo frozen states
late final StreamSubscription<List<UTXO>> _streamSub;
Future<void> _startInit() async {
Expand Down Expand Up @@ -189,6 +194,10 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>

@override
Future<void> open() async {
if (_torStatusListener == null || _torPreferenceListener == null) {
_attachTorListeners();
}

bool wasNull = false;

if (wallet == null) {
Expand Down Expand Up @@ -419,7 +428,7 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
);

if (this.wallet != null) {
await exit();
await _exitNative();
}

this.wallet = wallet;
Expand Down Expand Up @@ -481,24 +490,50 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>

@override
Future<void> updateNode() async {
final node = getCurrentNode();

if (_torNodeMismatchGuard(node)) {
throw Exception("TOR – clearnet mismatch");
if (wallet == null) {
return;
}

final host = node.host.endsWith(".onion")
? node.host
: Uri.parse(node.host).host;
final ({InternetAddress host, int port})? proxy =
AppConfig.hasFeature(AppFeature.tor) && prefs.useTor && !node.forceNoTor
? TorService.sharedInstance.getProxyInfo()
: null;
await _updateNodeMutex.protect(() async {
if (wallet == null) {
return;
}

final node = getCurrentNode();

_setSyncStatus(ConnectingSyncStatus());
try {
if (_requireMutex) {
await _torConnectingLock.protect(() async {
if (_torNodeMismatchGuard(node)) {
throw Exception("TOR – clearnet mismatch");
}

final host = node.host.endsWith(".onion")
? node.host
: Uri.parse(node.host).host;
final ({InternetAddress host, int port})? proxy =
AppConfig.hasFeature(AppFeature.tor) &&
prefs.useTor &&
!node.forceNoTor
? TorService.sharedInstance.getProxyInfo()
: null;

_setSyncStatus(ConnectingSyncStatus());
try {
if (_requireMutex) {
await _torConnectingLock.protect(() async {
await csSalvium.connect(
wallet!,
daemonAddress: "$host:${node.port}",
daemonUsername: node.loginName,
daemonPassword: await node.getPassword(secureStorageInterface),
trusted: node.trusted ?? false,
useSSL: node.useSSL,
socksProxyAddress: node.forceNoTor
? null
: proxy == null
? null
: "${proxy.host.address}:${proxy.port}",
);
});
} else {
await csSalvium.connect(
wallet!,
daemonAddress: "$host:${node.port}",
Expand All @@ -512,37 +547,21 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
? null
: "${proxy.host.address}:${proxy.port}",
);
});
} else {
await csSalvium.connect(
wallet!,
daemonAddress: "$host:${node.port}",
daemonUsername: node.loginName,
daemonPassword: await node.getPassword(secureStorageInterface),
trusted: node.trusted ?? false,
useSSL: node.useSSL,
socksProxyAddress: node.forceNoTor
? null
: proxy == null
? null
: "${proxy.host.address}:${proxy.port}",
}
csSalvium.startSyncing(wallet!);
csSalvium.startListeners(wallet!);
csSalvium.startAutoSaving(wallet!);

// _setSyncStatus(ConnectedSyncStatus());
} catch (e, s) {
// _setSyncStatus(FailedSyncStatus());
Logging.instance.e(
"Exception caught in $runtimeType.updateNode(): ",
error: e,
stackTrace: s,
);
}
csSalvium.startSyncing(wallet!);
csSalvium.startListeners(wallet!);
csSalvium.startAutoSaving(wallet!);

// _setSyncStatus(ConnectedSyncStatus());
} catch (e, s) {
// _setSyncStatus(FailedSyncStatus());
Logging.instance.e(
"Exception caught in $runtimeType.updateNode(): ",
error: e,
stackTrace: s,
);
}

return;
});
}

@override
Expand Down Expand Up @@ -727,6 +746,14 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
@override
Future<void> exit() async {
Logging.instance.i("exit called on $walletId");
await _exitNative();
await _torStatusListener?.cancel();
await _torPreferenceListener?.cancel();
_torStatusListener = null;
_torPreferenceListener = null;
}

Future<void> _exitNative() async {
if (wallet != null) {
csSalvium.stopAutoSaving(wallet!);
csSalvium.stopListeners(wallet!);
Expand Down Expand Up @@ -1544,7 +1571,7 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
);

if (this.wallet != null) {
await exit();
await _exitNative();
}

this.wallet = wallet;
Expand Down Expand Up @@ -1596,6 +1623,8 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>

final Mutex _torConnectingLock = Mutex();
bool _requireMutex = false;

final Mutex _updateNodeMutex = Mutex();
}

String _libSalviumWalletPasswordKey(String walletName) =>
Expand Down
Loading
Loading