2018-09-30 10:31:09 +02:00
|
|
|
/* global alert */
|
2020-10-24 19:20:59 +02:00
|
|
|
import React, { useEffect, useState, useCallback, useContext } from 'react';
|
2020-05-24 11:17:26 +02:00
|
|
|
import { ScrollView, Alert, Platform, TouchableOpacity, TouchableWithoutFeedback, StyleSheet } from 'react-native';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
2020-07-20 15:38:46 +02:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
2019-11-10 12:43:41 +01:00
|
|
|
import {
|
2020-11-30 05:18:54 +01:00
|
|
|
BlueLoading,
|
2019-11-10 12:43:41 +01:00
|
|
|
SafeBlueArea,
|
|
|
|
BlueSpacing20,
|
|
|
|
BlueCard,
|
2020-09-22 03:53:28 +02:00
|
|
|
BlueListItem,
|
2020-07-15 19:32:59 +02:00
|
|
|
BlueHeaderDefaultSubHooks,
|
2020-11-22 09:04:04 +01:00
|
|
|
BlueText,
|
2020-12-04 14:39:47 +01:00
|
|
|
BlueNavigationStyle,
|
2019-11-10 12:43:41 +01:00
|
|
|
} from '../../BlueComponents';
|
|
|
|
import Biometric from '../../class/biometrics';
|
2020-07-20 15:38:46 +02:00
|
|
|
import loc from '../../loc';
|
2020-12-04 14:39:47 +01:00
|
|
|
import { colors } from 'react-native-elements';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2020-12-28 00:33:57 +01:00
|
|
|
import { isCatalyst } from '../../blue_modules/environment';
|
2020-07-01 13:56:52 +02:00
|
|
|
const prompt = require('../../blue_modules/prompt');
|
2018-09-30 10:31:09 +02:00
|
|
|
|
2020-07-15 19:32:59 +02:00
|
|
|
const EncryptStorage = () => {
|
2020-11-03 21:23:34 +01:00
|
|
|
const {
|
|
|
|
isStorageEncrypted,
|
|
|
|
setResetOnAppUninstallTo,
|
|
|
|
encryptStorage,
|
|
|
|
isDeleteWalletAfterUninstallEnabled,
|
|
|
|
decryptStorage,
|
|
|
|
saveToDisk,
|
|
|
|
} = useContext(BlueStorageContext);
|
2020-07-15 19:32:59 +02:00
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
const [deleteWalletsAfterUninstall, setDeleteWalletsAfterUninstall] = useState(false);
|
|
|
|
const [biometrics, setBiometrics] = useState({ isDeviceBiometricCapable: false, isBiometricsEnabled: false, biometricsType: '' });
|
2020-10-24 19:20:59 +02:00
|
|
|
const [storageIsEncryptedSwitchEnabled, setStorageIsEncryptedSwitchEnabled] = useState(false);
|
|
|
|
const { navigate, popToTop } = useNavigation();
|
2020-07-15 19:32:59 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: colors.background,
|
|
|
|
},
|
2018-10-29 23:11:48 +01:00
|
|
|
});
|
2018-09-30 10:31:09 +02:00
|
|
|
|
2020-07-15 19:32:59 +02:00
|
|
|
const initialState = useCallback(async () => {
|
2019-11-10 12:43:41 +01:00
|
|
|
const isBiometricsEnabled = await Biometric.isBiometricUseEnabled();
|
|
|
|
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
|
2020-12-28 00:33:57 +01:00
|
|
|
const biometricsType = (await Biometric.biometricType()) || loc.settings.biometrics.toLowercase();
|
2020-10-24 19:20:59 +02:00
|
|
|
const deleteWalletsAfterUninstall = await isDeleteWalletAfterUninstallEnabled();
|
|
|
|
const isStorageEncryptedSwitchEnabled = await isStorageEncrypted();
|
|
|
|
setStorageIsEncryptedSwitchEnabled(isStorageEncryptedSwitchEnabled);
|
2020-07-15 19:32:59 +02:00
|
|
|
setDeleteWalletsAfterUninstall(deleteWalletsAfterUninstall);
|
|
|
|
setBiometrics({ isBiometricsEnabled, isDeviceBiometricCapable, biometricsType });
|
|
|
|
setIsLoading(false);
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
|
|
initialState();
|
2020-10-24 19:20:59 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2018-09-30 10:31:09 +02:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
const handleDecryptStorage = async () => {
|
2019-08-23 09:04:23 +02:00
|
|
|
const password = await prompt(loc.settings.password, loc._.storage_is_encrypted).catch(() => {
|
2020-07-15 19:32:59 +02:00
|
|
|
setIsLoading(false);
|
2019-08-23 09:04:23 +02:00
|
|
|
});
|
|
|
|
try {
|
2020-10-24 19:20:59 +02:00
|
|
|
await decryptStorage(password);
|
2020-11-02 14:11:28 +01:00
|
|
|
await saveToDisk();
|
2020-10-24 19:20:59 +02:00
|
|
|
popToTop();
|
2019-08-23 09:04:23 +02:00
|
|
|
} catch (e) {
|
2019-08-23 09:25:12 +02:00
|
|
|
if (password) {
|
|
|
|
alert(loc._.bad_password);
|
2019-09-14 06:34:24 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-08-23 09:25:12 +02:00
|
|
|
}
|
2020-07-15 19:32:59 +02:00
|
|
|
|
|
|
|
setIsLoading(false);
|
2020-10-24 19:20:59 +02:00
|
|
|
setStorageIsEncryptedSwitchEnabled(await isStorageEncrypted());
|
|
|
|
setDeleteWalletsAfterUninstall(await isDeleteWalletAfterUninstallEnabled());
|
2019-08-23 09:04:23 +02:00
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
};
|
2019-08-23 09:04:23 +02:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
const onDeleteWalletsAfterUninstallSwitch = async value => {
|
|
|
|
await setResetOnAppUninstallTo(value);
|
2020-07-15 19:32:59 +02:00
|
|
|
setDeleteWalletsAfterUninstall(value);
|
2020-10-24 19:20:59 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
};
|
2019-08-23 09:04:23 +02:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
const onEncryptStorageSwitch = async value => {
|
|
|
|
setIsLoading(true);
|
|
|
|
if (value === true) {
|
|
|
|
let p1 = await prompt(loc.settings.password, loc.settings.password_explain).catch(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
p1 = undefined;
|
|
|
|
});
|
|
|
|
if (!p1) {
|
|
|
|
setIsLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const p2 = await prompt(loc.settings.password, loc.settings.retype_password).catch(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
});
|
|
|
|
if (p1 === p2) {
|
|
|
|
await encryptStorage(p1);
|
|
|
|
setIsLoading(false);
|
|
|
|
setStorageIsEncryptedSwitchEnabled(await isStorageEncrypted());
|
2020-11-02 14:11:28 +01:00
|
|
|
saveToDisk();
|
2019-08-23 09:04:23 +02:00
|
|
|
} else {
|
2020-10-24 19:20:59 +02:00
|
|
|
setIsLoading(false);
|
|
|
|
alert(loc.settings.passwords_do_not_match);
|
2019-08-23 09:04:23 +02:00
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
} else {
|
|
|
|
Alert.alert(
|
|
|
|
loc.settings.encrypt_decrypt,
|
|
|
|
loc.settings.encrypt_decrypt_q,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: loc._.cancel,
|
|
|
|
style: 'cancel',
|
|
|
|
onPress: () => setIsLoading(false),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: loc._.ok,
|
|
|
|
style: 'destructive',
|
|
|
|
onPress: handleDecryptStorage,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ cancelable: false },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
};
|
2019-08-23 09:04:23 +02:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
const onUseBiometricSwitch = async value => {
|
|
|
|
const isBiometricsEnabled = {
|
|
|
|
isDeviceBiometricCapable: biometrics.isDeviceBiometricCapable,
|
|
|
|
isBiometricsEnabled: biometrics.isBiometricsEnabled,
|
|
|
|
biometricsType: biometrics.biometricsType,
|
|
|
|
};
|
|
|
|
if (await Biometric.unlockWithBiometrics()) {
|
|
|
|
isBiometricsEnabled.isBiometricsEnabled = value;
|
|
|
|
await Biometric.setBiometricUseEnabled(value);
|
|
|
|
setBiometrics(isBiometricsEnabled);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
};
|
2019-11-10 12:43:41 +01:00
|
|
|
|
2020-07-15 19:32:59 +02:00
|
|
|
const navigateToPlausibleDeniability = () => {
|
|
|
|
navigate('PlausibleDeniability');
|
|
|
|
};
|
2018-09-30 10:31:09 +02:00
|
|
|
|
2020-07-15 19:32:59 +02:00
|
|
|
return isLoading ? (
|
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
|
2020-11-30 05:18:54 +01:00
|
|
|
<BlueLoading />
|
2020-07-15 19:32:59 +02:00
|
|
|
</SafeBlueArea>
|
|
|
|
) : (
|
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
|
|
|
|
<ScrollView contentContainerStyle={styles.root}>
|
|
|
|
{biometrics.isDeviceBiometricCapable && (
|
|
|
|
<>
|
2020-12-28 00:33:57 +01:00
|
|
|
<BlueHeaderDefaultSubHooks leftText={loc.settings.biometrics} rightComponent={null} />
|
2020-09-22 03:53:28 +02:00
|
|
|
<BlueListItem
|
2020-07-20 15:38:46 +02:00
|
|
|
title={loc.formatString(loc.settings.encrypt_use, { type: biometrics.biometricsType })}
|
2020-07-15 19:32:59 +02:00
|
|
|
Component={TouchableWithoutFeedback}
|
|
|
|
switch={{ value: biometrics.isBiometricsEnabled, onValueChange: onUseBiometricSwitch }}
|
|
|
|
/>
|
|
|
|
<BlueCard>
|
2020-11-22 09:04:04 +01:00
|
|
|
<BlueText>{loc.formatString(loc.settings.encrypt_use_expl, { type: biometrics.biometricsType })}</BlueText>
|
2020-07-15 19:32:59 +02:00
|
|
|
</BlueCard>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
</>
|
|
|
|
)}
|
2020-07-20 15:38:46 +02:00
|
|
|
<BlueHeaderDefaultSubHooks leftText={loc.settings.encrypt_tstorage} rightComponent={null} />
|
2020-09-22 03:53:28 +02:00
|
|
|
<BlueListItem
|
2020-07-15 19:32:59 +02:00
|
|
|
testID="EncyptedAndPasswordProtected"
|
|
|
|
hideChevron
|
2020-07-20 15:38:46 +02:00
|
|
|
title={loc.settings.encrypt_enc_and_pass}
|
2020-07-15 19:32:59 +02:00
|
|
|
Component={TouchableWithoutFeedback}
|
2020-10-24 19:20:59 +02:00
|
|
|
switch={{ onValueChange: onEncryptStorageSwitch, value: storageIsEncryptedSwitchEnabled }}
|
2020-07-15 19:32:59 +02:00
|
|
|
/>
|
2020-12-28 00:33:57 +01:00
|
|
|
{Platform.OS === 'ios' && !isCatalyst && (
|
2020-09-22 03:53:28 +02:00
|
|
|
<BlueListItem
|
2019-08-23 09:04:23 +02:00
|
|
|
hideChevron
|
2020-07-20 15:38:46 +02:00
|
|
|
title={loc.settings.encrypt_del_uninstall}
|
2020-05-03 20:17:49 +02:00
|
|
|
Component={TouchableWithoutFeedback}
|
2020-07-15 19:32:59 +02:00
|
|
|
switch={{
|
|
|
|
onValueChange: onDeleteWalletsAfterUninstallSwitch,
|
|
|
|
value: deleteWalletsAfterUninstall,
|
|
|
|
}}
|
2019-08-23 09:04:23 +02:00
|
|
|
/>
|
2020-07-15 19:32:59 +02:00
|
|
|
)}
|
2020-10-24 19:20:59 +02:00
|
|
|
{storageIsEncryptedSwitchEnabled && (
|
2020-09-22 03:53:28 +02:00
|
|
|
<BlueListItem
|
2020-07-15 19:32:59 +02:00
|
|
|
onPress={navigateToPlausibleDeniability}
|
|
|
|
title={loc.settings.plausible_deniability}
|
|
|
|
chevron
|
|
|
|
testID="PlausibleDeniabilityButton"
|
|
|
|
Component={TouchableOpacity}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ScrollView>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
2018-09-30 10:31:09 +02:00
|
|
|
};
|
2020-07-15 19:32:59 +02:00
|
|
|
|
|
|
|
export default EncryptStorage;
|
2020-12-04 14:39:47 +01:00
|
|
|
EncryptStorage.navigationOptions = () => ({
|
|
|
|
...BlueNavigationStyle(),
|
2020-07-20 15:38:46 +02:00
|
|
|
headerTitle: loc.settings.encrypt_title,
|
2020-07-15 19:32:59 +02:00
|
|
|
});
|