11import 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" ;
33import Constants , { AppOwnership } from "expo-constants" ;
44import * as Linking from "expo-linking" ;
55import "@ethersproject/shims" ;
@@ -9,8 +9,17 @@ import * as WebBrowser from "expo-web-browser";
99import * as SecureStore from "expo-secure-store" ;
1010import Web3Auth , { WEB3AUTH_NETWORK , LOGIN_PROVIDER , ChainNamespace } from "@web3auth/react-native-sdk" ;
1111import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider" ;
12+ import { MMKVLoader , useMMKVStorage } from "react-native-mmkv-storage" ;
1213// IMP END - Quick Start
1314import { ethers } from "ethers" ;
15+ import {
16+ AccountAbstractionProvider ,
17+ BiconomySmartAccount ,
18+ ISmartAccount ,
19+ KernelSmartAccount ,
20+ SafeSmartAccount ,
21+ TrustSmartAccount ,
22+ } from "@web3auth/account-abstraction-provider" ;
1423
1524// IMP START - Whitelist bundle ID
1625const redirectUrl =
@@ -45,24 +54,97 @@ const privateKeyProvider = new EthereumPrivateKeyProvider({
4554 } ,
4655} ) ;
4756
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 ( ) ;
5678// IMP END - SDK Initialization
5779
5880export default function App ( ) {
81+ const [ web3auth , setWeb3auth ] = useState < Web3Auth | null > ( null ) ;
5982 const [ loggedIn , setLoggedIn ] = useState ( false ) ;
6083 const [ provider , setProvider ] = useState < any > ( null ) ;
6184 const [ console , setConsole ] = useState < string > ( "" ) ;
6285 const [ email , setEmail ] = useState < string > ( "" ) ;
86+ const [ useAccountAbstraction , setUseAccountAbstraction ] = useMMKVStorage < boolean > ( "useAccountAbstraction" , storage , false ) ;
87+
88+ const toggleAccountAbstraction = ( ) => {
89+ setUseAccountAbstraction ( ( prevState ) => ! prevState ) ;
90+ } ;
6391
6492 useEffect ( ( ) => {
6593 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 ) ;
66148 // IMP START - SDK Initialization
67149 await web3auth . init ( ) ;
68150 // IMP END - SDK Initialization
@@ -73,11 +155,11 @@ export default function App() {
73155 }
74156 } ;
75157 init ( ) ;
76- } , [ ] ) ;
158+ } , [ useAccountAbstraction ] ) ;
77159
78160 const login = async ( ) => {
79161 try {
80- if ( ! web3auth . ready ) {
162+ if ( ! web3auth ? .ready ) {
81163 setConsole ( "Web3auth not initialized" ) ;
82164 return ;
83165 }
@@ -107,7 +189,7 @@ export default function App() {
107189 } ;
108190
109191 const logout = async ( ) => {
110- if ( ! web3auth . ready ) {
192+ if ( ! web3auth ? .ready ) {
111193 setConsole ( "Web3auth not initialized" ) ;
112194 return ;
113195 }
@@ -220,6 +302,18 @@ export default function App() {
220302
221303 const unloggedInView = (
222304 < 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 >
223317 < TextInput style = { styles . inputEmail } placeholder = "Enter email" onChangeText = { setEmail } />
224318 < Button title = "Login with Web3Auth" onPress = { login } />
225319 </ View >
0 commit comments