1
1
import React , { useState , useEffect } from "react" ;
2
- import { Button , Dimensions , ScrollView , StyleSheet , Text , View , TextInput } from "react-native" ;
2
+ import { Button , Dimensions , ScrollView , StyleSheet , Text , View , TextInput , Switch } from "react-native" ;
3
3
import Constants , { AppOwnership } from "expo-constants" ;
4
4
import * as Linking from "expo-linking" ;
5
5
import "@ethersproject/shims" ;
@@ -9,8 +9,17 @@ import * as WebBrowser from "expo-web-browser";
9
9
import * as SecureStore from "expo-secure-store" ;
10
10
import Web3Auth , { WEB3AUTH_NETWORK , LOGIN_PROVIDER , ChainNamespace } from "@web3auth/react-native-sdk" ;
11
11
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider" ;
12
+ import { MMKVLoader , useMMKVStorage } from "react-native-mmkv-storage" ;
12
13
// IMP END - Quick Start
13
14
import { ethers } from "ethers" ;
15
+ import {
16
+ AccountAbstractionProvider ,
17
+ BiconomySmartAccount ,
18
+ ISmartAccount ,
19
+ KernelSmartAccount ,
20
+ SafeSmartAccount ,
21
+ TrustSmartAccount ,
22
+ } from "@web3auth/account-abstraction-provider" ;
14
23
15
24
// IMP START - Whitelist bundle ID
16
25
const redirectUrl =
@@ -45,24 +54,97 @@ const privateKeyProvider = new EthereumPrivateKeyProvider({
45
54
} ,
46
55
} ) ;
47
56
48
- const web3auth = new Web3Auth ( WebBrowser , SecureStore , {
49
- clientId,
50
- privateKeyProvider,
51
- // IMP START - Whitelist bundle ID
52
- redirectUrl,
53
- // IMP END - Whitelist bundle ID
54
- network : WEB3AUTH_NETWORK . SAPPHIRE_MAINNET , // or other networks
55
- } ) ;
57
+ const PIMLICO_API_KEY = "YOUR_PIMLICO_API_KEY" ;
58
+
59
+ export const getDefaultBundlerUrl = ( chainId : string ) : string => {
60
+ return `https://api.pimlico.io/v2/${ Number ( chainId ) } /rpc?apikey=${ PIMLICO_API_KEY } ` ;
61
+ } ;
62
+
63
+ export type SmartAccountType = "safe" | "kernel" | "biconomy" | "trust" ;
64
+
65
+ export type AccountAbstractionConfig = {
66
+ bundlerUrl ?: string ;
67
+ paymasterUrl ?: string ;
68
+ smartAccountType ?: SmartAccountType ;
69
+ } ;
70
+
71
+ const AAConfig : AccountAbstractionConfig = {
72
+ // bundlerUrl: "https://bundler.safe.global",
73
+ // paymasterUrl: "https://paymaster.safe.global",
74
+ smartAccountType : "safe" ,
75
+ } ;
76
+
77
+ const storage = new MMKVLoader ( ) . initialize ( ) ;
56
78
// IMP END - SDK Initialization
57
79
58
80
export default function App ( ) {
81
+ const [ web3auth , setWeb3auth ] = useState < Web3Auth | null > ( null ) ;
59
82
const [ loggedIn , setLoggedIn ] = useState ( false ) ;
60
83
const [ provider , setProvider ] = useState < any > ( null ) ;
61
84
const [ console , setConsole ] = useState < string > ( "" ) ;
62
85
const [ email , setEmail ] = useState < string > ( "" ) ;
86
+ const [ useAccountAbstraction , setUseAccountAbstraction ] = useMMKVStorage < boolean > ( "useAccountAbstraction" , storage , false ) ;
87
+
88
+ const toggleAccountAbstraction = ( ) => {
89
+ setUseAccountAbstraction ( ( prevState ) => ! prevState ) ;
90
+ } ;
63
91
64
92
useEffect ( ( ) => {
65
93
const init = async ( ) => {
94
+ // setup aa provider
95
+ let aaProvider : AccountAbstractionProvider | undefined ;
96
+ if ( useAccountAbstraction ) {
97
+ const { bundlerUrl, paymasterUrl, smartAccountType } = AAConfig ;
98
+
99
+ let smartAccountInit : ISmartAccount ;
100
+ switch ( smartAccountType ) {
101
+ case "biconomy" :
102
+ smartAccountInit = new BiconomySmartAccount ( ) ;
103
+ break ;
104
+ case "kernel" :
105
+ smartAccountInit = new KernelSmartAccount ( ) ;
106
+ break ;
107
+ case "trust" :
108
+ smartAccountInit = new TrustSmartAccount ( ) ;
109
+ break ;
110
+ // case "light":
111
+ // smartAccountInit = new LightSmartAccount();
112
+ // break;
113
+ // case "simple":
114
+ // smartAccountInit = new SimpleSmartAccount();
115
+ // break;
116
+ case "safe" :
117
+ default :
118
+ smartAccountInit = new SafeSmartAccount ( ) ;
119
+ break ;
120
+ }
121
+
122
+ aaProvider = new AccountAbstractionProvider ( {
123
+ config : {
124
+ chainConfig,
125
+ bundlerConfig : {
126
+ url : bundlerUrl ?? getDefaultBundlerUrl ( chainConfig . chainId ) ,
127
+ } ,
128
+ paymasterConfig : paymasterUrl
129
+ ? {
130
+ url : paymasterUrl ,
131
+ }
132
+ : undefined ,
133
+ smartAccountInit,
134
+ } ,
135
+ } ) ;
136
+ }
137
+
138
+ const web3auth = new Web3Auth ( WebBrowser , SecureStore , {
139
+ clientId,
140
+ privateKeyProvider,
141
+ accountAbstractionProvider : aaProvider ,
142
+ // IMP START - Whitelist bundle ID
143
+ redirectUrl,
144
+ // IMP END - Whitelist bundle ID
145
+ network : WEB3AUTH_NETWORK . SAPPHIRE_MAINNET , // or other networks
146
+ } ) ;
147
+ setWeb3auth ( web3auth ) ;
66
148
// IMP START - SDK Initialization
67
149
await web3auth . init ( ) ;
68
150
// IMP END - SDK Initialization
@@ -73,11 +155,11 @@ export default function App() {
73
155
}
74
156
} ;
75
157
init ( ) ;
76
- } , [ ] ) ;
158
+ } , [ useAccountAbstraction ] ) ;
77
159
78
160
const login = async ( ) => {
79
161
try {
80
- if ( ! web3auth . ready ) {
162
+ if ( ! web3auth ? .ready ) {
81
163
setConsole ( "Web3auth not initialized" ) ;
82
164
return ;
83
165
}
@@ -107,7 +189,7 @@ export default function App() {
107
189
} ;
108
190
109
191
const logout = async ( ) => {
110
- if ( ! web3auth . ready ) {
192
+ if ( ! web3auth ? .ready ) {
111
193
setConsole ( "Web3auth not initialized" ) ;
112
194
return ;
113
195
}
@@ -220,6 +302,18 @@ export default function App() {
220
302
221
303
const unloggedInView = (
222
304
< View style = { styles . buttonAreaLogin } >
305
+ < View style = { { marginBottom : 20 } } >
306
+ < View
307
+ style = { {
308
+ flexDirection : "row" ,
309
+ alignItems : "center" ,
310
+ justifyContent : "space-between" ,
311
+ } }
312
+ >
313
+ < Text style = { { paddingRight : 6 } } > Use Account Abstraction:</ Text >
314
+ < Switch onValueChange = { toggleAccountAbstraction } value = { useAccountAbstraction } />
315
+ </ View >
316
+ </ View >
223
317
< TextInput style = { styles . inputEmail } placeholder = "Enter email" onChangeText = { setEmail } />
224
318
< Button title = "Login with Web3Auth" onPress = { login } />
225
319
</ View >
0 commit comments