diff --git a/package.json b/package.json index 906d76f..7930a3b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@walletconnect/se-sdk", "description": "Single-Ethreum-SDK for WalletConnect Protocol", "private": false, - "version": "1.8.0", + "version": "1.8.1", "author": "WalletConnect, Inc. ", "homepage": "https://github.com/walletconnect/walletconnect-monorepo/", "license": "Apache-2.0", diff --git a/src/controllers/engine.ts b/src/controllers/engine.ts index 438b54b..ae46fa4 100644 --- a/src/controllers/engine.ts +++ b/src/controllers/engine.ts @@ -250,6 +250,14 @@ export class Engine extends ISingleEthereumEngine { private onSessionRequest = async (event: SingleEthereumTypes.SessionRequest) => { event.params.chainId = parseChain(event.params.chainId); + if ( + ["wallet_switchEthereumChain", "wallet_addEthereumChain"].includes( + event.params.request.method, + ) + ) { + event.params.chainId = this.chainId.toString(); + } + if (parseInt(event.params.chainId) !== this.chainId || this.isSwitchChainRequest(event)) { this.client.logger.info( `Session request chainId ${event.params.chainId} does not match current chainId ${this.chainId}. Attempting to switch`, diff --git a/test/sign.spec.ts b/test/sign.spec.ts index 3fa2b34..3f388e6 100644 --- a/test/sign.spec.ts +++ b/test/sign.spec.ts @@ -422,6 +422,66 @@ describe("Sign Integration", () => { ]); expect(wallet.engine.chainId).to.eq(originalChainId); }); + it("should receive wallet_switchEthereumChain in the wallet's active chainId", async () => { + // first pair and approve session + await Promise.all([ + new Promise((resolve) => { + wallet.on("session_proposal", async (sessionProposal) => { + const { id, params } = sessionProposal; + session = await wallet.approveSession({ + id, + chainId: TEST_ETHEREUM_CHAIN_PARSED, + accounts: [cryptoWallet.address], + }); + resolve(session); + }); + }), + sessionApproval(), + wallet.pair({ uri: uriString }), + ]); + const originalChainId = wallet.engine.chainId; + const expectedChainId = 5; + + expect(originalChainId).to.not.eq(expectedChainId); + + // change chainId + wallet.engine.chainId = expectedChainId; + + const requestChainId = 0x10; + + await Promise.all([ + new Promise((resolve) => { + wallet.on("session_request", async (sessionRequest) => { + const { id, params, topic, verifyContext } = sessionRequest; + expect(params.request.method).to.eq("wallet_switchEthereumChain"); + expect(params.chainId).to.eq(expectedChainId.toString()); + expect(params.chainId).to.not.eq(requestChainId); + await wallet.approveRequest({ + id, + topic: session.topic, + result: null, + }); + resolve(); + }); + }), + new Promise(async (resolve) => { + const result = await dapp.request({ + topic: session.topic, + request: { + method: "wallet_switchEthereumChain", + params: [ + { + chainId: requestChainId, + }, + ], + }, + chainId: TEST_ETHEREUM_CHAIN, + }); + expect(result).to.be.null; + resolve(); + }), + ]); + }); it("should handle reject to session request in different chain", async () => { // first pair and approve session