BlueWallet/screen/wallets/add.js

352 lines
14 KiB
JavaScript
Raw Normal View History

2019-05-19 21:49:42 +02:00
/* global alert */
2018-01-30 23:42:38 +01:00
import React, { Component } from 'react';
2019-05-19 21:49:42 +02:00
import { Alert, Text, LayoutAnimation, ActivityIndicator, Keyboard, KeyboardAvoidingView, Platform, View, TextInput } from 'react-native';
2019-05-02 22:33:03 +02:00
import AsyncStorage from '@react-native-community/async-storage';
2018-07-14 20:54:27 +02:00
import {
BlueTextCentered,
2018-08-15 01:49:49 +02:00
BlueText,
2018-07-14 20:54:27 +02:00
LightningButton,
BitcoinButton,
BlueFormLabel,
BlueButton,
SafeBlueArea,
2019-05-19 21:49:42 +02:00
BlueFormInput,
BlueNavigationStyle,
2019-05-19 21:49:42 +02:00
BlueButtonLink,
2018-10-31 21:34:12 +01:00
BlueSpacing20,
2019-05-22 14:08:31 +02:00
BlueSpacing10,
2018-07-14 20:54:27 +02:00
} from '../../BlueComponents';
2018-08-15 01:49:49 +02:00
import { RadioGroup, RadioButton } from 'react-native-flexi-radio-button';
2018-03-18 03:48:23 +01:00
import PropTypes from 'prop-types';
2018-08-15 01:49:49 +02:00
import { HDSegwitP2SHWallet } from '../../class/hd-segwit-p2sh-wallet';
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
2019-06-01 22:44:39 +02:00
import { AppStorage, HDSegwitBech32Wallet, SegwitP2SHWallet } from '../../class';
2018-12-13 17:50:18 +01:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2018-03-17 21:39:21 +01:00
let EV = require('../../events');
let A = require('../../analytics');
2018-07-06 18:28:04 +02:00
/** @type {AppStorage} */
2018-03-18 03:48:23 +01:00
let BlueApp = require('../../BlueApp');
2018-05-28 21:18:11 +02:00
let loc = require('../../loc');
2018-01-30 23:42:38 +01:00
export default class WalletsAdd extends Component {
static navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(navigation, true),
title: loc.wallets.add.title,
headerLeft: null,
});
2018-01-30 23:42:38 +01:00
constructor(props) {
super(props);
this.state = {
isLoading: true,
2019-05-19 21:49:42 +02:00
walletBaseURI: '',
2018-03-17 21:39:21 +01:00
};
2018-01-30 23:42:38 +01:00
}
async componentDidMount() {
2019-05-19 21:49:42 +02:00
let walletBaseURI = await AsyncStorage.getItem(AppStorage.LNDHUB);
let isAdvancedOptionsEnabled = !!(await AsyncStorage.getItem(AppStorage.ADVANCED_MODE_ENABLED));
2019-06-02 23:32:56 +02:00
walletBaseURI = walletBaseURI || '';
2018-01-30 23:42:38 +01:00
this.setState({
isLoading: false,
2019-05-19 21:49:42 +02:00
activeBitcoin: undefined,
2018-07-14 20:54:27 +02:00
label: '',
2019-05-19 21:49:42 +02:00
isAdvancedOptionsEnabled,
walletBaseURI,
2018-03-17 21:39:21 +01:00
});
2018-01-30 23:42:38 +01:00
}
2018-07-14 20:54:27 +02:00
setLabel(text) {
this.setState({
label: text,
}); /* also, a hack to make screen update new typed text */
}
2018-08-15 01:49:49 +02:00
onSelect(index, value) {
this.setState({
selectedIndex: index,
selectedValue: value,
});
}
2019-05-19 21:49:42 +02:00
showAdvancedOptions = () => {
Keyboard.dismiss();
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
this.setState({ isAdvancedOptionsEnabled: true });
};
2018-01-30 23:42:38 +01:00
render() {
if (this.state.isLoading) {
return (
2018-03-17 21:39:21 +01:00
<View style={{ flex: 1, paddingTop: 20 }}>
2018-01-30 23:42:38 +01:00
<ActivityIndicator />
</View>
);
}
return (
2018-07-07 15:04:32 +02:00
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1, paddingTop: 40 }}>
2019-05-19 21:49:42 +02:00
<KeyboardAvoidingView enabled behavior={Platform.OS === 'ios' ? 'position' : null} keyboardVerticalOffset={20}>
<BlueFormLabel>{loc.wallets.add.wallet_name}</BlueFormLabel>
<View
style={{
flexDirection: 'row',
borderColor: '#d2d2d2',
borderBottomColor: '#d2d2d2',
borderWidth: 1.0,
borderBottomWidth: 0.5,
backgroundColor: '#f5f5f5',
minHeight: 44,
height: 44,
marginHorizontal: 20,
alignItems: 'center',
marginVertical: 16,
borderRadius: 4,
}}
>
<TextInput
value={this.state.label}
placeholderTextColor="#81868e"
placeholder="my first wallet"
onChangeText={text => {
this.setLabel(text);
}}
2019-05-19 21:49:42 +02:00
style={{ flex: 1, marginHorizontal: 8, color: '#81868e' }}
editable={!this.state.isLoading}
underlineColorAndroid="transparent"
/>
</View>
<BlueFormLabel>{loc.wallets.add.wallet_type}</BlueFormLabel>
2019-05-19 21:49:42 +02:00
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
paddingTop: 10,
marginHorizontal: 20,
borderWidth: 0,
minHeight: 100,
}}
>
<BitcoinButton
active={this.state.activeBitcoin}
onPress={() => {
Keyboard.dismiss();
this.setState({
activeBitcoin: true,
activeLightning: false,
});
}}
style={{
width: '45%',
2019-05-19 21:49:42 +02:00
height: 88,
}}
/>
<View style={{ borderWidth: 0, justifyContent: 'center', marginHorizontal: 8, alignSelf: 'center' }}>
<BlueTextCentered style={{ color: '#0c2550' }}>{loc.wallets.add.or}</BlueTextCentered>
2018-07-14 20:54:27 +02:00
</View>
2019-05-19 21:49:42 +02:00
<LightningButton
active={this.state.activeLightning}
onPress={() => {
Keyboard.dismiss();
this.setState({
activeBitcoin: false,
activeLightning: true,
});
}}
style={{
width: '45%',
2019-05-19 21:49:42 +02:00
height: 88,
}}
/>
</View>
2018-07-14 20:54:27 +02:00
2019-05-19 21:49:42 +02:00
<View style={{ marginHorizontal: 20 }}>
{(() => {
if (this.state.activeBitcoin && this.state.isAdvancedOptionsEnabled) {
return (
<View
style={{
2019-06-01 22:44:39 +02:00
height: 140,
2019-05-19 21:49:42 +02:00
}}
>
<BlueSpacing20 />
<Text style={{ color: '#0c2550', fontWeight: '500' }}>{loc.settings.advanced_options}</Text>
<RadioGroup onSelect={(index, value) => this.onSelect(index, value)} selectedIndex={0}>
<RadioButton value={HDSegwitP2SHWallet.type}>
<BlueText>{HDSegwitP2SHWallet.typeReadable} - Multiple addresses</BlueText>
</RadioButton>
<RadioButton value={SegwitP2SHWallet.type}>
<BlueText>{SegwitP2SHWallet.typeReadable} - Single address</BlueText>
</RadioButton>
2019-06-01 22:44:39 +02:00
<RadioButton value={HDSegwitBech32Wallet.type}>
<BlueText>{HDSegwitBech32Wallet.typeReadable} - Multiple addresses</BlueText>
</RadioButton>
2019-05-19 21:49:42 +02:00
</RadioGroup>
</View>
);
} else if (this.state.activeLightning && this.state.isAdvancedOptionsEnabled) {
return (
<React.Fragment>
<BlueSpacing20 />
<Text style={{ color: '#0c2550', fontWeight: '500' }}>{loc.settings.advanced_options}</Text>
<BlueSpacing20 />
<BlueText>Connect to your LNDHub</BlueText>
<BlueFormInput
value={this.state.walletBaseURI}
onChangeText={text => {
this.setState({ walletBaseURI: text });
}}
2019-05-19 21:49:42 +02:00
onSubmitEditing={Keyboard.dismiss}
placeholder="your node address"
clearButtonMode="while-editing"
autoCapitalize="none"
/>
</React.Fragment>
);
} else if (this.state.activeBitcoin === undefined && this.state.isAdvancedOptionsEnabled) {
return <View />;
}
})()}
<View
style={{
alignItems: 'center',
2019-05-19 21:49:42 +02:00
flex: 1,
marginVertical: 32,
2018-07-14 20:54:27 +02:00
}}
>
{!this.state.isLoading ? (
<BlueButton
title={loc.wallets.add.create}
2019-05-19 21:49:42 +02:00
disabled={this.state.activeBitcoin === undefined}
onPress={() => {
this.setState(
{ isLoading: true },
async () => {
let w;
if (this.state.activeLightning) {
// eslint-disable-next-line
this.createLightningWallet = async () => {
w = new LightningCustodianWallet();
2019-05-19 21:49:42 +02:00
w.setLabel(this.state.label || loc.wallets.details.title);
try {
2019-05-19 21:49:42 +02:00
let lndhub =
this.state.walletBaseURI.trim().length > 0
? this.state.walletBaseURI
: LightningCustodianWallet.defaultBaseUri;
if (lndhub) {
2019-05-19 21:49:42 +02:00
const isValidNodeAddress = await LightningCustodianWallet.isValidNodeAddress(lndhub);
if (isValidNodeAddress) {
w.setBaseURI(lndhub);
w.init();
} else {
throw new Error('The provided node address is not valid LNDHub node.');
}
}
await w.createAccount();
await w.authorize();
} catch (Err) {
this.setState({ isLoading: false });
console.warn('lnd create failure', Err);
2019-05-19 21:49:42 +02:00
return alert(Err);
// giving app, not adding anything
}
A(A.ENUM.CREATED_LIGHTNING_WALLET);
await w.generate();
BlueApp.wallets.push(w);
await BlueApp.saveToDisk();
EV(EV.enum.WALLETS_COUNT_CHANGED);
A(A.ENUM.CREATED_WALLET);
2019-05-03 14:36:11 +02:00
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
this.props.navigation.dismiss();
};
if (!BlueApp.getWallets().some(wallet => wallet.type !== LightningCustodianWallet.type)) {
Alert.alert(
loc.wallets.add.lightning,
loc.wallets.createBitcoinWallet,
[
{
text: loc.send.details.cancel,
style: 'cancel',
onPress: () => {
this.setState({ isLoading: false });
},
},
{
text: loc._.ok,
style: 'default',
onPress: () => {
this.createLightningWallet();
},
},
],
{ cancelable: false },
);
} else {
this.createLightningWallet();
}
2019-06-01 22:44:39 +02:00
} else if (this.state.selectedIndex === 2) {
// btc was selected
// index 2 radio - hd bip84
w = new HDSegwitBech32Wallet();
w.setLabel(this.state.label || loc.wallets.details.title);
} else if (this.state.selectedIndex === 1) {
// btc was selected
// index 1 radio - segwit single address
w = new SegwitP2SHWallet();
2019-05-19 21:49:42 +02:00
w.setLabel(this.state.label || loc.wallets.details.title);
} else {
// zero index radio - HD segwit
w = new HDSegwitP2SHWallet();
2019-05-19 21:49:42 +02:00
w.setLabel(this.state.label || loc.wallets.details.title);
}
if (this.state.activeBitcoin) {
await w.generate();
BlueApp.wallets.push(w);
await BlueApp.saveToDisk();
EV(EV.enum.WALLETS_COUNT_CHANGED);
A(A.ENUM.CREATED_WALLET);
2019-05-03 14:36:11 +02:00
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
2019-06-01 22:44:39 +02:00
if (w.type === HDSegwitP2SHWallet.type || w.type === HDSegwitBech32Wallet.type) {
this.props.navigation.navigate('PleaseBackup', {
secret: w.getSecret(),
});
} else {
this.props.navigation.dismiss();
}
}
},
1,
);
}}
/>
) : (
<ActivityIndicator />
)}
</View>
2019-05-22 14:08:31 +02:00
<BlueSpacing10 />
<BlueButtonLink
title={loc.wallets.add.import_wallet}
onPress={() => {
this.props.navigation.navigate('ImportWallet');
}}
/>
2019-05-19 21:49:42 +02:00
</View>
</KeyboardAvoidingView>
2018-01-30 23:42:38 +01:00
</SafeBlueArea>
);
}
}
2018-03-18 03:48:23 +01:00
WalletsAdd.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
goBack: PropTypes.func,
2018-12-29 19:24:51 +01:00
dismiss: PropTypes.func,
2018-03-18 03:48:23 +01:00
}),
};