1- import * as WebBrowser from ' @toruslabs/react-native-web-browser' ;
1+ import * as WebBrowser from " @toruslabs/react-native-web-browser" ;
22
3- import {
4- Button ,
5- Dimensions ,
6- ScrollView ,
7- StyleSheet ,
8- Text ,
9- TextInput ,
10- View ,
11- } from 'react-native' ;
12- import Web3Auth , {
13- IWeb3Auth ,
14- LOGIN_PROVIDER ,
15- OpenloginUserInfo ,
16- } from '@web3auth/react-native-sdk' ;
17- import { useEffect , useState } from 'react' ;
3+ import { Button , Dimensions , ScrollView , StyleSheet , Text , TextInput , View } from "react-native" ;
4+ import Web3Auth , { IWeb3Auth , LOGIN_PROVIDER , OpenloginUserInfo } from "@web3auth/react-native-sdk" ;
5+ import { useEffect , useState } from "react" ;
186
19- import { ChainNamespace } from ' @web3auth/react-native-sdk' ;
20- import EncryptedStorage from ' react-native-encrypted-storage' ;
21- import RPC from ' ./ethersRPC' ; // for using ethers.js
7+ import { ChainNamespace } from " @web3auth/react-native-sdk" ;
8+ import EncryptedStorage from " react-native-encrypted-storage" ;
9+ import RPC from " ./ethersRPC" ; // for using ethers.js
2210
23- const scheme = ' web3authrnbareexample' ; // Or your desired app redirection scheme
11+ const scheme = " web3authrnbareexample" ; // Or your desired app redirection scheme
2412const resolvedRedirectUrl = `${ scheme } ://openlogin` ;
25- const clientId =
26- 'BFuUqebV5I8Pz5F7a5A2ihW7YVmbv_OHXnHYDv6OltAD5NGr6e-ViNvde3U4BHdn6HvwfkgobhVu4VwC-OSJkik' ;
13+ const clientId = "BFuUqebV5I8Pz5F7a5A2ihW7YVmbv_OHXnHYDv6OltAD5NGr6e-ViNvde3U4BHdn6HvwfkgobhVu4VwC-OSJkik" ;
2714
2815const chainConfig = {
2916 chainNamespace : ChainNamespace . EIP155 ,
30- chainId : ' 0xaa36a7' ,
31- rpcTarget : ' https://rpc.ankr.com/eth_sepolia' ,
17+ chainId : " 0xaa36a7" ,
18+ rpcTarget : " https://rpc.ankr.com/eth_sepolia" ,
3219 // Avoid using public rpcTarget in production.
3320 // Use services like Infura, Quicknode etc
34- displayName : ' Ethereum Sepolia Testnet' ,
35- blockExplorerUrl : ' https://sepolia.etherscan.io' ,
36- ticker : ' ETH' ,
37- tickerName : ' Ethereum' ,
21+ displayName : " Ethereum Sepolia Testnet" ,
22+ blockExplorerUrl : " https://sepolia.etherscan.io" ,
23+ ticker : " ETH" ,
24+ tickerName : " Ethereum" ,
3825 decimals : 18 ,
39- logo : ' https://cryptologos.cc/logos/ethereum-eth-logo.png' ,
26+ logo : " https://cryptologos.cc/logos/ethereum-eth-logo.png" ,
4027} ;
4128
4229export default function App ( ) {
43- const [ userInfo , setUserInfo ] = useState < OpenloginUserInfo | undefined > ( ) ;
44- const [ key , setKey ] = useState < string | undefined > ( '' ) ;
45- const [ console , setConsole ] = useState < string > ( '' ) ;
30+ const [ userInfo , setUserInfo ] = useState < OpenloginUserInfo | null > ( null ) ;
31+ const [ key , setKey ] = useState < string | undefined > ( "" ) ;
32+ const [ localConsole , setLocalConsole ] = useState < string > ( "" ) ;
4633 const [ web3auth , setWeb3Auth ] = useState < IWeb3Auth | null > ( null ) ;
47- const [ email , setEmail ] = useState ( ' [email protected] ' ) ; 34+ const [ email , setEmail ] = useState ( " [email protected] " ) ; 4835
4936 const login = async ( ) => {
5037 try {
5138 if ( ! web3auth ) {
52- setConsole ( ' Web3auth not initialized' ) ;
39+ setLocalConsole ( " Web3auth not initialized" ) ;
5340 return ;
5441 }
5542
56- setConsole ( ' Logging in' ) ;
43+ setLocalConsole ( " Logging in" ) ;
5744 await web3auth . login ( {
5845 loginProvider : LOGIN_PROVIDER . EMAIL_PASSWORDLESS ,
5946 extraLoginOptions : {
6047 login_hint : email ,
6148 } ,
6249 } ) ;
63- setConsole ( `Logged in ${ web3auth . privKey } ` ) ;
50+ setLocalConsole ( `Logged in ${ web3auth . privKey } ` ) ;
6451 if ( web3auth . privKey ) {
65- setUserInfo ( web3auth . userInfo ( ) ) ;
52+ const userInfo = web3auth . userInfo ( ) ;
53+ if ( userInfo ) setUserInfo ( userInfo ) ;
6654 setKey ( web3auth . privKey ) ;
67- uiConsole ( ' Logged In' ) ;
55+ uiConsole ( " Logged In" ) ;
6856 }
6957 } catch ( e : unknown ) {
70- setConsole ( ( e as Error ) . message ) ;
58+ console . error ( e ) ;
59+ setLocalConsole ( ( e as Error ) . message ) ;
7160 }
7261 } ;
7362
7463 const logout = async ( ) => {
7564 if ( ! web3auth ) {
76- setConsole ( ' Web3auth not initialized' ) ;
65+ setLocalConsole ( " Web3auth not initialized" ) ;
7766 return ;
7867 }
7968
80- setConsole ( ' Logging out' ) ;
69+ setLocalConsole ( " Logging out" ) ;
8170 await web3auth . logout ( ) ;
8271
8372 if ( ! web3auth . privKey ) {
84- setUserInfo ( undefined ) ;
85- setKey ( '' ) ;
86- uiConsole ( ' Logged out' ) ;
73+ setUserInfo ( null ) ;
74+ setKey ( "" ) ;
75+ uiConsole ( " Logged out" ) ;
8776 }
8877 } ;
8978
9079 const enableMFA = async ( ) => {
9180 if ( ! web3auth ) {
92- setConsole ( ' Web3auth not initialized' ) ;
81+ setLocalConsole ( " Web3auth not initialized" ) ;
9382 return ;
9483 }
9584
96- setConsole ( ' Enable MFA' ) ;
85+ setLocalConsole ( " Enable MFA" ) ;
9786 await web3auth . enableMFA ( ) ;
98- uiConsole ( ' MFA enabled' ) ;
87+ uiConsole ( " MFA enabled" ) ;
9988 } ;
10089
10190 const launchWalletSerices = async ( ) => {
10291 if ( ! web3auth ) {
103- setConsole ( ' Web3auth not initialized' ) ;
92+ setLocalConsole ( " Web3auth not initialized" ) ;
10493 return ;
10594 }
10695
107- setConsole ( ' Launch Wallet Services' ) ;
96+ setLocalConsole ( " Launch Wallet Services" ) ;
10897 await web3auth . launchWalletServices ( chainConfig ) ;
10998 } ;
11099
111100 const requestSignature = async ( ) => {
112101 if ( ! web3auth ) {
113- setConsole ( ' Web3auth not initialized' ) ;
102+ setLocalConsole ( " Web3auth not initialized" ) ;
114103 return ;
115104 }
116105 if ( ! key ) {
117- setConsole ( ' User not logged in' ) ;
106+ setLocalConsole ( " User not logged in" ) ;
118107 return ;
119108 }
120109
@@ -127,7 +116,7 @@ export default function App() {
127116 // },
128117 // null,
129118 // ];
130- const params = [ ' Hello World' , address ] ;
119+ const params = [ " Hello World" , address ] ;
131120 // const params = [{ }];
132121 // params.push('Hello World');
133122 // params.push(address);
@@ -200,92 +189,87 @@ export default function App() {
200189 // },
201190 // ];
202191
203- setConsole ( ' Request Signature' ) ;
204- const res = await web3auth . request ( chainConfig , ' personal_sign' , params ) ;
192+ setLocalConsole ( " Request Signature" ) ;
193+ const res = await web3auth . request ( chainConfig , " personal_sign" , params ) ;
205194 uiConsole ( res ) ;
206195 } ;
207196
208197 useEffect ( ( ) => {
209198 const init = async ( ) => {
210199 const auth = new Web3Auth ( WebBrowser , EncryptedStorage , {
211200 clientId,
212- network : ' sapphire_devnet' , // or other networks
201+ network : " sapphire_devnet" , // or other networks
213202 useCoreKitKey : false ,
214203 loginConfig : { } ,
215204 enableLogging : true ,
216- buildEnv : ' testing' ,
205+ buildEnv : " testing" ,
217206 redirectUrl : resolvedRedirectUrl ,
218207 } ) ;
219208 setWeb3Auth ( auth ) ;
220209 await auth . init ( ) ;
221210 if ( auth ?. privKey ) {
222- uiConsole ( 'Re logged in' ) ;
223- setUserInfo ( auth . userInfo ( ) ) ;
211+ uiConsole ( "Re logged in" ) ;
212+ const userInfo = auth . userInfo ( ) ;
213+ if ( userInfo ) setUserInfo ( userInfo ) ;
224214 setKey ( auth . privKey ) ;
225215 }
226216 } ;
227217 init ( ) ;
228218 } , [ ] ) ;
229219
230220 const getChainId = async ( ) => {
231- setConsole ( ' Getting chain id' ) ;
221+ setLocalConsole ( " Getting chain id" ) ;
232222 const networkDetails = await RPC . getChainId ( ) ;
233223 uiConsole ( networkDetails ) ;
234224 } ;
235225
236226 const getAccounts = async ( ) => {
237227 if ( ! key ) {
238- setConsole ( ' User not logged in' ) ;
228+ setLocalConsole ( " User not logged in" ) ;
239229 return ;
240230 }
241- setConsole ( ' Getting account' ) ;
231+ setLocalConsole ( " Getting account" ) ;
242232 const address = await RPC . getAccounts ( key ) ;
243233 uiConsole ( address ) ;
244234 } ;
245235 const getBalance = async ( ) => {
246236 if ( ! key ) {
247- setConsole ( ' User not logged in' ) ;
237+ setLocalConsole ( " User not logged in" ) ;
248238 return ;
249239 }
250- setConsole ( ' Fetching balance' ) ;
240+ setLocalConsole ( " Fetching balance" ) ;
251241 const balance = await RPC . getBalance ( key ) ;
252242 uiConsole ( balance ) ;
253243 } ;
254244 const sendTransaction = async ( ) => {
255245 if ( ! key ) {
256- setConsole ( ' User not logged in' ) ;
246+ setLocalConsole ( " User not logged in" ) ;
257247 return ;
258248 }
259- setConsole ( ' Sending transaction' ) ;
249+ setLocalConsole ( " Sending transaction" ) ;
260250 const tx = await RPC . sendTransaction ( key ) ;
261251 uiConsole ( tx ) ;
262252 } ;
263253 const signMessage = async ( ) => {
264254 if ( ! key ) {
265- setConsole ( ' User not logged in' ) ;
255+ setLocalConsole ( " User not logged in" ) ;
266256 return ;
267257 }
268- setConsole ( ' Signing message' ) ;
258+ setLocalConsole ( " Signing message" ) ;
269259 const message = await RPC . signMessage ( key ) ;
270260 uiConsole ( message ) ;
271261 } ;
272262
273263 const uiConsole = ( ...args : unknown [ ] ) => {
274- setConsole ( JSON . stringify ( args || { } , null , 2 ) + ' \n\n\n\n' + console ) ;
264+ setLocalConsole ( JSON . stringify ( args || { } , null , 2 ) + " \n\n\n\n" + localConsole ) ;
275265 } ;
276266
277267 const loggedInView = (
278268 < View style = { styles . buttonArea } >
279269 < Button title = "Get User Info" onPress = { ( ) => uiConsole ( userInfo ) } />
280270 < Button title = "Enable MFA" onPress = { ( ) => enableMFA ( ) } />
281- < Button
282- title = "launch Wallet Services"
283- onPress = { ( ) => launchWalletSerices ( ) }
284- />
285- < Button
286- title = "Request Signature from Wallet Services"
287- onPress = { ( ) => requestSignature ( ) }
288- />
271+ < Button title = "launch Wallet Services" onPress = { ( ) => launchWalletSerices ( ) } />
272+ < Button title = "Request Signature from Wallet Services" onPress = { ( ) => requestSignature ( ) } />
289273 < Button title = "Get Chain ID" onPress = { ( ) => getChainId ( ) } />
290274 < Button title = "Get Accounts" onPress = { ( ) => getAccounts ( ) } />
291275 < Button title = "Get Balance" onPress = { ( ) => getBalance ( ) } />
@@ -300,7 +284,7 @@ export default function App() {
300284 < View style = { styles . buttonArea } >
301285 < TextInput
302286 editable
303- onChangeText = { text => setEmail ( text ) }
287+ onChangeText = { ( text ) => setEmail ( text ) }
304288 value = { email }
305289 // eslint-disable-next-line react-native/no-inline-styles
306290 style = { { padding : 10 } }
@@ -315,7 +299,7 @@ export default function App() {
315299 < View style = { styles . consoleArea } >
316300 < Text style = { styles . consoleText } > Console:</ Text >
317301 < ScrollView style = { styles . console } >
318- < Text > { console } </ Text >
302+ < Text > { localConsole } </ Text >
319303 </ ScrollView >
320304 </ View >
321305 </ View >
@@ -325,32 +309,32 @@ export default function App() {
325309const styles = StyleSheet . create ( {
326310 container : {
327311 flex : 1 ,
328- backgroundColor : ' #fff' ,
329- alignItems : ' center' ,
330- justifyContent : ' center' ,
312+ backgroundColor : " #fff" ,
313+ alignItems : " center" ,
314+ justifyContent : " center" ,
331315 paddingTop : 50 ,
332316 paddingBottom : 30 ,
333317 } ,
334318 consoleArea : {
335319 margin : 20 ,
336- alignItems : ' center' ,
337- justifyContent : ' center' ,
320+ alignItems : " center" ,
321+ justifyContent : " center" ,
338322 flex : 1 ,
339323 } ,
340324 console : {
341325 flex : 1 ,
342- backgroundColor : ' #CCCCCC' ,
343- color : ' #ffffff' ,
326+ backgroundColor : " #CCCCCC" ,
327+ color : " #ffffff" ,
344328 padding : 10 ,
345- width : Dimensions . get ( ' window' ) . width - 60 ,
329+ width : Dimensions . get ( " window" ) . width - 60 ,
346330 } ,
347331 consoleText : {
348332 padding : 10 ,
349333 } ,
350334 buttonArea : {
351335 flex : 2 ,
352- alignItems : ' center' ,
353- justifyContent : ' space-around' ,
336+ alignItems : " center" ,
337+ justifyContent : " space-around" ,
354338 paddingBottom : 30 ,
355339 } ,
356340} ) ;
0 commit comments