Merge branch 'master' into tooltip

This commit is contained in:
marcosrdz 2021-02-26 18:09:34 -05:00
commit b752c7b832
2 changed files with 85 additions and 2 deletions

View File

@ -1,8 +1,10 @@
/* global alert */
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Platform } from 'react-native';
import { Platform, Alert } from 'react-native';
import { AppStorage, LegacyWallet, SegwitBech32Wallet, SegwitP2SHWallet } from '../class';
import DefaultPreference from 'react-native-default-preference';
import RNWidgetCenter from 'react-native-widget-center';
import loc from '../loc';
const bitcoin = require('bitcoinjs-lib');
const ElectrumClient = require('electrum-client');
const reverse = require('buffer-reverse');
@ -32,6 +34,7 @@ let mainConnected = false;
let wasConnectedAtLeastOnce = false;
let serverName = false;
let disableBatching = false;
let connectionAttempt = 0;
let latestBlockheight = false;
let latestBlockheightTimestamp = false;
@ -96,13 +99,89 @@ async function connectMain() {
if (!mainConnected) {
console.log('retry');
connectionAttempt = connectionAttempt + 1;
mainClient.close && mainClient.close();
setTimeout(connectMain, 500);
if (connectionAttempt >= 5) {
presentNetworkErrorAlert(usingPeer);
} else {
setTimeout(connectMain, 500);
}
}
}
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.
@ -454,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);

View File

@ -274,7 +274,9 @@
"electrum_clear_alert_ok": "Ok",
"electrum_select": "Select",
"electrum_reset": "Reset to default",
"electrum_unable_to_connect": "Unable to connect to {server}.",
"electrum_history": "Server history",
"electrum_reset_to_default": "Are you sure to want to reset your Electrum settings to default?",
"electrum_clear": "Clear",
"encrypt_decrypt": "Decrypt Storage",
"encrypt_decrypt_q": "Are you sure you want to decrypt your storage? This will allow your wallets to be accessed without a password.",