2018-01-30 23:42:38 +01:00
|
|
|
import React, { Component } from 'react';
|
2019-01-07 04:50:33 +01:00
|
|
|
import { Alert, AsyncStorage, ActivityIndicator, Keyboard, Dimensions, View, TextInput, TouchableWithoutFeedback } from 'react-native';
|
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,
|
|
|
|
BlueButtonLink,
|
|
|
|
BlueFormLabel,
|
|
|
|
BlueButton,
|
|
|
|
SafeBlueArea,
|
|
|
|
BlueCard,
|
2018-10-29 23:11:48 +01:00
|
|
|
BlueNavigationStyle,
|
2018-10-31 21:34:12 +01:00
|
|
|
BlueSpacing20,
|
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';
|
2018-09-01 01:28:19 +02:00
|
|
|
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
|
2018-11-04 22:21:07 +01:00
|
|
|
import { AppStorage, 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');
|
2019-01-10 16:04:16 +01:00
|
|
|
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-07-14 20:54:27 +02:00
|
|
|
const { width } = Dimensions.get('window');
|
2018-01-30 23:42:38 +01:00
|
|
|
|
|
|
|
export default class WalletsAdd extends Component {
|
2018-10-29 23:11:48 +01:00
|
|
|
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,
|
2018-03-17 21:39:21 +01:00
|
|
|
};
|
2018-01-30 23:42:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
2018-07-14 20:54:27 +02:00
|
|
|
activeBitcoin: true,
|
|
|
|
label: '',
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-01-31 01:56:34 +01:00
|
|
|
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false} style={{ flex: 1 }}>
|
2018-12-11 23:52:46 +01:00
|
|
|
<BlueCard>
|
|
|
|
<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,
|
2018-10-20 23:10:21 +02:00
|
|
|
}}
|
2018-12-11 23:52:46 +01:00
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
value={this.state.label}
|
|
|
|
placeholderTextColor="#81868e"
|
2018-12-20 03:06:06 +01:00
|
|
|
placeholder={this.state.activeBitcoin ? loc.wallets.add.label_new_segwit : loc.wallets.add.label_new_lightning}
|
2018-12-11 23:52:46 +01:00
|
|
|
onChangeText={text => {
|
|
|
|
this.setLabel(text);
|
2018-07-14 20:54:27 +02:00
|
|
|
}}
|
2018-12-11 23:52:46 +01:00
|
|
|
style={{ flex: 1, marginHorizontal: 8, color: '#81868e' }}
|
|
|
|
editable={!this.state.isLoading}
|
|
|
|
underlineColorAndroid="transparent"
|
2018-07-14 20:54:27 +02:00
|
|
|
/>
|
|
|
|
</View>
|
2018-12-11 23:52:46 +01:00
|
|
|
<BlueFormLabel>{loc.wallets.add.wallet_type}</BlueFormLabel>
|
|
|
|
|
|
|
|
<View style={{ flexDirection: 'row', paddingTop: 10, paddingLeft: 20, width: width - 80, borderColor: 'red', borderWidth: 0 }}>
|
|
|
|
<View style={{ width: (width - 60) / 3, height: (width - 60) / 3, backgroundColor: 'transparent' }}>
|
|
|
|
<BitcoinButton
|
|
|
|
active={this.state.activeBitcoin}
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({
|
|
|
|
activeBitcoin: true,
|
|
|
|
activeLightning: false,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
width: (width - 60) / 3,
|
|
|
|
height: (width - 60) / 3,
|
|
|
|
}}
|
|
|
|
title={loc.wallets.add.create}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={{ top: 40, width: (width - 185) / 3, height: 50, borderColor: 'red', borderWidth: 0 }}>
|
|
|
|
<BlueTextCentered style={{ textAlign: 'center' }}>{loc.wallets.add.or}</BlueTextCentered>
|
|
|
|
</View>
|
|
|
|
<View style={{ width: (width - 60) / 3, height: (width - 60) / 3, position: 'absolute', top: 10, right: 0 }}>
|
|
|
|
<LightningButton
|
|
|
|
active={this.state.activeLightning}
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({
|
|
|
|
activeBitcoin: false,
|
|
|
|
activeLightning: true,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
width: (width - 60) / 3,
|
|
|
|
height: (width - 60) / 3,
|
|
|
|
}}
|
|
|
|
title={loc.wallets.add.create}
|
|
|
|
/>
|
|
|
|
</View>
|
2018-07-14 20:54:27 +02:00
|
|
|
</View>
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
<View>
|
|
|
|
{(() => {
|
|
|
|
if (this.state.activeBitcoin) {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
width: 200,
|
|
|
|
height: 100,
|
|
|
|
left: 10,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<RadioGroup onSelect={(index, value) => this.onSelect(index, value)} selectedIndex={0}>
|
2018-12-28 16:52:06 +01:00
|
|
|
<RadioButton value={HDSegwitP2SHWallet.type}>
|
|
|
|
<BlueText>{HDSegwitP2SHWallet.typeReadable}</BlueText>
|
2018-12-11 23:52:46 +01:00
|
|
|
</RadioButton>
|
2018-12-28 16:52:06 +01:00
|
|
|
<RadioButton value={SegwitP2SHWallet.type}>
|
|
|
|
<BlueText>{SegwitP2SHWallet.typeReadable}</BlueText>
|
2018-12-11 23:52:46 +01:00
|
|
|
</RadioButton>
|
|
|
|
</RadioGroup>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
</View>
|
2018-08-15 01:49:49 +02:00
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
alignItems: 'center',
|
2018-07-14 20:54:27 +02:00
|
|
|
}}
|
2018-12-11 23:52:46 +01:00
|
|
|
>
|
2019-01-05 01:23:09 +01:00
|
|
|
{!this.state.isLoading ? (
|
|
|
|
<BlueButton
|
|
|
|
title={loc.wallets.add.create}
|
|
|
|
onPress={() => {
|
|
|
|
this.setState(
|
|
|
|
{ isLoading: true },
|
|
|
|
async () => {
|
|
|
|
let w;
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-05 01:23:09 +01:00
|
|
|
if (this.state.activeLightning) {
|
|
|
|
// eslint-disable-next-line
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-07 04:50:33 +01:00
|
|
|
this.createLightningWallet = async () => {
|
|
|
|
w = new LightningCustodianWallet();
|
|
|
|
w.setLabel(this.state.label || w.typeReadable);
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-07 04:50:33 +01:00
|
|
|
try {
|
|
|
|
let lndhub = await AsyncStorage.getItem(AppStorage.LNDHUB);
|
|
|
|
if (lndhub) {
|
|
|
|
w.setBaseURI(lndhub);
|
|
|
|
w.init();
|
|
|
|
}
|
|
|
|
await w.createAccount();
|
|
|
|
await w.authorize();
|
|
|
|
} catch (Err) {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
console.warn('lnd create failure', Err);
|
|
|
|
// giving app, not adding anything
|
2019-01-05 01:23:09 +01:00
|
|
|
}
|
2019-01-10 16:04:16 +01:00
|
|
|
A(A.ENUM.CREATED_LIGHTNING_WALLET);
|
2019-01-07 04:50:33 +01:00
|
|
|
await w.generate();
|
|
|
|
BlueApp.wallets.push(w);
|
|
|
|
await BlueApp.saveToDisk();
|
|
|
|
EV(EV.enum.WALLETS_COUNT_CHANGED);
|
2019-01-10 16:04:16 +01:00
|
|
|
A(A.ENUM.CREATED_WALLET);
|
2019-01-07 04:50:33 +01:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationSuccess', false);
|
|
|
|
this.props.navigation.dismiss();
|
|
|
|
};
|
|
|
|
|
2019-01-09 16:25:03 +01:00
|
|
|
if (!BlueApp.getWallets().some(wallet => wallet.type !== LightningCustodianWallet.type)) {
|
2019-01-07 04:50:33 +01:00
|
|
|
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-01-05 01:23:09 +01:00
|
|
|
}
|
|
|
|
} else if (this.state.selectedIndex === 1) {
|
|
|
|
// btc was selected
|
|
|
|
// index 1 radio - segwit single address
|
|
|
|
w = new SegwitP2SHWallet();
|
|
|
|
w.setLabel(this.state.label || loc.wallets.add.label_new_segwit);
|
|
|
|
} else {
|
|
|
|
// zero index radio - HD segwit
|
|
|
|
w = new HDSegwitP2SHWallet();
|
|
|
|
w.setLabel((this.state.label || loc.wallets.add.label_new_segwit) + ' HD');
|
2018-12-11 23:52:46 +01:00
|
|
|
}
|
2019-01-07 04:50:33 +01:00
|
|
|
if (this.state.activeBitcoin) {
|
|
|
|
await w.generate();
|
|
|
|
BlueApp.wallets.push(w);
|
|
|
|
await BlueApp.saveToDisk();
|
|
|
|
EV(EV.enum.WALLETS_COUNT_CHANGED);
|
2019-01-10 16:04:16 +01:00
|
|
|
A(A.ENUM.CREATED_WALLET);
|
2019-01-07 04:50:33 +01:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationSuccess', false);
|
|
|
|
this.props.navigation.dismiss();
|
|
|
|
}
|
2019-01-05 01:23:09 +01:00
|
|
|
},
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ActivityIndicator />
|
|
|
|
)}
|
2018-12-11 23:52:46 +01:00
|
|
|
|
|
|
|
<BlueButtonLink
|
|
|
|
title={loc.wallets.add.import_wallet}
|
|
|
|
onPress={() => {
|
|
|
|
this.props.navigation.navigate('ImportWallet');
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</BlueCard>
|
|
|
|
</TouchableWithoutFeedback>
|
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
|
|
|
}),
|
|
|
|
};
|