-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.tsx
More file actions
341 lines (310 loc) Β· 16.1 KB
/
index.tsx
File metadata and controls
341 lines (310 loc) Β· 16.1 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { useState } from 'react';
import {
View,
Text,
ScrollView,
TouchableOpacity,
Alert,
Linking,
RefreshControl,
} from 'react-native';
import * as Clipboard from 'expo-clipboard';
import { LinearGradient } from 'expo-linear-gradient';
import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
import { useLazorkitWalletConnect } from '@/hooks/useLazorkitWalletConnect';
import { useBalances } from '@/hooks/useBalances';
import { getConnection, shortenAddress } from '@/lib/solana-utils';
import { useThemeStyles } from '@/hooks/useThemeStyles';
import { spacing, borderRadius, fontSize } from '@/lib/theme';
import { Footer } from '@/components/Footer';
import { Stack } from 'expo-router';
export default function ConnectWalletScreen() {
const theme = useThemeStyles();
const { colors } = theme;
const { wallet, isConnected, connect, disconnect, connecting } = useLazorkitWalletConnect();
const { solBalance, usdcBalance, loading, fetchBalances } = useBalances(
isConnected ? wallet?.smartWallet : null
);
const [airdropping, setAirdropping] = useState(false);
const handleConnect = () => {
connect('examples/01-connect-wallet');
};
const handleCopyAddress = async () => {
if (!wallet?.smartWallet) return;
await Clipboard.setStringAsync(wallet.smartWallet);
Alert.alert('Copied', 'Wallet address copied to clipboard');
};
const handleAirdrop = async () => {
if (!wallet?.smartWallet) return;
setAirdropping(true);
try {
const connection = getConnection();
const publicKey = new PublicKey(wallet.smartWallet);
const signature = await connection.requestAirdrop(publicKey, 1 * LAMPORTS_PER_SOL);
await connection.confirmTransaction(signature);
Alert.alert('Success', 'Airdrop successful! You received 1 SOL');
await fetchBalances();
} catch (error) {
console.error('Airdrop error:', error);
Alert.alert(
'Airdrop Failed',
'Devnet faucets have rate limits. Try faucet.solana.com directly.'
);
} finally {
setAirdropping(false);
}
};
const handleViewExplorer = () => {
if (!wallet?.smartWallet) return;
Linking.openURL(
`https://explorer.solana.com/address/${wallet.smartWallet}?cluster=devnet`
);
};
const handleOpenFaucet = (type: 'sol' | 'usdc') => {
const url = type === 'sol'
? 'https://faucet.solana.com/'
: 'https://faucet.circle.com/';
Linking.openURL(url);
};
return (
<>
<Stack.Screen options={{ title: 'Connect Wallet' }} />
<LinearGradient
colors={[colors.gradient.start, colors.gradient.middle, colors.gradient.end]}
style={theme.container}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
>
<ScrollView
style={theme.container}
contentContainerStyle={theme.scrollContent}
showsVerticalScrollIndicator={false}
refreshControl={
isConnected ? (
<RefreshControl
refreshing={loading}
onRefresh={fetchBalances}
tintColor={colors.accent.purple}
/>
) : undefined
}
>
{/* Header */}
<View style={{ marginBottom: spacing.lg }}>
<Text style={theme.emoji}>π</Text>
<Text style={theme.title}>Passkey Wallet Basics</Text>
<Text style={theme.subtitle}>
Create a wallet using passkey authentication with deep linking
</Text>
</View>
{!isConnected ? (
/* Not Connected State */
<View style={theme.section}>
<View style={[theme.card, { alignItems: 'center' }]}>
<Text style={{ fontSize: 64, marginBottom: spacing.lg }}>π</Text>
<Text style={[theme.sectionTitle, { textAlign: 'center' }]}>Create Your Wallet</Text>
<Text style={[theme.textMuted, { textAlign: 'center', marginBottom: spacing.lg }]}>
Tap the button below to create a wallet using Face ID or Touch ID.
No seed phrases needed!
</Text>
<TouchableOpacity
onPress={handleConnect}
disabled={connecting}
activeOpacity={0.8}
style={{ width: '100%' }}
>
<LinearGradient
colors={[colors.button.primary.start, colors.button.primary.end]}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
style={theme.btnPrimary}
>
<Text style={theme.btnPrimaryText}>
{connecting ? 'Creating Wallet...' : 'π Create Wallet with Passkey'}
</Text>
</LinearGradient>
</TouchableOpacity>
</View>
<View style={theme.cardWarning}>
<Text style={theme.textWarning}>
π‘ This will open the LazorKit portal for passkey authentication.
After signing, you'll be redirected back to this app.
</Text>
</View>
</View>
) : (
/* Connected State */
<View style={theme.section}>
{/* Wallet Info Card */}
<View style={theme.cardSuccess}>
<View style={[theme.row, { marginBottom: spacing.md }]}>
<View style={theme.statusDot} />
<Text style={theme.textSuccess}>Connected</Text>
</View>
<Text style={[theme.textMuted, { marginBottom: spacing.xs }]}>Wallet Address</Text>
<TouchableOpacity
onPress={handleCopyAddress}
style={[theme.input, theme.rowBetween, { marginBottom: spacing.lg }]}
>
<Text style={theme.mono}>
{shortenAddress(wallet?.smartWallet || '', 8)}
</Text>
<Text style={theme.textAccent}>Tap to copy</Text>
</TouchableOpacity>
{/* Balances */}
<View style={theme.balanceRow}>
<View style={{ alignItems: 'center' }}>
<Text style={theme.balanceLabel}>SOL</Text>
<Text style={theme.balanceValue}>
{solBalance !== null ? solBalance.toFixed(4) : '...'}
</Text>
</View>
<View style={{ alignItems: 'center' }}>
<Text style={theme.balanceLabel}>USDC</Text>
<Text style={theme.balanceValue}>
{usdcBalance !== null ? usdcBalance.toFixed(2) : '...'}
</Text>
</View>
</View>
</View>
{/* Actions */}
<View style={[theme.card, { gap: spacing.sm }]}>
<TouchableOpacity
onPress={handleAirdrop}
disabled={airdropping}
style={{ borderRadius: borderRadius.md, overflow: 'hidden' }}
>
<LinearGradient
colors={[colors.status.infoBg, 'rgba(59, 130, 246, 0.05)']}
style={[theme.btnOutline, { borderColor: colors.status.infoBorder }]}
>
<Text style={theme.btnOutlineText}>
{airdropping ? 'β³ Requesting...' : 'π§ Request 1 SOL Airdrop'}
</Text>
</LinearGradient>
</TouchableOpacity>
<TouchableOpacity
onPress={() => handleOpenFaucet('usdc')}
style={{ borderRadius: borderRadius.md, overflow: 'hidden' }}
>
<LinearGradient
colors={[colors.status.successBg, 'rgba(34, 197, 94, 0.05)']}
style={[theme.btnOutline, { borderColor: colors.status.successBorder }]}
>
<Text style={theme.btnOutlineText}>π΅ Get USDC (Circle Faucet)</Text>
</LinearGradient>
</TouchableOpacity>
<TouchableOpacity onPress={handleViewExplorer}>
<View style={theme.btnOutline}>
<Text style={theme.btnOutlineText}>π View on Explorer</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={disconnect}>
<View style={theme.btnDanger}>
<Text style={theme.btnDangerText}>π Disconnect</Text>
</View>
</TouchableOpacity>
</View>
{/* Info */}
<View style={theme.cardWarning}>
<Text style={theme.textWarning}>
π‘ Your wallet is secured by your device's biometrics.
No seed phrase needed!
</Text>
</View>
</View>
)}
{/* How It Works */}
<View style={[theme.card, { marginTop: spacing.lg }]}>
<Text style={theme.sectionTitle}>How It Works</Text>
<View style={theme.stepRow}>
<View style={theme.stepNumber}>
<Text style={theme.stepNumberText}>1</Text>
</View>
<View style={theme.stepContent}>
<Text style={theme.stepTitle}>Passkey Authentication</Text>
<Text style={theme.stepDescription}>
LazorKit uses WebAuthn (Face ID/Touch ID) to secure your wallet.
Your private keys never leave your device.
</Text>
</View>
</View>
<View style={theme.stepRow}>
<View style={theme.stepNumber}>
<Text style={theme.stepNumberText}>2</Text>
</View>
<View style={theme.stepContent}>
<Text style={theme.stepTitle}>Deep Link Redirect</Text>
<Text style={theme.stepDescription}>
On mobile, authentication happens via the LazorKit portal.
After signing, you're redirected back to the app.
</Text>
</View>
</View>
<View style={[theme.stepRow, { marginBottom: 0 }]}>
<View style={theme.stepNumber}>
<Text style={theme.stepNumberText}>3</Text>
</View>
<View style={theme.stepContent}>
<Text style={theme.stepTitle}>Smart Wallet</Text>
<Text style={theme.stepDescription}>
A smart wallet address is created on Solana that can receive
tokens and interact with any program.
</Text>
</View>
</View>
</View>
{/* Code Example */}
<View style={[theme.codeCard, { marginTop: spacing.md }]}>
<Text style={[theme.sectionTitle, { padding: spacing.md, paddingBottom: 0 }]}>Code Example</Text>
<View style={theme.codeHighlight}>
<Text style={theme.codeHighlightText}>
π± Mobile: Uses deep linking for redirect
</Text>
</View>
<View style={theme.codeBlock}>
<Text style={theme.codeText}>
{`import { useWallet } from '@lazorkit/wallet-mobile-adapter';
import * as Linking from 'expo-linking';
const { wallet, isConnected, connect } = useWallet();
// Connect with deep link redirect
await connect({
redirectUrl: Linking.createURL('your/return/path'),
onSuccess: (connectedWallet) => {
console.log('Connected:', connectedWallet.smartWallet);
},
onFail: (error) => {
console.error('Failed:', error.message);
},
});
// Access wallet address
if (isConnected && wallet) {
console.log(wallet.smartWallet);
}`}
</Text>
</View>
<View style={theme.codeNote}>
<Text style={theme.codeNoteText}>
π‘ This cookbook includes a WalletContext wrapper that simplifies
state management across screens.{'\n\n'}
π See:{' '}
<Text
style={theme.link}
onPress={() =>
Linking.openURL(
'https://github.com/0xharp/lazorkit-cookbook/blob/main/docs/mobile/03-cookbook-patterns.md'
)
}
>
docs/mobile/03-cookbook-patterns.md
</Text>
</Text>
</View>
</View>
</ScrollView>
{/* Footer */}
<Footer />
</LinearGradient>
</>
);
}