Merge remote-tracking branch 'origin/CW-453-silent-payments' into CW-453-silent-payments

This commit is contained in:
OmarHatem 2024-05-25 06:00:46 +03:00
commit c71318ca03
6 changed files with 58 additions and 26 deletions

View file

@ -41,7 +41,7 @@ class ElectrumClient {
bool get isConnected => _isConnected; bool get isConnected => _isConnected;
Socket? socket; Socket? socket;
void Function(bool)? onConnectionStatusChange; void Function(bool?)? onConnectionStatusChange;
int _id; int _id;
final Map<String, SocketTask> _tasks; final Map<String, SocketTask> _tasks;
Map<String, SocketTask> get tasks => _tasks; Map<String, SocketTask> get tasks => _tasks;
@ -91,7 +91,7 @@ class ElectrumClient {
_setIsConnected(false); _setIsConnected(false);
}, onDone: () { }, onDone: () {
unterminatedString = ''; unterminatedString = '';
_setIsConnected(false); _setIsConnected(null);
}); });
keepAlive(); keepAlive();
} }
@ -146,11 +146,12 @@ class ElectrumClient {
await callWithTimeout(method: 'server.ping'); await callWithTimeout(method: 'server.ping');
_setIsConnected(true); _setIsConnected(true);
} on RequestFailedTimeoutException catch (_) { } on RequestFailedTimeoutException catch (_) {
_setIsConnected(false); _setIsConnected(null);
} }
} }
Future<List<String>> version() => call(method: 'server.version').then((dynamic result) { Future<List<String>> version() =>
call(method: 'server.version', params: ["", "1.4"]).then((dynamic result) {
if (result is List) { if (result is List) {
return result.map((dynamic val) => val.toString()).toList(); return result.map((dynamic val) => val.toString()).toList();
} }
@ -287,9 +288,8 @@ class ElectrumClient {
); );
} }
Future<Map<String, dynamic>> getTweaks({required int height}) async => Future<dynamic> getTweaks({required int height}) async =>
await callWithTimeout(method: 'blockchain.tweaks.get', params: [height], timeout: 10000) await callWithTimeout(method: 'blockchain.tweaks.subscribe', params: [height, 1, false]);
as Map<String, dynamic>;
Future<double> estimatefee({required int p}) => Future<double> estimatefee({required int p}) =>
call(method: 'blockchain.estimatefee', params: [p]).then((dynamic result) { call(method: 'blockchain.estimatefee', params: [p]).then((dynamic result) {
@ -480,12 +480,12 @@ class ElectrumClient {
} }
} }
void _setIsConnected(bool isConnected) { void _setIsConnected(bool? isConnected) {
if (_isConnected != isConnected) { if (_isConnected != isConnected) {
onConnectionStatusChange?.call(isConnected); onConnectionStatusChange?.call(isConnected);
} }
_isConnected = isConnected; _isConnected = isConnected ?? false;
} }
void _handleResponse(Map<String, dynamic> response) { void _handleResponse(Map<String, dynamic> response) {

View file

@ -202,7 +202,7 @@ abstract class ElectrumWalletBase
silentPaymentsScanningActive = active; silentPaymentsScanningActive = active;
if (active) { if (active) {
final tip = await getCurrentChainTip(); final tip = await getUpdatedChainTip();
if (tip == walletInfo.restoreHeight) { if (tip == walletInfo.restoreHeight) {
syncStatus = SyncedTipSyncStatus(tip); syncStatus = SyncedTipSyncStatus(tip);
@ -235,7 +235,10 @@ abstract class ElectrumWalletBase
} }
Future<int> getUpdatedChainTip() async { Future<int> getUpdatedChainTip() async {
_currentChainTip = await electrumClient.getCurrentBlockChainTip(); final newTip = await electrumClient.getCurrentBlockChainTip();
if (newTip != null && newTip > (_currentChainTip ?? 0)) {
_currentChainTip = newTip;
}
return _currentChainTip ?? 0; return _currentChainTip ?? 0;
} }
@ -444,13 +447,15 @@ abstract class ElectrumWalletBase
await electrumClient.close(); await electrumClient.close();
electrumClient.onConnectionStatusChange = (bool isConnected) async { electrumClient.onConnectionStatusChange = (bool? isConnected) async {
if (syncStatus is SyncingSyncStatus) return; if (syncStatus is SyncingSyncStatus) return;
if (isConnected && syncStatus is! SyncedSyncStatus) { if (isConnected == true && syncStatus is! SyncedSyncStatus) {
syncStatus = ConnectedSyncStatus(); syncStatus = ConnectedSyncStatus();
} else if (!isConnected) { } else if (isConnected == false) {
syncStatus = LostConnectionSyncStatus(); syncStatus = LostConnectionSyncStatus();
} else if (!(isConnected ?? false) && syncStatus is! ConnectingSyncStatus) {
syncStatus = NotConnectedSyncStatus();
} }
}; };

View file

@ -509,8 +509,7 @@ class CWBitcoin extends Bitcoin {
Future<void> setScanningActive(Object wallet, bool active) async { Future<void> setScanningActive(Object wallet, bool active) async {
final bitcoinWallet = wallet as ElectrumWallet; final bitcoinWallet = wallet as ElectrumWallet;
// TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore if (!(await getNodeIsElectrsSPEnabled(wallet))) {
if (!getNodeIsCakeElectrs(wallet)) {
final node = Node( final node = Node(
useSSL: false, useSSL: false,
uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}', uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}',
@ -535,8 +534,7 @@ class CWBitcoin extends Bitcoin {
@override @override
Future<void> rescan(Object wallet, {required int height, bool? doSingleScan}) async { Future<void> rescan(Object wallet, {required int height, bool? doSingleScan}) async {
final bitcoinWallet = wallet as ElectrumWallet; final bitcoinWallet = wallet as ElectrumWallet;
// TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore if (!(await getNodeIsElectrsSPEnabled(wallet))) {
if (!getNodeIsCakeElectrs(wallet)) {
final node = Node( final node = Node(
useSSL: false, useSSL: false,
uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}', uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}',
@ -547,13 +545,40 @@ class CWBitcoin extends Bitcoin {
bitcoinWallet.rescan(height: height, doSingleScan: doSingleScan); bitcoinWallet.rescan(height: height, doSingleScan: doSingleScan);
} }
@override Future<bool> getNodeIsElectrs(Object wallet) async {
bool getNodeIsCakeElectrs(Object wallet) {
final bitcoinWallet = wallet as ElectrumWallet; final bitcoinWallet = wallet as ElectrumWallet;
final node = bitcoinWallet.node;
return node?.uri.host == 'electrs.cakewallet.com' && final version = await bitcoinWallet.electrumClient.version();
node?.uri.port == (wallet.network == BitcoinNetwork.testnet ? 50002 : 50001);
if (version.isEmpty) {
return false;
}
final server = version[0];
if (server.toLowerCase().contains('electrs')) {
return true;
}
return false;
}
@override
Future<bool> getNodeIsElectrsSPEnabled(Object wallet) async {
if (!(await getNodeIsElectrs(wallet))) {
return false;
}
final bitcoinWallet = wallet as ElectrumWallet;
final tweaksResponse = await bitcoinWallet.electrumClient.getTweaks(height: 0);
print('tweaksResponse: $tweaksResponse');
if (tweaksResponse != null) {
return true;
}
return false;
} }
@override @override

View file

@ -329,7 +329,8 @@ class CryptoBalanceWidget extends StatelessWidget {
final isSilentPaymentsScanningActive = dashboardViewModel.silentPaymentsScanningActive; final isSilentPaymentsScanningActive = dashboardViewModel.silentPaymentsScanningActive;
final newValue = !isSilentPaymentsScanningActive; final newValue = !isSilentPaymentsScanningActive;
final needsToSwitch = bitcoin!.getNodeIsCakeElectrs(dashboardViewModel.wallet) == false; final needsToSwitch = !isSilentPaymentsScanningActive &&
await bitcoin!.getNodeIsElectrsSPEnabled(dashboardViewModel.wallet) == false;
if (needsToSwitch) { if (needsToSwitch) {
return showPopUp<void>( return showPopUp<void>(

View file

@ -55,7 +55,8 @@ class RescanPage extends BasePage {
} }
Future<void> _toggleSilentPaymentsScanning(BuildContext context) async { Future<void> _toggleSilentPaymentsScanning(BuildContext context) async {
final needsToSwitch = bitcoin!.getNodeIsCakeElectrs(_rescanViewModel.wallet) == false; final needsToSwitch =
await bitcoin!.getNodeIsElectrsSPEnabled(_rescanViewModel.wallet) == false;
if (needsToSwitch) { if (needsToSwitch) {
return showPopUp<void>( return showPopUp<void>(

View file

@ -213,7 +213,7 @@ abstract class Bitcoin {
{int? outputsCount, int? size}); {int? outputsCount, int? size});
int getHeightByDate({required DateTime date}); int getHeightByDate({required DateTime date});
Future<void> rescan(Object wallet, {required int height, bool? doSingleScan}); Future<void> rescan(Object wallet, {required int height, bool? doSingleScan});
bool getNodeIsCakeElectrs(Object wallet); Future<bool> getNodeIsElectrsSPEnabled(Object wallet);
void deleteSilentPaymentAddress(Object wallet, String address); void deleteSilentPaymentAddress(Object wallet, String address);
Future<void> updateFeeRates(Object wallet); Future<void> updateFeeRates(Object wallet);
int getMaxCustomFeeRate(Object wallet); int getMaxCustomFeeRate(Object wallet);