forked from Web3Auth/web3auth-pnp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
139 lines (126 loc) · 4.01 KB
/
App.js
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
import Web3Auth, { LOGIN_PROVIDER, OPENLOGIN_NETWORK } from "@web3auth/react-native-sdk";
import Constants, { AppOwnership } from "expo-constants";
import * as Linking from "expo-linking";
import * as WebBrowser from "expo-web-browser";
import React, { useState } from "react";
import { Button, Dimensions, ScrollView, StyleSheet, Text, View } from "react-native";
import RPC from "./ethersRPC"; // for using ethers.js
const resolvedRedirectUrl =
Constants.appOwnership == AppOwnership.Expo || Constants.appOwnership == AppOwnership.Guest
? Linking.createURL("web3auth", {})
: Linking.createURL("web3auth", { scheme });
const clientId = "BEglQSgt4cUWcj6SKRdu5QkOXTsePmMcusG5EAoyjyOYKlVRjIF1iCNnMOTfpzCiunHRrMui8TIwQPXdkQ8Yxuk";
const providerUrl = "https://rpc.ankr.com/eth"; // Or your desired provider url
export default function App() {
const [key, setKey] = useState("");
const [userInfo, setUserInfo] = useState("");
const [console, setConsole] = useState("");
const login = async () => {
try {
setConsole("Logging in");
const web3auth = new Web3Auth(WebBrowser, {
clientId,
network: OPENLOGIN_NETWORK.CYAN, // or other networks
});
const info = await web3auth.login({
loginProvider: LOGIN_PROVIDER.GOOGLE,
redirectUrl: resolvedRedirectUrl,
mfaLevel: "none",
curve: "secp256k1",
});
setUserInfo(info);
setKey(info.privKey);
uiConsole("Logged In");
} catch (e) {
uiConsole(e);
}
};
const getChainId = async () => {
setConsole("Getting chain id");
const networkDetails = await RPC.getChainId();
uiConsole(networkDetails);
};
const getAccounts = async () => {
setConsole("Getting account");
const address = await RPC.getAccounts(key);
uiConsole(address);
};
const getBalance = async () => {
setConsole("Fetching balance");
const balance = await RPC.getBalance(key);
uiConsole(balance);
};
const sendTransaction = async () => {
setConsole("Sending transaction");
const tx = await RPC.sendTransaction(key);
uiConsole(tx);
};
const signMessage = async () => {
setConsole("Signing message");
const message = await RPC.signMessage(key);
uiConsole(message);
};
const uiConsole = (...args) => {
setConsole(`${JSON.stringify(args || {}, null, 2)}\n\n\n\n${console}`);
};
const loggedInView = (
<View style={styles.buttonArea}>
<Button title="Get User Info" onPress={() => uiConsole(userInfo)} />
<Button title="Get Chain ID" onPress={() => getChainId()} />
<Button title="Get Accounts" onPress={() => getAccounts()} />
<Button title="Get Balance" onPress={() => getBalance()} />
<Button title="Send Transaction" onPress={() => sendTransaction()} />
<Button title="Sign Message" onPress={() => signMessage()} />
<Button title="Get Private Key" onPress={() => uiConsole(key)} />
<Button title="Log Out" onPress={() => setKey("")} />
</View>
);
const unloggedInView = (
<View style={styles.buttonArea}>
<Button title="Login with Web3Auth" onPress={login} />
</View>
);
return (
<View style={styles.container}>
{key ? 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,
},
});