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

View file

@ -202,7 +202,7 @@ abstract class ElectrumWalletBase
silentPaymentsScanningActive = active;
if (active) {
final tip = await getCurrentChainTip();
final tip = await getUpdatedChainTip();
if (tip == walletInfo.restoreHeight) {
syncStatus = SyncedTipSyncStatus(tip);
@ -235,7 +235,10 @@ abstract class ElectrumWalletBase
}
Future<int> getUpdatedChainTip() async {
_currentChainTip = await electrumClient.getCurrentBlockChainTip();
final newTip = await electrumClient.getCurrentBlockChainTip();
if (newTip != null && newTip > (_currentChainTip ?? 0)) {
_currentChainTip = newTip;
}
return _currentChainTip ?? 0;
}
@ -444,13 +447,15 @@ abstract class ElectrumWalletBase
await electrumClient.close();
electrumClient.onConnectionStatusChange = (bool isConnected) async {
electrumClient.onConnectionStatusChange = (bool? isConnected) async {
if (syncStatus is SyncingSyncStatus) return;
if (isConnected && syncStatus is! SyncedSyncStatus) {
if (isConnected == true && syncStatus is! SyncedSyncStatus) {
syncStatus = ConnectedSyncStatus();
} else if (!isConnected) {
} else if (isConnected == false) {
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 {
final bitcoinWallet = wallet as ElectrumWallet;
// TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore
if (!getNodeIsCakeElectrs(wallet)) {
if (!(await getNodeIsElectrsSPEnabled(wallet))) {
final node = Node(
useSSL: false,
uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}',
@ -535,8 +534,7 @@ class CWBitcoin extends Bitcoin {
@override
Future<void> rescan(Object wallet, {required int height, bool? doSingleScan}) async {
final bitcoinWallet = wallet as ElectrumWallet;
// TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore
if (!getNodeIsCakeElectrs(wallet)) {
if (!(await getNodeIsElectrsSPEnabled(wallet))) {
final node = Node(
useSSL: false,
uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}',
@ -547,13 +545,40 @@ class CWBitcoin extends Bitcoin {
bitcoinWallet.rescan(height: height, doSingleScan: doSingleScan);
}
@override
bool getNodeIsCakeElectrs(Object wallet) {
Future<bool> getNodeIsElectrs(Object wallet) async {
final bitcoinWallet = wallet as ElectrumWallet;
final node = bitcoinWallet.node;
return node?.uri.host == 'electrs.cakewallet.com' &&
node?.uri.port == (wallet.network == BitcoinNetwork.testnet ? 50002 : 50001);
final version = await bitcoinWallet.electrumClient.version();
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

View file

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

View file

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

View file

@ -213,7 +213,7 @@ abstract class Bitcoin {
{int? outputsCount, int? size});
int getHeightByDate({required DateTime date});
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);
Future<void> updateFeeRates(Object wallet);
int getMaxCustomFeeRate(Object wallet);