1
1
import React , { useEffect , useState } from "react" ;
2
- import { StyleSheet , Text , View , Button , ScrollView , Dimensions , TextInput } from "react-native" ;
2
+ import { StyleSheet , Text , View , Button , ScrollView , Dimensions , TextInput , Switch } from "react-native" ;
3
3
import "@ethersproject/shims" ;
4
4
import { ethers } from "ethers" ;
5
5
@@ -8,6 +8,15 @@ import * as WebBrowser from "@toruslabs/react-native-web-browser";
8
8
import EncryptedStorage from "react-native-encrypted-storage" ;
9
9
import Web3Auth , { LOGIN_PROVIDER , WEB3AUTH_NETWORK , ChainNamespace } from "@web3auth/react-native-sdk" ;
10
10
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider" ;
11
+ import { MMKVLoader , useMMKVStorage } from "react-native-mmkv-storage" ;
12
+ import {
13
+ AccountAbstractionProvider ,
14
+ BiconomySmartAccount ,
15
+ ISmartAccount ,
16
+ KernelSmartAccount ,
17
+ SafeSmartAccount ,
18
+ TrustSmartAccount ,
19
+ } from "@web3auth/account-abstraction-provider" ;
11
20
// IMP END - Quick Start
12
21
13
22
const scheme = "web3authrnexample" ; // Or your desired app redirection scheme
@@ -40,24 +49,98 @@ const privateKeyProvider = new EthereumPrivateKeyProvider({
40
49
} ,
41
50
} ) ;
42
51
43
- const web3auth = new Web3Auth ( WebBrowser , EncryptedStorage , {
44
- clientId,
45
- // IMP START - Whitelist bundle ID
46
- redirectUrl,
47
- // IMP END - Whitelist bundle ID
48
- network : WEB3AUTH_NETWORK . SAPPHIRE_MAINNET , // or other networks
49
- privateKeyProvider,
50
- } ) ;
52
+ const PIMLICO_API_KEY = "YOUR_PIMLICO_API_KEY" ;
53
+
54
+ export const getDefaultBundlerUrl = ( chainId : string ) : string => {
55
+ return `https://api.pimlico.io/v2/${ Number ( chainId ) } /rpc?apikey=${ PIMLICO_API_KEY } ` ;
56
+ } ;
57
+
58
+ export type SmartAccountType = "safe" | "kernel" | "biconomy" | "trust" ;
59
+
60
+ export type AccountAbstractionConfig = {
61
+ bundlerUrl ?: string ;
62
+ paymasterUrl ?: string ;
63
+ smartAccountType ?: SmartAccountType ;
64
+ } ;
65
+
66
+ const AAConfig : AccountAbstractionConfig = {
67
+ // bundlerUrl: "https://bundler.safe.global",
68
+ // paymasterUrl: "https://paymaster.safe.global",
69
+ smartAccountType : "safe" ,
70
+ } ;
71
+
72
+ const storage = new MMKVLoader ( ) . initialize ( ) ;
51
73
// IMP END - SDK Initialization
52
74
53
75
export default function App ( ) {
76
+ const [ web3auth , setWeb3auth ] = useState < Web3Auth | null > ( null ) ;
54
77
const [ loggedIn , setLoggedIn ] = useState ( false ) ;
55
78
const [ provider , setProvider ] = useState < any > ( null ) ;
56
79
const [ console , setConsole ] = useState < string > ( "" ) ;
57
80
const [ email , setEmail ] = useState < string > ( "" ) ;
81
+ const [ useAccountAbstraction , setUseAccountAbstraction ] = useMMKVStorage < boolean > ( "useAccountAbstraction" , storage , false ) ;
82
+
83
+ const toggleAccountAbstraction = ( ) => {
84
+ setUseAccountAbstraction ( ( prevState ) => ! prevState ) ;
85
+ } ;
58
86
59
87
useEffect ( ( ) => {
60
88
const init = async ( ) => {
89
+ // setup aa provider
90
+ let aaProvider : AccountAbstractionProvider | undefined ;
91
+ if ( useAccountAbstraction ) {
92
+ const { bundlerUrl, paymasterUrl, smartAccountType } = AAConfig ;
93
+
94
+ let smartAccountInit : ISmartAccount ;
95
+ switch ( smartAccountType ) {
96
+ case "biconomy" :
97
+ smartAccountInit = new BiconomySmartAccount ( ) ;
98
+ break ;
99
+ case "kernel" :
100
+ smartAccountInit = new KernelSmartAccount ( ) ;
101
+ break ;
102
+ case "trust" :
103
+ smartAccountInit = new TrustSmartAccount ( ) ;
104
+ break ;
105
+ // case "light":
106
+ // smartAccountInit = new LightSmartAccount();
107
+ // break;
108
+ // case "simple":
109
+ // smartAccountInit = new SimpleSmartAccount();
110
+ // break;
111
+ case "safe" :
112
+ default :
113
+ smartAccountInit = new SafeSmartAccount ( ) ;
114
+ break ;
115
+ }
116
+
117
+ aaProvider = new AccountAbstractionProvider ( {
118
+ config : {
119
+ chainConfig,
120
+ bundlerConfig : {
121
+ url : bundlerUrl ?? getDefaultBundlerUrl ( chainConfig . chainId ) ,
122
+ } ,
123
+ paymasterConfig : paymasterUrl
124
+ ? {
125
+ url : paymasterUrl ,
126
+ }
127
+ : undefined ,
128
+ smartAccountInit,
129
+ } ,
130
+ } ) ;
131
+ }
132
+
133
+ const web3auth = new Web3Auth ( WebBrowser , EncryptedStorage , {
134
+ clientId,
135
+ // IMP START - Whitelist bundle ID
136
+ redirectUrl,
137
+ // IMP END - Whitelist bundle ID
138
+ network : WEB3AUTH_NETWORK . SAPPHIRE_MAINNET , // or other networks
139
+ privateKeyProvider,
140
+ accountAbstractionProvider : aaProvider ,
141
+ } ) ;
142
+ setWeb3auth ( web3auth ) ;
143
+
61
144
// IMP START - SDK Initialization
62
145
await web3auth . init ( ) ;
63
146
@@ -68,11 +151,11 @@ export default function App() {
68
151
}
69
152
} ;
70
153
init ( ) ;
71
- } , [ ] ) ;
154
+ } , [ useAccountAbstraction ] ) ;
72
155
73
156
const login = async ( ) => {
74
157
try {
75
- if ( ! web3auth . ready ) {
158
+ if ( ! web3auth ? .ready ) {
76
159
setConsole ( "Web3auth not initialized" ) ;
77
160
return ;
78
161
}
@@ -103,7 +186,7 @@ export default function App() {
103
186
} ;
104
187
105
188
const logout = async ( ) => {
106
- if ( ! web3auth . ready ) {
189
+ if ( ! web3auth ? .ready ) {
107
190
setConsole ( "Web3auth not initialized" ) ;
108
191
return ;
109
192
}
@@ -222,7 +305,7 @@ export default function App() {
222
305
223
306
const loggedInView = (
224
307
< View style = { styles . buttonArea } >
225
- < Button title = "Get User Info" onPress = { ( ) => uiConsole ( web3auth . userInfo ( ) ) } />
308
+ < Button title = "Get User Info" onPress = { ( ) => uiConsole ( web3auth ? .userInfo ( ) ) } />
226
309
< Button title = "Get Accounts" onPress = { ( ) => getAccounts ( ) } />
227
310
< Button title = "Get Balance" onPress = { ( ) => getBalance ( ) } />
228
311
< Button title = "Sign Message" onPress = { ( ) => signMessage ( ) } />
@@ -234,6 +317,18 @@ export default function App() {
234
317
235
318
const unloggedInView = (
236
319
< View style = { styles . buttonAreaLogin } >
320
+ < View style = { { marginBottom : 20 } } >
321
+ < View
322
+ style = { {
323
+ flexDirection : "row" ,
324
+ alignItems : "center" ,
325
+ justifyContent : "space-between" ,
326
+ } }
327
+ >
328
+ < Text style = { { paddingRight : 6 } } > Use Account Abstraction:</ Text >
329
+ < Switch onValueChange = { toggleAccountAbstraction } value = { useAccountAbstraction } />
330
+ </ View >
331
+ </ View >
237
332
< TextInput style = { styles . inputEmail } placeholder = "Enter email" onChangeText = { setEmail } />
238
333
< Button title = "Login with Web3Auth" onPress = { login } />
239
334
</ View >
0 commit comments