2020-05-27 13:12:17 +02:00
|
|
|
import 'react-native-gesture-handler'; // should be on top
|
2018-12-12 14:29:10 +01:00
|
|
|
import React from 'react';
|
2020-06-24 05:17:27 +02:00
|
|
|
import { Linking, Appearance, DeviceEventEmitter, AppState, StyleSheet, KeyboardAvoidingView, Platform, View } from 'react-native';
|
2020-06-09 23:02:25 +02:00
|
|
|
import Clipboard from '@react-native-community/clipboard';
|
2019-03-02 13:13:12 +01:00
|
|
|
import Modal from 'react-native-modal';
|
2020-06-24 05:17:27 +02:00
|
|
|
import { NavigationContainer, CommonActions, DefaultTheme, DarkTheme } from '@react-navigation/native';
|
2020-05-27 13:12:17 +02:00
|
|
|
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
|
|
|
import Navigation from './Navigation';
|
2020-06-19 02:18:11 +02:00
|
|
|
import { navigationRef } from './NavigationService';
|
|
|
|
import * as NavigationService from './NavigationService';
|
2019-03-02 13:13:12 +01:00
|
|
|
import { BlueTextCentered, BlueButton } from './BlueComponents';
|
2019-03-05 02:04:40 +01:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
2019-08-25 03:14:26 +02:00
|
|
|
import { Chain } from './models/bitcoinUnits';
|
2019-11-03 01:25:55 +01:00
|
|
|
import QuickActions from 'react-native-quick-actions';
|
2019-10-30 04:00:07 +01:00
|
|
|
import * as Sentry from '@sentry/react-native';
|
2020-05-24 12:27:08 +02:00
|
|
|
import OnAppLaunch from './class/on-app-launch';
|
2020-04-28 16:48:36 +02:00
|
|
|
import DeeplinkSchemaMatch from './class/deeplink-schema-match';
|
2020-07-01 13:56:52 +02:00
|
|
|
const A = require('./blue_modules/analytics');
|
2019-10-30 04:00:07 +01:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
|
|
|
Sentry.init({
|
|
|
|
dsn: 'https://23377936131848ca8003448a893cb622@sentry.io/1295736',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:13:12 +01:00
|
|
|
const bitcoinModalString = 'Bitcoin address';
|
|
|
|
const lightningModalString = 'Lightning Invoice';
|
2019-03-05 02:04:40 +01:00
|
|
|
const loc = require('./loc');
|
|
|
|
const BlueApp = require('./BlueApp');
|
2020-07-01 13:56:52 +02:00
|
|
|
const EV = require('./blue_modules/events');
|
2018-12-12 14:29:10 +01:00
|
|
|
|
|
|
|
export default class App extends React.Component {
|
2019-03-02 13:13:12 +01:00
|
|
|
state = {
|
|
|
|
appState: AppState.currentState,
|
|
|
|
isClipboardContentModalVisible: false,
|
|
|
|
clipboardContentModalAddressType: bitcoinModalString,
|
|
|
|
clipboardContent: '',
|
|
|
|
};
|
|
|
|
|
2019-12-12 05:23:19 +01:00
|
|
|
componentDidMount() {
|
2020-06-21 02:16:05 +02:00
|
|
|
EV(EV.enum.WALLETS_INITIALIZED, this.addListeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
addListeners = () => {
|
2018-12-12 14:29:10 +01:00
|
|
|
Linking.addEventListener('url', this.handleOpenURL);
|
2019-03-02 13:13:12 +01:00
|
|
|
AppState.addEventListener('change', this._handleAppStateChange);
|
2019-11-03 01:25:55 +01:00
|
|
|
DeviceEventEmitter.addListener('quickActionShortcut', this.walletQuickActions);
|
2020-06-21 02:16:05 +02:00
|
|
|
QuickActions.popInitialAction().then(this.popInitialAction);
|
2020-04-06 17:33:45 +02:00
|
|
|
this._handleAppStateChange(undefined);
|
2020-06-21 02:16:05 +02:00
|
|
|
};
|
2018-12-12 14:29:10 +01:00
|
|
|
|
2019-11-03 01:25:55 +01:00
|
|
|
popInitialAction = async data => {
|
|
|
|
if (data) {
|
|
|
|
const wallet = BlueApp.getWallets().find(wallet => wallet.getID() === data.userInfo.url.split('wallet/')[1]);
|
2020-06-19 02:18:11 +02:00
|
|
|
NavigationService.dispatch(
|
2020-05-27 13:12:17 +02:00
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
2020-05-28 15:57:01 +02:00
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
2019-11-03 01:25:55 +01:00
|
|
|
params: {
|
|
|
|
wallet,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const url = await Linking.getInitialURL();
|
|
|
|
if (url) {
|
2019-12-28 01:53:34 +01:00
|
|
|
if (DeeplinkSchemaMatch.hasSchema(url)) {
|
2019-11-03 01:25:55 +01:00
|
|
|
this.handleOpenURL({ url });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const isViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
|
|
|
|
if (!isViewAllWalletsEnabled) {
|
|
|
|
const selectedDefaultWallet = await OnAppLaunch.getSelectedDefaultWallet();
|
|
|
|
const wallet = BlueApp.getWallets().find(wallet => wallet.getID() === selectedDefaultWallet.getID());
|
|
|
|
if (wallet) {
|
2020-06-19 02:18:11 +02:00
|
|
|
NavigationService.dispatch(
|
2020-05-27 13:12:17 +02:00
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
2019-11-03 01:25:55 +01:00
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
params: {
|
|
|
|
wallet,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
walletQuickActions = data => {
|
|
|
|
const wallet = BlueApp.getWallets().find(wallet => wallet.getID() === data.userInfo.url.split('wallet/')[1]);
|
2020-06-19 02:18:11 +02:00
|
|
|
NavigationService.dispatch(
|
2020-05-27 13:12:17 +02:00
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
2019-11-03 01:25:55 +01:00
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
params: {
|
|
|
|
wallet,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-12 14:29:10 +01:00
|
|
|
componentWillUnmount() {
|
|
|
|
Linking.removeEventListener('url', this.handleOpenURL);
|
2019-03-02 13:13:12 +01:00
|
|
|
AppState.removeEventListener('change', this._handleAppStateChange);
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:38:24 +01:00
|
|
|
_handleAppStateChange = async nextAppState => {
|
2019-03-05 02:04:40 +01:00
|
|
|
if (BlueApp.getWallets().length > 0) {
|
2020-04-06 17:33:45 +02:00
|
|
|
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();
|
2020-03-30 20:13:48 +02:00
|
|
|
const isAddressFromStoredWallet = BlueApp.getWallets().some(wallet => {
|
|
|
|
if (wallet.chain === Chain.ONCHAIN) {
|
2020-05-29 23:10:09 +02:00
|
|
|
// checking address validity is faster than unwrapping hierarchy only to compare it to garbage
|
|
|
|
return wallet.isAddressValid && wallet.isAddressValid(clipboard) && wallet.weOwnAddress(clipboard);
|
2020-03-30 20:13:48 +02:00
|
|
|
} else {
|
|
|
|
return wallet.isInvoiceGeneratedByWallet(clipboard) || wallet.weOwnAddress(clipboard);
|
|
|
|
}
|
|
|
|
});
|
2020-06-15 03:15:58 +02:00
|
|
|
const isBitcoinAddress = DeeplinkSchemaMatch.isBitcoinAddress(clipboard);
|
2019-12-28 01:53:34 +01:00
|
|
|
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 });
|
2019-03-02 13:13:12 +01:00
|
|
|
}
|
2020-04-06 17:33:45 +02:00
|
|
|
if (nextAppState) {
|
|
|
|
this.setState({ appState: nextAppState });
|
|
|
|
}
|
2019-03-02 13:13:12 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-15 14:16:29 +01:00
|
|
|
isBothBitcoinAndLightningWalletSelect = wallet => {
|
|
|
|
const clipboardContent = this.state.clipboardContent;
|
|
|
|
if (wallet.chain === Chain.ONCHAIN) {
|
2020-05-28 15:57:01 +02:00
|
|
|
this.navigation &&
|
2020-06-19 02:18:11 +02:00
|
|
|
NavigationService.dispatch(
|
2020-05-27 13:12:17 +02:00
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'SendDetails',
|
2019-12-15 14:16:29 +01:00
|
|
|
params: {
|
|
|
|
uri: clipboardContent.bitcoin,
|
|
|
|
fromWallet: wallet,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else if (wallet.chain === Chain.OFFCHAIN) {
|
2020-05-28 15:57:01 +02:00
|
|
|
this.navigation &&
|
2020-06-19 02:18:11 +02:00
|
|
|
NavigationService.dispatch(
|
2020-05-27 13:12:17 +02:00
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'ScanLndInvoice',
|
2019-12-15 14:16:29 +01:00
|
|
|
params: {
|
|
|
|
uri: clipboardContent.lndInvoice,
|
|
|
|
fromSecret: wallet.getSecret(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-12 14:29:10 +01:00
|
|
|
handleOpenURL = event => {
|
2020-06-19 02:18:11 +02:00
|
|
|
DeeplinkSchemaMatch.navigationRouteFor(event, value => NavigationService.navigate(...value));
|
2018-12-12 14:29:10 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 13:13:12 +01:00
|
|
|
renderClipboardContentModal = () => {
|
2018-12-12 14:29:10 +01:00
|
|
|
return (
|
2019-03-02 13:13:12 +01:00
|
|
|
<Modal
|
2019-05-03 14:36:11 +02:00
|
|
|
onModalShow={() => ReactNativeHapticFeedback.trigger('impactLight', { ignoreAndroidSystemSettings: false })}
|
2019-03-02 13:13:12 +01:00
|
|
|
isVisible={this.state.isClipboardContentModalVisible}
|
|
|
|
style={styles.bottomModal}
|
|
|
|
onBackdropPress={() => {
|
|
|
|
this.setState({ isClipboardContentModalVisible: false });
|
2018-12-12 14:29:10 +01:00
|
|
|
}}
|
2019-03-02 13:13:12 +01:00
|
|
|
>
|
|
|
|
<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 })}
|
|
|
|
/>
|
2020-05-30 10:16:03 +02:00
|
|
|
<View style={styles.space} />
|
2019-03-02 13:13:12 +01:00
|
|
|
<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>
|
2020-05-30 10:16:03 +02:00
|
|
|
<View style={styles.root}>
|
2020-06-24 05:17:27 +02:00
|
|
|
<NavigationContainer ref={navigationRef} theme={Appearance.getColorScheme() === 'dark' ? { ...DarkTheme } : DefaultTheme} tr>
|
2020-05-27 13:12:17 +02:00
|
|
|
<Navigation />
|
|
|
|
</NavigationContainer>
|
|
|
|
{this.renderClipboardContentModal()}
|
|
|
|
</View>
|
|
|
|
</SafeAreaProvider>
|
2018-12-12 14:29:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 13:13:12 +01:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2020-05-30 10:16:03 +02:00
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
space: {
|
|
|
|
marginHorizontal: 8,
|
|
|
|
},
|
2019-03-02 13:13:12 +01:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
});
|