Skip to content

Commit 841c940

Browse files
committed
Update expo example
1 parent 2e067f3 commit 841c940

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+7670
-11917
lines changed

demo/rn-expo-example/.gitignore

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1+
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files
2+
3+
# dependencies
14
node_modules/
5+
6+
# Expo
27
.expo/
38
dist/
4-
npm-debug.*
9+
web-build/
10+
11+
# Native
12+
*.orig.*
513
*.jks
614
*.p8
715
*.p12
816
*.key
917
*.mobileprovision
10-
*.orig.*
11-
web-build/
18+
19+
# Metro
20+
.metro-health-check*
21+
22+
# debug
23+
npm-debug.*
24+
yarn-debug.*
25+
yarn-error.*
1226

1327
# macOS
1428
.DS_Store
29+
*.pem
1530

16-
# @generated expo-cli sync-2b81b286409207a5da26e14c78851eb30d8ccbdb
17-
# The following patterns were generated by expo-cli
31+
# local env files
32+
.env*.local
1833

19-
expo-env.d.ts
20-
# @end expo-cli
34+
# typescript
35+
*.tsbuildinfo

demo/rn-expo-example/App.tsx

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
import React, { useState, useEffect } from "react";
2+
import { Button, Dimensions, ScrollView, StyleSheet, Text, View, TextInput } from "react-native";
3+
import Constants, { AppOwnership } from "expo-constants";
4+
import * as Linking from "expo-linking";
5+
import "@ethersproject/shims";
6+
import "./globals";
7+
// IMP START - Quick Start
8+
import * as WebBrowser from "expo-web-browser";
9+
import * as SecureStore from "expo-secure-store";
10+
import Web3Auth, { WEB3AUTH_NETWORK, LOGIN_PROVIDER, ChainNamespace } from "@web3auth/react-native-sdk";
11+
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
12+
// IMP END - Quick Start
13+
import { ethers } from "ethers";
14+
15+
// IMP START - Whitelist bundle ID
16+
const redirectUrl =
17+
//@ts-ignore
18+
Constants.appOwnership == AppOwnership.Expo || Constants.appOwnership == AppOwnership.Guest
19+
? Linking.createURL("web3auth", {})
20+
: Linking.createURL("web3auth", { scheme: "web3authexpoexample" });
21+
// IMP END - Whitelist bundle ID
22+
23+
// IMP START - Dashboard Registration
24+
const clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; // get from https://dashboard.web3auth.io
25+
// IMP END - Dashboard Registration
26+
27+
// IMP START - SDK Initialization
28+
const chainConfig = {
29+
chainNamespace: ChainNamespace.EIP155,
30+
chainId: "0xaa36a7",
31+
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
32+
// Avoid using public rpcTarget in production.
33+
// Use services like Infura, Quicknode etc
34+
displayName: "Ethereum Sepolia Testnet",
35+
blockExplorerUrl: "https://sepolia.etherscan.io",
36+
ticker: "ETH",
37+
tickerName: "Ethereum",
38+
decimals: 18,
39+
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
40+
};
41+
42+
const ethereumPrivateKeyProvider = new EthereumPrivateKeyProvider({
43+
config: {
44+
chainConfig,
45+
},
46+
});
47+
48+
const web3auth = new Web3Auth(WebBrowser, SecureStore, {
49+
clientId,
50+
// IMP START - Whitelist bundle ID
51+
redirectUrl,
52+
// IMP END - Whitelist bundle ID
53+
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, // or other networks
54+
});
55+
// IMP END - SDK Initialization
56+
57+
export default function App() {
58+
const [loggedIn, setLoggedIn] = useState(false);
59+
const [provider, setProvider] = useState<any>(null);
60+
const [console, setConsole] = useState<string>("");
61+
const [email, setEmail] = useState<string>("");
62+
63+
useEffect(() => {
64+
const init = async () => {
65+
// IMP START - SDK Initialization
66+
await web3auth.init();
67+
68+
if (web3auth.privKey) {
69+
await ethereumPrivateKeyProvider.setupProvider(web3auth.privKey);
70+
// IMP END - SDK Initialization
71+
setProvider(ethereumPrivateKeyProvider);
72+
setLoggedIn(true);
73+
}
74+
};
75+
init();
76+
}, []);
77+
78+
const login = async () => {
79+
try {
80+
if (!web3auth.ready) {
81+
setConsole("Web3auth not initialized");
82+
return;
83+
}
84+
if (!email) {
85+
setConsole("Enter email first");
86+
return;
87+
}
88+
89+
setConsole("Logging in");
90+
// IMP START - Login
91+
await web3auth.login({
92+
loginProvider: LOGIN_PROVIDER.EMAIL_PASSWORDLESS,
93+
extraLoginOptions: {
94+
login_hint: email,
95+
},
96+
});
97+
98+
if (web3auth.privKey) {
99+
await ethereumPrivateKeyProvider.setupProvider(web3auth.privKey);
100+
// IMP END - Login
101+
setProvider(ethereumPrivateKeyProvider);
102+
uiConsole("Logged In");
103+
setLoggedIn(true);
104+
}
105+
} catch (e: any) {
106+
setConsole(e.message);
107+
}
108+
};
109+
110+
const logout = async () => {
111+
if (!web3auth.ready) {
112+
setConsole("Web3auth not initialized");
113+
return;
114+
}
115+
116+
setConsole("Logging out");
117+
// IMP START - Logout
118+
await web3auth.logout();
119+
// IMP END - Logout
120+
121+
if (!web3auth.privKey) {
122+
setProvider(null);
123+
uiConsole("Logged out");
124+
setLoggedIn(false);
125+
}
126+
};
127+
128+
// IMP START - Blockchain Calls
129+
const getAccounts = async () => {
130+
if (!provider) {
131+
uiConsole("provider not set");
132+
return;
133+
}
134+
setConsole("Getting account");
135+
// For ethers v5
136+
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
137+
const ethersProvider = new ethers.BrowserProvider(provider!);
138+
139+
// For ethers v5
140+
// const signer = ethersProvider.getSigner();
141+
const signer = await ethersProvider.getSigner();
142+
143+
// Get user's Ethereum public address
144+
const address = signer.getAddress();
145+
uiConsole(address);
146+
};
147+
148+
const getBalance = async () => {
149+
if (!provider) {
150+
uiConsole("provider not set");
151+
return;
152+
}
153+
setConsole("Fetching balance");
154+
// For ethers v5
155+
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
156+
const ethersProvider = new ethers.BrowserProvider(provider!);
157+
158+
// For ethers v5
159+
// const signer = ethersProvider.getSigner();
160+
const signer = await ethersProvider.getSigner();
161+
162+
// Get user's Ethereum public address
163+
const address = signer.getAddress();
164+
165+
// Get user's balance in ether
166+
// For ethers v5
167+
// const balance = ethers.utils.formatEther(
168+
// await ethersProvider.getBalance(address) // Balance is in wei
169+
// );
170+
const balance = ethers.formatEther(
171+
await ethersProvider.getBalance(address) // Balance is in wei
172+
);
173+
uiConsole(balance);
174+
};
175+
176+
const signMessage = async () => {
177+
if (!provider) {
178+
uiConsole("provider not set");
179+
return;
180+
}
181+
setConsole("Signing message");
182+
// For ethers v5
183+
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
184+
const ethersProvider = new ethers.BrowserProvider(provider!);
185+
186+
// For ethers v5
187+
// const signer = ethersProvider.getSigner();
188+
const signer = await ethersProvider.getSigner();
189+
const originalMessage = "YOUR_MESSAGE";
190+
191+
// Sign the message
192+
const signedMessage = await signer.signMessage(originalMessage);
193+
uiConsole(signedMessage);
194+
};
195+
// IMP END - Blockchain Calls
196+
197+
const launchWalletServices = async () => {
198+
if (!web3auth) {
199+
setConsole("Web3auth not initialized");
200+
return;
201+
}
202+
203+
setConsole("Launch Wallet Services");
204+
await web3auth.launchWalletServices(chainConfig);
205+
};
206+
207+
const uiConsole = (...args: unknown[]) => {
208+
setConsole(JSON.stringify(args || {}, null, 2) + "\n\n\n\n" + console);
209+
};
210+
211+
const loggedInView = (
212+
<View style={styles.buttonArea}>
213+
<Button title="Get User Info" onPress={() => uiConsole(web3auth.userInfo())} />
214+
<Button title="Get Accounts" onPress={() => getAccounts()} />
215+
<Button title="Get Balance" onPress={() => getBalance()} />
216+
<Button title="Sign Message" onPress={() => signMessage()} />
217+
<Button title="Show Wallet UI" onPress={() => launchWalletServices()} />
218+
<Button title="Log Out" onPress={logout} />
219+
</View>
220+
);
221+
222+
const unloggedInView = (
223+
<View style={styles.buttonAreaLogin}>
224+
<TextInput style={styles.inputEmail} placeholder="Enter email" onChangeText={setEmail} />
225+
<Button title="Login with Web3Auth" onPress={login} />
226+
</View>
227+
);
228+
229+
return (
230+
<View style={styles.container}>
231+
{loggedIn ? loggedInView : unloggedInView}
232+
<View style={styles.consoleArea}>
233+
<Text style={styles.consoleText}>Console:</Text>
234+
<ScrollView style={styles.console}>
235+
<Text>{console}</Text>
236+
</ScrollView>
237+
</View>
238+
</View>
239+
);
240+
}
241+
242+
const styles = StyleSheet.create({
243+
container: {
244+
flex: 1,
245+
backgroundColor: "#fff",
246+
alignItems: "center",
247+
justifyContent: "center",
248+
paddingTop: 50,
249+
paddingBottom: 30,
250+
},
251+
consoleArea: {
252+
margin: 20,
253+
alignItems: "center",
254+
justifyContent: "center",
255+
flex: 1,
256+
},
257+
console: {
258+
flex: 1,
259+
backgroundColor: "#CCCCCC",
260+
color: "#ffffff",
261+
padding: 10,
262+
width: Dimensions.get("window").width - 60,
263+
},
264+
consoleText: {
265+
padding: 10,
266+
},
267+
buttonArea: {
268+
flex: 2,
269+
alignItems: "center",
270+
justifyContent: "space-around",
271+
paddingBottom: 30,
272+
},
273+
buttonAreaLogin: {
274+
flex: 2,
275+
alignItems: "center",
276+
justifyContent: "center",
277+
paddingBottom: 30,
278+
},
279+
inputEmail: {
280+
height: 40,
281+
width: 300,
282+
borderColor: "gray",
283+
borderWidth: 1,
284+
padding: 10,
285+
borderRadius: 5,
286+
marginBottom: 20,
287+
},
288+
});

demo/rn-expo-example/README.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

demo/rn-expo-example/android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ android {
8383
buildToolsVersion rootProject.ext.buildToolsVersion
8484
compileSdk rootProject.ext.compileSdkVersion
8585

86-
namespace 'com.anonymous.rnexpoexample'
86+
namespace 'com.web3authexpoexample'
8787
defaultConfig {
88-
applicationId 'com.anonymous.rnexpoexample'
88+
applicationId 'com.web3authexpoexample'
8989
minSdkVersion rootProject.ext.minSdkVersion
9090
targetSdkVersion rootProject.ext.targetSdkVersion
9191
versionCode 1

0 commit comments

Comments
 (0)