15
15
using Nethereum . RPC . Eth . DTOs ;
16
16
using WalletConnectUnity . Core . Evm ;
17
17
using Nethereum . Hex . HexTypes ;
18
+ using UnityEngine ;
18
19
19
20
namespace Thirdweb . Unity
20
21
{
@@ -48,22 +49,26 @@ public async static Task<WalletConnectWallet> Create(ThirdwebClient client, BigI
48
49
_isConnected = false ;
49
50
_supportedChains = eip155ChainsSupported ;
50
51
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
+ }
54
64
55
- if ( WalletConnect . Instance . IsInitialized )
56
- CreateNewSession ( ) ;
65
+ CreateNewSession ( eip155ChainsSupported ) ;
57
66
58
- while ( ! _isConnected && _exception == null )
67
+ while ( ! WalletConnect . Instance . IsConnected && _exception == null )
59
68
{
60
69
await Task . Delay ( 100 ) ;
61
70
}
62
71
63
- WalletConnectModal . Ready -= OnReady ;
64
- WalletConnect . Instance . ActiveSessionChanged -= OnActiveSessionChanged ;
65
- WalletConnect . Instance . SessionDisconnected -= OnSessionDisconnected ;
66
-
67
72
if ( _exception != null )
68
73
{
69
74
throw _exception ;
@@ -72,17 +77,9 @@ public async static Task<WalletConnectWallet> Create(ThirdwebClient client, BigI
72
77
{
73
78
try
74
79
{
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
86
83
await WalletConnect . Instance . SignClient . AddressProvider . SetDefaultChainIdAsync ( $ "eip155:{ initialChainId } ") ;
87
84
}
88
85
catch ( Exception e )
@@ -107,15 +104,26 @@ public async Task EnsureCorrectNetwork(BigInteger chainId)
107
104
blockExplorerUrls = chainInfo . Explorers == null || chainInfo . Explorers . Count == 0 ? null : new string [ ] { chainInfo . Explorers [ 0 ] . Url } ,
108
105
chainIdDecimal = chainInfo . ChainId . ToString ( ) ,
109
106
} ;
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
111
119
await WalletConnect . Instance . SignClient . AddressProvider . SetDefaultChainIdAsync ( $ "eip155:{ chainId } ") ;
112
120
}
113
121
114
122
#region IThirdwebWallet
115
123
116
124
public Task < string > GetAddress ( )
117
125
{
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 ( ) ) ;
119
127
}
120
128
121
129
public Task < string > EthSign ( byte [ ] rawMessage )
@@ -272,54 +280,19 @@ public Task<string> RecoverAddressFromTypedDataV4<T, TDomain>(T data, TypedData<
272
280
273
281
#region UI
274
282
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 )
311
284
{
312
285
try
313
286
{
314
- var optionalNamespaces = new Dictionary < string , ProposedNamespace >
287
+ var optionalNamespaces = new Dictionary < string , ProposedNamespace > ( )
315
288
{
316
289
{
317
290
"eip155" ,
318
291
new ProposedNamespace
319
292
{
320
293
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" } ,
323
296
}
324
297
}
325
298
} ;
0 commit comments