BlueWallet/App.js

264 lines
9.2 KiB
JavaScript
Raw Normal View History

2020-05-27 13:12:17 +02:00
import 'react-native-gesture-handler'; // should be on top
import React from 'react';
import { Linking, DeviceEventEmitter, AppState, Clipboard, StyleSheet, KeyboardAvoidingView, Platform, View } from 'react-native';
import Modal from 'react-native-modal';
2020-05-27 13:12:17 +02:00
import { NavigationContainer, CommonActions } from '@react-navigation/native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import Navigation from './Navigation';
2019-01-24 08:36:01 +01:00
import NavigationService from './NavigationService';
import { BlueTextCentered, BlueButton } from './BlueComponents';
2019-03-05 02:04:40 +01:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import { Chain } from './models/bitcoinUnits';
import QuickActions from 'react-native-quick-actions';
import * as Sentry from '@sentry/react-native';
import OnAppLaunch from './class/onAppLaunch';
import DeeplinkSchemaMatch from './class/deeplink-schema-match';
2019-12-28 01:53:34 +01:00
import BitcoinBIP70TransactionDecode from './bip70/bip70';
2019-11-29 00:16:04 +01:00
const A = require('./analytics');
if (process.env.NODE_ENV !== 'development') {
Sentry.init({
dsn: 'https://23377936131848ca8003448a893cb622@sentry.io/1295736',
});
}
const bitcoinModalString = 'Bitcoin address';
const lightningModalString = 'Lightning Invoice';
2019-03-05 02:04:40 +01:00
const loc = require('./loc');
const BlueApp = require('./BlueApp');
export default class App extends React.Component {
navigator = null;
state = {
appState: AppState.currentState,
isClipboardContentModalVisible: false,
clipboardContentModalAddressType: bitcoinModalString,
clipboardContent: '',
};
componentDidMount() {
Linking.addEventListener('url', this.handleOpenURL);
AppState.addEventListener('change', this._handleAppStateChange);
QuickActions.popInitialAction().then(this.popInitialAction);
DeviceEventEmitter.addListener('quickActionShortcut', this.walletQuickActions);
this._handleAppStateChange(undefined);
}
popInitialAction = async data => {
if (data) {
// eslint-disable-next-line no-unused-expressions
this.navigator.dismiss;
const wallet = BlueApp.getWallets().find(wallet => wallet.getID() === data.userInfo.url.split('wallet/')[1]);
this.navigator.dispatch(
2020-05-27 13:12:17 +02:00
CommonActions.navigate({
key: `WalletTransactions-${wallet.getID()}`,
2020-05-27 13:12:17 +02:00
name: 'WalletTransactions',
params: {
wallet,
},
}),
);
} else {
const url = await Linking.getInitialURL();
if (url) {
2019-12-28 01:53:34 +01:00
if (DeeplinkSchemaMatch.hasSchema(url)) {
this.handleOpenURL({ url });
}
} else {
const isViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
if (!isViewAllWalletsEnabled) {
// eslint-disable-next-line no-unused-expressions
this.navigator.dismiss;
const selectedDefaultWallet = await OnAppLaunch.getSelectedDefaultWallet();
const wallet = BlueApp.getWallets().find(wallet => wallet.getID() === selectedDefaultWallet.getID());
if (wallet) {
this.navigator.dispatch(
2020-05-27 13:12:17 +02:00
CommonActions.navigate({
name: 'WalletTransactions',
key: `WalletTransactions-${wallet.getID()}`,
params: {
wallet,
},
}),
);
}
}
}
}
};
walletQuickActions = data => {
const wallet = BlueApp.getWallets().find(wallet => wallet.getID() === data.userInfo.url.split('wallet/')[1]);
// eslint-disable-next-line no-unused-expressions
this.navigator.dismiss;
this.navigator.dispatch(
2020-05-27 13:12:17 +02:00
CommonActions.navigate({
name: 'WalletTransactions',
key: `WalletTransactions-${wallet.getID()}`,
params: {
wallet,
},
}),
);
};
componentWillUnmount() {
Linking.removeEventListener('url', this.handleOpenURL);
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = async nextAppState => {
2019-03-05 02:04:40 +01:00
if (BlueApp.getWallets().length > 0) {
if ((this.state.appState.match(/background/) && nextAppState) === 'active' || nextAppState === undefined) {
2019-11-30 21:12:35 +01:00
setTimeout(() => A(A.ENUM.APP_UNSUSPENDED), 2000);
2019-03-05 02:04:40 +01:00
const clipboard = await Clipboard.getString();
const isAddressFromStoredWallet = BlueApp.getWallets().some(wallet => {
if (wallet.chain === Chain.ONCHAIN) {
return wallet.weOwnAddress(clipboard);
} else {
return wallet.isInvoiceGeneratedByWallet(clipboard) || wallet.weOwnAddress(clipboard);
}
});
2019-12-28 01:53:34 +01:00
const isBitcoinAddress =
DeeplinkSchemaMatch.isBitcoinAddress(clipboard) || BitcoinBIP70TransactionDecode.matchesPaymentURL(clipboard);
const isLightningInvoice = DeeplinkSchemaMatch.isLightningInvoice(clipboard);
const isLNURL = DeeplinkSchemaMatch.isLnUrl(clipboard);
const isBothBitcoinAndLightning = DeeplinkSchemaMatch.isBothBitcoinAndLightning(clipboard);
2019-09-17 19:27:09 +02:00
if (
2019-12-28 01:53:34 +01:00
!isAddressFromStoredWallet &&
this.state.clipboardContent !== clipboard &&
(isBitcoinAddress || isLightningInvoice || isLNURL || isBothBitcoinAndLightning)
2019-09-17 19:27:09 +02:00
) {
2019-12-28 01:53:34 +01:00
if (isBitcoinAddress) {
this.setState({ clipboardContentModalAddressType: bitcoinModalString });
} else if (isLightningInvoice || isLNURL) {
this.setState({ clipboardContentModalAddressType: lightningModalString });
} else if (isBothBitcoinAndLightning) {
this.setState({ clipboardContentModalAddressType: bitcoinModalString });
}
2019-03-05 02:04:40 +01:00
this.setState({ isClipboardContentModalVisible: true });
}
this.setState({ clipboardContent: clipboard });
}
if (nextAppState) {
this.setState({ appState: nextAppState });
}
}
};
isBothBitcoinAndLightningWalletSelect = wallet => {
const clipboardContent = this.state.clipboardContent;
if (wallet.chain === Chain.ONCHAIN) {
this.navigator &&
this.navigator.dispatch(
2020-05-27 13:12:17 +02:00
CommonActions.navigate({
name: 'SendDetails',
params: {
uri: clipboardContent.bitcoin,
fromWallet: wallet,
},
}),
);
} else if (wallet.chain === Chain.OFFCHAIN) {
this.navigator &&
this.navigator.dispatch(
2020-05-27 13:12:17 +02:00
CommonActions.navigate({
name: 'ScanLndInvoice',
params: {
uri: clipboardContent.lndInvoice,
fromSecret: wallet.getSecret(),
},
}),
);
}
};
handleOpenURL = event => {
2020-05-27 13:12:17 +02:00
// DeeplinkSchemaMatch.navigationRouteFor(event, value => this.navigator && this.navigator.dispatch(CommonActions.navigate(value)));
DeeplinkSchemaMatch.navigationRouteFor(event, value => this.navigator && this.navigator.navigate(...value));
};
renderClipboardContentModal = () => {
return (
<Modal
2019-05-03 14:36:11 +02:00
onModalShow={() => ReactNativeHapticFeedback.trigger('impactLight', { ignoreAndroidSystemSettings: false })}
isVisible={this.state.isClipboardContentModalVisible}
style={styles.bottomModal}
onBackdropPress={() => {
this.setState({ isClipboardContentModalVisible: false });
}}
>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'position' : null}>
<View style={styles.modalContent}>
<BlueTextCentered>
You have a {this.state.clipboardContentModalAddressType} on your clipboard. Would you like to use it for a transaction?
</BlueTextCentered>
<View style={styles.modelContentButtonLayout}>
<BlueButton
noMinWidth
title={loc.send.details.cancel}
onPress={() => this.setState({ isClipboardContentModalVisible: false })}
/>
<View style={{ marginHorizontal: 8 }} />
<BlueButton
noMinWidth
title="OK"
onPress={() => {
this.setState({ isClipboardContentModalVisible: false }, async () => {
const clipboard = await Clipboard.getString();
setTimeout(() => this.handleOpenURL({ url: clipboard }), 100);
});
}}
/>
</View>
</View>
</KeyboardAvoidingView>
</Modal>
);
};
render() {
return (
2020-05-27 13:12:17 +02:00
<SafeAreaProvider>
<View style={{ flex: 1 }}>
<NavigationContainer
ref={nav => {
this.navigator = nav;
NavigationService.setTopLevelNavigator(nav);
}}
>
<Navigation />
</NavigationContainer>
{this.renderClipboardContentModal()}
</View>
</SafeAreaProvider>
);
}
}
const styles = StyleSheet.create({
modalContent: {
backgroundColor: '#FFFFFF',
padding: 22,
justifyContent: 'center',
alignItems: 'center',
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
borderColor: 'rgba(0, 0, 0, 0.1)',
minHeight: 200,
height: 200,
},
bottomModal: {
justifyContent: 'flex-end',
margin: 0,
},
modelContentButtonLayout: {
flexDirection: 'row',
margin: 16,
justifyContent: 'space-between',
alignItems: 'flex-end',
},
});