forked from Web3Auth/web3auth-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
186 lines (171 loc) · 5.22 KB
/
Copy pathApp.js
File metadata and controls
186 lines (171 loc) · 5.22 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
import React, {useState} from 'react';
import {
StyleSheet,
Text,
View,
Button,
ScrollView,
Dimensions,
} from 'react-native';
import * as WebBrowser from '@toruslabs/react-native-web-browser';
import Web3Auth, {
LOGIN_PROVIDER,
OPENLOGIN_NETWORK,
} from '@web3auth/react-native-sdk';
import RPC from './ethersRPC'; // for using ethers.js
import auth from '@react-native-firebase/auth';
import {GoogleSignin} from '@react-native-google-signin/google-signin';
const scheme = 'web3authrnbarefirebase'; // Or your desired app redirection scheme
const resolvedRedirectUrl = `${scheme}://openlogin`;
const clientId =
'BEglQSgt4cUWcj6SKRdu5QkOXTsePmMcusG5EAoyjyOYKlVRjIF1iCNnMOTfpzCiunHRrMui8TIwQPXdkQ8Yxuk';
const providerUrl = 'https://rpc.ankr.com/eth'; // Or your desired provider url
async function signInWithGoogle() {
try {
GoogleSignin.configure({
webClientId:
'461819774167-5iv443bdf5a6pnr2drt4tubaph270obl.apps.googleusercontent.com',
});
// Check if your device supports Google Play
await GoogleSignin.hasPlayServices({showPlayServicesUpdateDialog: true});
// Get the users ID token
const {idToken} = await GoogleSignin.signIn();
// Create a Google credential with the token
const googleCredential = auth.GoogleAuthProvider.credential(idToken);
// Sign-in the user with the credential
const res = await auth().signInWithCredential(googleCredential);
return res;
} catch (error) {
console.error(error);
}
}
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
loginConfig: {
jwt: {
name: 'Web3Auth-Auth0-JWT',
verifier: 'web3auth-firebase-examples',
typeOfLogin: 'jwt',
clientId,
},
},
});
const loginRes = await signInWithGoogle();
uiConsole('Google login success', loginRes);
const idToken = await loginRes.user.getIdToken(true);
uiConsole('idToken', idToken);
const info = await web3auth.login({
loginProvider: LOGIN_PROVIDER.JWT,
redirectUrl: resolvedRedirectUrl,
mfaLevel: 'none',
curve: 'secp256k1',
extraLoginOptions: {
id_token: idToken,
verifierIdField: 'sub',
domain: 'http://localhost:3000',
},
});
setUserInfo(info);
setKey(info.privKey);
uiConsole('Logged In');
} catch (e) {
console.error(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,
},
});