From 2853058d8db940272bde2c629e7fb55244211cc6 Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Wed, 27 Nov 2024 07:46:39 +0700 Subject: [PATCH] fix modal closed edge case more consistent in webgl --- .../Unity/Wallets/Core/WalletConnectWallet.cs | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/Assets/Thirdweb/Runtime/Unity/Wallets/Core/WalletConnectWallet.cs b/Assets/Thirdweb/Runtime/Unity/Wallets/Core/WalletConnectWallet.cs index c1ee4e09..682db346 100644 --- a/Assets/Thirdweb/Runtime/Unity/Wallets/Core/WalletConnectWallet.cs +++ b/Assets/Thirdweb/Runtime/Unity/Wallets/Core/WalletConnectWallet.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Numerics; -using System.Text; using System.Threading.Tasks; using Nethereum.ABI.EIP712; using WalletConnectSharp.Sign.Models; @@ -60,6 +59,7 @@ public async static Task Create(ThirdwebClient client, BigI } CreateNewSession(eip155ChainsSupported); + WalletConnectModal.ModalClosed += OnModalClosed; while (!WalletConnect.Instance.IsConnected && _exception == null) { @@ -68,6 +68,7 @@ public async static Task Create(ThirdwebClient client, BigI if (_exception != null) { + WalletConnectModal.ModalClosed -= OnModalClosed; throw _exception; } else @@ -93,7 +94,7 @@ public async static Task Create(ThirdwebClient client, BigI return new WalletConnectWallet(client); } - public async Task EnsureCorrectNetwork(BigInteger chainId) + public async Task SwitchNetwork(BigInteger chainId) { var currentChainId = WalletConnect.Instance.ActiveChainId; if (currentChainId == $"eip155:{chainId}") @@ -125,6 +126,12 @@ public async Task EnsureCorrectNetwork(BigInteger chainId) await WalletConnect.Instance.SignClient.AddressProvider.SetDefaultChainIdAsync($"eip155:{chainId}"); } + [Obsolete("Use SwitchNetwork instead.")] + public Task EnsureCorrectNetwork(BigInteger chainId) + { + return SwitchNetwork(chainId); + } + #region IThirdwebWallet public Task GetAddress() @@ -254,6 +261,26 @@ public Task RecoverAddressFromTypedDataV4(T data, TypedData< throw new NotImplementedException(); } + public Task> LinkAccount( + IThirdwebWallet walletToLink, + string otp = null, + bool? isMobile = null, + Action browserOpenAction = null, + string mobileRedirectScheme = "thirdweb://", + IThirdwebBrowser browser = null, + BigInteger? chainId = null, + string jwt = null, + string payload = null + ) + { + throw new InvalidOperationException("LinkAccount is not supported by external wallets."); + } + + public Task> GetLinkedAccounts() + { + throw new InvalidOperationException("GetLinkedAccounts is not supported by external wallets."); + } + #endregion #region UI @@ -276,13 +303,7 @@ protected static void CreateNewSession(string[] supportedChains) }; var connectOptions = new ConnectOptions { OptionalNamespaces = optionalNamespaces, }; - - // Open modal WalletConnectModal.Open(new WalletConnectModalOptions { ConnectOptions = connectOptions, IncludedWalletIds = _includedWalletIds }); - WalletConnectModal.ModalClosed += (sender, e) => - { - _exception = new Exception("WalletConnect modal was closed."); - }; } catch (Exception e) { @@ -290,6 +311,14 @@ protected static void CreateNewSession(string[] supportedChains) } } + protected static void OnModalClosed(object sender, EventArgs e) + { + if (!WalletConnect.Instance.IsConnected) + { + _exception = new Exception("WalletConnect modal was closed."); + } + } + #endregion private void SessionRequestDeeplink() @@ -299,25 +328,5 @@ private void SessionRequestDeeplink() WalletConnect.Instance.Linker.OpenSessionRequestDeepLinkAfterMessageFromSession(activeSessionTopic); #endif } - - public Task> LinkAccount( - IThirdwebWallet walletToLink, - string otp = null, - bool? isMobile = null, - Action browserOpenAction = null, - string mobileRedirectScheme = "thirdweb://", - IThirdwebBrowser browser = null, - BigInteger? chainId = null, - string jwt = null, - string payload = null - ) - { - throw new InvalidOperationException("LinkAccount is not supported by external wallets."); - } - - public Task> GetLinkedAccounts() - { - throw new InvalidOperationException("GetLinkedAccounts is not supported by external wallets."); - } } }