Skip to content

Commit

Permalink
.NET 2.12.0 // Unlinking, 7702, Auth, Switch, Burn
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Dec 25, 2024
1 parent e79c916 commit e8c282d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
Binary file modified Assets/Thirdweb/Runtime/NET/Thirdweb.dll
Binary file not shown.
83 changes: 62 additions & 21 deletions Assets/Thirdweb/Runtime/Unity/Wallets/Core/MetaMaskWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,37 @@ public static async Task<MetaMaskWallet> Create(ThirdwebClient client, BigIntege
}

var metaMaskInstance = WebGLMetaMask.Instance;
var mmWallet = new MetaMaskWallet();

if (metaMaskInstance.IsConnected() && !string.IsNullOrEmpty(metaMaskInstance.GetAddress()))
{
ThirdwebDebug.Log("MetaMask already initialized.");
await EnsureCorrectNetwork(activeChainId);
return new MetaMaskWallet();
await mmWallet.SwitchNetwork(activeChainId);
return mmWallet;
}

if (metaMaskInstance.IsMetaMaskAvailable())
else
{
ThirdwebDebug.Log("MetaMask is available. Enabling Ethereum...");
var isEnabled = await metaMaskInstance.EnableEthereumAsync();
ThirdwebDebug.Log($"Ethereum enabled: {isEnabled}");
if (isEnabled && !string.IsNullOrEmpty(metaMaskInstance.GetAddress()))
if (metaMaskInstance.IsMetaMaskAvailable())
{
ThirdwebDebug.Log("MetaMask initialized successfully.");
await EnsureCorrectNetwork(activeChainId);
return new MetaMaskWallet();
ThirdwebDebug.Log("MetaMask is available. Enabling Ethereum...");
var isEnabled = await metaMaskInstance.EnableEthereumAsync();
ThirdwebDebug.Log($"Ethereum enabled: {isEnabled}");
if (isEnabled && !string.IsNullOrEmpty(metaMaskInstance.GetAddress()))
{
ThirdwebDebug.Log("MetaMask initialized successfully.");
await mmWallet.SwitchNetwork(activeChainId);
return mmWallet;
}
else
{
throw new Exception("MetaMask initialization failed or address is empty.");
}
}
else
{
throw new Exception("MetaMask initialization failed or address is empty.");
throw new Exception("MetaMask is not available.");
}
}
else
{
throw new Exception("MetaMask is not available.");
}
}

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

public Task<List<LinkedAccount>> UnlinkAccount(LinkedAccount accountToUnlink)
{
throw new InvalidOperationException("UnlinkAccount is not supported by external wallets.");
}

public Task<EIP7702Authorization> SignAuthorization(BigInteger chainId, string contractAddress, bool willSelfExecute)
{
throw new InvalidOperationException("SignAuthorization is not supported by external wallets.");
}

public async Task SwitchNetwork(BigInteger chainId)
{
if (WebGLMetaMask.Instance.GetActiveChainId() != chainId)
{
try
{
await SwitchNetworkInternal(chainId);
}
catch
{
await AddNetworkInternal(chainId);
if (WebGLMetaMask.Instance.GetActiveChainId() == chainId)
{
return;
}
try
{
await SwitchNetworkInternal(chainId);
}
catch
{
// no-op, later metamask extension versions do not necessarily require switching post adding
}
}
}
}

#endregion

#region Network Switching

[Obsolete("Use IThirdwebWallet.SwitchNetwork instead.")]
public static async Task EnsureCorrectNetwork(BigInteger chainId)
{
if (WebGLMetaMask.Instance.GetActiveChainId() != chainId)
{
try
{
await SwitchNetwork(chainId);
await SwitchNetworkInternal(chainId);
}
catch
{
await AddNetwork(chainId);
await AddNetworkInternal(chainId);
if (WebGLMetaMask.Instance.GetActiveChainId() == chainId)
{
return;
}
await SwitchNetwork(chainId);
await SwitchNetworkInternal(chainId);
}
}
}

private static async Task SwitchNetwork(BigInteger chainId)
private static async Task SwitchNetworkInternal(BigInteger chainId)
{
var switchEthereumChainParameter = new SwitchEthereumChainParameter { ChainId = new HexBigInteger(chainId) };
var rpcRequest = new RpcRequest { Method = "wallet_switchEthereumChain", Params = new object[] { switchEthereumChainParameter } };
_ = await WebGLMetaMask.Instance.RequestAsync<string>(rpcRequest);
}

private static async Task AddNetwork(BigInteger chainId)
private static async Task AddNetworkInternal(BigInteger chainId)
{
ThirdwebDebug.Log($"Fetching chain data for chainId {chainId}...");
var twChainData = await Utils.GetChainMetadata(_client, chainId) ?? throw new Exception($"Chain data for chainId {chainId} could not be fetched.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public async Task SwitchNetwork(BigInteger chainId)
await WalletConnect.Instance.SignClient.AddressProvider.SetDefaultChainIdAsync($"eip155:{chainId}");
}

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

public Task<List<LinkedAccount>> UnlinkAccount(LinkedAccount accountToUnlink)
{
throw new InvalidOperationException("UnlinkAccount is not supported by external wallets.");
}

public Task<EIP7702Authorization> SignAuthorization(BigInteger chainId, string contractAddress, bool willSelfExecute)
{
throw new InvalidOperationException("SignAuthorization is not supported by external wallets.");
}

#endregion

#region UI
Expand Down

0 comments on commit e8c282d

Please sign in to comment.