Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch chain request chain #31

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading