BlueWallet/class/biometrics.js

148 lines
4.5 KiB
JavaScript
Raw Normal View History

import FingerprintScanner from 'react-native-fingerprint-scanner';
import { Platform, Alert } from 'react-native';
import PasscodeAuth from 'react-native-passcode-auth';
import * as NavigationService from '../NavigationService';
2020-07-21 00:14:02 +02:00
import { StackActions, CommonActions } from '@react-navigation/native';
import RNSecureKeyStore from 'react-native-secure-key-store';
2020-07-21 00:14:02 +02:00
import loc from '../loc';
import { useContext } from 'react';
import { BlueStorageContext } from '../blue_modules/storage-context';
import alert from '../components/Alert';
function Biometric() {
2021-05-18 22:38:18 +02:00
const { getItem, setItem } = useContext(BlueStorageContext);
Biometric.STORAGEKEY = 'Biometrics';
Biometric.FaceID = 'Face ID';
Biometric.TouchID = 'Touch ID';
Biometric.Biometrics = 'Biometrics';
Biometric.isDeviceBiometricCapable = async () => {
2020-09-09 02:01:55 +02:00
try {
const isDeviceBiometricCapable = await FingerprintScanner.isSensorAvailable();
if (isDeviceBiometricCapable) {
return true;
}
} catch (e) {
console.log('Biometrics isDeviceBiometricCapable failed');
console.log(e);
2020-09-09 02:01:55 +02:00
Biometric.setBiometricUseEnabled(false);
return false;
}
};
Biometric.biometricType = async () => {
2019-10-29 23:13:19 +01:00
try {
const isSensorAvailable = await FingerprintScanner.isSensorAvailable();
return isSensorAvailable;
2019-10-29 23:13:19 +01:00
} catch (e) {
console.log('Biometrics biometricType failed');
2019-10-29 23:13:19 +01:00
console.log(e);
}
return false;
};
Biometric.isBiometricUseEnabled = async () => {
try {
const enabledBiometrics = await getItem(Biometric.STORAGEKEY);
return !!enabledBiometrics;
2021-05-18 22:38:18 +02:00
} catch (_) {}
return false;
};
Biometric.isBiometricUseCapableAndEnabled = async () => {
const isBiometricUseEnabled = await Biometric.isBiometricUseEnabled();
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
2019-10-06 00:36:16 +02:00
return isBiometricUseEnabled && isDeviceBiometricCapable;
};
Biometric.setBiometricUseEnabled = async value => {
await setItem(Biometric.STORAGEKEY, value === true ? '1' : '');
};
Biometric.unlockWithBiometrics = async () => {
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
if (isDeviceBiometricCapable) {
return new Promise(resolve => {
2020-12-21 19:27:49 +01:00
FingerprintScanner.authenticate({ description: loc.settings.biom_conf_identity, fallbackEnabled: true })
.then(() => resolve(true))
.catch(error => {
console.log('Biometrics authentication failed');
console.log(error);
resolve(false);
})
.finally(() => FingerprintScanner.release());
});
}
return false;
};
Biometric.clearKeychain = async () => {
await RNSecureKeyStore.remove('data');
await RNSecureKeyStore.remove('data_encrypted');
2020-07-21 00:14:02 +02:00
await RNSecureKeyStore.remove(Biometric.STORAGEKEY);
NavigationService.dispatch(StackActions.replace('WalletsRoot'));
};
Biometric.requestDevicePasscode = async () => {
let isDevicePasscodeSupported = false;
try {
isDevicePasscodeSupported = await PasscodeAuth.isSupported();
if (isDevicePasscodeSupported) {
const isAuthenticated = await PasscodeAuth.authenticate();
if (isAuthenticated) {
Alert.alert(
2020-12-21 19:27:49 +01:00
loc.settings.encrypt_tstorage,
loc.settings.biom_remove_decrypt,
[
2020-07-21 00:14:02 +02:00
{ text: loc._.cancel, style: 'cancel' },
{
text: loc._.ok,
onPress: () => Biometric.clearKeychain(),
},
],
{ cancelable: false },
);
}
}
} catch {
isDevicePasscodeSupported = undefined;
}
if (isDevicePasscodeSupported === false) {
2020-12-21 19:27:49 +01:00
alert(loc.settings.biom_no_passcode);
}
};
Biometric.showKeychainWipeAlert = () => {
if (Platform.OS === 'ios') {
Alert.alert(
2020-12-21 19:27:49 +01:00
loc.settings.encrypt_tstorage,
loc.settings.biom_10times,
[
{
text: loc._.cancel,
onPress: () => {
NavigationService.dispatch(
CommonActions.setParams({
index: 0,
routes: [{ name: 'UnlockWithScreenRoot' }, { params: { unlockOnComponentMount: false } }],
}),
);
},
style: 'cancel',
},
{
text: loc._.ok,
onPress: () => Biometric.requestDevicePasscode(),
style: 'default',
},
],
{ cancelable: false },
);
}
};
return null;
}
export default Biometric;