Skip to content

Commit

Permalink
fix: switch chain with walletconnect (#10394)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**
MetaMask calls updateSession multiple times when the user tries to
switch chain from the dapp. This causes the dapp to receive a
`chainChanged` event before receiving the updated session from the
wallet.


* Compare addresses in lowercase
* Parse chainId correctly
* Clearing state variables of BackgroundBridge when the session is
disconnected
* Bumped walletconnect sdk versions to latest

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to https://lab.web3modal.com/library/ethers/ with an iPhone
2. Press the "Connect Wallet" button and select "Metamask"
3. Press "Open" to open MetaMask (if applies)
4. Accept the session proposal
5. Go back to the browser
6. Switch chain to Polygon (chain must be Already added as know network
in Metamask)
7. Go to MetaMask and accept the switch popup
8. Go back to the browser and see there is a new redirect popup, which
causes issues in the Wallet. The popup shouldn't be there

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**


https://github.com/user-attachments/assets/4a0b4a1c-6496-4e2c-8e03-d04a51df2ce3

### **After**


https://github.com/user-attachments/assets/7441400e-6dfe-4595-a608-2eec30b1c478

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: Andrea Salvatore <andrea.salvatore@consensys.net>
  • Loading branch information
ignaciosantise and andreahaku authored Aug 14, 2024
1 parent 2df9eb5 commit c317aba
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 105 deletions.
7 changes: 5 additions & 2 deletions app/core/BackgroundBridge/BackgroundBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,12 @@ export class BackgroundBridge extends EventEmitter {
this.networkVersionSent = publicState.networkVersion;
this.notifyChainChanged(publicState);
}

// ONLY NEEDED FOR WC FOR NOW, THE BROWSER HANDLES THIS NOTIFICATION BY ITSELF
if (this.isWalletConnect || this.isRemoteConn) {
if (this.addressSent !== memState.selectedAddress) {
if (
this.addressSent?.toLowerCase() !==
memState.selectedAddress?.toLowerCase()
) {
this.addressSent = memState.selectedAddress;
this.notifySelectedAddressChanged(memState.selectedAddress);
}
Expand Down Expand Up @@ -335,6 +337,7 @@ export class BackgroundBridge extends EventEmitter {
'PreferencesController:stateChange',
this.sendStateUpdate,
);

this.port.emit('disconnect', { name: this.port.name, data: null });
};

Expand Down
2 changes: 1 addition & 1 deletion app/core/BackgroundBridge/WalletConnectPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WalletConnectPort extends EventEmitter {
} else if (msg?.data?.method === NOTIFICATION_NAMES.accountsChanged) {
const chainId = selectChainId(store.getState());
this._wcRequestActions?.updateSession?.({
chainId: parseInt(chainId, 10),
chainId: parseInt(chainId),
accounts: msg.data.params,
});
} else if (msg?.data?.method === NOTIFICATION_NAMES.unlockStateChanged) {
Expand Down
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ PODS:
- React-Core
- react-native-camera/RN (3.44.3):
- React-Core
- react-native-compat (2.13.0):
- react-native-compat (2.14.0):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-cookies (6.2.1):
Expand Down Expand Up @@ -1214,7 +1214,7 @@ SPEC CHECKSUMS:
react-native-blur: 507cf3dd4434eb9d5ca5f183e49d8bcccdd66826
react-native-branch: 4e42fda662d96893afbbd02839806931398e3d2e
react-native-camera: b8cc03e2feec0c04403d0998e37cf519d8fd4c6f
react-native-compat: e40ba72806755b12697fcfc0f826a0e245ac81a6
react-native-compat: 24c27f0076c5d094ac54a7c803aa5fd4f582444d
react-native-cookies: f54fcded06bb0cda05c11d86788020b43528a26c
react-native-get-random-values: a6ea6a8a65dc93e96e24a11105b1a9c8cfe1d72a
react-native-gzip: c5e87ee9e359f02350e3a2ee52eb35eddc398868
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@
"@sentry/react-native": "5.24.1",
"@tradle/react-native-http": "2.0.1",
"@walletconnect/client": "^1.8.0",
"@walletconnect/core": "2.13.0",
"@walletconnect/core": "2.14.0",
"@walletconnect/jsonrpc-types": "^1.0.2",
"@walletconnect/react-native-compat": "2.13.0",
"@walletconnect/se-sdk": "1.8.1",
"@walletconnect/utils": "2.13.0",
"@walletconnect/react-native-compat": "2.14.0",
"@walletconnect/se-sdk": "1.9.0",
"@walletconnect/utils": "2.14.0",
"@xmldom/xmldom": "^0.8.10",
"appium-adb": "^9.11.4",
"asyncstorage-down": "4.2.0",
Expand Down
137 changes: 41 additions & 96 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10535,10 +10535,10 @@
"@walletconnect/types" "^1.8.0"
"@walletconnect/utils" "^1.8.0"

"@walletconnect/core@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.13.0.tgz#6b79b039930643e8ee85a0f512b143a35fdb8b52"
integrity sha512-blDuZxQenjeXcVJvHxPznTNl6c/2DO4VNrFnus+qHmO6OtT5lZRowdMtlCaCNb1q0OxzgrmBDcTOCbFcCpio/g==
"@walletconnect/core@2.14.0", "@walletconnect/core@^2.10.1":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.14.0.tgz#e8afb01455968b02aaf26c74f3bfcc9b82678a39"
integrity sha512-E/dgBM9q3judXnTfZQ5ILvDpeSdDpabBLsXtYXa3Nyc26cfNplfLJ2nXm9FgtTdhM1nZ7yx4+zDPiXawBRZl2g==
dependencies:
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-provider" "1.0.14"
Expand All @@ -10551,31 +10551,8 @@
"@walletconnect/relay-auth" "1.0.4"
"@walletconnect/safe-json" "1.0.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.0"
"@walletconnect/utils" "2.13.0"
events "3.3.0"
isomorphic-unfetch "3.1.0"
lodash.isequal "4.5.0"
uint8arrays "3.1.0"

"@walletconnect/core@2.13.3", "@walletconnect/core@^2.10.1":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.13.3.tgz#d98fccefe36c6b365812fd0f7237a0f9634bafb6"
integrity sha512-TdF+rC6rONJGyOUtt/nLkbyQWjnkwbD3kXq3ZA0Q7+tYtmSjTDE4wbArlLbHIbtf69g+9/DpEVEQimWWcEOn2g==
dependencies:
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-provider" "1.0.14"
"@walletconnect/jsonrpc-types" "1.0.4"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/jsonrpc-ws-connection" "1.0.14"
"@walletconnect/keyvaluestorage" "1.1.1"
"@walletconnect/logger" "2.1.2"
"@walletconnect/relay-api" "1.0.10"
"@walletconnect/relay-auth" "1.0.4"
"@walletconnect/safe-json" "1.0.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.3"
"@walletconnect/utils" "2.13.3"
"@walletconnect/types" "2.14.0"
"@walletconnect/utils" "2.14.0"
events "3.3.0"
isomorphic-unfetch "3.1.0"
lodash.isequal "4.5.0"
Expand Down Expand Up @@ -10707,10 +10684,10 @@
randombytes "^2.1.0"
tslib "1.14.1"

"@walletconnect/react-native-compat@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/react-native-compat/-/react-native-compat-2.13.0.tgz#1327bff05cd079e3344af21453ef8b7b9bb7ffd8"
integrity sha512-yOUUxQ1P3gpeifUu1bIo2seYYXF+B77npqO27EnvD7A3v4kG+nu4VIJdIvHiZjn/rPWDwqZI6oa0kaR4+DLgTA==
"@walletconnect/react-native-compat@2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@walletconnect/react-native-compat/-/react-native-compat-2.14.0.tgz#71d2275a941c878a3e0287ab5416e3be7331c582"
integrity sha512-fk7qhUPMEXH7gmuZYkuczE+j9kE5TPOOLNZV6sWECzeo4xpWqy3Ews44pBnyTZzpZk1bMyOabEPuqOi6TZWWbQ==
dependencies:
events "3.3.0"
fast-text-encoding "1.0.6"
Expand Down Expand Up @@ -10747,26 +10724,26 @@
dependencies:
tslib "1.14.1"

"@walletconnect/se-sdk@1.8.1":
version "1.8.1"
resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.8.1.tgz#a4755e8a27dd9de84c0a30b3a99dae7231e6635b"
integrity sha512-bTbr3EHPY6TcGzfGWYm1lR8JXK9hfLAo8bMTeSQ5X1DSsVi8Xq7rKH04Z2vWGKbG+ma53/NSkH+BNyIUaU8/IA==
"@walletconnect/se-sdk@1.9.0":
version "1.9.0"
resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.9.0.tgz#913ea41b23213f9c35c7a8014eaf2c283b54c4a1"
integrity sha512-fYtLYy0lNrLfJoqHb+96b8OlnLYzhHWVgKpbBUDs87uRC7iZl9IjksfNm3OVYZ4zaleTdJEFvtTNCYBzFiTyQQ==
dependencies:
"@walletconnect/web3wallet" "1.12.3"
"@walletconnect/web3wallet" "1.13.0"

"@walletconnect/sign-client@2.13.3":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.13.3.tgz#9f8c826000bf3d6ea782f7325bc87e9f260e71ce"
integrity sha512-3Pcq6trHWdBZn5X0VUFQ3zJaaqyEbMW9WNVKcZ2SakIpQAwySd08Mztvq48G98jfucdgP3tjGPbBvzHX9vJX7w==
"@walletconnect/sign-client@2.14.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.14.0.tgz#36533ef0976a869d815624217527482c90937fc8"
integrity sha512-UrB3S3eLjPYfBLCN3WJ5u7+WcZ8kFMe/QIDqLf76Jk6TaLwkSUy563LvnSw4KW/kA+/cY1KBSdUDfX1tzYJJXg==
dependencies:
"@walletconnect/core" "2.13.3"
"@walletconnect/core" "2.14.0"
"@walletconnect/events" "1.0.1"
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "2.1.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.3"
"@walletconnect/utils" "2.13.3"
"@walletconnect/types" "2.14.0"
"@walletconnect/utils" "2.14.0"
events "3.3.0"

"@walletconnect/socket-transport@^1.8.0":
Expand All @@ -10785,22 +10762,10 @@
dependencies:
tslib "1.14.1"

"@walletconnect/types@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.13.0.tgz#cdac083651f5897084fe9ed62779f11810335ac6"
integrity sha512-MWaVT0FkZwzYbD3tvk8F+2qpPlz1LUSWHuqbINUtMXnSzJtXN49Y99fR7FuBhNFtDalfuWsEK17GrNA+KnAsPQ==
dependencies:
"@walletconnect/events" "1.0.1"
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-types" "1.0.4"
"@walletconnect/keyvaluestorage" "1.1.1"
"@walletconnect/logger" "2.1.2"
events "3.3.0"

"@walletconnect/types@2.13.3", "@walletconnect/types@^2.9.0":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.13.3.tgz#0280b5c64df9a2e07752c4121eeb81dc4a59b2c2"
integrity sha512-9UdtLoQqwGFfepCPprUAXeUbKg9zyDarPRmEJVco51OWXHCOpvRgroWk54fQHDhCUIfDELjObY6XNAzNrmNYUA==
"@walletconnect/types@2.14.0", "@walletconnect/types@^2.9.0":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.14.0.tgz#af3d4799b8ac5d166251af12bc024276f82f9b91"
integrity sha512-vevMi4jZLJ55vLuFOicQFmBBbLyb+S0sZS4IsaBdZkQflfGIq34HkN13c/KPl4Ye0aoR4/cUcUSitmGIzEQM5g==
dependencies:
"@walletconnect/events" "1.0.1"
"@walletconnect/heartbeat" "1.2.2"
Expand All @@ -10814,30 +10779,10 @@
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-1.8.0.tgz#3f5e85b2d6b149337f727ab8a71b8471d8d9a195"
integrity sha512-Cn+3I0V0vT9ghMuzh1KzZvCkiAxTq+1TR2eSqw5E5AVWfmCtECFkVZBP6uUJZ8YjwLqXheI+rnjqPy7sVM4Fyg==

"@walletconnect/utils@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.13.0.tgz#1fc1fbff0d26db0830e65d1ba8cfe1a13a0616ad"
integrity sha512-q1eDCsRHj5iLe7fF8RroGoPZpdo2CYMZzQSrw1iqL+2+GOeqapxxuJ1vaJkmDUkwgklfB22ufqG6KQnz78sD4w==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
"@stablelib/random" "1.0.2"
"@stablelib/sha256" "1.0.1"
"@stablelib/x25519" "1.0.3"
"@walletconnect/relay-api" "1.0.10"
"@walletconnect/safe-json" "1.0.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.0"
"@walletconnect/window-getters" "1.0.1"
"@walletconnect/window-metadata" "1.0.1"
detect-browser "5.3.0"
query-string "7.1.3"
uint8arrays "3.1.0"

"@walletconnect/utils@2.13.3", "@walletconnect/utils@^2.10.1":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.13.3.tgz#500d88342c193ce92ab9d2fae3bd343be71821b2"
integrity sha512-hjyyNhnhTCezGNr6OCfKRzqRsiak+p+YP57iRo1Tsf222fsj/9JD++MP97YiDwc4e4xXaZp/boiLB+8hJHsCog==
"@walletconnect/utils@2.14.0", "@walletconnect/utils@^2.10.1":
version "2.14.0"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.14.0.tgz#48493ffe1e902815fda3cbd5cc5409288a066d35"
integrity sha512-vRVomYQEtEAyCK2c5bzzEvtgxaGGITF8mWuIL+WYSAMyEJLY97mirP2urDucNwcUczwxUgI+no9RiNFbUHreQQ==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
Expand All @@ -10847,7 +10792,7 @@
"@walletconnect/relay-api" "1.0.10"
"@walletconnect/safe-json" "1.0.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.3"
"@walletconnect/types" "2.14.0"
"@walletconnect/window-getters" "1.0.1"
"@walletconnect/window-metadata" "1.0.1"
detect-browser "5.3.0"
Expand All @@ -10867,19 +10812,19 @@
js-sha3 "0.8.0"
query-string "6.13.5"

"@walletconnect/web3wallet@1.12.3":
version "1.12.3"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.12.3.tgz#26765f52cb653bd3696198ce4cc871df2061a01e"
integrity sha512-HBzYDjf3ZVzyTCqycyMjJnn/1sQtRe0beGc3qtq9bLX/dmezkO6Oc1lg1WlvxT7KtTqKx41usw5tjiwfNZ2+ug==
"@walletconnect/web3wallet@1.13.0":
version "1.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.13.0.tgz#e3994a13f9aabdffef4ed8d67b03b921339b7154"
integrity sha512-5fAe4rIe7B0Q8NpyEYfyYBjrbtC5H4/VZMSgg9LHxpChXIpjUX01hj7jdTPvE7EOzN1mt2w7hgnpI5jY883gXg==
dependencies:
"@walletconnect/auth-client" "2.1.2"
"@walletconnect/core" "2.13.3"
"@walletconnect/core" "2.14.0"
"@walletconnect/jsonrpc-provider" "1.0.14"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "2.1.2"
"@walletconnect/sign-client" "2.13.3"
"@walletconnect/types" "2.13.3"
"@walletconnect/utils" "2.13.3"
"@walletconnect/sign-client" "2.14.0"
"@walletconnect/types" "2.14.0"
"@walletconnect/utils" "2.14.0"

"@walletconnect/window-getters@1.0.0":
version "1.0.0"
Expand Down Expand Up @@ -28105,14 +28050,14 @@ strip-ansi@6.0.1, strip-ansi@^6.0.0, strip-ansi@^6.0.1:
strip-ansi@^3.0.0, strip-ansi@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf"
integrity sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=
integrity sha512-VhumSSbBqDTP8p2ZLKj40UjBCV4+v8bUSEpUb4KjRgWk9pbqGF4REFj6KEagidb2f/M6AzC0EmFyDNGaw9OCzg==
dependencies:
ansi-regex "^2.0.0"

strip-ansi@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-4.0.0.tgz#a8479022eb1ac368a871389b635262c505ee368f"
integrity sha1-qEeQIusaw2iocTibY1JixQXuNo8=
integrity sha512-4XaJ2zQdCzROZDivEVIDPkcQn8LMFSa8kj8Gxb/Lnwzv9A8VctNZ+lfivC/sV3ivW8ElJTERXZoPBRrZKkNKow==
dependencies:
ansi-regex "^3.0.0"

Expand Down

0 comments on commit c317aba

Please sign in to comment.