BlueWallet/class/biometrics.ts

178 lines
5.5 KiB
TypeScript
Raw Normal View History

import { useContext } from 'react';
import { Alert, Platform } from 'react-native';
import { CommonActions, StackActions } from '@react-navigation/native';
import ReactNativeBiometrics, { BiometryTypes as RNBiometryTypes } from 'react-native-biometrics';
import PasscodeAuth from 'react-native-passcode-auth';
import RNSecureKeyStore from 'react-native-secure-key-store';
2020-07-21 00:14:02 +02:00
import loc from '../loc';
import * as NavigationService from '../NavigationService';
import { BlueStorageContext } from '../blue_modules/storage-context';
import presentAlert from '../components/Alert';
const STORAGEKEY = 'Biometrics';
const rnBiometrics = new ReactNativeBiometrics({ allowDeviceCredentials: true });
2024-02-12 12:36:51 +01:00
// Define a function type with properties
type DescribableFunction = {
(): null; // Call signature
FaceID: 'Face ID';
TouchID: 'Touch ID';
Biometrics: 'Biometrics';
isBiometricUseCapableAndEnabled: () => Promise<boolean>;
isDeviceBiometricCapable: () => Promise<boolean>;
setBiometricUseEnabled: (arg: boolean) => Promise<void>;
biometricType: () => Promise<keyof typeof RNBiometryTypes | undefined>;
isBiometricUseEnabled: () => Promise<boolean>;
unlockWithBiometrics: () => Promise<boolean>;
showKeychainWipeAlert: () => void;
};
// Bastard component/module. All properties are added in runtime
const Biometric = function () {
2021-05-18 22:38:18 +02:00
const { getItem, setItem } = useContext(BlueStorageContext);
Biometric.FaceID = 'Face ID';
Biometric.TouchID = 'Touch ID';
Biometric.Biometrics = 'Biometrics';
Biometric.isDeviceBiometricCapable = async () => {
2020-09-09 02:01:55 +02:00
try {
const { available } = await rnBiometrics.isSensorAvailable();
return available;
} 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 { available, biometryType } = await rnBiometrics.isSensorAvailable();
if (!available) {
return undefined;
}
return biometryType;
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 undefined; // Explicitly return false in case of an error
2019-10-29 23:13:19 +01:00
}
};
Biometric.isBiometricUseEnabled = async () => {
try {
const enabledBiometrics = await getItem(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(STORAGEKEY, value === true ? '1' : '');
};
Biometric.unlockWithBiometrics = async () => {
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
if (isDeviceBiometricCapable) {
return new Promise(resolve => {
rnBiometrics
.simplePrompt({ promptMessage: loc.settings.biom_conf_identity })
.then((result: { success: any }) => {
if (result.success) {
resolve(true);
} else {
console.log('Biometrics authentication failed');
resolve(false);
}
})
.catch((error: Error) => {
console.log('Biometrics authentication error');
console.log(error);
resolve(false);
});
});
}
return false;
};
const clearKeychain = async () => {
await RNSecureKeyStore.remove('data');
await RNSecureKeyStore.remove('data_encrypted');
await RNSecureKeyStore.remove(STORAGEKEY);
NavigationService.dispatch(StackActions.replace('WalletsRoot'));
};
const requestDevicePasscode = async () => {
let isDevicePasscodeSupported: boolean | undefined = 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: () => clearKeychain(),
},
],
{ cancelable: false },
);
}
}
} catch {
isDevicePasscodeSupported = undefined;
}
if (isDevicePasscodeSupported === false) {
presentAlert({ message: 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: () => requestDevicePasscode(),
style: 'default',
},
],
{ cancelable: false },
);
}
};
return null;
} as DescribableFunction;
export default Biometric;
export { RNBiometryTypes as BiometricType };