-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathApp.tsx
More file actions
301 lines (269 loc) · 8.56 KB
/
Copy pathApp.tsx
File metadata and controls
301 lines (269 loc) · 8.56 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
import "@ethersproject/shims";
// IMP START - Quick Start
import * as WebBrowser from "@toruslabs/react-native-web-browser";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import Web3Auth, { AUTH_CONNECTION, CHAIN_NAMESPACES, WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk";
import { ethers } from "ethers";
import React, { useEffect, useState } from "react";
import { Button, Dimensions, ScrollView, StyleSheet, Switch, Text, TextInput, View } from "react-native";
import EncryptedStorage from "react-native-encrypted-storage";
// IMP END - Quick Start
const scheme = "web3authrnbareaggregateexample"; // Or your desired app redirection scheme
// IMP START - Whitelist bundle ID
const redirectUrl = `${scheme}://auth`;
// IMP END - Whitelist bundle ID
// IMP START - Dashboard Registration
const clientId = "BHgArYmWwSeq21czpcarYh0EVq2WWOzflX-NTK-tY1-1pauPzHKRRLgpABkmYiIV_og9jAvoIxQ8L3Smrwe04Lw";
// IMP END - Dashboard Registration
// IMP START - SDK Initialization
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xaa36a7",
rpcTarget: `https://api.web3auth.io/infura-service/v1/0xaa36a7/${clientId}`,
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
decimals: 18,
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig,
},
});
export default function App() {
const [web3auth, setWeb3auth] = useState<Web3Auth | null>(null);
const [loggedIn, setLoggedIn] = useState(false);
const [provider, setProvider] = useState<any>(null);
const [console, setConsole] = useState<string>("");
useEffect(() => {
const init = async () => {
try {
// EncryptedStorage.clear();
const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, {
clientId,
// IMP START - Whitelist bundle ID
redirectUrl,
// IMP END - Whitelist bundle ID
network: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET, // or other networks
privateKeyProvider,
enableLogging: true, // Enable debug logging
});
setWeb3auth(web3auth);
// IMP START - SDK Initialization
await web3auth.init();
if (web3auth.connected) {
// IMP END - SDK Initialization
setProvider(web3auth.provider);
setLoggedIn(true);
}
} catch (error: any) {
setConsole(`Init error: ${error.message}`);
}
};
init();
}, []);
const loginWithGoogle = async () => {
try {
if (!web3auth?.ready) {
setConsole("Web3auth not initialized");
return;
}
setConsole("Logging in with Google");
await web3auth.connectTo({
authConnection: AUTH_CONNECTION.GOOGLE,
authConnectionId: "w3a-google",
groupedAuthConnectionId: "aggregate-sapphire",
});
if (web3auth.connected) {
setProvider(web3auth.provider);
uiConsole("Logged In");
setLoggedIn(true);
}
} catch (e: any) {
setConsole(e.message);
}
};
const loginWithAuth0EmailPasswordless = async () => {
try {
if (!web3auth?.ready) {
setConsole("Web3auth not initialized");
return;
}
setConsole("Logging in with Auth0 Email Passwordless");
await web3auth.connectTo({
authConnection: AUTH_CONNECTION.CUSTOM,
authConnectionId: "w3a-a0-email-passwordless",
groupedAuthConnectionId: "aggregate-sapphire",
});
if (web3auth.connected) {
setProvider(web3auth.provider);
uiConsole("Logged In");
setLoggedIn(true);
}
} catch (e: any) {
setConsole(e.message);
}
};
const loginWithAuth0GitHub = async () => {
try {
if (!web3auth?.ready) {
setConsole("Web3auth not initialized");
return;
}
setConsole("Logging in with Auth0 GitHub");
await web3auth.connectTo({
authConnection: AUTH_CONNECTION.CUSTOM,
authConnectionId: "w3a-a0-github",
groupedAuthConnectionId: "aggregate-sapphire",
});
if (web3auth.connected) {
setProvider(web3auth.provider);
uiConsole("Logged In");
setLoggedIn(true);
}
} catch (e: any) {
setConsole(e.message);
}
};
const logout = async () => {
if (!web3auth?.ready) {
setConsole("Web3auth not initialized");
return;
}
setConsole("Logging out");
await web3auth.logout();
if (!web3auth.connected) {
setProvider(null);
uiConsole("Logged out");
setLoggedIn(false);
}
};
// IMP START - Blockchain Calls
const getAccounts = async (): Promise<string> => {
if (!provider) {
uiConsole("provider not set");
return "";
}
setConsole("Getting account");
const ethersProvider = new ethers.BrowserProvider(provider!);
const signer = await ethersProvider.getSigner();
const address = signer.getAddress();
uiConsole(address);
return address;
};
const getBalance = async () => {
if (!provider) {
uiConsole("provider not set");
return;
}
setConsole("Fetching balance");
const ethersProvider = new ethers.BrowserProvider(provider!);
const signer = await ethersProvider.getSigner();
const address = signer.getAddress();
const balance = ethers.formatEther(await ethersProvider.getBalance(address));
uiConsole(balance);
};
const signMessage = async () => {
if (!provider) {
uiConsole("provider not set");
return;
}
setConsole("Signing message");
const ethersProvider = new ethers.BrowserProvider(provider!);
const signer = await ethersProvider.getSigner();
const originalMessage = "YOUR_MESSAGE";
const signedMessage = await signer.signMessage(originalMessage);
uiConsole(signedMessage);
};
// IMP END - Blockchain Calls
const launchWalletServices = async () => {
if (!web3auth) {
setConsole("Web3auth not initialized");
return;
}
setConsole("Launch Wallet Services");
await web3auth.launchWalletServices();
};
const requestSignature = async () => {
if (!web3auth) {
setConsole("Web3auth not initialized");
return;
}
try {
const address: string = await getAccounts();
const params = ["Hello World", address];
const res = await web3auth.request("personal_sign", params);
uiConsole(res);
} catch (error) {
uiConsole("Error in requestSignature:", error);
}
};
const uiConsole = (...args: unknown[]) => {
setConsole(`${JSON.stringify(args || {}, null, 2)}\n\n\n\n${console}`);
};
const loggedInView = (
<View style={styles.buttonArea}>
<Button title="Get User Info" onPress={() => uiConsole(web3auth?.userInfo())} />
<Button title="Get Accounts" onPress={() => getAccounts()} />
<Button title="Get Balance" onPress={() => getBalance()} />
<Button title="Sign Message" onPress={() => signMessage()} />
<Button title="Show Wallet UI" onPress={() => launchWalletServices()} />
<Button title="Request Signature UI" onPress={() => requestSignature()} />
<Button title="Log Out" onPress={() => logout()} />
</View>
);
const unloggedInView = (
<View style={styles.buttonArea}>
<Button title="Login with Google" onPress={() => loginWithGoogle()} />
<Button title="Login with Auth0 Email Passwordless" onPress={() => loginWithAuth0EmailPasswordless()} />
<Button title="Login with Auth0 GitHub" onPress={() => loginWithAuth0GitHub()} />
</View>
);
return (
<View style={styles.container}>
{loggedIn ? loggedInView : unloggedInView}
<View style={styles.consoleArea}>
<Text style={styles.consoleText}>Console:</Text>
<ScrollView style={styles.console}>
<Text>{console}</Text>
</ScrollView>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
paddingTop: 50,
paddingBottom: 30,
},
consoleArea: {
margin: 20,
alignItems: "center",
justifyContent: "center",
flex: 1,
},
console: {
flex: 1,
backgroundColor: "#CCCCCC",
color: "#ffffff",
padding: 10,
width: Dimensions.get("window").width - 60,
},
consoleText: {
padding: 10,
},
buttonArea: {
flex: 2,
alignItems: "center",
justifyContent: "space-around",
paddingBottom: 30,
},
});