Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet_addEthereumChain doesn't get redirect to Metamask #227

Open
ammarvohra opened this issue May 7, 2024 · 16 comments
Open

wallet_addEthereumChain doesn't get redirect to Metamask #227

ammarvohra opened this issue May 7, 2024 · 16 comments
Assignees
Labels
bug Something isn't working

Comments

@ammarvohra
Copy link

Describe the bug
Some of my users has this issue that when they try to add chain network, they are not taken back to Metamask from my Game, instead it throws error:

Fatal Exception: java.lang.Exception: WalletConnectException : No matching key with tag: {tag}
       at WalletConnectSharp.Sign.Engine.IsValidSessionTopic(WalletConnectSharp.Sign.Engine)
       at WalletConnectSharp.Sign.Engine.Request[T,TR](WalletConnectSharp.Sign.Engine)
       at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine](System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult])
       at WalletConnectSharp.Sign.Engine.Request[T,TR](WalletConnectSharp.Sign.Engine)
       at WalletConnectSharp.Sign.WalletConnectSignClient.Request[T,TR](WalletConnectSharp.Sign.WalletConnectSignClient)
       at WalletConnectUnity.Core.WalletConnect.RequestAsync[TRequestData,TResponse](WalletConnectUnity.Core.WalletConnect)

To Reproduce
Steps to reproduce the behavior:

  1. I connect metamask and Unity Game on Android
  2. I came back to my game after authorization and then I click Unity button to add new network on Metamask
  3. It doesn't take me back to Metamask application

Expected behavior
It should take me to Metamask application where a popup appears to add new network chain

Screenshots

Environment

  • OS: Build on Windows 10 game running on Android 10
  • Unity version 2022.3.20
  • WalletConnectUnity Modal v1.1.5

Is this happening in Unity Editor or on a player like Android, iOS, Windows?
Android 10

Wallets used for testing
Metamask

Additional context
None

@skibitsky
Copy link
Member

Hey @ammarvohra, could you please upgrade to v1.1.6 and let me know if the issue persists?

@ammarvohra
Copy link
Author

Sure, I'll do it and will update you.

Thanks

@ammarvohra
Copy link
Author

@skibitsky still no luck of working, even worse, in this version sometimes users are not getting back to the game after connection is successful. They need to comeback to the game close the Modal view and open and do connection again

@skibitsky
Copy link
Member

skibitsky commented May 15, 2024

@ammarvohra

in this version sometimes users are not getting back to the game after connection is successful. They need to comeback to the game close the Modal view and open and do connection again

Could you please describe the exact steps so I could reproduce them? What version of MetaMask do you have?

still no luck of working,

Do you still get WalletConnectException : No matching key with tag: {tag}?

@ammarvohra
Copy link
Author

ammarvohra commented May 15, 2024

Could you please describe the exact steps so I could reproduce them? What version of MetaMask do you have?

Opened my game -> Clicked on button to connect the Metamask -> Metamask opened -> Popup comes to connect the wallet -> Connect pressed -> Doesn't redirect to my game -> Opened my game from app stack -> Closed the WalletConnectModal -> Clicked on connect -> Metamask opens up with connection popup -> Clicked on Connect -> Redirected back to my game.

