11import 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" ;
33import "@ethersproject/shims" ;
44import { ethers } from "ethers" ;
55
@@ -8,6 +8,15 @@ import * as WebBrowser from "@toruslabs/react-native-web-browser";
88import EncryptedStorage from "react-native-encrypted-storage" ;
99import Web3Auth , { LOGIN_PROVIDER , WEB3AUTH_NETWORK , ChainNamespace } from "@web3auth/react-native-sdk" ;
1010import { 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" ;
1120// IMP END - Quick Start
1221
1322const scheme = "web3authrnexample" ; // Or your desired app redirection scheme
@@ -40,24 +49,98 @@ const privateKeyProvider = new EthereumPrivateKeyProvider({
4049 } ,
4150} ) ;
4251
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 ( ) ;
5173// IMP END - SDK Initialization
5274
5375export default function App ( ) {
76+ const [ web3auth , setWeb3auth ] = useState < Web3Auth | null > ( null ) ;
5477 const [ loggedIn , setLoggedIn ] = useState ( false ) ;
5578 const [ provider , setProvider ] = useState < any > ( null ) ;
5679 const [ console , setConsole ] = useState < string > ( "" ) ;
5780 const [ email , setEmail ] = useState < string > ( "" ) ;
81+ const [ useAccountAbstraction , setUseAccountAbstraction ] = useMMKVStorage < boolean > ( "useAccountAbstraction" , storage , false ) ;
82+
83+ const toggleAccountAbstraction = ( ) => {
84+ setUseAccountAbstraction ( ( prevState ) => ! prevState ) ;
85+ } ;
5886
5987 useEffect ( ( ) => {
6088 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+
61144 // IMP START - SDK Initialization
62145 await web3auth . init ( ) ;
63146
@@ -68,11 +151,11 @@ export default function App() {
68151 }
69152 } ;
70153 init ( ) ;
71- } , [ ] ) ;
154+ } , [ useAccountAbstraction ] ) ;
72155
73156 const login = async ( ) => {
74157 try {
75- if ( ! web3auth . ready ) {
158+ if ( ! web3auth ? .ready ) {
76159 setConsole ( "Web3auth not initialized" ) ;
77160 return ;
78161 }
@@ -103,7 +186,7 @@ export default function App() {
103186 } ;
104187
105188 const logout = async ( ) => {
106- if ( ! web3auth . ready ) {
189+ if ( ! web3auth ? .ready ) {
107190 setConsole ( "Web3auth not initialized" ) ;
108191 return ;
109192 }
@@ -222,7 +305,7 @@ export default function App() {
222305
223306 const loggedInView = (
224307 < View style = { styles . buttonArea } >
225- < Button title = "Get User Info" onPress = { ( ) => uiConsole ( web3auth . userInfo ( ) ) } />
308+ < Button title = "Get User Info" onPress = { ( ) => uiConsole ( web3auth ? .userInfo ( ) ) } />
226309 < Button title = "Get Accounts" onPress = { ( ) => getAccounts ( ) } />
227310 < Button title = "Get Balance" onPress = { ( ) => getBalance ( ) } />
228311 < Button title = "Sign Message" onPress = { ( ) => signMessage ( ) } />
@@ -234,6 +317,18 @@ export default function App() {
234317
235318 const unloggedInView = (
236319 < 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 >
237332 < TextInput style = { styles . inputEmail } placeholder = "Enter email" onChangeText = { setEmail } />
238333 < Button title = "Login with Web3Auth" onPress = { login } />
239334 </ View >
0 commit comments