From e14ca13cf0963ef7680185df240c694f3ca8feba Mon Sep 17 00:00:00 2001 From: marcosrdz Date: Thu, 25 Feb 2021 23:01:23 -0500 Subject: [PATCH] Update BlueElectrum.js --- blue_modules/BlueElectrum.js | 139 +++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 65 deletions(-) diff --git a/blue_modules/BlueElectrum.js b/blue_modules/BlueElectrum.js index 7a62a4e4e..a32e9721a 100644 --- a/blue_modules/BlueElectrum.js +++ b/blue_modules/BlueElectrum.js @@ -102,71 +102,7 @@ async function connectMain() { connectionAttempt = connectionAttempt + 1; mainClient.close && mainClient.close(); if (connectionAttempt >= 5) { - Alert.alert( - loc.errors.network, - loc.formatString(loc.settings.electrum_unable_to_connect, { server: `${usingPeer.host}:${usingPeer.ssl ?? usingPeer.tcp}` }), - [ - { - text: loc.wallets.list_tryagain, - onPress: () => { - connectionAttempt = 0; - setTimeout(connectMain, 500); - }, - style: 'default', - }, - { - text: loc.settings.electrum_reset, - onPress: () => { - Alert.alert( - loc.settings.electrum_reset, - loc.settings.electrum_reset_to_default, - [ - { - text: loc._.cancel, - style: 'cancel', - onPress: () => { - connectionAttempt = 0; - }, - }, - { - text: loc._.ok, - style: 'destructive', - onPress: async () => { - await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, ''); - await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, ''); - await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, ''); - try { - await DefaultPreference.setName('group.io.bluewallet.bluewallet'); - await DefaultPreference.clear(AppStorage.ELECTRUM_HOST); - await DefaultPreference.clear(AppStorage.ELECTRUM_SSL_PORT); - await DefaultPreference.clear(AppStorage.ELECTRUM_TCP_PORT); - RNWidgetCenter.reloadAllTimelines(); - } catch (e) { - // Must be running on Android - console.log(e); - } - alert(loc.settings.electrum_saved); - - connectionAttempt = 0; - }, - }, - ], - { cancelable: false }, - ); - connectionAttempt = 0; - }, - style: 'destructive', - }, - { - text: loc._.cancel, - onPress: () => { - connectionAttempt = 0; - }, - style: 'cancel', - }, - ], - { cancelable: false }, - ); + presentNetworkErrorAlert(usingPeer); } else { setTimeout(connectMain, 500); } @@ -175,6 +111,77 @@ async function connectMain() { connectMain(); +async function presentNetworkErrorAlert(usingPeer) { + Alert.alert( + loc.errors.network, + loc.formatString( + usingPeer ? loc.settings.electrum_unable_to_connect : loc.settings.electrum_error_connect, + usingPeer ? { server: `${usingPeer.host}:${usingPeer.ssl ?? usingPeer.tcp}` } : {}, + ), + [ + { + text: loc.wallets.list_tryagain, + onPress: () => { + connectionAttempt = 0; + mainClient.close() && mainClient.close(); + setTimeout(connectMain, 500); + }, + style: 'default', + }, + { + text: loc.settings.electrum_reset, + onPress: () => { + Alert.alert( + loc.settings.electrum_reset, + loc.settings.electrum_reset_to_default, + [ + { + text: loc._.cancel, + style: 'cancel', + onPress: () => {}, + }, + { + text: loc._.ok, + style: 'destructive', + onPress: async () => { + await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, ''); + await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, ''); + await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, ''); + try { + await DefaultPreference.setName('group.io.bluewallet.bluewallet'); + await DefaultPreference.clear(AppStorage.ELECTRUM_HOST); + await DefaultPreference.clear(AppStorage.ELECTRUM_SSL_PORT); + await DefaultPreference.clear(AppStorage.ELECTRUM_TCP_PORT); + RNWidgetCenter.reloadAllTimelines(); + } catch (e) { + // Must be running on Android + console.log(e); + } + alert(loc.settings.electrum_saved); + setTimeout(connectMain, 500); + }, + }, + ], + { cancelable: true }, + ); + connectionAttempt = 0; + mainClient.close() && mainClient.close(); + }, + style: 'destructive', + }, + { + text: loc._.cancel, + onPress: () => { + connectionAttempt = 0; + mainClient.close() && mainClient.close(); + }, + style: 'cancel', + }, + ], + { cancelable: false }, + ); +} + /** * Returns random hardcoded electrum server guaranteed to work * at the time of writing. @@ -526,6 +533,8 @@ module.exports.waitTillConnected = async function () { if (retriesCounter++ >= 30) { clearInterval(waitTillConnectedInterval); + connectionAttempt = 0; + presentNetworkErrorAlert(); reject(new Error('Waiting for Electrum connection timeout')); } }, 500);