Skip to content

Commit

Permalink
Merge pull request #4403 from WalletConnect/fix/fallback-listener
Browse files Browse the repository at this point in the history
fix: fallback event listener cleanup
  • Loading branch information
ganchoradkov authored Apr 5, 2024
2 parents 340c7db + 0c0acea commit b48ae14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/sign-client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,20 @@ export class Engine extends IEngine {
};
// handle session authenticate response
const onAuthenticate = async (payload: any) => {
// remove cleanup for fallback response
this.events.off(engineEvent("session_connect"), onSessionConnect);
if (payload.error) {
// wallets that do not support wc_sessionAuthenticate will return an error
// we should not reject the promise in this case as the fallback session proposal will be used
const error = getSdkError("WC_METHOD_UNSUPPORTED", "wc_sessionAuthenticate");
if (payload.error.code === error.code) return;

// cleanup listener for fallback response
this.events.off(engineEvent("session_connect"), onSessionConnect);
return reject(payload.error.message);
}

// cleanup listener for fallback response
this.events.off(engineEvent("session_connect"), onSessionConnect);

const {
cacaos,
responder,
Expand Down
11 changes: 10 additions & 1 deletion packages/sign-client/test/sdk/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,15 @@ describe("Authenticated Sessions", () => {
metadata: TEST_APP_METADATA_B,
});

// force wallet to not support `wc_sessionAuthenticate` by removing it from registered methods
const supportedMethods = ENGINE_RPC_OPTS;
const toRegisterMethods = Object.keys(supportedMethods).filter(
(method) => method !== "wc_sessionAuthenticate",
);
//@ts-expect-error
wallet.core.pairing.registeredMethods = [];
wallet.core.pairing.register({ methods: toRegisterMethods });

await Promise.all([
new Promise<void>((resolve) => {
wallet.on("session_proposal", async (payload) => {
Expand Down Expand Up @@ -1151,7 +1160,7 @@ describe("Authenticated Sessions", () => {
});
}),
new Promise<void>((resolve) => {
wallet.pair({ uri: uri.replace("methods", "") });
wallet.pair({ uri });
resolve();
}),
]);
Expand Down

0 comments on commit b48ae14

Please sign in to comment.