/* global alert */ import React, { Component } from 'react'; import { Alert, AsyncStorage, ActivityIndicator, Keyboard, Dimensions, View, TextInput, TouchableWithoutFeedback } from 'react-native'; import { BlueTextCentered, BlueText, LightningButton, BitcoinButton, BlueButtonLink, BlueFormLabel, BlueButton, SafeBlueArea, BlueCard, BlueNavigationStyle, BlueSpacing20, } from '../../BlueComponents'; import { RadioGroup, RadioButton } from 'react-native-flexi-radio-button'; import PropTypes from 'prop-types'; import { HDSegwitP2SHWallet } from '../../class/hd-segwit-p2sh-wallet'; import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet'; import { AppStorage, SegwitP2SHWallet } from '../../class'; import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; let EV = require('../../events'); let A = require('../../analytics'); /** @type {AppStorage} */ let BlueApp = require('../../BlueApp'); let loc = require('../../loc'); const { width } = Dimensions.get('window'); export default class WalletsAdd extends Component { static navigationOptions = ({ navigation }) => ({ ...BlueNavigationStyle(navigation, true), title: loc.wallets.add.title, headerLeft: null, }); constructor(props) { super(props); this.state = { isLoading: true, }; } async componentDidMount() { this.setState({ isLoading: false, activeBitcoin: true, label: '', }); } setLabel(text) { this.setState({ label: text, }); /* also, a hack to make screen update new typed text */ } onSelect(index, value) { this.setState({ selectedIndex: index, selectedValue: value, }); } render() { if (this.state.isLoading) { return ( ); } return ( {loc.wallets.add.wallet_name} { this.setLabel(text); }} style={{ flex: 1, marginHorizontal: 8, color: '#81868e' }} editable={!this.state.isLoading} underlineColorAndroid="transparent" /> {loc.wallets.add.wallet_type} { this.setState({ activeBitcoin: true, activeLightning: false, }); }} style={{ width: (width - 60) / 3, height: (width - 60) / 3, }} title={loc.wallets.add.create} /> {loc.wallets.add.or} { this.setState({ activeBitcoin: false, activeLightning: true, }); }} style={{ width: (width - 60) / 3, height: (width - 60) / 3, }} title={loc.wallets.add.create} /> {(() => { if (this.state.activeBitcoin) { return ( this.onSelect(index, value)} selectedIndex={0}> {HDSegwitP2SHWallet.typeReadable} {SegwitP2SHWallet.typeReadable} ); } else { return ( ); } })()} {!this.state.isLoading ? ( { this.setState( { isLoading: true }, async () => { let w; if (this.state.activeLightning) { // eslint-disable-next-line let hasBitcoinWallet = false; for (let t of BlueApp.getWallets()) { if (t.type === LightningCustodianWallet.type) { // already exist this.setState({ isLoading: false }); return alert('Only 1 Lightning wallet allowed for now'); } else if (t.type !== LightningCustodianWallet.type) { hasBitcoinWallet = true; } } this.createLightningWallet = async () => { w = new LightningCustodianWallet(); w.setLabel(this.state.label || w.typeReadable); 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 } 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); ReactNativeHapticFeedback.trigger('notificationSuccess', false); this.props.navigation.dismiss(); }; if (!hasBitcoinWallet) { 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(); } } 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'); } 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); ReactNativeHapticFeedback.trigger('notificationSuccess', false); this.props.navigation.dismiss(); } }, 1, ); }} /> ) : ( )} { this.props.navigation.navigate('ImportWallet'); }} /> ); } } WalletsAdd.propTypes = { navigation: PropTypes.shape({ navigate: PropTypes.func, goBack: PropTypes.func, dismiss: PropTypes.func, }), };