Skip to content

Commit

Permalink
Merge branch 'v2.0' into fix/link-localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov authored Dec 21, 2023
2 parents 665c670 + b02bd5f commit c595719
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
13 changes: 12 additions & 1 deletion packages/sign-client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ export class Engine extends IEngine {
this.client.core.storage
.removeItem(WALLETCONNECT_DEEPLINK_CHOICE)
.catch((e) => this.client.logger.warn(e));
this.getPendingSessionRequests().forEach((r) => {
if (r.topic === topic) {
this.deletePendingSessionRequest(r.id, getSdkError("USER_DISCONNECTED"));
}
});
};

private deleteProposal: EnginePrivate["deleteProposal"] = async (id, expirerHasDeleted) => {
Expand Down Expand Up @@ -1388,7 +1393,13 @@ export class Engine extends IEngine {
throw new Error(message);
}
const { topic, response } = params;
await this.isValidSessionTopic(topic);
try {
// if the session is already disconnected, we can't respond to the request so we need to delete it
await this.isValidSessionTopic(topic);
} catch (error) {
if (params?.response?.id) this.cleanupAfterResponse(params);
throw error;
}
if (!isValidResponse(response)) {
const { message } = getInternalError(
"MISSING_OR_INVALID",
Expand Down
84 changes: 83 additions & 1 deletion packages/sign-client/test/sdk/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@walletconnect/jsonrpc-utils";
import { RelayerTypes } from "@walletconnect/types";
import { getSdkError, parseUri } from "@walletconnect/utils";
import { expect, describe, it, vi, beforeEach, afterEach } from "vitest";
import { expect, describe, it, vi } from "vitest";
import SignClient, { WALLETCONNECT_DEEPLINK_CHOICE } from "../../src";

import {
Expand Down Expand Up @@ -346,6 +346,88 @@ describe("Sign Client Integration", () => {
await throttle(1000);
await deleteClients(clients);
});
/**
* this test simulates the case where a session is disconnected
* while session request is being approved
* the queue should continue operating normally after the `respond` rejection
*/
it("continue processing requests queue after respond rejection due to disconnected session", async () => {
// create the clients and pair them
const {
clients,
sessionA: { topic: topicA },
} = await initTwoPairedClients({}, {}, { logger: "error" });
const dapp = clients.A as SignClient;
const wallet = clients.B as SignClient;
const { uri, approval } = await dapp.connect({
requiredNamespaces: {},
});

let topicB = "";
await Promise.all([
new Promise<void>((resolve) => {
wallet.once("session_proposal", async (args) => {
const { id } = args.params;
await wallet.approve({
id,
namespaces: TEST_NAMESPACES,
});
resolve();
});
}),
wallet.pair({ uri: uri! }),
new Promise<void>(async (resolve) => {
const session = await approval();
topicB = session.topic;
resolve();
}),
]);

const expectedRequests = 5;
let receivedRequests = 0;
await Promise.all([
new Promise<void>((resolve) => {
clients.B.on("session_request", async (args) => {
receivedRequests++;
const { id, topic } = args;

// capture the request on topicB, disconnect and try to approve the request
if (topic === topicB) {
await new Promise<void>(async (_resolve) => {
await wallet.disconnect({
topic,
reason: getSdkError("USER_DISCONNECTED"),
});
_resolve();
});
}
await clients.B.respond({
topic,
response: formatJsonRpcResult(id, "ok"),
}).catch((err) => {
// eslint-disable-next-line no-console
console.log("respond error", err);
});
if (receivedRequests >= expectedRequests) resolve();
});
}),
new Promise<void>((resolve) => {
clients.A.request({
topic: topicB,
...TEST_REQUEST_PARAMS,
});
resolve();
}),
Array.from(Array(expectedRequests).keys()).map(() =>
clients.A.request({
topic: topicA,
...TEST_REQUEST_PARAMS,
}),
),
]);
await throttle(1000);
await deleteClients(clients);
});
});
});
});
Expand Down

0 comments on commit c595719

Please sign in to comment.