-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAccount.tsx
195 lines (179 loc) · 5.71 KB
/
CreateAccount.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
184
185
186
187
188
189
190
191
192
193
194
195
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet, Alert, TouchableOpacity } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
const CreateAccount: React.FC = () => {
const [keypairs, setKeypairs] = useState<{ publicKey: string; secret: string }[]>([]);
const [selectedAccountIndex, setSelectedAccountIndex] = useState<number | null>(null);
const [fundingMessage, setFundingMessage] = useState<string>('');
const storeKeys = async (newKeypair: { publicKey: string; secret: string }) => {
try {
const existingKeypairs = JSON.parse(await AsyncStorage.getItem('keypairs') || '[]');
existingKeypairs.push(newKeypair);
await AsyncStorage.setItem('keypairs', JSON.stringify(existingKeypairs));
setKeypairs(existingKeypairs);
setSelectedAccountIndex(existingKeypairs.length - 1);
} catch (error) {
console.error('Error storing keys:', error);
}
};
const retrieveKeys = async () => {
try {
const existingKeypairs = JSON.parse(await AsyncStorage.getItem('keypairs') || '[]');
setKeypairs(existingKeypairs);
if (existingKeypairs.length > 0) {
setSelectedAccountIndex(0);
}
} catch (error) {
console.error('Error retrieving keys:', error);
}
};
useEffect(() => {
retrieveKeys();
}, []);
const generateKeypair = async () => {
try {
const response = await fetch('http://10.0.2.2:3001/create-keypair', { method: 'POST' });
const data = await response.json();
await storeKeys({ publicKey: data.publicKey, secret: data.secret });
} catch (error) {
console.error('Error generating keypair:', error);
}
};
const fundAccount = async () => {
if (selectedAccountIndex === null) return;
const publicKeyToFund = keypairs[selectedAccountIndex]?.publicKey; // Use optional chaining
try {
const response = await fetch('http://10.0.2.2:3001/fund-account', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ publicKey: publicKeyToFund }),
});
const data = await response.json();
setFundingMessage(data.message);
} catch (error) {
console.error('Error funding account:', error);
setFundingMessage('Error funding account.');
}
};
const switchAccount = (index: number) => {
setSelectedAccountIndex(index);
};
const removeAccount = async (index: number) => {
Alert.alert(
'Remove Account',
'Are you sure you want to remove this account?',
[
{ text: 'Cancel', style: 'cancel' },
{
text: 'OK',
onPress: async () => {
const updatedKeypairs = keypairs.filter((_, i) => i !== index);
await AsyncStorage.setItem('keypairs', JSON.stringify(updatedKeypairs));
setKeypairs(updatedKeypairs);
if (selectedAccountIndex === index) {
setSelectedAccountIndex(updatedKeypairs.length > 0 ? Math.min(0, updatedKeypairs.length - 1) : null);
}
},
},
],
{ cancelable: false }
);
};
return (
<View style={styles.container}>
<Text style={styles.heading}>Create Account</Text>
{selectedAccountIndex !== null && keypairs[selectedAccountIndex] && (
<View style={styles.keysContainer}>
<Text style={styles.keyText}>Public Key: {keypairs[selectedAccountIndex].publicKey}</Text>
<Text style={styles.keyText}>Secret Key: {keypairs[selectedAccountIndex].secret}</Text>
<TouchableOpacity style={styles.button} onPress={fundAccount}>
<Text style={styles.buttonText}>Fund Account</Text>
</TouchableOpacity>
</View>
)}
<TouchableOpacity style={styles.button} onPress={generateKeypair}>
<Text style={styles.buttonText}>Generate Keypair</Text>
</TouchableOpacity>
<Text style={styles.messageText}>{fundingMessage}</Text>
{keypairs.map((_, index) => (
<View key={index} style={styles.accountContainer}>
<TouchableOpacity
style={styles.switchButton}
onPress={() => switchAccount(index)}
>
<Text style={styles.buttonText}>Switch to Account {index + 1}</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.removeButton}
onPress={() => removeAccount(index)}
>
<Text style={styles.buttonText}>Remove</Text>
</TouchableOpacity>
</View>
))}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#0d0d0d',
padding: 20,
justifyContent: 'center',
},
heading: {
fontSize: 28,
color: '#00FFCC',
marginBottom: 20,
textAlign: 'center',
},
keysContainer: {
backgroundColor: '#1a1a1a',
borderRadius: 10,
padding: 15,
marginBottom: 20,
},
keyText: {
fontSize: 16,
color: '#FFFFFF',
marginBottom: 5,
},
button: {
backgroundColor: '#00FFCC',
padding: 10,
borderRadius: 8,
marginVertical: 10,
alignItems: 'center',
},
switchButton: {
flex: 1,
backgroundColor: '#005f5f',
padding: 10,
borderRadius: 8,
marginRight: 10,
justifyContent: 'center',
alignItems: 'center',
},
removeButton: {
backgroundColor: '#ff3b3b',
padding: 10,
borderRadius: 8,
justifyContent: 'center',
alignItems: 'center',
},
buttonText: {
color: '#000000',
fontWeight: 'bold',
},
messageText: {
color: '#FFFFFF',
marginBottom: 20,
textAlign: 'center',
},
accountContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 10,
},
});
export default CreateAccount;