-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
298 lines (266 loc) · 9.05 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import "./shim";
import soroban from './src/libs/soroban'
import { StatusBar } from 'expo-status-bar';
import { ActivityIndicator, Animated, Dimensions, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { NavigationContainer } from '@react-navigation/native'
import HomeScreen from "./src/screens/Home";
import AccountScreen from "./src/screens/Account";
import { useEffect, useState } from "react";
import Storage from "./src/libs/storage"
// import AuthScreen from "./src/screens/Auth";
import { AuthStyle, GlobalStyles, ModalStyle, CardStyle } from './src/styles/global'
import axios from "axios";
import { LinearGradient } from "expo-linear-gradient";
import { AntDesign, Ionicons } from '@expo/vector-icons';
import Biometrics from './src/libs/biometrics'
export default function App() {
const [biometrics, setBiometrics] = useState()
const [account, setAccount] = useState()
const [modalStatus, setModalStatus] = useState()
const [isAuthenticated, setAuthtenticated] = useState(false)
const [error, setError] = useState(false)
const [loading, setLoading] = useState(true)
const [triggerTopUpdate, setTriggerTopUpdate] = useState()
const Stack = createNativeStackNavigator();
const AuthNavigator = () => {
const [accountKey, setAccountKey] = useState()
return (
<LinearGradient
colors={ error ? ['#fc3d39', '#d92f2b', '#fc3158'] : ['#7932F4', '#5924F1', '#6410F5'] }
style={{width: '100%', flex: 1, justifyContent: 'center'}}
end={{ x: 0.1, y: 0.9 }}>
<View style={{paddingTop: 100, padding: 20}}>
<Text style={AuthStyle.headerText}>StashApp.</Text>
<Text style={AuthStyle.subText}>FUTURENET - SOROBAN</Text>
<View style={{flexDirection: 'row', justifyContent: 'center'}}>
<View style={[AuthStyle.button, {marginBottom: 20}]}>
<TextInput value={accountKey} onChangeText={setAccountKey} placeholder={"Keyphrase"} secureTextEntry={true} style={{width: '100%', fontSize: 18}} />
</View>
<TouchableOpacity style={{marginLeft: 20, marginTop: 10}} onPress={() => {importAccount(accountKey)}}>
<View style={CardStyle.button}>
<AntDesign name="arrowright" size={24} color="#fff" />
</View>
</TouchableOpacity>
</View>
<View style={{height: 20}}>
<Text style={[AuthStyle.btnText, {color: '#fff'}]}>{error && error}</Text>
</View>
</View>
<View style={{padding: 20, marginTop: 140, justifyContent: 'flex-end', flex: 0.4}}>
<TouchableOpacity onPress={() => {setModalStatus("create")}}>
<View style={AuthStyle.button}>
<Text style={AuthStyle.btnText}>Create account</Text>
<AntDesign name="arrowright" size={24} color="#000" />
</View>
</TouchableOpacity>
</View>
</LinearGradient>
)
}
const AuthScreen = (props) => {
const [account, setAccount] = useState()
useEffect(() => {
}, [])
const authenticate = async () => {
let auth = await Biometrics.authenticate()
if (auth.error) {
setAuthtenticated(false)
return
}
setAuthtenticated(true)
}
return (
<LinearGradient
colors={ error ? ['#fc3d39', '#d92f2b', '#fc3158'] : ['#7932F4', '#5924F1', '#6410F5'] }
style={{width: '100%', flex: 1, paddingTop: 100, paddingBottom: 120, justifyContent: 'space-between', alignItems: 'center'}}
end={{ x: 0.1, y: 0.9 }}>
<View style={{flex: 0.5, justifyContent: 'flex-end'}}>
<Text style={[GlobalStyles.headerText, {color: "#fff"}]}>Please authenticate</Text>
</View>
<View style={{flex: 0.5, paddingTop: 40, justifyContent: 'flex-start'}}>
<TouchableOpacity onPress={() => {authenticate()}}>
<Ionicons name="finger-print" size={60} color="#fff" />
</TouchableOpacity>
</View>
</LinearGradient>
)
}
const TabNavigator = () => {
const Tab = createBottomTabNavigator()
const updateAccountState = (state) => {
setAccount(state)
}
return (
<Tab.Navigator screenOptions={{ headerShown: false }}>
<Tab.Screen name="Home" initialParams={{account: account}} component={HomeScreen} />
<Tab.Screen name="Account" initialParams={{account: account, callback: updateAccountState}} component={AccountScreen} />
</Tab.Navigator>
);
}
const Loading = () => {
return (
<LinearGradient
colors={ error ? ['#fc3d39', '#d92f2b', '#fc3158'] : ['#7932F4', '#5924F1', '#6410F5'] }
style={{width: '100%', alignItems: 'center', flexDirection: 'column', flex: 1, justifyContent: 'center'}}
end={{ x: 0.1, y: 0.9 }}>
<Text style={[GlobalStyles.headerText, {marginBottom: 40, color: "#fff"}]}>StashApp</Text>
<ActivityIndicator
textContent={"Loading..."} />
</LinearGradient>
)
}
const createAccount = async () => {
let keypair = soroban.generateRandomKeypair()
/**
* TODO: Add encryption into the library
*/
try {
await axios.get(`https://friendbot-futurenet.stellar.org/?addr=${keypair.publicKey()}`)
await Storage.save("account", {secretKey: keypair.secret(), publicKey: keypair.publicKey()})
setAccount(keypair)
setModalStatus(false)
} catch (error) {
console.log(error)
}
}
const importAccount = async (accountKey) => {
setError(false)
if (!/^S[0-9A-Z]{55}$/.test(accountKey)) {
setError("Invalid secret")
return
}
setModalStatus("import")
setTimeout(async() => {
let keypair = await soroban.getKeysFromSecret(accountKey)
/**
* TODO: Add encryption into the library
*/
await Storage.save("account", {secretKey: keypair.secret(), publicKey: keypair.publicKey()})
setAccount(keypair)
setModalStatus(false)
}, 5000)
}
useEffect(() => {
setLoading(true)
Storage.read("account")
.then(async account => {
if (account == null || !account.secretKey) {
setLoading(false)
return setAccount(false)
}
setAccount(account)
/**
* Check to see if biometrics is enabled
* if == true: present auth screen
*/
let security = await Storage.read("security")
if (security == null) {
setBiometrics(true)
setAuthtenticated(true)
setLoading(false)
return
}
setBiometrics(true)
setLoading(false)
})
.catch(error => {
console.log(error)
setLoading(false)
setAccount(false)
})
}, [])
const Modal = ({status, secret}) => {
const windowHeight = Dimensions.get('window').height
const animated = new Animated.Value(windowHeight)
useEffect(() => {
if (!status) {
Animated.spring(
animated,
{
toValue: windowHeight,
velocity: 3,
tension: 2,
friction: 8,
duration: 500,
useNativeDriver: false
}
).start()
return
}
Animated.spring(
animated,
{
toValue: 0,
velocity: 3,
tension: 2,
friction: 8,
duration: 500,
useNativeDriver: false
}
).start()
setTimeout(() => {
if (status == "import") {
return
}
createAccount()
}, 3000)
}, [status])
const close = () => {
Animated.spring(
animated,
{
toValue: windowHeight,
velocity: 3,
tension: 2,
friction: 8,
duration: 500,
useNativeDriver: false
}
).start()
}
if (!status) {
return (
<></>
)
}
return (
<Animated.View style={[ModalStyle.backdrop, {transform: [{translateY: animated}]}]}>
<View style={ModalStyle.container}>
<View style={ModalStyle.top}>
<TouchableOpacity onPress={close}>
<AntDesign name="close" size={30} color="#000" />
</TouchableOpacity>
</View>
<ActivityIndicator />
<Text style={{marginTop: 20, fontWeight: '600'}}>{status == "import" ? "Importing account" : "Creating account"}</Text>
</View>
</Animated.View>
)
}
return (
<NavigationContainer>
{!loading ?
(biometrics && isAuthenticated) ?
!account ?
<AuthNavigator />
:
<TabNavigator />
:
<AuthScreen />
:
<Loading />
}
<Modal status={modalStatus} secret={"123"} />
</NavigationContainer>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
})