@@ -32,34 +32,37 @@ public static async Task<MetaMaskWallet> Create(ThirdwebClient client, BigIntege
32
32
}
33
33
34
34
var metaMaskInstance = WebGLMetaMask . Instance ;
35
+ var mmWallet = new MetaMaskWallet ( ) ;
35
36
36
37
if ( metaMaskInstance . IsConnected ( ) && ! string . IsNullOrEmpty ( metaMaskInstance . GetAddress ( ) ) )
37
38
{
38
39
ThirdwebDebug . Log ( "MetaMask already initialized." ) ;
39
- await EnsureCorrectNetwork ( activeChainId ) ;
40
- return new MetaMaskWallet ( ) ;
40
+ await mmWallet . SwitchNetwork ( activeChainId ) ;
41
+ return mmWallet ;
41
42
}
42
-
43
- if ( metaMaskInstance . IsMetaMaskAvailable ( ) )
43
+ else
44
44
{
45
- ThirdwebDebug . Log ( "MetaMask is available. Enabling Ethereum..." ) ;
46
- var isEnabled = await metaMaskInstance . EnableEthereumAsync ( ) ;
47
- ThirdwebDebug . Log ( $ "Ethereum enabled: { isEnabled } ") ;
48
- if ( isEnabled && ! string . IsNullOrEmpty ( metaMaskInstance . GetAddress ( ) ) )
45
+ if ( metaMaskInstance . IsMetaMaskAvailable ( ) )
49
46
{
50
- ThirdwebDebug . Log ( "MetaMask initialized successfully." ) ;
51
- await EnsureCorrectNetwork ( activeChainId ) ;
52
- return new MetaMaskWallet ( ) ;
47
+ ThirdwebDebug . Log ( "MetaMask is available. Enabling Ethereum..." ) ;
48
+ var isEnabled = await metaMaskInstance . EnableEthereumAsync ( ) ;
49
+ ThirdwebDebug . Log ( $ "Ethereum enabled: { isEnabled } ") ;
50
+ if ( isEnabled && ! string . IsNullOrEmpty ( metaMaskInstance . GetAddress ( ) ) )
51
+ {
52
+ ThirdwebDebug . Log ( "MetaMask initialized successfully." ) ;
53
+ await mmWallet . SwitchNetwork ( activeChainId ) ;
54
+ return mmWallet ;
55
+ }
56
+ else
57
+ {
58
+ throw new Exception ( "MetaMask initialization failed or address is empty." ) ;
59
+ }
53
60
}
54
61
else
55
62
{
56
- throw new Exception ( "MetaMask initialization failed or address is empty ." ) ;
63
+ throw new Exception ( "MetaMask is not available ." ) ;
57
64
}
58
65
}
59
- else
60
- {
61
- throw new Exception ( "MetaMask is not available." ) ;
62
- }
63
66
}
64
67
65
68
#region IThirdwebWallet
@@ -214,38 +217,76 @@ public Task<List<LinkedAccount>> GetLinkedAccounts()
214
217
throw new InvalidOperationException ( "GetLinkedAccounts is not supported by external wallets." ) ;
215
218
}
216
219
220
+ public Task < List < LinkedAccount > > UnlinkAccount ( LinkedAccount accountToUnlink )
221
+ {
222
+ throw new InvalidOperationException ( "UnlinkAccount is not supported by external wallets." ) ;
223
+ }
224
+
225
+ public Task < EIP7702Authorization > SignAuthorization ( BigInteger chainId , string contractAddress , bool willSelfExecute )
226
+ {
227
+ throw new InvalidOperationException ( "SignAuthorization is not supported by external wallets." ) ;
228
+ }
229
+
230
+ public async Task SwitchNetwork ( BigInteger chainId )
231
+ {
232
+ if ( WebGLMetaMask . Instance . GetActiveChainId ( ) != chainId )
233
+ {
234
+ try
235
+ {
236
+ await SwitchNetworkInternal ( chainId ) ;
237
+ }
238
+ catch
239
+ {
240
+ await AddNetworkInternal ( chainId ) ;
241
+ if ( WebGLMetaMask . Instance . GetActiveChainId ( ) == chainId )
242
+ {
243
+ return ;
244
+ }
245
+ try
246
+ {
247
+ await SwitchNetworkInternal ( chainId ) ;
248
+ }
249
+ catch
250
+ {
251
+ // no-op, later metamask extension versions do not necessarily require switching post adding
252
+ }
253
+ }
254
+ }
255
+ }
256
+
217
257
#endregion
218
258
219
259
#region Network Switching
220
260
261
+ [ Obsolete ( "Use IThirdwebWallet.SwitchNetwork instead." ) ]
221
262
public static async Task EnsureCorrectNetwork ( BigInteger chainId )
222
263
{
223
264
if ( WebGLMetaMask . Instance . GetActiveChainId ( ) != chainId )
224
265
{
225
266
try
226
267
{
227
- await SwitchNetwork ( chainId ) ;
268
+ await SwitchNetworkInternal ( chainId ) ;
228
269
}
229
270
catch
230
271
{
231
- await AddNetwork ( chainId ) ;
272
+ await AddNetworkInternal ( chainId ) ;
232
273
if ( WebGLMetaMask . Instance . GetActiveChainId ( ) == chainId )
233
274
{
234
275
return ;
235
276
}
236
- await SwitchNetwork ( chainId ) ;
277
+ await SwitchNetworkInternal ( chainId ) ;
237
278
}
238
279
}
239
280
}
240
281
241
- private static async Task SwitchNetwork ( BigInteger chainId )
282
+ private static async Task SwitchNetworkInternal ( BigInteger chainId )
242
283
{
243
284
var switchEthereumChainParameter = new SwitchEthereumChainParameter { ChainId = new HexBigInteger ( chainId ) } ;
244
285
var rpcRequest = new RpcRequest { Method = "wallet_switchEthereumChain" , Params = new object [ ] { switchEthereumChainParameter } } ;
245
286
_ = await WebGLMetaMask . Instance . RequestAsync < string > ( rpcRequest ) ;
246
287
}
247
288
248
- private static async Task AddNetwork ( BigInteger chainId )
289
+ private static async Task AddNetworkInternal ( BigInteger chainId )
249
290
{
250
291
ThirdwebDebug . Log ( $ "Fetching chain data for chainId { chainId } ...") ;
251
292
var twChainData = await Utils . GetChainMetadata ( _client , chainId ) ?? throw new Exception ( $ "Chain data for chainId { chainId } could not be fetched.") ;
0 commit comments