1+ import { CHAIN_NAMESPACES } from "@web3auth/modal" ;
12import {
3+ useChain ,
24 useCheckout ,
35 useEnableMFA ,
46 useIdentityToken ,
57 useManageMFA ,
8+ useSwitchChain as useWeb3AuthSwitchChain ,
69 useWalletConnectScanner ,
710 useWalletUI ,
811 useWeb3Auth ,
912 useWeb3AuthConnect ,
1013 useWeb3AuthDisconnect ,
1114 useWeb3AuthUser ,
1215} from "@web3auth/modal/react" ;
16+ import { useSolanaWallet } from "@web3auth/modal/react/solana" ;
17+ import { useMemo } from "react" ;
1318import { useAccount , useBalance , useChainId , useSignMessage , useSignTypedData , useSwitchChain } from "wagmi" ;
1419
1520import styles from "../styles/Home.module.css" ;
1621
1722const Main = ( ) => {
18- const { provider, isConnected } = useWeb3Auth ( ) ;
23+ const { provider, isConnected, web3Auth, status } = useWeb3Auth ( ) ;
24+ const { accounts : solanaAccounts } = useSolanaWallet ( ) ;
25+ const { chainNamespace : currentChainNamespace , chainId : currentChainId } = useChain ( ) ;
1926 const { loading : connecting , connect, error : connectingError , connectorName, connectTo } = useWeb3AuthConnect ( ) ;
2027 const { disconnect } = useWeb3AuthDisconnect ( ) ;
2128 const { signMessageAsync, data : signedMessageData } = useSignMessage ( ) ;
@@ -30,16 +37,36 @@ const Main = () => {
3037 const { showWalletUI, loading : isWalletUILoading , error : walletUIError } = useWalletUI ( ) ;
3138 const { token, loading : isUserTokenLoading , error : userTokenError , getIdentityToken } = useIdentityToken ( ) ;
3239 const { switchChainAsync, chains } = useSwitchChain ( ) ;
40+ const { switchChain : switchWeb3AuthChain } = useWeb3AuthSwitchChain ( ) ;
41+
3342 const chainId = useChainId ( ) ;
3443
44+ const chainNamespaces = useMemo ( ( ) => {
45+ if ( status && web3Auth ?. coreOptions ?. chains ) {
46+ return [ ...new Set ( web3Auth . coreOptions . chains . map ( ( x ) => x . chainNamespace ) || [ ] ) ] ;
47+ }
48+ return [ ] ;
49+ } , [ status , web3Auth ] ) ;
50+
51+ const switchNamespace = ( chainNamespace : string ) => {
52+ const chainId = web3Auth ?. coreOptions . chains ?. find ( ( x ) => x . chainNamespace === chainNamespace ) ?. chainId ;
53+ if ( chainId ) {
54+ switchWeb3AuthChain ( chainId ) ;
55+ return ;
56+ }
57+ throw new Error ( `No chain found for the selected namespace: ${ chainNamespace } ` ) ;
58+ } ;
59+
3560 const loggedInView = (
3661 < >
3762 < div className = { styles . container } >
3863 < div style = { { marginTop : "16px" , marginBottom : "16px" } } >
39- < p > Account Address: { address } </ p >
64+ < p > Account Address: { currentChainNamespace === CHAIN_NAMESPACES . EIP155 ? address : solanaAccounts ?. [ 0 ] } </ p >
4065 < p > Account Balance: { balance ?. value } </ p >
4166 < p > MFA Enabled: { isMFAEnabled ? "Yes" : "No" } </ p >
42- < p > ConnectedChain ID: { chainId } </ p >
67+ < p > Wagmi ConnectedChain ID: { chainId } </ p >
68+ < p > Web3Auth ConnectedChain ID: { currentChainId } </ p >
69+ < p > Web3Auth ConnectedChain Namespace: { currentChainNamespace } </ p >
4370 </ div >
4471
4572 { /* User Info */ }
@@ -194,6 +221,22 @@ const Main = () => {
194221 ) ) }
195222 </ div >
196223
224+ < div style = { { marginTop : "16px" , marginBottom : "16px" } } >
225+ < p > Chain Namespace</ p >
226+
227+ { chainNamespaces . map ( ( namespace ) => (
228+ < button
229+ key = { namespace }
230+ onClick = { async ( ) => switchNamespace ( namespace ) }
231+ className = { styles . card }
232+ disabled = { namespace === currentChainNamespace }
233+ style = { { opacity : namespace === currentChainNamespace ? 0.5 : 1 } }
234+ >
235+ Switch to { namespace === CHAIN_NAMESPACES . EIP155 ? "EVM" : "Solana" }
236+ </ button >
237+ ) ) }
238+ </ div >
239+
197240 { /* Disconnect */ }
198241 < div style = { { marginTop : "16px" , marginBottom : "16px" } } >
199242 < p > Logout</ p >
0 commit comments