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