2018-12-12 14:29:10 +01:00
|
|
|
import React from 'react';
|
2019-11-03 01:25:55 +01:00
|
|
|
import { Linking, DeviceEventEmitter, AppState, Clipboard, StyleSheet, KeyboardAvoidingView, Platform, View } from 'react-native';
|
2019-05-02 22:33:03 +02:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
2019-03-02 13:13:12 +01:00
|
|
|
import Modal from 'react-native-modal';
|
2018-12-12 14:29:10 +01:00
|
|
|
import { NavigationActions } from 'react-navigation';
|
|
|
|
import MainBottomTabs from './MainBottomTabs';
|
2019-01-24 08:36:01 +01:00
|
|
|
import 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-04-10 01:06:33 +02:00
|
|
|
import url from 'url';
|
|
|
|
import { AppStorage, LightningCustodianWallet } from './class';
|
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';
|
2019-11-03 01:25:55 +01:00
|
|
|
import OnAppLaunch from './class/onAppLaunch';
|
2019-11-29 00:16:04 +01:00
|
|
|
const A = require('./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 bitcoin = require('bitcoinjs-lib');
|
|
|
|
const bitcoinModalString = 'Bitcoin address';
|
|
|
|
const lightningModalString = 'Lightning Invoice';
|
2019-03-05 02:04:40 +01:00
|
|
|
const loc = require('./loc');
|
|
|
|
/** @type {AppStorage} */
|
|
|
|
const BlueApp = require('./BlueApp');
|
2018-12-12 14:29:10 +01:00
|
|
|
|
|
|
|
export default class App extends React.Component {
|
|
|
|
navigator = null;
|
|
|
|
|
2019-03-02 13:13:12 +01:00
|
|
|
state = {
|
|
|
|
appState: AppState.currentState,
|
|
|
|
isClipboardContentModalVisible: false,
|
|
|
|
clipboardContentModalAddressType: bitcoinModalString,
|
|
|
|
clipboardContent: '',
|
|
|
|
};
|
|
|
|
|
2019-11-03 01:25:55 +01:00
|
|
|
async componentDidMount() {
|
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
|
|
|
QuickActions.popInitialAction().then(this.popInitialAction);
|
|
|
|
DeviceEventEmitter.addListener('quickActionShortcut', this.walletQuickActions);
|
2018-12-12 14:29:10 +01:00
|
|
|
}
|
|
|
|
|
2019-11-03 01:25:55 +01:00
|
|
|
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(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
routeName: 'WalletTransactions',
|
|
|
|
params: {
|
|
|
|
wallet,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const url = await Linking.getInitialURL();
|
|
|
|
if (url) {
|
|
|
|
if (this.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(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
routeName: '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(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
routeName: 'WalletTransactions',
|
|
|
|
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) {
|
2019-03-07 19:38:24 +01:00
|
|
|
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
|
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();
|
2019-08-25 03:14:26 +02:00
|
|
|
const isAddressFromStoredWallet = BlueApp.getWallets().some(wallet =>
|
2019-09-12 03:59:11 +02:00
|
|
|
wallet.chain === Chain.ONCHAIN ? wallet.weOwnAddress(clipboard) : wallet.isInvoiceGeneratedByWallet(clipboard),
|
2019-08-25 03:14:26 +02:00
|
|
|
);
|
2019-09-17 19:27:09 +02:00
|
|
|
if (
|
|
|
|
!isAddressFromStoredWallet &&
|
|
|
|
this.state.clipboardContent !== clipboard &&
|
|
|
|
(this.isBitcoinAddress(clipboard) || this.isLightningInvoice(clipboard) || this.isLnUrl(clipboard))
|
|
|
|
) {
|
2019-03-05 02:04:40 +01:00
|
|
|
this.setState({ isClipboardContentModalVisible: true });
|
|
|
|
}
|
|
|
|
this.setState({ clipboardContent: clipboard });
|
2019-03-02 13:13:12 +01:00
|
|
|
}
|
2019-03-05 02:04:40 +01:00
|
|
|
this.setState({ appState: nextAppState });
|
2019-03-02 13:13:12 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-07 19:38:24 +01:00
|
|
|
hasSchema(schemaString) {
|
|
|
|
if (typeof schemaString !== 'string' || schemaString.length <= 0) return false;
|
|
|
|
const lowercaseString = schemaString.trim().toLowerCase();
|
2019-04-10 01:06:33 +02:00
|
|
|
return (
|
|
|
|
lowercaseString.startsWith('bitcoin:') ||
|
|
|
|
lowercaseString.startsWith('lightning:') ||
|
|
|
|
lowercaseString.startsWith('blue:') ||
|
|
|
|
lowercaseString.startsWith('bluewallet:') ||
|
|
|
|
lowercaseString.startsWith('lapp:')
|
|
|
|
);
|
2019-03-07 19:38:24 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 13:13:12 +01:00
|
|
|
isBitcoinAddress(address) {
|
|
|
|
let isValidBitcoinAddress = false;
|
|
|
|
try {
|
|
|
|
bitcoin.address.toOutputScript(address);
|
|
|
|
isValidBitcoinAddress = true;
|
|
|
|
this.setState({ clipboardContentModalAddressType: bitcoinModalString });
|
|
|
|
} catch (err) {
|
|
|
|
isValidBitcoinAddress = false;
|
|
|
|
}
|
|
|
|
if (!isValidBitcoinAddress) {
|
|
|
|
if (address.indexOf('bitcoin:') === 0 || address.indexOf('BITCOIN:') === 0) {
|
|
|
|
isValidBitcoinAddress = true;
|
|
|
|
this.setState({ clipboardContentModalAddressType: bitcoinModalString });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isValidBitcoinAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLightningInvoice(invoice) {
|
|
|
|
let isValidLightningInvoice = false;
|
2019-09-03 22:44:57 +02:00
|
|
|
if (invoice.toLowerCase().startsWith('lightning:lnb') || invoice.toLowerCase().startsWith('lnb')) {
|
2019-03-02 13:13:12 +01:00
|
|
|
this.setState({ clipboardContentModalAddressType: lightningModalString });
|
|
|
|
isValidLightningInvoice = true;
|
|
|
|
}
|
|
|
|
return isValidLightningInvoice;
|
2018-12-12 14:29:10 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:44:57 +02:00
|
|
|
isLnUrl(text) {
|
|
|
|
if (text.toLowerCase().startsWith('lightning:lnurl') || text.toLowerCase().startsWith('lnurl')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:17:01 +02:00
|
|
|
isSafelloRedirect(event) {
|
|
|
|
let urlObject = url.parse(event.url, true) // eslint-disable-line
|
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
return !!urlObject.query['safello-state-token'];
|
2019-05-09 12:17:01 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 14:29:10 +01:00
|
|
|
handleOpenURL = event => {
|
2018-12-22 15:25:34 +01:00
|
|
|
if (event.url === null) {
|
2018-12-22 04:23:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-22 15:25:34 +01:00
|
|
|
if (typeof event.url !== 'string') {
|
2018-12-22 04:23:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-03-02 13:13:12 +01:00
|
|
|
if (this.isBitcoinAddress(event.url)) {
|
2018-12-29 19:24:51 +01:00
|
|
|
this.navigator &&
|
|
|
|
this.navigator.dispatch(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
routeName: 'SendDetails',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2019-03-02 13:13:12 +01:00
|
|
|
} else if (this.isLightningInvoice(event.url)) {
|
2018-12-29 19:24:51 +01:00
|
|
|
this.navigator &&
|
|
|
|
this.navigator.dispatch(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
routeName: 'ScanLndInvoice',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2019-09-03 22:44:57 +02:00
|
|
|
} else if (this.isLnUrl(event.url)) {
|
|
|
|
this.navigator &&
|
|
|
|
this.navigator.dispatch(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
routeName: 'LNDCreateInvoice',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2019-05-09 12:17:01 +02:00
|
|
|
} else if (this.isSafelloRedirect(event)) {
|
|
|
|
let urlObject = url.parse(event.url, true) // eslint-disable-line
|
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
const safelloStateToken = urlObject.query['safello-state-token'];
|
2019-05-09 12:17:01 +02:00
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
this.navigator &&
|
2019-05-09 12:17:01 +02:00
|
|
|
this.navigator.dispatch(
|
|
|
|
NavigationActions.navigate({
|
2019-05-12 22:50:08 +02:00
|
|
|
routeName: 'BuyBitcoin',
|
2019-05-09 12:17:01 +02:00
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
safelloStateToken,
|
|
|
|
},
|
|
|
|
}),
|
2019-05-12 22:50:08 +02:00
|
|
|
);
|
2019-04-10 01:06:33 +02:00
|
|
|
} else {
|
|
|
|
let urlObject = url.parse(event.url, true); // eslint-disable-line
|
|
|
|
console.log('parsed', urlObject);
|
|
|
|
(async () => {
|
|
|
|
if (urlObject.protocol === 'bluewallet:' || urlObject.protocol === 'lapp:' || urlObject.protocol === 'blue:') {
|
|
|
|
switch (urlObject.host) {
|
|
|
|
case 'openlappbrowser':
|
|
|
|
console.log('opening LAPP', urlObject.query.url);
|
|
|
|
// searching for LN wallet:
|
|
|
|
let haveLnWallet = false;
|
|
|
|
for (let w of BlueApp.getWallets()) {
|
|
|
|
if (w.type === LightningCustodianWallet.type) {
|
|
|
|
haveLnWallet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!haveLnWallet) {
|
|
|
|
// need to create one
|
|
|
|
let 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) {
|
|
|
|
// giving up, not doing anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BlueApp.wallets.push(w);
|
|
|
|
await BlueApp.saveToDisk();
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, opening lapp browser and navigating it to URL.
|
|
|
|
// looking for a LN wallet:
|
|
|
|
let lnWallet;
|
|
|
|
for (let w of BlueApp.getWallets()) {
|
|
|
|
if (w.type === LightningCustodianWallet.type) {
|
|
|
|
lnWallet = w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lnWallet) {
|
|
|
|
// something went wrong
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.navigator &&
|
|
|
|
this.navigator.dispatch(
|
|
|
|
NavigationActions.navigate({
|
|
|
|
routeName: 'LappBrowser',
|
|
|
|
params: {
|
|
|
|
fromSecret: lnWallet.getSecret(),
|
|
|
|
fromWallet: lnWallet,
|
|
|
|
url: urlObject.query.url,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
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 })}
|
|
|
|
/>
|
|
|
|
<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 (
|
|
|
|
<View style={{ flex: 1 }}>
|
|
|
|
<MainBottomTabs
|
|
|
|
ref={nav => {
|
|
|
|
this.navigator = nav;
|
|
|
|
NavigationService.setTopLevelNavigator(nav);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{this.renderClipboardContentModal()}
|
|
|
|
</View>
|
2018-12-12 14:29:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 13:13:12 +01:00
|
|
|
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
});
|