Do you still get WalletConnectException : No matching key with tag: {tag}?`

Yes if Metamask is not opening after the connection this error is thrown, and if Metamask get's open it doesn't throw error but there is also no Popup for adding new Chain Network.

@skibitsky
Copy link
Member

I haven't changed anything related to the redirection in the last release; that may be an issue on the MetaMask side.

... Opened my game from app stack -> Closed the WalletConnectModal -> Clicked on connect -> ...

What happens if you keep the modal open for 5-8 seconds after you open the game instead of closing it immediately?

@ammarvohra
Copy link
Author

I haven't changed anything related to the redirection in the last release; that may be an issue on the MetaMask side.

Idk but what I noticed was this that 1.1.4 was having some similar issue, while 1.1.5 was smooth and 1.1.6 got that issue.

What happens if you keep the modal open for 5-8 seconds after you open the game instead of closing it immediately?

Doesn't do anything just keep saying trying to connect or try again.

But the thing I couldn't understand is some people can open the Metamask when they click on Add chain network, but even after waiting for some 10-15 seconds on Metamask the option/popup to add chain doesn't come up.

@skibitsky
Copy link
Member

But the thing I couldn't understand is some people can open the Metamask when they click on Add chain network, but even after waiting for some 10-15 seconds on Metamask the option/popup to add chain doesn't come up.

MetaMask fixed that in the latest release

@ammarvohra
Copy link
Author

ammarvohra commented May 16, 2024

User's phone configuration:
Brand: Samsung
Model: Galaxy S20 5G
Android Version: Android 13
Metamask version: 7.21.0
Location: Europe/London

Still no luck

@skibitsky
Copy link
Member

skibitsky commented May 16, 2024

User's phone configuration:
Brand: Samsung
Model: Galaxy S20 5G
Android Version: Android 13
Metamask version: 7.21.0
Location: Europe/London

A few more questions 🙏

  1. Just to make sure I got it right. This user gets WalletConnectException : No matching key with tag: {tag} when sending wallet_addEthereumChain?
  2. Do they have WalletConnectUnity Modal v1.1.6 or older version?
  3. In your game, how exactly do you resume session after restart? Do you use WalletConnect.Instance.TryResumeSessionAsync() directly or have ResumeSessionOnInit option enabled in the WalletConnectModal component? Or do you use another approach?
  4. Have they tried to reinstall the game? Does the issue persist?
  5. Have they tried to disconnect wallet and connect again? Does the issue persist?

@ammarvohra
Copy link
Author

Just to make sure I got it right. This user gets WalletConnectException : No matching key with tag: {tag} when sending wallet_addEthereumChain?

No this uesr doesn't get exception as when clicking on Add chain it take him back to Metamask but Metamask does nothing

Do they have WalletConnectUnity Modal v1.1.6 or older version?

1.1.6

In your game, how exactly do you resume session after restart? Do you use WalletConnect.Instance.TryResumeSessionAsync() directly or have ResumeSessionOnInit option enabled in the WalletConnectModal component? Or do you use another approach?

ResumeSessionOnInit option is enabled in the WalletConnectModal

Have they tried to reinstall the game? Does the issue persist?

Yeap, same issue

Have they tried to disconnect wallet and connect again? Does the issue persist?

Yeap they reinstall the application everytime, so the previous session get's expired so they need to make a new connection

@skibitsky
Copy link
Member

Okay, it's good that they don't get the exception.

Could you please share a code snippet demonstrating how you detect the need to add a new chain to MetaMask and how you add and switch the chain?

@ammarvohra
Copy link
Author

Here is the code:

public async void OnExternalWalletConnected()
{
    while (!WalletConnect.Instance.IsConnected)
        await Task.Delay(25);

    var chainNew = new WalletConnectUnity.Core.Evm.EthereumChain
    {
        chainIdHex = "0x" + web3Params.ChainID.ToString("X").ToLower(),
        blockExplorerUrls = new string[] { web3Params.BlockExplorerURL },
        name = web3Params.NetworkName,
        nativeCurrency = new Currency(web3Params.CurrencySymbol, web3Params.CurrencySymbol, 18),
        rpcUrls = new string[] { web3Params.NetworkURL }
    };
    WalletConnectUnity.Core.Evm.WalletAddEthereumChain walletAddEthereumChain = new WalletConnectUnity.Core.Evm.WalletAddEthereumChain
    {
        chainNew
    };
    var connectNetwork = await WalletConnect.Instance.RequestAsync<WalletConnectUnity.Core.Evm.WalletAddEthereumChain, string>(walletAddEthereumChain);
    Debug.LogError(connectNetwork);

}

@deveshbeniwal
Copy link

deveshbeniwal commented May 29, 2024

Same issue! I was trying to switching the chain after user successfully connected to session. @skibitsky @ammarvohra
GOT THE ERROR - User disapproved requested chains

Below is my function code to change the chain -

    private async Task CheckAndChange_ChainAsync()
    {
        string activeChain = WalletConnect.Instance.ActiveChainId;
        Debug.Log("Active chain: " + activeChain);

        Chain to_add_chain = ChainConstants.Chains.Arbitrum;
        try
        {
            Debug.Log("Switching chain...");
            await WalletConnect.Instance.SwitchEthereumChainAsync(new EthereumChain(to_add_chain));

            Debug.Log("Chain switched!");
        }
        catch (System.Exception e)
        {
            Debug.Log("error: " + e.Message);
        }
    }

Below is the code for extention method provided by WalletConnectUnity.Core.Evm i.e SwitchEthereumChainAsync

        public static async Task SwitchEthereumChainAsync(this IWalletConnect walletConnect, EthereumChain ethereumChain)
        {
            if (ethereumChain == null)
                throw new ArgumentNullException(nameof(ethereumChain));

            var ciap2ChainId = $"eip155:{ethereumChain.chainIdDecimal}";
            if (!walletConnect.ActiveSession.Namespaces.TryGetValue("eip155", out var @namespace)
                || !@namespace.Chains.Contains(ciap2ChainId))
            {
                var request = new WalletAddEthereumChain(ethereumChain);
                try
                {
                    await walletConnect.RequestAsync<WalletAddEthereumChain, string>(request);
                }
                catch (WalletConnectException e)
                {
                    Debug.Log("Error inside: " + e.Message);
                }
            }

            var data = new WalletSwitchEthereumChain(ethereumChain.chainIdHex);
            await walletConnect.RequestAsync<WalletSwitchEthereumChain, string>(data);
        }

and below is my namespace from which i am creating the session

var requiredNamespaces = new RequiredNamespaces
        {
            {
                "eip155", new ProposedNamespace
                {
                    Methods = new[]
                    {
                        "eth_sendTransaction",
                        "eth_signTransaction",
                        "eth_sign",
                        "personal_sign",
                        "eth_signTypedData",
                        "wallet_addEthereumChain",
                        "wallet_switchEthereumChain"
                    },
                    Chains = new[]
                    {
                        "eip155:42161"
                    },
                    Events = new[]
                    {
                        "chainChanged",
                        "accountsChanged"
                    }
                }
            }
        };

@skibitsky
Copy link
Member

@deveshbeniwal could you please open a new issue with the bug report template? The error message you're getting is different than the one reported here.

@deveshbeniwal
Copy link

@deveshbeniwal could you please open a new issue with the bug report template? The error message you're getting is different than the one reported here.

Added - #232 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants