2020-05-27 14:12:17 +03:00
|
|
|
import 'react-native-gesture-handler'; // should be on top
|
2021-09-24 15:32:22 -04:00
|
|
|
import React, { useContext, useEffect, useRef } from 'react';
|
2020-12-26 05:57:42 -05:00
|
|
|
import {
|
|
|
|
AppState,
|
|
|
|
NativeModules,
|
|
|
|
NativeEventEmitter,
|
|
|
|
Linking,
|
|
|
|
Platform,
|
|
|
|
StyleSheet,
|
2021-02-25 21:52:17 -05:00
|
|
|
UIManager,
|
2020-12-26 05:57:42 -05:00
|
|
|
useColorScheme,
|
|
|
|
View,
|
2022-06-19 14:05:30 +01:00
|
|
|
LogBox,
|
2020-12-26 05:57:42 -05:00
|
|
|
} from 'react-native';
|
2023-10-20 13:59:56 -04:00
|
|
|
import { NavigationContainer, CommonActions } from '@react-navigation/native';
|
2020-05-27 14:12:17 +03:00
|
|
|
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
2020-06-18 20:18:11 -04:00
|
|
|
import { navigationRef } from './NavigationService';
|
|
|
|
import * as NavigationService from './NavigationService';
|
2019-08-24 21:14:26 -04:00
|
|
|
import { Chain } from './models/bitcoinUnits';
|
2020-04-28 15:48:36 +01:00
|
|
|
import DeeplinkSchemaMatch from './class/deeplink-schema-match';
|
2020-07-20 16:38:46 +03:00
|
|
|
import loc from './loc';
|
2022-07-02 14:56:15 -04:00
|
|
|
import { BlueDefaultTheme, BlueDarkTheme } from './components/themes';
|
2020-09-08 12:06:41 -04:00
|
|
|
import InitRoot from './Navigation';
|
2020-10-10 15:19:42 -04:00
|
|
|
import BlueClipboard from './blue_modules/clipboard';
|
2020-10-24 13:20:59 -04:00
|
|
|
import { BlueStorageContext } from './blue_modules/storage-context';
|
|
|
|
import WatchConnectivity from './WatchConnectivity';
|
|
|
|
import DeviceQuickActions from './class/quick-actions';
|
|
|
|
import Notifications from './blue_modules/notifications';
|
|
|
|
import Biometric from './class/biometrics';
|
2024-04-09 14:37:10 -04:00
|
|
|
import WidgetCommunication from './components/WidgetCommunication';
|
2021-09-24 15:32:22 -04:00
|
|
|
import ActionSheet from './screen/ActionSheet';
|
2023-12-29 07:52:12 -04:00
|
|
|
import triggerHapticFeedback, { HapticFeedbackTypes } from './blue_modules/hapticFeedback';
|
2024-01-18 20:28:06 -04:00
|
|
|
import MenuElements from './components/MenuElements';
|
2024-01-28 11:11:08 -04:00
|
|
|
import { updateExchangeRate } from './blue_modules/currency';
|
2024-03-23 23:48:44 -04:00
|
|
|
import { NavigationProvider } from './components/NavigationProvider';
|
2024-03-31 20:59:14 +01:00
|
|
|
import A from './blue_modules/analytics';
|
2024-04-02 21:50:19 -04:00
|
|
|
import HandOffComponentListener from './components/HandOffComponentListener';
|
2020-11-20 21:28:34 +03:00
|
|
|
|
2022-02-12 12:03:10 -05:00
|
|
|
const eventEmitter = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.EventEmitter) : undefined;
|
2024-04-02 21:50:19 -04:00
|
|
|
const { SplashScreen } = NativeModules;
|
2020-12-26 05:57:42 -05:00
|
|
|
|
2024-01-04 20:31:29 -04:00
|
|
|
LogBox.ignoreLogs(['Require cycle:', 'Battery state `unknown` and monitoring disabled, this is normal for simulators and tvOS.']);
|
2022-06-19 14:05:30 +01:00
|
|
|
|
2020-11-20 21:28:34 +03:00
|
|
|
const ClipboardContentType = Object.freeze({
|
|
|
|
BITCOIN: 'BITCOIN',
|
|
|
|
LIGHTNING: 'LIGHTNING',
|
|
|
|
});
|
2018-12-12 14:29:10 +01:00
|
|
|
|
2021-02-25 21:52:17 -05:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
if (UIManager.setLayoutAnimationEnabledExperimental) {
|
|
|
|
UIManager.setLayoutAnimationEnabledExperimental(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:07:22 -04:00
|
|
|
const App = () => {
|
2023-09-17 18:28:54 +02:00
|
|
|
const {
|
|
|
|
walletsInitialized,
|
|
|
|
wallets,
|
|
|
|
addWallet,
|
|
|
|
saveToDisk,
|
|
|
|
fetchAndSaveWalletTransactions,
|
|
|
|
refreshAllWalletTransactions,
|
|
|
|
setSharedCosigner,
|
|
|
|
} = useContext(BlueStorageContext);
|
2020-10-11 03:07:22 -04:00
|
|
|
const appState = useRef(AppState.currentState);
|
2020-12-19 03:39:07 -05:00
|
|
|
const clipboardContent = useRef();
|
2020-10-11 03:07:22 -04:00
|
|
|
const colorScheme = useColorScheme();
|
2019-03-02 07:13:12 -05:00
|
|
|
|
2021-01-01 19:15:40 +00:00
|
|
|
const onNotificationReceived = async notification => {
|
2020-12-27 12:04:32 -05:00
|
|
|
const payload = Object.assign({}, notification, notification.data);
|
|
|
|
if (notification.data && notification.data.data) Object.assign(payload, notification.data.data);
|
2021-01-01 19:15:40 +00:00
|
|
|
payload.foreground = true;
|
|
|
|
|
|
|
|
await Notifications.addNotification(payload);
|
|
|
|
// if user is staring at the app when he receives the notification we process it instantly
|
|
|
|
// so app refetches related wallet
|
|
|
|
if (payload.foreground) await processPushNotifications();
|
2020-12-26 05:57:42 -05:00
|
|
|
};
|
|
|
|
|
2020-10-11 03:07:22 -04:00
|
|
|
const addListeners = () => {
|
2024-03-23 23:48:44 -04:00
|
|
|
const urlSubscription = Linking.addEventListener('url', handleOpenURL);
|
|
|
|
const appStateSubscription = AppState.addEventListener('change', handleAppStateChange);
|
|
|
|
|
|
|
|
const notificationSubscription = eventEmitter?.addListener('onNotificationReceived', onNotificationReceived);
|
|
|
|
|
|
|
|
// Store subscriptions in a ref or state to remove them later
|
|
|
|
return {
|
|
|
|
urlSubscription,
|
|
|
|
appStateSubscription,
|
|
|
|
notificationSubscription,
|
|
|
|
};
|
2020-06-20 20:16:05 -04:00
|
|
|
};
|
2018-12-12 14:29:10 +01:00
|
|
|
|
2024-03-23 23:48:44 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (walletsInitialized) {
|
|
|
|
const subscriptions = addListeners();
|
|
|
|
|
|
|
|
// Cleanup function
|
|
|
|
return () => {
|
|
|
|
subscriptions.urlSubscription?.remove();
|
|
|
|
subscriptions.appStateSubscription?.remove();
|
|
|
|
subscriptions.notificationSubscription?.remove();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [walletsInitialized]); // Re-run when walletsInitialized changes
|
|
|
|
|
2020-08-10 15:17:50 +01:00
|
|
|
/**
|
|
|
|
* Processes push notifications stored in AsyncStorage. Might navigate to some screen.
|
|
|
|
*
|
|
|
|
* @returns {Promise<boolean>} returns TRUE if notification was processed _and acted_ upon, i.e. navigation happened
|
|
|
|
* @private
|
|
|
|
*/
|
2020-10-11 03:07:22 -04:00
|
|
|
const processPushNotifications = async () => {
|
2021-09-07 20:30:07 +01:00
|
|
|
if (!walletsInitialized) {
|
|
|
|
console.log('not processing push notifications because wallets are not initialized');
|
|
|
|
return;
|
|
|
|
}
|
2020-08-10 15:17:50 +01:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
|
|
|
// sleep needed as sometimes unsuspend is faster than notification module actually saves notifications to async storage
|
2020-10-24 13:20:59 -04:00
|
|
|
const notifications2process = await Notifications.getStoredNotifications();
|
2021-01-01 19:15:40 +00:00
|
|
|
|
|
|
|
await Notifications.clearStoredNotifications();
|
|
|
|
Notifications.setApplicationIconBadgeNumber(0);
|
2020-12-29 17:54:56 -05:00
|
|
|
const deliveredNotifications = await Notifications.getDeliveredNotifications();
|
2021-01-01 19:15:40 +00:00
|
|
|
setTimeout(() => Notifications.removeAllDeliveredNotifications(), 5000); // so notification bubble wont disappear too fast
|
|
|
|
|
|
|
|
for (const payload of notifications2process) {
|
2021-01-01 23:39:26 +00:00
|
|
|
const wasTapped = payload.foreground === false || (payload.foreground === true && payload.userInteraction);
|
2020-08-10 15:17:50 +01:00
|
|
|
|
|
|
|
console.log('processing push notification:', payload);
|
|
|
|
let wallet;
|
|
|
|
switch (+payload.type) {
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2020-10-24 13:20:59 -04:00
|
|
|
wallet = wallets.find(w => w.weOwnAddress(payload.address));
|
2020-08-10 15:17:50 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 4:
|
2020-10-24 13:20:59 -04:00
|
|
|
wallet = wallets.find(w => w.weOwnTransaction(payload.txid || payload.hash));
|
2020-08-10 15:17:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wallet) {
|
2020-10-24 13:20:59 -04:00
|
|
|
const walletID = wallet.getID();
|
|
|
|
fetchAndSaveWalletTransactions(walletID);
|
2020-12-19 04:11:37 -05:00
|
|
|
if (wasTapped) {
|
2021-08-31 09:54:29 -04:00
|
|
|
if (payload.type !== 3 || wallet.chain === Chain.OFFCHAIN) {
|
|
|
|
NavigationService.dispatch(
|
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
|
|
|
params: {
|
|
|
|
walletID,
|
|
|
|
walletType: wallet.type,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
NavigationService.navigate('ReceiveDetailsRoot', {
|
|
|
|
screen: 'ReceiveDetails',
|
2020-12-19 04:11:37 -05:00
|
|
|
params: {
|
|
|
|
walletID,
|
2021-08-31 09:54:29 -04:00
|
|
|
address: payload.address,
|
2020-12-19 04:11:37 -05:00
|
|
|
},
|
2021-08-31 09:54:29 -04:00
|
|
|
});
|
|
|
|
}
|
2021-01-01 19:15:40 +00:00
|
|
|
|
|
|
|
return true;
|
2020-12-19 04:11:37 -05:00
|
|
|
}
|
2020-08-10 15:17:50 +01:00
|
|
|
} else {
|
2021-01-01 19:15:40 +00:00
|
|
|
console.log('could not find wallet while processing push notification, NOP');
|
2020-08-10 15:17:50 +01:00
|
|
|
}
|
2021-01-01 19:15:40 +00:00
|
|
|
} // end foreach notifications loop
|
2020-12-26 05:57:42 -05:00
|
|
|
|
2021-01-01 19:15:40 +00:00
|
|
|
if (deliveredNotifications.length > 0) {
|
|
|
|
// notification object is missing userInfo. We know we received a notification but don't have sufficient
|
|
|
|
// data to refresh 1 wallet. let's refresh all.
|
2020-12-29 21:13:56 -05:00
|
|
|
refreshAllWalletTransactions();
|
|
|
|
}
|
2021-01-01 19:15:40 +00:00
|
|
|
|
|
|
|
// if we are here - we did not act upon any push
|
2020-12-26 05:57:42 -05:00
|
|
|
return false;
|
2020-10-11 03:07:22 -04:00
|
|
|
};
|
2020-08-10 15:17:50 +01:00
|
|
|
|
2020-10-11 03:07:22 -04:00
|
|
|
const handleAppStateChange = async nextAppState => {
|
2021-09-09 20:36:35 +01:00
|
|
|
if (wallets.length === 0) return;
|
|
|
|
if ((appState.current.match(/background/) && nextAppState === 'active') || nextAppState === undefined) {
|
|
|
|
setTimeout(() => A(A.ENUM.APP_UNSUSPENDED), 2000);
|
2024-01-28 11:11:08 -04:00
|
|
|
updateExchangeRate();
|
2021-09-09 20:36:35 +01:00
|
|
|
const processed = await processPushNotifications();
|
|
|
|
if (processed) return;
|
2023-03-29 20:46:11 -04:00
|
|
|
const clipboard = await BlueClipboard().getClipboardContent();
|
2021-09-09 20:36:35 +01:00
|
|
|
const isAddressFromStoredWallet = wallets.some(wallet => {
|
|
|
|
if (wallet.chain === Chain.ONCHAIN) {
|
|
|
|
// checking address validity is faster than unwrapping hierarchy only to compare it to garbage
|
|
|
|
return wallet.isAddressValid && wallet.isAddressValid(clipboard) && wallet.weOwnAddress(clipboard);
|
|
|
|
} else {
|
|
|
|
return wallet.isInvoiceGeneratedByWallet(clipboard) || wallet.weOwnAddress(clipboard);
|
2019-03-04 20:04:40 -05:00
|
|
|
}
|
2021-09-09 20:36:35 +01:00
|
|
|
});
|
|
|
|
const isBitcoinAddress = DeeplinkSchemaMatch.isBitcoinAddress(clipboard);
|
|
|
|
const isLightningInvoice = DeeplinkSchemaMatch.isLightningInvoice(clipboard);
|
|
|
|
const isLNURL = DeeplinkSchemaMatch.isLnUrl(clipboard);
|
|
|
|
const isBothBitcoinAndLightning = DeeplinkSchemaMatch.isBothBitcoinAndLightning(clipboard);
|
|
|
|
if (
|
|
|
|
!isAddressFromStoredWallet &&
|
|
|
|
clipboardContent.current !== clipboard &&
|
|
|
|
(isBitcoinAddress || isLightningInvoice || isLNURL || isBothBitcoinAndLightning)
|
|
|
|
) {
|
2021-09-24 15:32:22 -04:00
|
|
|
let contentType;
|
2021-09-09 20:36:35 +01:00
|
|
|
if (isBitcoinAddress) {
|
2021-09-24 15:32:22 -04:00
|
|
|
contentType = ClipboardContentType.BITCOIN;
|
2021-09-09 20:36:35 +01:00
|
|
|
} else if (isLightningInvoice || isLNURL) {
|
2021-09-24 15:32:22 -04:00
|
|
|
contentType = ClipboardContentType.LIGHTNING;
|
2021-09-09 20:36:35 +01:00
|
|
|
} else if (isBothBitcoinAndLightning) {
|
2021-09-24 15:32:22 -04:00
|
|
|
contentType = ClipboardContentType.BITCOIN;
|
2021-09-09 20:36:35 +01:00
|
|
|
}
|
2021-09-24 15:32:22 -04:00
|
|
|
showClipboardAlert({ contentType });
|
2020-04-06 11:33:45 -04:00
|
|
|
}
|
2021-09-09 20:36:35 +01:00
|
|
|
clipboardContent.current = clipboard;
|
|
|
|
}
|
|
|
|
if (nextAppState) {
|
|
|
|
appState.current = nextAppState;
|
2019-03-02 07:13:12 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-11 03:07:22 -04:00
|
|
|
const handleOpenURL = event => {
|
2023-09-17 18:28:54 +02:00
|
|
|
DeeplinkSchemaMatch.navigationRouteFor(event, value => NavigationService.navigate(...value), {
|
|
|
|
wallets,
|
|
|
|
addWallet,
|
|
|
|
saveToDisk,
|
|
|
|
setSharedCosigner,
|
|
|
|
});
|
2019-12-15 08:16:29 -05:00
|
|
|
};
|
|
|
|
|
2021-09-24 15:32:22 -04:00
|
|
|
const showClipboardAlert = ({ contentType }) => {
|
2023-12-29 07:52:12 -04:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.ImpactLight);
|
2023-03-29 20:46:11 -04:00
|
|
|
BlueClipboard()
|
|
|
|
.getClipboardContent()
|
|
|
|
.then(clipboard => {
|
2024-03-13 17:44:53 -04:00
|
|
|
ActionSheet.showActionSheetWithOptions(
|
|
|
|
{
|
2023-03-29 20:46:11 -04:00
|
|
|
title: loc._.clipboard,
|
|
|
|
message: contentType === ClipboardContentType.BITCOIN ? loc.wallets.clipboard_bitcoin : loc.wallets.clipboard_lightning,
|
2024-03-13 17:44:53 -04:00
|
|
|
options: [loc._.cancel, loc._.continue],
|
|
|
|
cancelButtonIndex: 0,
|
|
|
|
},
|
|
|
|
buttonIndex => {
|
|
|
|
switch (buttonIndex) {
|
|
|
|
case 0: // Cancel
|
|
|
|
break;
|
2024-03-13 17:54:32 -04:00
|
|
|
case 1:
|
2024-03-13 17:44:53 -04:00
|
|
|
handleOpenURL({ url: clipboard });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-03-29 20:46:11 -04:00
|
|
|
});
|
2019-03-02 07:13:12 -05:00
|
|
|
};
|
2021-09-24 15:32:22 -04:00
|
|
|
|
2024-01-24 20:23:06 -04:00
|
|
|
useEffect(() => {
|
2024-01-24 22:53:33 -04:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
// Call hide to setup the listener on the native side
|
2024-01-30 11:41:52 -04:00
|
|
|
SplashScreen?.addObserver();
|
2024-01-24 22:53:33 -04:00
|
|
|
}
|
2024-01-24 20:23:06 -04:00
|
|
|
}, []);
|
|
|
|
|
2020-10-11 03:07:22 -04:00
|
|
|
return (
|
|
|
|
<SafeAreaProvider>
|
|
|
|
<View style={styles.root}>
|
|
|
|
<NavigationContainer ref={navigationRef} theme={colorScheme === 'dark' ? BlueDarkTheme : BlueDefaultTheme}>
|
2024-03-23 23:48:44 -04:00
|
|
|
<NavigationProvider>
|
|
|
|
<InitRoot />
|
|
|
|
<Notifications onProcessNotifications={processPushNotifications} />
|
|
|
|
<MenuElements />
|
|
|
|
<DeviceQuickActions />
|
2024-03-24 10:52:10 -04:00
|
|
|
<Biometric />
|
2024-04-02 21:50:19 -04:00
|
|
|
<HandOffComponentListener />
|
2024-03-23 23:48:44 -04:00
|
|
|
</NavigationProvider>
|
2020-10-11 03:07:22 -04:00
|
|
|
</NavigationContainer>
|
|
|
|
</View>
|
2023-10-30 12:38:12 -04:00
|
|
|
<WatchConnectivity />
|
2020-11-02 08:11:28 -05:00
|
|
|
<WidgetCommunication />
|
2020-10-11 03:07:22 -04:00
|
|
|
</SafeAreaProvider>
|
|
|
|
);
|
|
|
|
};
|
2019-03-02 07:13:12 -05:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2020-05-30 11:16:03 +03:00
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
2019-03-02 07:13:12 -05:00
|
|
|
});
|
2020-10-11 03:07:22 -04:00
|
|
|
|
|
|
|
export default App;
|