forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnlockWith.tsx
183 lines (161 loc) · 5.68 KB
/
UnlockWith.tsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, { useCallback, useEffect, useReducer, useRef } from 'react';
import { ActivityIndicator, Image, StyleSheet, View } from 'react-native';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
import { BlueTextCentered } from '../BlueComponents';
import Button from '../components/Button';
import SafeArea from '../components/SafeArea';
import { BiometricType, unlockWithBiometrics, useBiometrics } from '../hooks/useBiometrics';
import loc from '../loc';
import { useStorage } from '../hooks/context/useStorage';
enum AuthType {
Encrypted,
Biometrics,
None,
BiometricsUnavailable,
}
type State = {
auth: {
type: AuthType;
detail: keyof typeof BiometricType | undefined;
};
isAuthenticating: boolean;
};
const SET_AUTH = 'SET_AUTH';
const SET_IS_AUTHENTICATING = 'SET_IS_AUTHENTICATING';
type Action =
| { type: typeof SET_AUTH; payload: { type: AuthType; detail: keyof typeof BiometricType | undefined } }
| { type: typeof SET_IS_AUTHENTICATING; payload: boolean };
const initialState: State = {
auth: {
type: AuthType.None,
detail: undefined,
},
isAuthenticating: false,
};
function reducer(state: State, action: Action): State {
switch (action.type) {
case SET_AUTH:
return { ...state, auth: action.payload };
case SET_IS_AUTHENTICATING:
return { ...state, isAuthenticating: action.payload };
default:
return state;
}
}
const UnlockWith: React.FC = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const isUnlockingWallets = useRef(false);
const { setWalletsInitialized, isStorageEncrypted, startAndDecrypt } = useStorage();
const { deviceBiometricType, isBiometricUseCapableAndEnabled, isBiometricUseEnabled } = useBiometrics();
useEffect(() => {
setWalletsInitialized(false);
}, [setWalletsInitialized]);
const successfullyAuthenticated = useCallback(() => {
setWalletsInitialized(true);
isUnlockingWallets.current = false;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const unlockUsingBiometrics = useCallback(async () => {
if (isUnlockingWallets.current || state.isAuthenticating) return;
isUnlockingWallets.current = true;
dispatch({ type: SET_IS_AUTHENTICATING, payload: true });
if (await unlockWithBiometrics()) {
await startAndDecrypt();
successfullyAuthenticated();
}
dispatch({ type: SET_IS_AUTHENTICATING, payload: false });
isUnlockingWallets.current = false;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.isAuthenticating]);
const unlockWithKey = useCallback(async () => {
if (isUnlockingWallets.current || state.isAuthenticating) return;
isUnlockingWallets.current = true;
dispatch({ type: SET_IS_AUTHENTICATING, payload: true });
if (await startAndDecrypt()) {
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
successfullyAuthenticated();
} else {
dispatch({ type: SET_IS_AUTHENTICATING, payload: false });
isUnlockingWallets.current = false;
}
}, [state.isAuthenticating, startAndDecrypt, successfullyAuthenticated]);
useEffect(() => {
const startUnlock = async () => {
const storageIsEncrypted = await isStorageEncrypted();
const biometricUseCapableAndEnabled = await isBiometricUseCapableAndEnabled();
const biometricsUseEnabled = await isBiometricUseEnabled();
const biometricType = biometricUseCapableAndEnabled ? deviceBiometricType : undefined;
if (storageIsEncrypted) {
dispatch({ type: SET_AUTH, payload: { type: AuthType.Encrypted, detail: undefined } });
unlockWithKey();
} else if (biometricUseCapableAndEnabled) {
dispatch({ type: SET_AUTH, payload: { type: AuthType.Biometrics, detail: biometricType } });
unlockUsingBiometrics();
} else if (biometricsUseEnabled && biometricType === undefined) {
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
dispatch({ type: SET_AUTH, payload: { type: AuthType.BiometricsUnavailable, detail: undefined } });
} else {
dispatch({ type: SET_AUTH, payload: { type: AuthType.None, detail: undefined } });
unlockWithKey();
}
};
startUnlock();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onUnlockPressed = () => {
if (state.auth.type === AuthType.Biometrics) {
unlockUsingBiometrics();
} else {
unlockWithKey();
}
};
const renderUnlockOptions = () => {
if (state.isAuthenticating) {
return <ActivityIndicator />;
} else {
switch (state.auth.type) {
case AuthType.Biometrics:
case AuthType.Encrypted:
return <Button onPress={onUnlockPressed} title={loc._.unlock} />;
case AuthType.BiometricsUnavailable:
return <BlueTextCentered>{loc.settings.biometrics_no_longer_available}</BlueTextCentered>;
default:
return null;
}
}
};
return (
<SafeArea style={styles.root}>
<View style={styles.container}>
<Image source={require('../img/icon.png')} style={styles.logoImage} resizeMode="contain" />
</View>
<View style={styles.biometricRow}>{renderUnlockOptions()}</View>
</SafeArea>
);
};
const styles = StyleSheet.create({
root: {
flex: 1,
justifyContent: 'space-between',
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
biometricRow: {
justifyContent: 'center',
flexDirection: 'row',
width: 300,
minHeight: 60,
alignSelf: 'center',
marginBottom: 20,
paddingHorizontal: 20,
},
logoImage: {
width: 100,
height: 75,
alignSelf: 'center',
},
});
export default UnlockWith;