forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnlockWith.js
167 lines (152 loc) · 5.45 KB
/
UnlockWith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { StackActions, useNavigation, useRoute } from '@react-navigation/native';
import LottieView from 'lottie-react-native';
import React, { useContext, useEffect, useState, useRef } from 'react';
import { ActivityIndicator, Image, LayoutAnimation, StatusBar, StyleSheet, TouchableOpacity, useColorScheme, View, Animated } from 'react-native';
import { Icon } from 'react-native-elements';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import { SafeAreaView } from 'react-native-safe-area-context';
import { isHandset } from './blue_modules/environment';
import { BlueStorageContext } from './blue_modules/storage-context';
import Biometric from './class/biometrics';
const styles = StyleSheet.create({
root: {
flex: 1,
},
container: {
flex: 1,
justifyContent: 'space-between',
alignItems: 'center',
},
biometric: {
flex: 1,
justifyContent: 'flex-end',
marginBottom: 58,
},
biometricRow: {
justifyContent: 'center',
flexDirection: 'row',
},
icon: {
width: 64,
height: 64,
},
});
const UnlockWith = () => {
const { setWalletsInitialized, isStorageEncrypted, startAndDecrypt } = useContext(BlueStorageContext);
const { dispatch } = useNavigation();
const { unlockOnComponentMount } = useRoute().params;
const [biometricType, setBiometricType] = useState(false);
const [isStorageEncryptedEnabled, setIsStorageEncryptedEnabled] = useState(false);
const [isAuthenticating, setIsAuthenticating] = useState(false);
const [animationDidFinish, setAnimationDidFinish] = useState(false);
const colorScheme = useColorScheme();
const fadeAnim = useRef(new Animated.Value(0)).current;
const initialRender = async () => {
let biometricType = false;
if (await Biometric.isBiometricUseCapableAndEnabled()) {
biometricType = await Biometric.biometricType();
}
setBiometricType(biometricType);
};
useEffect(() => {
initialRender();
onAnimationFinish();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
console.log('animate start')
Animated.timing(fadeAnim, {
toValue: 1,
duration: 2000,
useNativeDriver: true,
}).start(onAnimationFinish);
}, [fadeAnim]);
const successfullyAuthenticated = () => {
setWalletsInitialized(true);
dispatch(StackActions.replace(isHandset ? 'Navigation' : 'DrawerRoot'));
};
const unlockWithBiometrics = async () => {
if (await isStorageEncrypted()) {
unlockWithKey();
}
setIsAuthenticating(true);
if (await Biometric.unlockWithBiometrics()) {
setIsAuthenticating(false);
await startAndDecrypt();
return successfullyAuthenticated();
}
setIsAuthenticating(false);
};
const unlockWithKey = async () => {
setIsAuthenticating(true);
if (await startAndDecrypt()) {
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
successfullyAuthenticated();
} else {
setIsAuthenticating(false);
}
};
const renderUnlockOptions = () => {
if (isAuthenticating) {
return <ActivityIndicator />;
} else {
const color = colorScheme === 'dark' ? '#FFFFFF' : '#000000';
if ((biometricType === Biometric.TouchID || biometricType === Biometric.Biometrics) && !isStorageEncryptedEnabled) {
return (
<TouchableOpacity accessibilityRole="button" disabled={isAuthenticating} onPress={unlockWithBiometrics}>
<Icon name="fingerprint" size={64} type="font-awesome5" color={color} />
</TouchableOpacity>
);
} else if (biometricType === Biometric.FaceID && !isStorageEncryptedEnabled) {
return (
<TouchableOpacity accessibilityRole="button" disabled={isAuthenticating} onPress={unlockWithBiometrics}>
<Image
source={colorScheme === 'dark' ? require('./img/faceid-default.png') : require('./img/faceid-dark.png')}
style={styles.icon}
/>
</TouchableOpacity>
);
} else if (isStorageEncryptedEnabled) {
return (
<TouchableOpacity accessibilityRole="button" disabled={isAuthenticating} onPress={unlockWithKey}>
<Icon name="lock" size={64} type="font-awesome5" color={color} />
</TouchableOpacity>
);
}
}
};
const onAnimationFinish = async () => {
console.log('animate end')
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
if (unlockOnComponentMount) {
const storageIsEncrypted = await isStorageEncrypted();
setIsStorageEncryptedEnabled(storageIsEncrypted);
if (!biometricType || storageIsEncrypted) {
unlockWithKey();
} else if (typeof biometricType === 'string') unlockWithBiometrics();
}
setAnimationDidFinish(true);
};
return (
<SafeAreaView style={styles.root}>
<StatusBar barStyle="default" />
<View style={styles.container}>
<Animated.View style={{
opacity: fadeAnim,
marginTop: 'auto',
marginBottom: 'auto',
flex: 1,
justifyContent: 'center'
}}>
<Image
source={(() => {
return require('./img/icon.png');
})()} style={{width: 150, height: 150}}
/>
</Animated.View>
<View style={styles.biometric}>{animationDidFinish && <View style={styles.biometricRow}>{renderUnlockOptions()}</View>}</View>
</View>
</SafeAreaView>
);
};
export default UnlockWith;