BlueWallet/screen/settings/encryptStorage.js

186 lines
6.4 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState, useCallback, useContext } from 'react';
import { View, ScrollView, Alert, TouchableOpacity, TouchableWithoutFeedback, Text, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
import { BlueLoading, BlueSpacing20, BlueCard, BlueText } from '../../BlueComponents';
import Biometric from '../../class/biometrics';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import alert from '../../components/Alert';
2023-12-16 17:44:35 -04:00
import ListItem from '../../components/ListItem';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import { useTheme } from '../../components/themes';
2022-09-05 19:34:02 +01:00
const prompt = require('../../helpers/prompt');
2020-07-15 13:32:59 -04:00
const EncryptStorage = () => {
2021-01-14 10:30:29 -05:00
const { isStorageEncrypted, encryptStorage, decryptStorage, saveToDisk } = useContext(BlueStorageContext);
2020-07-15 13:32:59 -04:00
const [isLoading, setIsLoading] = useState(true);
const [biometrics, setBiometrics] = useState({ isDeviceBiometricCapable: false, isBiometricsEnabled: false, biometricsType: '' });
const [storageIsEncryptedSwitchEnabled, setStorageIsEncryptedSwitchEnabled] = useState(false);
const { navigate, popToTop } = useNavigation();
const { colors } = useTheme();
const styleHooks = StyleSheet.create({
2020-07-15 13:32:59 -04:00
root: {
backgroundColor: colors.background,
},
headerText: {
color: colors.foregroundColor,
},
});
2020-07-15 13:32:59 -04:00
const initialState = useCallback(async () => {
const isBiometricsEnabled = await Biometric.isBiometricUseEnabled();
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
2020-12-29 15:38:36 -05:00
const biometricsType = (await Biometric.biometricType()) || loc.settings.biometrics;
const isStorageEncryptedSwitchEnabled = await isStorageEncrypted();
setStorageIsEncryptedSwitchEnabled(isStorageEncryptedSwitchEnabled);
2020-07-15 13:32:59 -04:00
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 03:04:23 -04:00
const password = await prompt(loc.settings.password, loc._.storage_is_encrypted).catch(() => {
2020-07-15 13:32:59 -04:00
setIsLoading(false);
2019-08-23 03:04:23 -04:00
});
try {
await decryptStorage(password);
2020-11-02 08:11:28 -05:00
await saveToDisk();
popToTop();
2019-08-23 03:04:23 -04:00
} catch (e) {
if (password) {
alert(loc._.bad_password);
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
}
2020-07-15 13:32:59 -04:00
setIsLoading(false);
setStorageIsEncryptedSwitchEnabled(await isStorageEncrypted());
2019-08-23 03:04:23 -04:00
}
};
2019-08-23 03:04:23 -04: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 08:11:28 -05:00
saveToDisk();
2019-08-23 03:04:23 -04:00
} else {
setIsLoading(false);
alert(loc.settings.passwords_do_not_match);
2019-08-23 03:04:23 -04: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 },
);
}
};
2019-08-23 03:04:23 -04: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);
}
};
2020-07-15 13:32:59 -04:00
const navigateToPlausibleDeniability = () => {
navigate('PlausibleDeniability');
};
2020-07-15 13:32:59 -04:00
return isLoading ? (
2024-01-13 10:56:29 -04:00
<ScrollView>
2020-11-29 23:18:54 -05:00
<BlueLoading />
2024-01-13 10:56:29 -04:00
</ScrollView>
2020-07-15 13:32:59 -04:00
) : (
2024-01-13 10:56:29 -04:00
<ScrollView contentContainerStyle={styles.root} automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
<View style={styles.paddingTop} />
2024-01-13 10:56:29 -04:00
{biometrics.isDeviceBiometricCapable && (
<>
<Text adjustsFontSizeToFit style={[styles.headerText, styleHooks.headerText]}>
{loc.settings.biometrics}
</Text>
2023-12-16 17:44:35 -04:00
<ListItem
2024-01-13 10:56:29 -04:00
title={loc.formatString(loc.settings.encrypt_use, { type: biometrics.biometricsType })}
Component={TouchableWithoutFeedback}
switch={{ value: biometrics.isBiometricsEnabled, onValueChange: onUseBiometricSwitch }}
2020-07-15 13:32:59 -04:00
/>
2024-01-13 10:56:29 -04:00
<BlueCard>
<BlueText>{loc.formatString(loc.settings.encrypt_use_expl, { type: biometrics.biometricsType })}</BlueText>
</BlueCard>
<BlueSpacing20 />
</>
)}
<Text adjustsFontSizeToFit style={[styles.headerText, styleHooks.headerText]}>
{loc.settings.encrypt_tstorage}
</Text>
2024-01-13 10:56:29 -04:00
<ListItem
testID="EncyptedAndPasswordProtected"
hideChevron
title={loc.settings.encrypt_enc_and_pass}
Component={TouchableWithoutFeedback}
switch={{ onValueChange: onEncryptStorageSwitch, value: storageIsEncryptedSwitchEnabled }}
/>
{storageIsEncryptedSwitchEnabled && (
<ListItem
onPress={navigateToPlausibleDeniability}
title={loc.settings.plausible_deniability}
chevron
testID="PlausibleDeniabilityButton"
Component={TouchableOpacity}
/>
)}
</ScrollView>
2020-07-15 13:32:59 -04:00
);
};
2020-07-15 13:32:59 -04:00
const styles = StyleSheet.create({
root: {
flex: 1,
},
paddingTop: { paddingTop: 19 },
headerText: {
fontWeight: 'bold',
fontSize: 30,
marginLeft: 17,
},
});
2020-07-15 13:32:59 -04:00
export default EncryptStorage;
2021-02-15 11:03:54 +03:00
EncryptStorage.navigationOptions = navigationStyle({}, opts => ({ ...opts, headerTitle: loc.settings.encrypt_title }));