Skip to content

Commit e8c282d

Browse files
committed
.NET 2.12.0 // Unlinking, 7702, Auth, Switch, Burn
1 parent e79c916 commit e8c282d

File tree

3 files changed

+73
-22
lines changed

3 files changed

+73
-22
lines changed
12 KB
Binary file not shown.

Assets/Thirdweb/Runtime/Unity/Wallets/Core/MetaMaskWallet.cs

+62-21
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,37 @@ public static async Task<MetaMaskWallet> Create(ThirdwebClient client, BigIntege
3232
}
3333

3434
var metaMaskInstance = WebGLMetaMask.Instance;
35+
var mmWallet = new MetaMaskWallet();
3536

3637
if (metaMaskInstance.IsConnected() && !string.IsNullOrEmpty(metaMaskInstance.GetAddress()))
3738
{
3839
ThirdwebDebug.Log("MetaMask already initialized.");
39-
await EnsureCorrectNetwork(activeChainId);
40-
return new MetaMaskWallet();
40+
await mmWallet.SwitchNetwork(activeChainId);
41+
return mmWallet;
4142
}
42-
43-
if (metaMaskInstance.IsMetaMaskAvailable())
43+
else
4444
{
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())
4946
{
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+
}
5360
}
5461
else
5562
{
56-
throw new Exception("MetaMask initialization failed or address is empty.");
63+
throw new Exception("MetaMask is not available.");
5764
}
5865
}
59-
else
60-
{
61-
throw new Exception("MetaMask is not available.");
62-
}
6366
}
6467

6568
#region IThirdwebWallet
@@ -214,38 +217,76 @@ public Task<List<LinkedAccount>> GetLinkedAccounts()
214217
throw new InvalidOperationException("GetLinkedAccounts is not supported by external wallets.");
215218
}
216219

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+
217257
#endregion
218258

219259
#region Network Switching
220260

261+
[Obsolete("Use IThirdwebWallet.SwitchNetwork instead.")]
221262
public static async Task EnsureCorrectNetwork(BigInteger chainId)
222263
{
223264
if (WebGLMetaMask.Instance.GetActiveChainId() != chainId)
224265
{
225266
try
226267
{
227-
await SwitchNetwork(chainId);
268+
await SwitchNetworkInternal(chainId);
228269
}
229270
catch
230271
{
231-
await AddNetwork(chainId);
272+
await AddNetworkInternal(chainId);
232273
if (WebGLMetaMask.Instance.GetActiveChainId() == chainId)
233274
{
234275
return;
235276
}
236-
await SwitchNetwork(chainId);
277+
await SwitchNetworkInternal(chainId);
237278
}
238279
}
239280
}
240281

241-
private static async Task SwitchNetwork(BigInteger chainId)
282+
private static async Task SwitchNetworkInternal(BigInteger chainId)
242283
{
243284
var switchEthereumChainParameter = new SwitchEthereumChainParameter { ChainId = new HexBigInteger(chainId) };
244285
var rpcRequest = new RpcRequest { Method = "wallet_switchEthereumChain", Params = new object[] { switchEthereumChainParameter } };
245286
_ = await WebGLMetaMask.Instance.RequestAsync<string>(rpcRequest);
246287
}
247288

248-
private static async Task AddNetwork(BigInteger chainId)
289+
private static async Task AddNetworkInternal(BigInteger chainId)
249290
{
250291
ThirdwebDebug.Log($"Fetching chain data for chainId {chainId}...");
251292
var twChainData = await Utils.GetChainMetadata(_client, chainId) ?? throw new Exception($"Chain data for chainId {chainId} could not be fetched.");

Assets/Thirdweb/Runtime/Unity/Wallets/Core/WalletConnectWallet.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public async Task SwitchNetwork(BigInteger chainId)
126126
await WalletConnect.Instance.SignClient.AddressProvider.SetDefaultChainIdAsync($"eip155:{chainId}");
127127
}
128128

129-
[Obsolete("Use SwitchNetwork instead.")]
129+
[Obsolete("Use IThirdwebWallet.SwitchNetwork instead.")]
130130
public Task EnsureCorrectNetwork(BigInteger chainId)
131131
{
132132
return SwitchNetwork(chainId);
@@ -281,6 +281,16 @@ public Task<List<LinkedAccount>> GetLinkedAccounts()
281281
throw new InvalidOperationException("GetLinkedAccounts is not supported by external wallets.");
282282
}
283283

284+
public Task<List<LinkedAccount>> UnlinkAccount(LinkedAccount accountToUnlink)
285+
{
286+
throw new InvalidOperationException("UnlinkAccount is not supported by external wallets.");
287+
}
288+
289+
public Task<EIP7702Authorization> SignAuthorization(BigInteger chainId, string contractAddress, bool willSelfExecute)
290+
{
291+
throw new InvalidOperationException("SignAuthorization is not supported by external wallets.");
292+
}
293+
284294
#endregion
285295

286296
#region UI

0 commit comments

Comments
 (0)