2018-09-30 10:31:09 +02:00
|
|
|
/* global alert */
|
|
|
|
import React, { Component } from 'react';
|
2019-08-23 09:04:23 +02:00
|
|
|
import { View, Alert, Platform, TouchableOpacity } from 'react-native';
|
|
|
|
import { BlueLoading, BlueHeaderDefaultSub, BlueListItem, SafeBlueArea, BlueNavigationStyle } from '../../BlueComponents';
|
2018-09-30 10:31:09 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2019-08-23 09:04:23 +02:00
|
|
|
import RNSecureKeyStore, { ACCESSIBLE } from 'react-native-secure-key-store';
|
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
|
|
|
import { AppStorage } from '../../class';
|
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
2018-09-30 10:31:09 +02:00
|
|
|
/** @type {AppStorage} */
|
|
|
|
let BlueApp = require('../../BlueApp');
|
|
|
|
let prompt = require('../../prompt');
|
|
|
|
let loc = require('../../loc');
|
|
|
|
|
|
|
|
export default class EncryptStorage extends Component {
|
2018-10-29 23:11:48 +01:00
|
|
|
static navigationOptions = () => ({
|
|
|
|
...BlueNavigationStyle(),
|
2019-08-23 09:04:23 +02:00
|
|
|
title: loc.settings.security,
|
2018-10-29 23:11:48 +01:00
|
|
|
});
|
2018-09-30 10:31:09 +02:00
|
|
|
|
2019-08-23 09:04:23 +02:00
|
|
|
static deleteWalletAfterUninstall = 'deleteWalletAfterUninstall';
|
|
|
|
|
2018-09-30 10:31:09 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
language: loc.getLanguage(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2019-08-23 09:04:23 +02:00
|
|
|
let deleteWalletsAfterUninstall = true;
|
|
|
|
try {
|
|
|
|
deleteWalletsAfterUninstall = await RNSecureKeyStore.get(EncryptStorage.deleteWalletAfterUninstall);
|
|
|
|
deleteWalletsAfterUninstall =
|
|
|
|
deleteWalletsAfterUninstall === true || deleteWalletsAfterUninstall === '1' || deleteWalletsAfterUninstall === 1;
|
|
|
|
} catch (_e) {
|
|
|
|
deleteWalletsAfterUninstall = true;
|
|
|
|
}
|
2018-09-30 10:31:09 +02:00
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
2019-08-23 09:04:23 +02:00
|
|
|
advancedModeEnabled: (await AsyncStorage.getItem(AppStorage.ADVANCED_MODE_ENABLED)) || false,
|
2018-09-30 10:31:09 +02:00
|
|
|
storageIsEncrypted: await BlueApp.storageIsEncrypted(),
|
2019-08-23 09:04:23 +02:00
|
|
|
deleteWalletsAfterUninstall,
|
2018-09-30 10:31:09 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-23 09:04:23 +02:00
|
|
|
decryptStorage = async () => {
|
|
|
|
const password = await prompt(loc.settings.password, loc._.storage_is_encrypted).catch(() => {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await BlueApp.decryptStorage(password);
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
storageIsEncrypted: await BlueApp.storageIsEncrypted(),
|
|
|
|
deleteWalletAfterUninstall: await RNSecureKeyStore.get(EncryptStorage.deleteWalletAfterUninstall),
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2019-08-23 09:25:12 +02:00
|
|
|
if (password) {
|
|
|
|
alert(loc._.bad_password);
|
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
|
|
|
}
|
2019-08-23 09:04:23 +02:00
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
storageIsEncrypted: await BlueApp.storageIsEncrypted(),
|
|
|
|
deleteWalletAfterUninstall: await RNSecureKeyStore.get(EncryptStorage.deleteWalletAfterUninstall),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onDeleteWalletsAfterUninstallSwitch = async value => {
|
|
|
|
await RNSecureKeyStore.setResetOnAppUninstallTo(value);
|
|
|
|
await RNSecureKeyStore.set(EncryptStorage.deleteWalletAfterUninstall, value, { accessible: ACCESSIBLE.WHEN_UNLOCKED });
|
|
|
|
this.setState({ deleteWalletsAfterUninstall: value });
|
|
|
|
};
|
|
|
|
|
|
|
|
onEncryptStorageSwitch = value => {
|
|
|
|
this.setState({ isLoading: true }, async () => {
|
|
|
|
if (value === true) {
|
|
|
|
let p1 = await prompt(loc.settings.password, loc.settings.password_explain).catch(() => {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
p1 = undefined;
|
|
|
|
});
|
|
|
|
if (!p1) {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let p2 = await prompt(loc.settings.password, loc.settings.retype_password).catch(() => {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
});
|
|
|
|
if (p1 === p2) {
|
|
|
|
await BlueApp.encryptStorage(p1);
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
storageIsEncrypted: await BlueApp.storageIsEncrypted(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
alert(loc.settings.passwords_do_not_match);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Alert.alert(
|
|
|
|
'Decrypt Storage',
|
|
|
|
'Are you sure you want to decrypt your storage?',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: loc.send.details.cancel,
|
|
|
|
style: 'cancel',
|
|
|
|
onPress: () => this.setState({ isLoading: false }),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: loc._.ok,
|
|
|
|
style: 'destructive',
|
|
|
|
onPress: this.decryptStorage,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ cancelable: false },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-09-30 10:31:09 +02:00
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return <BlueLoading />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
2019-08-23 09:04:23 +02:00
|
|
|
<View>
|
|
|
|
<BlueHeaderDefaultSub leftText="storage" rightComponent={null} />
|
|
|
|
<BlueListItem
|
|
|
|
hideChevron
|
|
|
|
title="Encypted and Password protected"
|
|
|
|
switchButton
|
|
|
|
onSwitch={this.onEncryptStorageSwitch}
|
|
|
|
switched={this.state.storageIsEncrypted}
|
|
|
|
/>
|
|
|
|
{Platform.OS === 'ios' && this.state.storageIsEncrypted && (
|
|
|
|
<BlueListItem
|
|
|
|
hideChevron
|
|
|
|
disabled={!this.state.storageIsEncrypted}
|
|
|
|
switchDisabled={!this.state.storageIsEncrypted}
|
|
|
|
title="Delete if BlueWallet is uninstalled"
|
|
|
|
switchButton
|
|
|
|
onSwitch={this.onDeleteWalletsAfterUninstallSwitch}
|
|
|
|
switched={this.state.deleteWalletsAfterUninstall}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{this.state.storageIsEncrypted && (
|
|
|
|
<TouchableOpacity
|
|
|
|
disabled={!this.state.storageIsEncrypted}
|
|
|
|
onPress={() => this.props.navigation.navigate('PlausibleDeniability')}
|
|
|
|
>
|
|
|
|
<BlueListItem disabled={!this.state.storageIsEncrypted} title={loc.settings.plausible_deniability} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
)}
|
|
|
|
</View>
|
2018-09-30 10:31:09 +02:00
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EncryptStorage.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
|
|
|
navigate: PropTypes.func,
|
|
|
|
goBack: PropTypes.func,
|
|
|
|
}),
|
|
|
|
};
|