BlueWallet/screen/wallets/add.js

447 lines
15 KiB
JavaScript
Raw Normal View History

2019-05-19 20:49:42 +01:00
/* global alert */
2018-01-30 22:42:38 +00:00
import React, { Component } from 'react';
import {
Text,
ScrollView,
LayoutAnimation,
ActivityIndicator,
Keyboard,
KeyboardAvoidingView,
Platform,
View,
StatusBar,
TextInput,
StyleSheet,
} from 'react-native';
2019-05-02 16:33:03 -04:00
import AsyncStorage from '@react-native-community/async-storage';
2018-07-14 19:54:27 +01:00
import {
BlueTextCentered,
2018-08-15 00:49:49 +01:00
BlueText,
2020-05-03 14:17:49 -04:00
BlueListItem,
2018-07-14 19:54:27 +01:00
LightningButton,
BitcoinButton,
BlueFormLabel,
BlueButton,
BlueNavigationStyle,
2019-05-19 20:49:42 +01:00
BlueButtonLink,
2018-10-31 20:34:12 +00:00
BlueSpacing20,
2018-07-14 19:54:27 +01:00
} from '../../BlueComponents';
2018-03-18 02:48:23 +00:00
import PropTypes from 'prop-types';
import { HDSegwitBech32Wallet, SegwitP2SHWallet, HDSegwitP2SHWallet, LightningCustodianWallet, AppStorage } from '../../class';
2020-07-15 13:32:59 -04:00
import { BlueCurrentTheme } from '../../components/themes';
2018-12-13 11:50:18 -05:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2020-05-03 14:17:49 -04:00
import { Icon } from 'react-native-elements';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
const EV = require('../../blue_modules/events');
const A = require('../../blue_modules/analytics');
const BlueApp: AppStorage = require('../../BlueApp');
const styles = StyleSheet.create({
loading: {
flex: 1,
paddingTop: 20,
2020-07-15 13:32:59 -04:00
backgroundColor: BlueCurrentTheme.colors.elevated,
},
label: {
flexDirection: 'row',
2020-07-15 13:32:59 -04:00
borderColor: BlueCurrentTheme.colors.formBorder,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderWidth: 1,
borderBottomWidth: 0.5,
2020-07-15 13:32:59 -04:00
backgroundColor: BlueCurrentTheme.colors.inputBackgroundColor,
minHeight: 44,
height: 44,
marginHorizontal: 20,
alignItems: 'center',
marginVertical: 16,
borderRadius: 4,
},
textInputCommon: {
flex: 1,
marginHorizontal: 8,
color: '#81868e',
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingTop: 10,
marginHorizontal: 20,
borderWidth: 0,
minHeight: 100,
},
button: {
width: '45%',
height: 88,
},
or: {
borderWidth: 0,
justifyContent: 'center',
marginHorizontal: 8,
alignSelf: 'center',
},
orCenter: {
color: '#0c2550',
},
advanced: {
marginHorizontal: 20,
},
advancedText: {
2020-07-15 13:32:59 -04:00
color: BlueCurrentTheme.colors.feeText,
fontWeight: '500',
},
lndUri: {
flexDirection: 'row',
2020-07-15 13:32:59 -04:00
borderColor: BlueCurrentTheme.colors.formBorder,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderWidth: 1,
borderBottomWidth: 0.5,
2020-07-15 13:32:59 -04:00
backgroundColor: BlueCurrentTheme.colors.inputBackgroundColor,
minHeight: 44,
height: 44,
alignItems: 'center',
marginVertical: 16,
borderRadius: 4,
},
createButton: {
alignItems: 'center',
flex: 1,
marginTop: 32,
},
import: {
marginBottom: 0,
marginTop: 24,
},
noPadding: {
paddingHorizontal: 0,
2020-07-15 13:32:59 -04:00
backgroundColor: BlueCurrentTheme.colors.elevated,
},
root: {
backgroundColor: BlueCurrentTheme.colors.elevated,
},
});
2018-01-30 22:42:38 +00:00
export default class WalletsAdd extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
2019-05-19 20:49:42 +01:00
walletBaseURI: '',
2020-05-03 14:17:49 -04:00
selectedIndex: 0,
2018-03-17 22:39:21 +02:00
};
2018-01-30 22:42:38 +00:00
}
async componentDidMount() {
2019-05-19 20:49:42 +01:00
let walletBaseURI = await AsyncStorage.getItem(AppStorage.LNDHUB);
const isAdvancedOptionsEnabled = await BlueApp.isAdancedModeEnabled();
2019-06-02 22:32:56 +01:00
walletBaseURI = walletBaseURI || '';
2018-01-30 22:42:38 +00:00
this.setState({
isLoading: false,
2019-05-19 20:49:42 +01:00
activeBitcoin: undefined,
2018-07-14 19:54:27 +01:00
label: '',
2019-05-19 20:49:42 +01:00
isAdvancedOptionsEnabled,
walletBaseURI,
2018-03-17 22:39:21 +02:00
});
2018-01-30 22:42:38 +00:00
}
2018-07-14 19:54:27 +01:00
setLabel(text) {
this.setState({
label: text,
}); /* also, a hack to make screen update new typed text */
}
2020-05-03 14:17:49 -04:00
onSelect(index) {
2018-08-15 00:49:49 +01:00
this.setState({
selectedIndex: index,
});
}
2019-05-19 20:49:42 +01:00
showAdvancedOptions = () => {
Keyboard.dismiss();
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
this.setState({ isAdvancedOptionsEnabled: true });
};
2018-01-30 22:42:38 +00:00
render() {
if (this.state.isLoading) {
return (
<View style={styles.loading}>
2018-01-30 22:42:38 +00:00
<ActivityIndicator />
</View>
);
}
2020-06-29 15:58:43 +03:00
let entropyTitle;
if (!this.state.entropy) {
2020-07-20 16:38:46 +03:00
entropyTitle = loc.wallets.add_entropy_provide;
2020-06-29 15:58:43 +03:00
} else if (this.state.entropy.length < 32) {
2020-07-20 16:38:46 +03:00
entropyTitle = loc.formatString(loc.wallets.add_entropy_remain, {
2020-06-29 15:58:43 +03:00
gen: this.state.entropy.length,
rem: 32 - this.state.entropy.length,
});
} else {
2020-07-20 16:38:46 +03:00
entropyTitle = loc.formatString(loc.wallets.add_entropy_generated, {
2020-06-29 15:58:43 +03:00
gen: this.state.entropy.length,
});
}
2018-01-30 22:42:38 +00:00
return (
2020-07-15 13:32:59 -04:00
<ScrollView style={styles.root}>
<StatusBar barStyle="default" />
<BlueSpacing20 />
<KeyboardAvoidingView enabled behavior={Platform.OS === 'ios' ? 'padding' : null} keyboardVerticalOffset={62}>
2020-07-20 16:38:46 +03:00
<BlueFormLabel>{loc.wallets.add_wallet_name}</BlueFormLabel>
2020-07-15 13:32:59 -04:00
<View style={styles.label}>
<TextInput
testID="WalletNameInput"
value={this.state.label}
placeholderTextColor="#81868e"
placeholder="my first wallet"
onChangeText={text => {
this.setLabel(text);
}}
style={styles.textInputCommon}
editable={!this.state.isLoading}
underlineColorAndroid="transparent"
/>
</View>
2020-07-20 16:38:46 +03:00
<BlueFormLabel>{loc.wallets.add_wallet_type}</BlueFormLabel>
2020-04-04 18:17:07 +02:00
2020-07-15 13:32:59 -04:00
<View style={styles.buttons}>
<BitcoinButton
testID="ActivateBitcoinButton"
active={this.state.activeBitcoin}
onPress={() => {
Keyboard.dismiss();
this.setState({
activeBitcoin: true,
activeLightning: false,
});
}}
style={styles.button}
/>
<View style={styles.or}>
2020-07-20 16:38:46 +03:00
<BlueTextCentered style={styles.orCenter}>{loc.wallets.add_or}</BlueTextCentered>
2020-04-04 18:17:07 +02:00
</View>
2020-07-15 13:32:59 -04:00
<LightningButton
active={this.state.activeLightning}
onPress={() => {
Keyboard.dismiss();
this.setState({
activeBitcoin: false,
activeLightning: true,
});
}}
style={styles.button}
/>
</View>
2018-07-14 19:54:27 +01:00
2020-07-15 13:32:59 -04:00
<View style={styles.advanced}>
{(() => {
if (this.state.activeBitcoin && this.state.isAdvancedOptionsEnabled) {
return (
<View>
<BlueSpacing20 />
<Text style={styles.advancedText}>{loc.settings.advanced_options}</Text>
<BlueListItem
containerStyle={styles.noPadding}
bottomDivider={false}
onPress={() => {
this.onSelect(0, HDSegwitBech32Wallet.type);
}}
title={HDSegwitBech32Wallet.typeReadable}
{...(this.state.selectedIndex === 0
? {
rightIcon: <Icon name="check" type="octaicon" color="#0070FF" />,
}
: { hideChevron: true })}
/>
<BlueListItem
containerStyle={styles.noPadding}
bottomDivider={false}
onPress={() => {
this.onSelect(1, SegwitP2SHWallet.type);
}}
title={SegwitP2SHWallet.typeReadable}
{...(this.state.selectedIndex === 1
? {
rightIcon: <Icon name="check" type="octaicon" color="#0070FF" />,
}
: { hideChevron: true })}
/>
<BlueListItem
containerStyle={styles.noPadding}
bottomDivider={false}
onPress={() => {
this.onSelect(2, HDSegwitP2SHWallet.typeReadable.type);
}}
title={HDSegwitP2SHWallet.typeReadable}
{...(this.state.selectedIndex === 2
? {
rightIcon: <Icon name="check" type="octaicon" color="#0070FF" />,
}
: { hideChevron: true })}
/>
</View>
);
} else if (this.state.activeLightning && this.state.isAdvancedOptionsEnabled) {
return (
<>
<BlueSpacing20 />
<Text style={styles.advancedText}>{loc.settings.advanced_options}</Text>
<BlueSpacing20 />
2020-07-20 16:38:46 +03:00
<BlueText>{loc.wallets.add_lndhub}</BlueText>
2020-07-15 13:32:59 -04:00
<View style={styles.lndUri}>
<TextInput
value={this.state.walletBaseURI}
2020-07-20 16:38:46 +03:00
onChangeText={text => this.setState({ walletBaseURI: text })}
2020-07-15 13:32:59 -04:00
onSubmitEditing={Keyboard.dismiss}
2020-07-20 16:38:46 +03:00
placeholder={loc.wallets.add_lndhub_placeholder}
2020-07-15 13:32:59 -04:00
clearButtonMode="while-editing"
autoCapitalize="none"
placeholderTextColor="#81868e"
style={styles.textInputCommon}
editable={!this.state.isLoading}
underlineColorAndroid="transparent"
2020-05-03 14:17:49 -04:00
/>
2020-04-04 18:17:07 +02:00
</View>
2020-07-15 13:32:59 -04:00
</>
);
} else if (this.state.activeBitcoin === undefined && this.state.isAdvancedOptionsEnabled) {
return <View />;
}
})()}
<View style={styles.createButton}>
{!this.state.isLoading ? (
<BlueButton
testID="Create"
2020-07-20 16:38:46 +03:00
title={loc.wallets.add_create}
2020-07-15 13:32:59 -04:00
disabled={this.state.activeBitcoin === undefined}
onPress={() => {
this.setState({ isLoading: true }, async () => {
let w;
2020-07-15 13:32:59 -04:00
if (this.state.activeLightning) {
this.createLightningWallet = async () => {
w = new LightningCustodianWallet();
2020-07-20 16:38:46 +03:00
w.setLabel(this.state.label || loc.wallets.details_title);
2020-07-15 13:32:59 -04:00
try {
const lndhub =
this.state.walletBaseURI.trim().length > 0
? this.state.walletBaseURI
: LightningCustodianWallet.defaultBaseUri;
if (lndhub) {
const isValidNodeAddress = await LightningCustodianWallet.isValidNodeAddress(lndhub);
if (isValidNodeAddress) {
w.setBaseURI(lndhub);
w.init();
} else {
2020-07-20 16:38:46 +03:00
throw new Error(loc.wallets.add_lndhub_error);
}
2020-04-04 18:17:07 +02:00
}
2020-07-20 16:38:46 +03:00
2020-07-15 13:32:59 -04:00
await w.createAccount();
await w.authorize();
} catch (Err) {
this.setState({ isLoading: false });
console.warn('lnd create failure', Err);
return alert(Err);
// giving app, not adding anything
2020-06-29 15:58:43 +03:00
}
2020-07-15 13:32:59 -04:00
A(A.ENUM.CREATED_LIGHTNING_WALLET);
await w.generate();
2020-04-04 18:17:07 +02:00
BlueApp.wallets.push(w);
await BlueApp.saveToDisk();
EV(EV.enum.WALLETS_COUNT_CHANGED);
A(A.ENUM.CREATED_WALLET);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
2020-07-15 13:32:59 -04:00
this.props.navigation.navigate('PleaseBackupLNDHub', {
wallet: w,
});
};
this.createLightningWallet();
} else if (this.state.selectedIndex === 2) {
// zero index radio - HD segwit
w = new HDSegwitP2SHWallet();
2020-07-20 16:38:46 +03:00
w.setLabel(this.state.label || loc.wallets.details_title);
2020-07-15 13:32:59 -04:00
} else if (this.state.selectedIndex === 1) {
// btc was selected
// index 1 radio - segwit single address
w = new SegwitP2SHWallet();
2020-07-20 16:38:46 +03:00
w.setLabel(this.state.label || loc.wallets.details_title);
2020-07-15 13:32:59 -04:00
} else {
// btc was selected
// index 2 radio - hd bip84
w = new HDSegwitBech32Wallet();
2020-07-20 16:38:46 +03:00
w.setLabel(this.state.label || loc.wallets.details_title);
2020-07-15 13:32:59 -04:00
}
if (this.state.activeBitcoin) {
if (this.state.entropy) {
try {
await w.generateFromEntropy(this.state.entropy);
} catch (e) {
console.log(e.toString());
alert(e.toString());
2020-05-27 14:12:17 +03:00
this.props.navigation.goBack();
2020-07-15 13:32:59 -04:00
return;
}
2020-07-15 13:32:59 -04:00
} else {
await w.generate();
2020-04-04 18:17:07 +02:00
}
2020-07-15 13:32:59 -04:00
BlueApp.wallets.push(w);
await BlueApp.saveToDisk();
EV(EV.enum.WALLETS_COUNT_CHANGED);
A(A.ENUM.CREATED_WALLET);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
if (w.type === HDSegwitP2SHWallet.type || w.type === HDSegwitBech32Wallet.type) {
this.props.navigation.navigate('PleaseBackup', {
secret: w.getSecret(),
});
} else {
this.props.navigation.goBack();
}
}
});
}}
/>
) : (
<ActivityIndicator />
)}
</View>
<BlueButtonLink
testID="ImportWallet"
style={styles.import}
2020-07-20 16:38:46 +03:00
title={loc.wallets.add_import_wallet}
2020-07-15 13:32:59 -04:00
onPress={() => {
this.props.navigation.navigate('ImportWallet');
}}
/>
{this.state.isAdvancedOptionsEnabled && (
2020-04-04 18:17:07 +02:00
<BlueButtonLink
style={styles.import}
2020-07-15 13:32:59 -04:00
title={entropyTitle}
2020-04-04 18:17:07 +02:00
onPress={() => {
2020-07-15 13:32:59 -04:00
this.props.navigation.navigate('ProvideEntropy', { onGenerated: entropy => this.setState({ entropy }) });
2020-04-04 18:17:07 +02:00
}}
/>
2020-07-15 13:32:59 -04:00
)}
</View>
</KeyboardAvoidingView>
</ScrollView>
2018-01-30 22:42:38 +00:00
);
}
}
2018-03-18 02:48:23 +00:00
2020-05-30 01:30:43 -04:00
WalletsAdd.navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(navigation, true),
2020-07-20 16:38:46 +03:00
title: loc.wallets.add_title,
2020-05-30 01:30:43 -04:00
headerLeft: null,
});
2018-03-18 02:48:23 +00:00
WalletsAdd.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
goBack: PropTypes.func,
}),
};