BlueWallet/screen/settings/encryptStorage.js

210 lines
7.4 KiB
JavaScript
Raw Normal View History

/* global alert */
import React, { useEffect, useState, useCallback, useContext } from 'react';
import { ScrollView, Alert, Platform, TouchableOpacity, TouchableWithoutFeedback, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';
2020-07-20 15:38:46 +02:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2020-12-25 17:09:53 +01:00
import { colors } from 'react-native-elements';
import navigationStyle from '../../components/navigationStyle';
import {
2020-11-30 05:18:54 +01:00
BlueLoading,
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,
} from '../../BlueComponents';
import Biometric from '../../class/biometrics';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import { isCatalyst } from '../../blue_modules/environment';
const prompt = require('../../blue_modules/prompt');
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: '' });
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,
},
});
2020-07-15 19:32:59 +02:00
const initialState = useCallback(async () => {
const isBiometricsEnabled = await Biometric.isBiometricUseEnabled();
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
2020-12-29 21:38:36 +01:00
const biometricsType = (await Biometric.biometricType()) || loc.settings.biometrics;
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();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
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 {
await decryptStorage(password);
2020-11-02 14:11:28 +01:00
await saveToDisk();
popToTop();
2019-08-23 09:04:23 +02:00
} catch (e) {
if (password) {
alert(loc._.bad_password);
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
}
2020-07-15 19:32:59 +02:00
setIsLoading(false);
setStorageIsEncryptedSwitchEnabled(await isStorageEncrypted());
setDeleteWalletsAfterUninstall(await isDeleteWalletAfterUninstallEnabled());
2019-08-23 09:04:23 +02:00
}
// eslint-disable-next-line react-hooks/exhaustive-deps
};
2019-08-23 09:04:23 +02:00
const onDeleteWalletsAfterUninstallSwitch = async value => {
await setResetOnAppUninstallTo(value);
2020-07-15 19:32:59 +02:00
setDeleteWalletsAfterUninstall(value);
// eslint-disable-next-line react-hooks/exhaustive-deps
};
2019-08-23 09:04:23 +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 {
setIsLoading(false);
alert(loc.settings.passwords_do_not_match);
2019-08-23 09:04:23 +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
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
};
2020-07-15 19:32:59 +02:00
const navigateToPlausibleDeniability = () => {
navigate('PlausibleDeniability');
};
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 && (
<>
<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}
switch={{ onValueChange: onEncryptStorageSwitch, value: storageIsEncryptedSwitchEnabled }}
2020-07-15 19:32:59 +02: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
)}
{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>
);
};
2020-07-15 19:32:59 +02:00
export default EncryptStorage;
2020-12-25 17:09:53 +01:00
EncryptStorage.navigationOptions = navigationStyle({
2020-07-20 15:38:46 +02:00
headerTitle: loc.settings.encrypt_title,
2020-07-15 19:32:59 +02:00
});