-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
85 lines (76 loc) · 2.61 KB
/
App.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
import React, { useReducer, useMemo, useEffect} from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import AsyncStorage from '@react-native-async-storage/async-storage';
import web3 from "./src/utils/Web3";
import { AuthContext } from './src/utils/AuthContext'
import Welcome from './src/containers/Welcome'
import Root from './src/navigation/Root'
const AuthStack = createNativeStackNavigator();
export default function App() {
const [state, dispatch] = useReducer(
(prevState, action) => {
switch (action.type) {
case 'CHECK_WALLET':
return {account: action.key};
case 'CREATE_WALLET':
return {account: action.key};
case 'RESTORE_WALLET':
return {account: action.key };
case 'DELETE_WALLET':
return {account: null };
}
},
{
account: null,
}
);
useEffect(() => {
// Fetch the token from storage then navigate to our appropriate place
const bootstrapAsync = async () => {
let account;
try {
account = await AsyncStorage.getItem('account');
account = JSON.parse(account);
} catch (e) {
console.log(e)
}
if (account !== undefined && account !== null) {
const restored_account = web3.eth.accounts.privateKeyToAccount(account.privateKey);
await dispatch({ type: 'CHECK_WALLET', key: account });
}
};
bootstrapAsync();
}, []);
const authOperation = useMemo(
() => ({
createWallet: async () => {
const account = await web3.eth.accounts.create();
await AsyncStorage.setItem('account', JSON.stringify(account));
await dispatch({ type: 'CREATE_WALLET', key: account })
},
restoreWallet: async (key) => {
const account = web3.eth.accounts.privateKeyToAccount(key);
await AsyncStorage.setItem('account', JSON.stringify(account));
await dispatch({ type: 'RESTORE_WALLET', key: account })
},
deleteWallet: async () => {
await dispatch({ type: 'DELETE_WALLET' })
},
}),
[]
);
return (
<AuthContext.Provider value={{authOperation, state}}>
<NavigationContainer>
<AuthStack.Navigator>
{state.account == null ? (
<AuthStack.Screen name="WelcomeScreen" component={Welcome} options={{headerShown: false}}/>
) : (
<AuthStack.Screen name="Root" component={Root} options={{headerShown: false}} />
)}
</AuthStack.Navigator>
</NavigationContainer>
</AuthContext.Provider>
);
}