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
165 stars 101 forks source link

Unable to getAddress from ethers Signer after connecting to MM #266

Closed iketw closed 11 months ago

iketw commented 1 year ago

Describe the bug Using the reactNativeDemo app I connect to MM 7.3.1. I then create an ethers Web3Provider and try to get the address from the signer. I get a warning saying MM is not connected/installed, please call eth_getRequestAccounts to connect first

To Reproduce Steps to reproduce the behaviour:

  1. Run the reactNativeDemo app
  2. Click on Connect and connect to MM
  3. In the sign button, remove the onPress code and the following code:

    const prov = new ethers.providers.Web3Provider(
      ethereum as unknown as providers.ExternalProvider,
    );
    
    const signer = prov.getSigner();
    
    const addr = await signer.getAddress();
    console.log('addr', addr);
  4. See error. The await signer.getAddress() hangs indefinitely.

Expected behaviour The address should be returned since the provider is already connected.

Provide environment information

christopherferreira9 commented 1 year ago

Hi @iketw ! Can you please share the piece of code where you initialise the SDK?

iketw commented 1 year ago

Hi @iketw ! Can you please share the piece of code where you initialise the SDK?

I just ran the code from the reactNativeDemo app, so this:

new MetaMaskSDK({
  openDeeplink: (link: string) => {
    if (canOpenLink) {
      Linking.openURL(link);
    }
  },
  timer: BackgroundTimer,

  dappMetadata: {
    url: 'ReactNativeTS',
    name: 'ReactNativeTS',
  },
});
christopherferreira9 commented 1 year ago

Hi @iketw ! Does this issue happens in both iOS and Android?

iketw commented 1 year ago

Hi @iketw ! Does this issue happens in both iOS and Android?

Just tried on iOS and it works, the issue is only on Android.

On Android I see this in the logs:

DEBUG  RCPMS::_write method eth_accounts doesn't need redirect.
DEBUG  RCPMS::_write method eth_chainId doesn't need redirect.

and finally this:

Error rpcId=4120858626 metamask_getProviderState [Error: RPC 4120858626 timed out]
WARN  Error rpcId=4120858627 eth_accounts [Error: RPC 4120858627 timed out]
WARN  Error rpcId=4120858628 eth_chainId [Error: RPC 4120858628 timed out]

Wallet version: iOS: 7.4.0 (1159) Android: 7.4.0 (1159)

christopherferreira9 commented 1 year ago

Hi @iketw ! We are aware of this issue and we have been working on this. I'll update this issue once we are able to release a fix for it.

andreahaku commented 1 year ago

Hi @iketw the fix is not yet ready and tested. Thanks for reporting.

abretonc7s commented 11 months ago

Hi @iketw , the issue is that getAddress is calling eth_accounts which requires the wallet to be 'online'. On react native, it automatically shuts the connection after 30 seconds so instead you should use eth_requestAccounts which will automatically deeplinks into metamask and wake up the connection. We are working on ways to prevent this situation with read-only calls on the latest sdk 0.7.1 which should help you.

christopherferreira9 commented 11 months ago

Hi @iketw ! This has been fixed on 0.7.1. I will close this issue but feel free to open a new one if you still encounter issues with it.