Skip to content

Commit

Permalink
Merge pull request #31 from WalletConnect/fix/switch-chain-request-ch…
Browse files Browse the repository at this point in the history
…ainId

fix: switch chain request chain
  • Loading branch information
ganchoradkov authored Jun 25, 2024
2 parents 8e6da31 + 5e3014d commit b58dffa
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. <walletconnect.com>",
"homepage": "https://github.com/walletconnect/walletconnect-monorepo/",
"license": "Apache-2.0",
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
60 changes: 60 additions & 0 deletions test/sign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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<void>(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
Expand Down

0 comments on commit b58dffa

Please sign in to comment.