/* global alert */ import React, { Component } from 'react'; import { ActivityIndicator, View, Text, TextInput, Alert, KeyboardAvoidingView, TouchableOpacity, Keyboard, TouchableWithoutFeedback, Switch, Platform, Linking, } from 'react-native'; import { BlueButton, SafeBlueArea, BlueCard, BlueSpacing20, BlueNavigationStyle, BlueText } from '../../BlueComponents'; import PropTypes from 'prop-types'; import { LightningCustodianWallet } from '../../class/wallets/lightning-custodian-wallet'; import { HDLegacyBreadwalletWallet } from '../../class/wallets/hd-legacy-breadwallet-wallet'; import { HDLegacyP2PKHWallet } from '../../class/wallets/hd-legacy-p2pkh-wallet'; import { HDSegwitP2SHWallet } from '../../class/wallets/hd-segwit-p2sh-wallet'; import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; import Biometric from '../../class/biometrics'; import { HDSegwitBech32Wallet, SegwitP2SHWallet, LegacyWallet, SegwitBech32Wallet, WatchOnlyWallet } from '../../class'; import { ScrollView } from 'react-native-gesture-handler'; const EV = require('../../events'); const prompt = require('../../prompt'); /** @type {AppStorage} */ const BlueApp = require('../../BlueApp'); const loc = require('../../loc'); export default class WalletDetails extends Component { static navigationOptions = ({ navigation }) => ({ ...BlueNavigationStyle(), title: loc.wallets.details.title, headerRight: ( { if (navigation.state.params.saveAction) { navigation.getParam('saveAction')(); } }} > {loc.wallets.details.save} ), }); constructor(props) { super(props); const wallet = props.navigation.getParam('wallet'); const isLoading = true; this.state = { isLoading, walletName: wallet.getLabel(), wallet, useWithHardwareWallet: wallet.useWithHardwareWalletEnabled(), }; this.props.navigation.setParams({ isLoading, saveAction: () => this.setLabel() }); } componentDidMount() { console.log('wallets/details componentDidMount'); this.setState({ isLoading: false, }); this.props.navigation.setParams({ isLoading: false, saveAction: () => this.setLabel() }); } setLabel() { this.props.navigation.setParams({ isLoading: true }); this.setState({ isLoading: true }, async () => { if (this.state.walletName.trim().length > 0) { this.state.wallet.setLabel(this.state.walletName); } BlueApp.saveToDisk(); alert('Wallet updated.'); this.props.navigation.goBack(null); }); } async presentWalletHasBalanceAlert() { ReactNativeHapticFeedback.trigger('notificationWarning', { ignoreAndroidSystemSettings: false }); const walletBalanceConfirmation = await prompt( 'Wallet Balance', `This wallet has a balance. Before proceeding, please be aware that you will not be able to recover the funds without this wallet's seed phrase. In order to avoid accidental removal this wallet, please enter your wallet's balance of ${this.state.wallet.getBalance()} satoshis.`, true, 'plain-text', ); if (Number(walletBalanceConfirmation) === this.state.wallet.getBalance()) { this.props.navigation.setParams({ isLoading: true }); this.setState({ isLoading: true }, async () => { BlueApp.deleteWallet(this.state.wallet); ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false }); await BlueApp.saveToDisk(); EV(EV.enum.TRANSACTIONS_COUNT_CHANGED); EV(EV.enum.WALLETS_COUNT_CHANGED); this.props.navigation.navigate('Wallets'); }); } else { ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false }); this.setState({ isLoading: false }, async () => { alert("The provided balance amount does not match this wallet's balance. Please, try again"); }); } } async onUseWithHardwareWalletSwitch(value) { this.setState((state, props) => { let wallet = state.wallet; wallet.setUseWithHardwareWalletEnabled(value); return { useWithHardwareWallet: !!value, wallet }; }); } renderMarketplaceButton = () => { return Platform.select({ android: ( this.props.navigation.navigate('Marketplace', { fromWallet: this.state.wallet, }) } title="Marketplace" /> ), ios: ( { Linking.openURL('https://bluewallet.io/marketplace-btc/'); }} title="Marketplace" /> ), }); }; render() { if (this.state.isLoading) { return ( ); } return ( {(() => { if ( [LegacyWallet.type, SegwitBech32Wallet.type, SegwitP2SHWallet.type].includes(this.state.wallet.type) || (this.state.wallet.type === WatchOnlyWallet.type && !this.state.wallet.isHd()) ) { return ( {loc.wallets.details.address.toLowerCase()} {this.state.wallet.getAddress()} ); } })()} {loc.wallets.add.wallet_name.toLowerCase()} { this.setState({ walletName: text }); }} onBlur={() => { if (this.state.walletName.trim().length === 0) { const walletLabel = this.state.wallet.getLabel(); this.setState({ walletName: walletLabel }); } }} numberOfLines={1} style={{ flex: 1, marginHorizontal: 8, minHeight: 33 }} editable={!this.state.isLoading} underlineColorAndroid="transparent" /> {loc.wallets.details.type.toLowerCase()} {this.state.wallet.typeReadable} {this.state.wallet.type === LightningCustodianWallet.type && ( {loc.wallets.details.connected_to.toLowerCase()} {this.state.wallet.getBaseURI()} )} {this.state.wallet.type === WatchOnlyWallet.type && this.state.wallet.getSecret().startsWith('zpub') && ( <> {loc.wallets.details.advanced.toLowerCase()} {loc.wallets.details.use_with_hardware_wallet} this.onUseWithHardwareWalletSwitch(value)} /> {loc.wallets.details.master_fingerprint.toLowerCase()} {this.state.wallet.getMasterFingerprintHex()} )} this.props.navigation.navigate('WalletExport', { wallet: this.state.wallet, }) } title={loc.wallets.details.export_backup} /> {(this.state.wallet.type === HDLegacyBreadwalletWallet.type || this.state.wallet.type === HDLegacyP2PKHWallet.type || this.state.wallet.type === HDSegwitBech32Wallet.type || this.state.wallet.type === HDSegwitP2SHWallet.type) && ( this.props.navigation.navigate('WalletXpub', { secret: this.state.wallet.getSecret(), }) } title={loc.wallets.details.show_xpub} /> {this.renderMarketplaceButton()} )} {this.state.wallet.type !== LightningCustodianWallet.type && ( this.props.navigation.navigate('Broadcast')} title="Broadcast transaction" /> )} { ReactNativeHapticFeedback.trigger('notificationWarning', { ignoreAndroidSystemSettings: false }); Alert.alert( loc.wallets.details.delete + ' ' + loc.wallets.details.title, loc.wallets.details.are_you_sure, [ { text: loc.wallets.details.yes_delete, onPress: async () => { const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled(); if (isBiometricsEnabled) { if (!(await Biometric.unlockWithBiometrics())) { return; } } if (this.state.wallet.getBalance() > 0 && this.state.wallet.allowSend()) { this.presentWalletHasBalanceAlert(); } else { this.props.navigation.setParams({ isLoading: true }); this.setState({ isLoading: true }, async () => { BlueApp.deleteWallet(this.state.wallet); ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false }); await BlueApp.saveToDisk(); EV(EV.enum.TRANSACTIONS_COUNT_CHANGED); EV(EV.enum.WALLETS_COUNT_CHANGED); this.props.navigation.navigate('Wallets'); }); } }, }, { text: loc.wallets.details.no_cancel, onPress: () => {}, style: 'cancel' }, ], { cancelable: false }, ); }} > {loc.wallets.details.delete} ); } } WalletDetails.propTypes = { navigation: PropTypes.shape({ getParam: PropTypes.func, state: PropTypes.shape({ params: PropTypes.shape({ secret: PropTypes.string, }), }), navigate: PropTypes.func, goBack: PropTypes.func, setParams: PropTypes.func, }), };