BlueWallet/screen/settings/electrumSettings.js

409 lines
14 KiB
JavaScript
Raw Normal View History

2019-07-18 12:22:01 +02:00
/* global alert */
import React, { Component } from 'react';
2020-12-25 17:09:53 +01:00
import PropTypes from 'prop-types';
2020-12-25 17:20:15 +01:00
import { Alert, View, TextInput, TouchableOpacity, StyleSheet } from 'react-native';
2020-12-25 17:09:53 +01:00
import DefaultPreference from 'react-native-default-preference';
import RNWidgetCenter from 'react-native-widget-center';
2020-12-16 04:15:57 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage';
import { ScrollView } from 'react-native-gesture-handler';
2020-12-25 17:09:53 +01:00
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
2020-12-25 17:09:53 +01:00
import { AppStorage } from '../../class';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../../components/navigationStyle';
import { BlueButton, BlueButtonLink, BlueCard, BlueLoading, BlueSpacing20, BlueText, SafeBlueArea } from '../../BlueComponents';
import { BlueCurrentTheme } from '../../components/themes';
const BlueElectrum = require('../../blue_modules/BlueElectrum');
2019-07-18 12:22:01 +02:00
export default class ElectrumSettings extends Component {
constructor(props) {
super(props);
const server = props?.route?.params?.server;
2019-07-18 12:22:01 +02:00
this.state = {
isLoading: true,
2020-12-25 17:20:15 +01:00
serverHistory: [],
2019-07-18 12:22:01 +02:00
config: {},
server,
2019-07-18 12:22:01 +02:00
};
}
componentWillUnmount() {
clearInterval(this.state.inverval);
}
2019-07-18 12:22:01 +02:00
async componentDidMount() {
const host = await AsyncStorage.getItem(AppStorage.ELECTRUM_HOST);
const port = await AsyncStorage.getItem(AppStorage.ELECTRUM_TCP_PORT);
const sslPort = await AsyncStorage.getItem(AppStorage.ELECTRUM_SSL_PORT);
2020-12-25 17:20:15 +01:00
const serverHistoryStr = await AsyncStorage.getItem(AppStorage.ELECTRUM_SERVER_HISTORY);
const serverHistory = JSON.parse(serverHistoryStr) || [];
2019-07-18 12:22:01 +02:00
this.setState({
isLoading: false,
host,
port,
sslPort,
2020-12-25 17:20:15 +01:00
serverHistory,
2019-07-18 12:22:01 +02:00
});
const inverval = setInterval(async () => {
this.setState({
config: await BlueElectrum.getConfig(),
});
2020-06-11 19:34:28 +02:00
}, 500);
2020-04-01 17:41:50 +02:00
this.setState({
2019-07-18 12:22:01 +02:00
config: await BlueElectrum.getConfig(),
inverval,
2019-07-18 12:22:01 +02:00
});
if (this.state.server) {
Alert.alert(
loc.formatString(loc.settings.set_electrum_server_as_default, { server: this.state.server }),
'',
[
{
text: loc._.ok,
onPress: () => {
this.onBarScanned(this.state.server);
},
style: 'default',
},
{ text: loc._.cancel, onPress: () => {}, style: 'cancel' },
],
{ cancelable: false },
);
}
2019-07-18 12:22:01 +02:00
}
checkServer = async () => {
this.setState({ isLoading: true }, async () => {
const features = await BlueElectrum.serverFeatures();
alert(JSON.stringify(features, null, 2));
this.setState({ isLoading: false });
});
};
2020-12-25 17:20:15 +01:00
selectServer = async server => {
this.setState({ host: server.host, port: server.port, sslPort: server.sslPort }, () => {
this.save();
});
};
clearHistoryAlert() {
Alert.alert(loc.settings.electrum_clear_alert_title, loc.settings.electrum_clear_alert_message, [
{ text: loc.settings.electrum_clear_alert_cancel, onPress: () => console.log('Cancel Pressed'), style: 'cancel' },
{ text: loc.settings.electrum_clear_alert_ok, onPress: () => this.clearHistory() },
]);
}
clearHistory = async () => {
this.setState({ isLoading: true }, async () => {
await AsyncStorage.setItem(AppStorage.ELECTRUM_SERVER_HISTORY, JSON.stringify([]));
this.setState({
serverHistory: [],
isLoading: false,
});
});
};
resetToDefault = async () => {
this.setState({ port: '', host: '', sslPort: '' }, () => {
this.save();
});
};
serverExists = server => {
const { serverHistory } = this.state;
return serverHistory.some(s => {
return `${s.host}${s.port}${s.sslPort}` === `${server.host}${server.port}${server.sslPort}`;
});
};
2019-07-18 12:22:01 +02:00
save = () => {
const host = this.state.host ? this.state.host : '';
const port = this.state.port ? this.state.port : '';
const sslPort = this.state.sslPort ? this.state.sslPort : '';
2020-12-25 17:20:15 +01:00
const serverHistory = this.state.serverHistory || [];
2019-07-18 12:22:01 +02:00
this.setState({ isLoading: true }, async () => {
try {
if (!host && !port && !sslPort) {
2019-07-19 22:19:53 +02:00
await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, '');
await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, '');
await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, '');
2020-11-02 14:11:28 +01:00
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);
}
2020-07-20 15:38:46 +02:00
alert(loc.settings.electrum_saved);
} else if (!(await BlueElectrum.testConnection(host, port, sslPort))) {
2020-07-20 15:38:46 +02:00
alert(loc.settings.electrum_error_connect);
2019-07-18 12:22:01 +02:00
} else {
await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, host);
await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, port);
await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, sslPort);
2020-12-25 17:20:15 +01:00
if (!this.serverExists({ host, port, sslPort })) {
serverHistory.push({
host,
port,
sslPort,
});
await AsyncStorage.setItem(AppStorage.ELECTRUM_SERVER_HISTORY, JSON.stringify(serverHistory));
}
2020-11-02 14:11:28 +01:00
try {
await DefaultPreference.setName('group.io.bluewallet.bluewallet');
await DefaultPreference.set(AppStorage.ELECTRUM_HOST, host);
await DefaultPreference.set(AppStorage.ELECTRUM_TCP_PORT, port);
await DefaultPreference.set(AppStorage.ELECTRUM_SSL_PORT, sslPort);
RNWidgetCenter.reloadAllTimelines();
} catch (e) {
// Must be running on Android
console.log(e);
}
2020-07-20 15:38:46 +02:00
alert(loc.settings.electrum_saved);
2019-07-18 12:22:01 +02:00
}
2020-06-11 19:34:28 +02:00
} catch (error) {
alert(error);
}
2019-07-18 12:22:01 +02:00
this.setState({ isLoading: false });
});
};
2020-10-26 16:31:23 +01:00
onBarScanned = value => {
if (DeeplinkSchemaMatch.getServerFromSetElectrumServerAction(value)) {
// in case user scans a QR with a deeplink like `bluewallet:setelectrumserver?server=electrum1.bluewallet.io%3A443%3As`
value = DeeplinkSchemaMatch.getServerFromSetElectrumServerAction(value);
}
var [host, port, type] = value.split(':');
this.setState({ host: host });
2020-10-26 16:31:23 +01:00
type === 's' ? this.setState({ sslPort: port }) : this.setState({ port: port });
};
importScan = () => {
this.props.navigation.navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: this.props.route.name,
onBarScanned: this.onBarScanned,
showFileImportButton: true,
},
});
};
2019-07-18 12:22:01 +02:00
render() {
2020-12-25 17:20:15 +01:00
const serverHistoryItems = this.state.serverHistory.map((server, i) => {
return (
<View key={i} style={styles.serverHistoryItem}>
<View>
<BlueText>{`${server.host}:${server.port || server.sslPort}`}</BlueText>
</View>
<TouchableOpacity onPress={() => this.selectServer(server)}>
<BlueText>{loc.settings.electrum_select}</BlueText>
</TouchableOpacity>
</View>
);
});
2019-07-18 12:22:01 +02:00
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
2020-03-29 13:16:49 +02:00
<ScrollView>
<BlueCard>
2020-07-20 15:38:46 +02:00
<BlueText style={styles.status}>{loc.settings.electrum_status}</BlueText>
<View style={styles.connectWrap}>
<View style={[styles.container, this.state.config.status === 1 ? styles.containerConnected : styles.containerDisconnected]}>
2020-06-15 12:05:11 +02:00
<BlueText style={this.state.config.status === 1 ? styles.textConnected : styles.textDisconnected}>
2020-07-20 15:38:46 +02:00
{this.state.config.status === 1 ? loc.settings.electrum_connected : loc.settings.electrum_connected_not}
2020-03-29 13:16:49 +02:00
</BlueText>
2020-05-03 20:17:49 +02:00
</View>
2020-03-29 13:16:49 +02:00
</View>
<BlueSpacing20 />
<BlueText style={styles.hostname} onPress={this.checkServer}>
2020-03-29 13:16:49 +02:00
{this.state.config.host}:{this.state.config.port}
</BlueText>
</BlueCard>
<BlueCard>
2020-12-25 17:20:15 +01:00
<View style={styles.serverAddTitle}>
<BlueText style={styles.explain}>{loc.settings.electrum_settings_explain}</BlueText>
2021-03-01 11:20:01 +01:00
<TouchableOpacity testID="ResetToDefault" onPress={() => this.resetToDefault()}>
2020-12-25 17:20:15 +01:00
<BlueText>{loc.settings.electrum_reset}</BlueText>
</TouchableOpacity>
</View>
2020-03-29 13:16:49 +02:00
</BlueCard>
<BlueCard>
<View style={styles.inputWrap}>
2020-03-29 13:16:49 +02:00
<TextInput
2020-07-20 15:38:46 +02:00
placeholder={loc.formatString(loc.settings.electrum_host, { example: '111.222.333.111' })}
2020-03-29 13:16:49 +02:00
value={this.state.host}
onChangeText={text => this.setState({ host: text.trim() })}
2020-03-29 13:16:49 +02:00
numberOfLines={1}
style={styles.inputText}
2020-03-29 13:16:49 +02:00
editable={!this.state.isLoading}
placeholderTextColor="#81868e"
2020-08-26 05:22:25 +02:00
autoCorrect={false}
2020-11-02 14:11:28 +01:00
autoCapitalize="none"
2020-03-29 13:16:49 +02:00
underlineColorAndroid="transparent"
2021-03-01 11:20:01 +01:00
testID="HostInput"
2020-03-29 13:16:49 +02:00
/>
</View>
<BlueSpacing20 />
<View style={styles.inputWrap}>
2020-03-29 13:16:49 +02:00
<TextInput
2020-07-20 15:38:46 +02:00
placeholder={loc.formatString(loc.settings.electrum_port, { example: '50001' })}
2020-03-29 13:16:49 +02:00
value={this.state.port}
onChangeText={text => this.setState({ port: text.trim() })}
2020-03-29 13:16:49 +02:00
numberOfLines={1}
style={styles.inputText}
2020-03-29 13:16:49 +02:00
editable={!this.state.isLoading}
placeholderTextColor="#81868e"
2020-03-29 13:16:49 +02:00
underlineColorAndroid="transparent"
2020-08-26 05:22:25 +02:00
autoCorrect={false}
2020-11-02 14:11:28 +01:00
autoCapitalize="none"
2021-03-01 11:20:01 +01:00
testID="PortInput"
2020-03-29 13:16:49 +02:00
/>
</View>
<BlueSpacing20 />
<View style={styles.inputWrap}>
2020-03-29 13:16:49 +02:00
<TextInput
2020-07-20 15:38:46 +02:00
placeholder={loc.formatString(loc.settings.electrum_port_ssl, { example: '50002' })}
2020-03-29 13:16:49 +02:00
value={this.state.sslPort}
onChangeText={text => this.setState({ sslPort: text.trim() })}
2020-03-29 13:16:49 +02:00
numberOfLines={1}
style={styles.inputText}
2020-03-29 13:16:49 +02:00
editable={!this.state.isLoading}
2020-08-26 05:22:25 +02:00
autoCorrect={false}
placeholderTextColor="#81868e"
2020-11-02 14:11:28 +01:00
autoCapitalize="none"
2020-03-29 13:16:49 +02:00
underlineColorAndroid="transparent"
2021-03-01 11:20:01 +01:00
testID="SSLPortInput"
2020-03-29 13:16:49 +02:00
/>
2020-03-28 22:39:16 +01:00
</View>
<BlueSpacing20 />
<BlueButtonLink title={loc.wallets.import_scan_qr} onPress={this.importScan} />
2020-03-29 13:16:49 +02:00
<BlueSpacing20 />
2021-03-01 11:20:01 +01:00
{this.state.isLoading ? <BlueLoading /> : <BlueButton testID="Save" onPress={this.save} title={loc.settings.save} />}
2020-03-29 13:16:49 +02:00
</BlueCard>
2020-12-25 17:20:15 +01:00
{serverHistoryItems.length > 0 && !this.state.isLoading && (
<BlueCard>
<View style={styles.serverHistoryTitle}>
<BlueText style={styles.explain}>{loc.settings.electrum_history}</BlueText>
<TouchableOpacity onPress={() => this.clearHistoryAlert()}>
<BlueText>{loc.settings.electrum_clear}</BlueText>
</TouchableOpacity>
</View>
{serverHistoryItems}
</BlueCard>
)}
</ScrollView>
2019-07-18 12:22:01 +02:00
</SafeBlueArea>
);
}
}
2020-10-26 16:31:23 +01:00
ElectrumSettings.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
goBack: PropTypes.func,
}),
route: PropTypes.shape({
name: PropTypes.string,
params: PropTypes.shape({
server: PropTypes.string,
}),
2020-10-26 16:31:23 +01:00
}),
};
2021-02-22 08:27:23 +01:00
ElectrumSettings.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.electrum_settings_server }));
2020-10-26 16:31:23 +01:00
const styles = StyleSheet.create({
root: {
flex: 1,
},
status: {
textAlign: 'center',
color: BlueCurrentTheme.colors.feeText,
marginBottom: 4,
},
connectWrap: {
width: 'auto',
height: 34,
flexWrap: 'wrap',
justifyContent: 'center',
flexDirection: 'row',
},
container: {
paddingTop: 6,
paddingBottom: 6,
paddingLeft: 16,
paddingRight: 16,
borderRadius: 20,
},
containerConnected: {
backgroundColor: BlueCurrentTheme.colors.feeLabel,
},
containerDisconnected: {
backgroundColor: BlueCurrentTheme.colors.redBG,
2020-10-26 16:31:23 +01:00
},
textConnected: {
color: BlueCurrentTheme.colors.feeValue,
fontWeight: 'bold',
},
textDisconnected: {
color: BlueCurrentTheme.colors.redText,
2020-10-26 16:31:23 +01:00
fontWeight: 'bold',
},
hostname: {
textAlign: 'center',
color: BlueCurrentTheme.colors.foregroundColor,
},
explain: {
color: BlueCurrentTheme.colors.feeText,
marginBottom: -24,
2021-02-22 08:27:23 +01:00
flexShrink: 1,
2020-10-26 16:31:23 +01:00
},
inputWrap: {
flexDirection: 'row',
borderColor: BlueCurrentTheme.colors.formBorder,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderWidth: 1,
borderBottomWidth: 0.5,
backgroundColor: BlueCurrentTheme.colors.inputBackgroundColor,
minHeight: 44,
height: 44,
alignItems: 'center',
borderRadius: 4,
},
inputText: {
flex: 1,
marginHorizontal: 8,
minHeight: 36,
color: '#81868e',
height: 36,
},
2020-12-25 17:20:15 +01:00
serverAddTitle: {
flexDirection: 'row',
justifyContent: 'space-between',
},
serverHistoryTitle: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingVertical: 10,
},
serverHistoryItem: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingVertical: 20,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderBottomWidth: 1,
},
2020-10-26 16:31:23 +01:00
});