MetaMask / metamask-sdk

The simplest yet most secure way to connect your blockchain-based applications to millions of MetaMask Wallet users.
https://metamask.io/sdk/
Other
148 stars 95 forks source link

[Bug]: Exception Clears Wallet Addresses #893

Closed WoodsFiend closed 1 week ago

WoodsFiend commented 1 month ago

SDK

Unity

Provide environment information

Unity version 2022.3 Unity Editor Windows OS

MetaMask SDK Version

2.0.0

MetaMask Mobile app Version

N/A

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

When an exception is thrown inside a function listening to Wallet.AccountChanged the SelectedAddress, ConnectedAddress, and ConnectedAddresses are cleared. This causes all contract interaction functionality to fail.

Expected Behavior

Exceptions inside listeners should not clear all addresses, or it should at least throw a warning/error internally so that it is know to have done so.

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

No response

To Reproduce

public class MetaMaskExceptionTest: MonoBehaviour
{
        protected void Start()
        {
            if (MetaMaskSDK.Instance != null)
            {
                //Add listeners
                MetaMaskSDK.Instance.Initialize();
                MetaMaskSDK.Instance.Wallet.AccountChanged += OnMetamaskAccountChanged;
            }
        }

        private void OnMetamaskAccountChanged(object sender, EventArgs e)
        {
            // Logs the expected wallet address that connected
            Debug.Log("Wallet Address: " + MetaMaskSDK.Instance.Wallet.SelectedAddress);
            throw new Exception("Test Exception");
            // The SelectedAddress, ConnectedAddress, and ConnectedAddresses are now cleared
            // Contract interactions will now fail if they use the above params (ex. ERC20.Allowance)
        }
        private void Update()
        {
            if(MetaMaskSDK.Instance != null && MetaMaskSDK.Instance.Wallet != null)
            {
                Debug.Log("Selected Address: " + MetaMaskSDK.Instance.Wallet.SelectedAddress);
                Debug.Log("Connected Address: " + MetaMaskSDK.Instance.Wallet.ConnectedAddress);
                Debug.Log("Connected Addresses Count: " + MetaMaskSDK.Instance.Wallet.ConnectedAddresses.Length);
            }
        }
}
  1. Connect your wallet with the above script included
  2. Wallet Address logs as expected before exception
  3. SelectedAddress, ConnectedAddress, ConnectedAddresses are empty after the exception
christopherferreira9 commented 2 weeks ago

Hi @WoodsFiend this has been fixed on the upcoming release that should be released soon.

christopherferreira9 commented 1 week ago

Hi! This should be fixed with the latest release 2.0.1.