1515using Nethereum . RPC . Eth . DTOs ;
1616using WalletConnectUnity . Core . Evm ;
1717using Nethereum . Hex . HexTypes ;
18+ using UnityEngine ;
1819
1920namespace Thirdweb . Unity
2021{
@@ -48,22 +49,26 @@ public async static Task<WalletConnectWallet> Create(ThirdwebClient client, BigI
4849 _isConnected = false ;
4950 _supportedChains = eip155ChainsSupported ;
5051
51- WalletConnectModal . Ready += OnReady ;
52- WalletConnect . Instance . ActiveSessionChanged += OnActiveSessionChanged ;
53- WalletConnect . Instance . SessionDisconnected += OnSessionDisconnected ;
52+ if ( WalletConnect . Instance != null && WalletConnect . Instance . IsConnected )
53+ {
54+ try
55+ {
56+ await WalletConnect . Instance . DisconnectAsync ( ) ;
57+ }
58+ catch
59+ {
60+ // no-op
61+ }
62+ await Task . Delay ( 100 ) ;
63+ }
5464
55- if ( WalletConnect . Instance . IsInitialized )
56- CreateNewSession ( ) ;
65+ CreateNewSession ( eip155ChainsSupported ) ;
5766
58- while ( ! _isConnected && _exception == null )
67+ while ( ! WalletConnect . Instance . IsConnected && _exception == null )
5968 {
6069 await Task . Delay ( 100 ) ;
6170 }
6271
63- WalletConnectModal . Ready -= OnReady ;
64- WalletConnect . Instance . ActiveSessionChanged -= OnActiveSessionChanged ;
65- WalletConnect . Instance . SessionDisconnected -= OnSessionDisconnected ;
66-
6772 if ( _exception != null )
6873 {
6974 throw _exception ;
@@ -72,17 +77,9 @@ public async static Task<WalletConnectWallet> Create(ThirdwebClient client, BigI
7277 {
7378 try
7479 {
75- var chainInfo = await Utils . FetchThirdwebChainDataAsync ( client , initialChainId ) ;
76- var wcChainInfo = new EthereumChain ( )
77- {
78- chainIdHex = new HexBigInteger ( chainInfo . ChainId ) . HexValue ,
79- name = chainInfo . Name ,
80- nativeCurrency = new Currency ( chainInfo . NativeCurrency . Name , chainInfo . NativeCurrency . Symbol , chainInfo . NativeCurrency . Decimals ) ,
81- rpcUrls = new string [ ] { $ "https://{ chainInfo . ChainId } .rpc.thirdweb.com" } ,
82- blockExplorerUrls = chainInfo . Explorers == null || chainInfo . Explorers . Count == 0 ? null : new string [ ] { chainInfo . Explorers [ 0 ] . Url } ,
83- chainIdDecimal = chainInfo . ChainId . ToString ( ) ,
84- } ;
85- await WalletConnect . Instance . SwitchEthereumChainAsync ( wcChainInfo ) ;
80+ var data = new WalletSwitchEthereumChain ( new HexBigInteger ( initialChainId ) . HexValue ) ;
81+ await WalletConnect . Instance . RequestAsync < WalletSwitchEthereumChain , string > ( data ) ;
82+ await Task . Delay ( 5000 ) ; // wait for chain switch to take effect
8683 await WalletConnect . Instance . SignClient . AddressProvider . SetDefaultChainIdAsync ( $ "eip155:{ initialChainId } ") ;
8784 }
8885 catch ( Exception e )
@@ -107,15 +104,26 @@ public async Task EnsureCorrectNetwork(BigInteger chainId)
107104 blockExplorerUrls = chainInfo . Explorers == null || chainInfo . Explorers . Count == 0 ? null : new string [ ] { chainInfo . Explorers [ 0 ] . Url } ,
108105 chainIdDecimal = chainInfo . ChainId . ToString ( ) ,
109106 } ;
110- await WalletConnect . Instance . SwitchEthereumChainAsync ( wcChainInfo ) ;
107+ var request = new WalletAddEthereumChain ( wcChainInfo ) ;
108+ try
109+ {
110+ await WalletConnect . Instance . RequestAsync < WalletAddEthereumChain , string > ( request ) ;
111+ }
112+ catch
113+ {
114+ // no-op
115+ }
116+ var data = new WalletSwitchEthereumChain ( new HexBigInteger ( chainId ) . HexValue ) ;
117+ await WalletConnect . Instance . RequestAsync < WalletSwitchEthereumChain , string > ( data ) ;
118+ await Task . Delay ( 5000 ) ; // wait for chain switch to take effect
111119 await WalletConnect . Instance . SignClient . AddressProvider . SetDefaultChainIdAsync ( $ "eip155:{ chainId } ") ;
112120 }
113121
114122 #region IThirdwebWallet
115123
116124 public Task < string > GetAddress ( )
117125 {
118- return Task . FromResult ( WalletConnect . Instance . SignClient . AddressProvider . CurrentAddress ( ) . Address . ToChecksumAddress ( ) ) ;
126+ return Task . FromResult ( WalletConnect . Instance . SignClient . AddressProvider . CurrentAddress ( WalletConnect . Instance . ActiveChainId ) . Address . ToChecksumAddress ( ) ) ;
119127 }
120128
121129 public Task < string > EthSign ( byte [ ] rawMessage )
@@ -272,54 +280,19 @@ public Task<string> RecoverAddressFromTypedDataV4<T, TDomain>(T data, TypedData<
272280
273281 #region UI
274282
275- protected static void OnSessionDisconnected ( object sender , EventArgs e )
276- {
277- _isConnected = false ;
278- }
279-
280- protected static void OnActiveSessionChanged ( object sender , SessionStruct sessionStruct )
281- {
282- if ( ! string . IsNullOrEmpty ( sessionStruct . Topic ) )
283- {
284- _isConnected = true ;
285- }
286- else
287- {
288- _isConnected = false ;
289- }
290- }
291-
292- protected static async void OnReady ( object sender , ModalReadyEventArgs args )
293- {
294- try
295- {
296- if ( args . SessionResumed )
297- {
298- // Session exists
299- await WalletConnect . Instance . DisconnectAsync ( ) ;
300- }
301-
302- CreateNewSession ( ) ;
303- }
304- catch ( Exception e )
305- {
306- _exception = e ;
307- }
308- }
309-
310- protected static void CreateNewSession ( )
283+ protected static void CreateNewSession ( string [ ] supportedChains )
311284 {
312285 try
313286 {
314- var optionalNamespaces = new Dictionary < string , ProposedNamespace >
287+ var optionalNamespaces = new Dictionary < string , ProposedNamespace > ( )
315288 {
316289 {
317290 "eip155" ,
318291 new ProposedNamespace
319292 {
320293 Methods = new [ ] { "eth_sendTransaction" , "personal_sign" , "eth_signTypedData_v4" , "wallet_switchEthereumChain" , "wallet_addEthereumChain" } ,
321- Chains = _supportedChains ,
322- Events = new [ ] { "chainChanged" , "accountsChanged" } ,
294+ Chains = supportedChains ,
295+ Events = new string [ ] { "chainChanged" , "accountsChanged" } ,
323296 }
324297 }
325298 } ;
0 commit comments