|
| 1 | +import "@ethersproject/shims"; |
| 2 | + |
| 3 | +// IMP START - Quick Start |
| 4 | +import * as WebBrowser from "@toruslabs/react-native-web-browser"; |
| 5 | +import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider"; |
| 6 | +import Web3Auth, { AUTH_CONNECTION, ChainNamespace, WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk"; |
| 7 | +import { ethers } from "ethers"; |
| 8 | +import React, { useEffect, useState } from "react"; |
| 9 | +import { Button, Dimensions, ScrollView, StyleSheet, Switch, Text, TextInput, View } from "react-native"; |
| 10 | +import EncryptedStorage from "react-native-encrypted-storage"; |
| 11 | +// IMP END - Quick Start |
| 12 | + |
| 13 | +const scheme = "web3authrnbareaggregateexample"; // Or your desired app redirection scheme |
| 14 | +// IMP START - Whitelist bundle ID |
| 15 | +const redirectUrl = `${scheme}://auth`; |
| 16 | +// IMP END - Whitelist bundle ID |
| 17 | + |
| 18 | +// IMP START - Dashboard Registration |
| 19 | +const clientId = "BHgArYmWwSeq21czpcarYh0EVq2WWOzflX-NTK-tY1-1pauPzHKRRLgpABkmYiIV_og9jAvoIxQ8L3Smrwe04Lw"; |
| 20 | +// IMP END - Dashboard Registration |
| 21 | + |
| 22 | +// IMP START - SDK Initialization |
| 23 | +const chainConfig = { |
| 24 | + chainNamespace: ChainNamespace.EIP155, |
| 25 | + chainId: "0xaa36a7", |
| 26 | + rpcTarget: "https://1rpc.io/sepolia", |
| 27 | + // Avoid using public rpcTarget in production. |
| 28 | + // Use services like Infura, Quicknode etc |
| 29 | + displayName: "Ethereum Sepolia Testnet", |
| 30 | + blockExplorerUrl: "https://sepolia.etherscan.io", |
| 31 | + ticker: "ETH", |
| 32 | + tickerName: "Ethereum", |
| 33 | + decimals: 18, |
| 34 | + logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png", |
| 35 | +}; |
| 36 | + |
| 37 | +const privateKeyProvider = new EthereumPrivateKeyProvider({ |
| 38 | + config: { |
| 39 | + chainConfig, |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +export default function App() { |
| 44 | + const [web3auth, setWeb3auth] = useState<Web3Auth | null>(null); |
| 45 | + const [loggedIn, setLoggedIn] = useState(false); |
| 46 | + const [provider, setProvider] = useState<any>(null); |
| 47 | + const [console, setConsole] = useState<string>(""); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const init = async () => { |
| 51 | + try { |
| 52 | + // EncryptedStorage.clear(); |
| 53 | + const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, { |
| 54 | + clientId, |
| 55 | + // IMP START - Whitelist bundle ID |
| 56 | + redirectUrl, |
| 57 | + // IMP END - Whitelist bundle ID |
| 58 | + network: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET, // or other networks |
| 59 | + privateKeyProvider, |
| 60 | + enableLogging: true, // Enable debug logging |
| 61 | + }); |
| 62 | + setWeb3auth(web3auth); |
| 63 | + |
| 64 | + // IMP START - SDK Initialization |
| 65 | + await web3auth.init(); |
| 66 | + |
| 67 | + if (web3auth.connected) { |
| 68 | + // IMP END - SDK Initialization |
| 69 | + setProvider(web3auth.provider); |
| 70 | + setLoggedIn(true); |
| 71 | + } |
| 72 | + } catch (error: any) { |
| 73 | + setConsole(`Init error: ${error.message}`); |
| 74 | + } |
| 75 | + }; |
| 76 | + init(); |
| 77 | + }, []); |
| 78 | + |
| 79 | + const loginWithGoogle = async () => { |
| 80 | + try { |
| 81 | + if (!web3auth?.ready) { |
| 82 | + setConsole("Web3auth not initialized"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + setConsole("Logging in with Google"); |
| 87 | + await web3auth.connectTo({ |
| 88 | + authConnection: AUTH_CONNECTION.GOOGLE, |
| 89 | + authConnectionId: "w3a-google", |
| 90 | + groupedAuthConnectionId: "aggregate-sapphire", |
| 91 | + }); |
| 92 | + |
| 93 | + if (web3auth.connected) { |
| 94 | + setProvider(web3auth.provider); |
| 95 | + uiConsole("Logged In"); |
| 96 | + setLoggedIn(true); |
| 97 | + } |
| 98 | + } catch (e: any) { |
| 99 | + setConsole(e.message); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + const loginWithAuth0EmailPasswordless = async () => { |
| 104 | + try { |
| 105 | + if (!web3auth?.ready) { |
| 106 | + setConsole("Web3auth not initialized"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + setConsole("Logging in with Auth0 Email Passwordless"); |
| 111 | + await web3auth.connectTo({ |
| 112 | + authConnection: AUTH_CONNECTION.CUSTOM, |
| 113 | + authConnectionId: "w3a-a0-email-passwordless", |
| 114 | + groupedAuthConnectionId: "aggregate-sapphire", |
| 115 | + }); |
| 116 | + |
| 117 | + if (web3auth.connected) { |
| 118 | + setProvider(web3auth.provider); |
| 119 | + uiConsole("Logged In"); |
| 120 | + setLoggedIn(true); |
| 121 | + } |
| 122 | + } catch (e: any) { |
| 123 | + setConsole(e.message); |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + const loginWithAuth0GitHub = async () => { |
| 128 | + try { |
| 129 | + if (!web3auth?.ready) { |
| 130 | + setConsole("Web3auth not initialized"); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + setConsole("Logging in with Auth0 GitHub"); |
| 135 | + await web3auth.connectTo({ |
| 136 | + authConnection: AUTH_CONNECTION.CUSTOM, |
| 137 | + authConnectionId: "w3a-a0-github", |
| 138 | + groupedAuthConnectionId: "aggregate-sapphire", |
| 139 | + }); |
| 140 | + |
| 141 | + if (web3auth.connected) { |
| 142 | + setProvider(web3auth.provider); |
| 143 | + uiConsole("Logged In"); |
| 144 | + setLoggedIn(true); |
| 145 | + } |
| 146 | + } catch (e: any) { |
| 147 | + setConsole(e.message); |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + const logout = async () => { |
| 152 | + if (!web3auth?.ready) { |
| 153 | + setConsole("Web3auth not initialized"); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + setConsole("Logging out"); |
| 158 | + await web3auth.logout(); |
| 159 | + |
| 160 | + if (!web3auth.connected) { |
| 161 | + setProvider(null); |
| 162 | + uiConsole("Logged out"); |
| 163 | + setLoggedIn(false); |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + // IMP START - Blockchain Calls |
| 168 | + const getAccounts = async (): Promise<string> => { |
| 169 | + if (!provider) { |
| 170 | + uiConsole("provider not set"); |
| 171 | + return ""; |
| 172 | + } |
| 173 | + setConsole("Getting account"); |
| 174 | + const ethersProvider = new ethers.BrowserProvider(provider!); |
| 175 | + const signer = await ethersProvider.getSigner(); |
| 176 | + const address = signer.getAddress(); |
| 177 | + uiConsole(address); |
| 178 | + return address; |
| 179 | + }; |
| 180 | + |
| 181 | + const getBalance = async () => { |
| 182 | + if (!provider) { |
| 183 | + uiConsole("provider not set"); |
| 184 | + return; |
| 185 | + } |
| 186 | + setConsole("Fetching balance"); |
| 187 | + const ethersProvider = new ethers.BrowserProvider(provider!); |
| 188 | + const signer = await ethersProvider.getSigner(); |
| 189 | + const address = signer.getAddress(); |
| 190 | + const balance = ethers.formatEther(await ethersProvider.getBalance(address)); |
| 191 | + uiConsole(balance); |
| 192 | + }; |
| 193 | + |
| 194 | + const signMessage = async () => { |
| 195 | + if (!provider) { |
| 196 | + uiConsole("provider not set"); |
| 197 | + return; |
| 198 | + } |
| 199 | + setConsole("Signing message"); |
| 200 | + const ethersProvider = new ethers.BrowserProvider(provider!); |
| 201 | + const signer = await ethersProvider.getSigner(); |
| 202 | + const originalMessage = "YOUR_MESSAGE"; |
| 203 | + const signedMessage = await signer.signMessage(originalMessage); |
| 204 | + uiConsole(signedMessage); |
| 205 | + }; |
| 206 | + // IMP END - Blockchain Calls |
| 207 | + |
| 208 | + const launchWalletServices = async () => { |
| 209 | + if (!web3auth) { |
| 210 | + setConsole("Web3auth not initialized"); |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + setConsole("Launch Wallet Services"); |
| 215 | + await web3auth.launchWalletServices(); |
| 216 | + }; |
| 217 | + |
| 218 | + const requestSignature = async () => { |
| 219 | + if (!web3auth) { |
| 220 | + setConsole("Web3auth not initialized"); |
| 221 | + return; |
| 222 | + } |
| 223 | + try { |
| 224 | + const address: string = await getAccounts(); |
| 225 | + const params = ["Hello World", address]; |
| 226 | + const res = await web3auth.request("personal_sign", params); |
| 227 | + uiConsole(res); |
| 228 | + } catch (error) { |
| 229 | + uiConsole("Error in requestSignature:", error); |
| 230 | + } |
| 231 | + }; |
| 232 | + |
| 233 | + const uiConsole = (...args: unknown[]) => { |
| 234 | + setConsole(`${JSON.stringify(args || {}, null, 2)}\n\n\n\n${console}`); |
| 235 | + }; |
| 236 | + |
| 237 | + const loggedInView = ( |
| 238 | + <View style={styles.buttonArea}> |
| 239 | + <Button title="Get User Info" onPress={() => uiConsole(web3auth?.userInfo())} /> |
| 240 | + <Button title="Get Accounts" onPress={() => getAccounts()} /> |
| 241 | + <Button title="Get Balance" onPress={() => getBalance()} /> |
| 242 | + <Button title="Sign Message" onPress={() => signMessage()} /> |
| 243 | + <Button title="Show Wallet UI" onPress={() => launchWalletServices()} /> |
| 244 | + <Button title="Request Signature UI" onPress={() => requestSignature()} /> |
| 245 | + <Button title="Log Out" onPress={() => logout()} /> |
| 246 | + </View> |
| 247 | + ); |
| 248 | + |
| 249 | + const unloggedInView = ( |
| 250 | + <View style={styles.buttonArea}> |
| 251 | + <Button title="Login with Google" onPress={() => loginWithGoogle()} /> |
| 252 | + <Button title="Login with Auth0 Email Passwordless" onPress={() => loginWithAuth0EmailPasswordless()} /> |
| 253 | + <Button title="Login with Auth0 GitHub" onPress={() => loginWithAuth0GitHub()} /> |
| 254 | + </View> |
| 255 | + ); |
| 256 | + |
| 257 | + return ( |
| 258 | + <View style={styles.container}> |
| 259 | + {loggedIn ? loggedInView : unloggedInView} |
| 260 | + <View style={styles.consoleArea}> |
| 261 | + <Text style={styles.consoleText}>Console:</Text> |
| 262 | + <ScrollView style={styles.console}> |
| 263 | + <Text>{console}</Text> |
| 264 | + </ScrollView> |
| 265 | + </View> |
| 266 | + </View> |
| 267 | + ); |
| 268 | +} |
| 269 | + |
| 270 | +const styles = StyleSheet.create({ |
| 271 | + container: { |
| 272 | + flex: 1, |
| 273 | + backgroundColor: "#fff", |
| 274 | + alignItems: "center", |
| 275 | + justifyContent: "center", |
| 276 | + paddingTop: 50, |
| 277 | + paddingBottom: 30, |
| 278 | + }, |
| 279 | + consoleArea: { |
| 280 | + margin: 20, |
| 281 | + alignItems: "center", |
| 282 | + justifyContent: "center", |
| 283 | + flex: 1, |
| 284 | + }, |
| 285 | + console: { |
| 286 | + flex: 1, |
| 287 | + backgroundColor: "#CCCCCC", |
| 288 | + color: "#ffffff", |
| 289 | + padding: 10, |
| 290 | + width: Dimensions.get("window").width - 60, |
| 291 | + }, |
| 292 | + consoleText: { |
| 293 | + padding: 10, |
| 294 | + }, |
| 295 | + buttonArea: { |
| 296 | + flex: 2, |
| 297 | + alignItems: "center", |
| 298 | + justifyContent: "space-around", |
| 299 | + paddingBottom: 30, |
| 300 | + }, |
| 301 | +}); |
0 commit comments