decentraland / decentraland-connect

Connect your Ethereum wallet from different providers
6 stars 12 forks source link

fix: update chainId if provider type is injected and chainId changed since previous connection #38

Closed cazala closed 2 years ago

cazala commented 2 years ago

Currently if you conenct using an INJECTED provider like MetaMask, the chainId passed to the connection.connect(type, chainId) method is stored in localStorage. The chainId used there is the app's chainId (the chainId the dapp is expecting the user to use). If the app chain is changed, this will be impacted when using the connection.connect(...) method again, but when using the tryPreviousConnection method, the app chainId is ignored, and the one from localStorage is used. This chainId is different to the one the dapp is expecting the user to use, but the connection still succeeds since the INJECTED type doesn't use the chainId parameter at all. The problem is that after that successful connection, the chainId that is left in localStorage is wrong, and it will stay like that until a new connection.connect(...) is called with the right data.

This PR fixes the issue by updating the chainId in localStorage after a successful tryPreviousConnection, and also return the current chainId in the ConnectionResponse. This is only done for INJECTED provider types since those are the only ones that don't fail on a wrong chainId.