Skip to content

Commit 0303d1a

Browse files
authored
fix: reconnect dappClient on resumed session (#43)
* fix: reconnect dappClient on resumed session * chore: update CHANGELOG.md * docs: address code review
1 parent 05ff9fa commit 0303d1a

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/connect-multichain/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Fixed
1616

17+
- Fix validation of `MWPTransport` connection state after resuming a session ([#43](https://github.com/MetaMask/connect-monorepo/pull/43))
1718
- Fix install modal not rendering when called from Vite application ([#42](https://github.com/MetaMask/connect-monorepo/pull/42))
1819
- Fix requests not being sent to the mobile wallet with proper wrapping metadata ([#28](https://github.com/MetaMask/connect-monorepo/pull/28))
1920
- Fix connections made from within the MetaMask Mobile In-App Browser ([#21](https://github.com/MetaMask/connect-monorepo/pull/21))

packages/connect-multichain/src/multichain/transports/mwp/index.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class MWPTransport implements ExtendedTransport {
148148
...messagePayload,
149149
method:
150150
request.method === 'wallet_getSession' ||
151-
request.method === 'wallet_createSession'
151+
request.method === 'wallet_createSession'
152152
? 'wallet_sessionChanged'
153153
: request.method,
154154
} as unknown as {
@@ -160,7 +160,7 @@ export class MWPTransport implements ExtendedTransport {
160160
...messagePayload,
161161
method:
162162
request.method === 'wallet_getSession' ||
163-
request.method === 'wallet_createSession'
163+
request.method === 'wallet_createSession'
164164
? 'wallet_sessionChanged'
165165
: request.method,
166166
params: requestWithName.result,
@@ -203,7 +203,6 @@ export class MWPTransport implements ExtendedTransport {
203203
}
204204
}
205205

206-
207206
private async onResumeSuccess(
208207
resumeResolve: () => void,
209208
resumeReject: (err: Error) => void,
@@ -449,6 +448,39 @@ export class MWPTransport implements ExtendedTransport {
449448
return (this.dappClient as any).state === 'CONNECTED';
450449
}
451450

451+
/**
452+
* Attempts to re-establish a connection via DappClient
453+
*
454+
* @returns Nothing
455+
*/
456+
// TODO: We should re-evaluate adding this to the WebSocketTransport layer from `@metamask/mobile-wallet-protocol-core`
457+
// ticket: https://consensyssoftware.atlassian.net/browse/WAPI-862
458+
private async attemptResumeSession(): Promise<void> {
459+
try {
460+
await this.dappClient.reconnect();
461+
// Wait for connection to be established
462+
await new Promise<void>((resolve, reject) => {
463+
const timeout = setTimeout(() => {
464+
reject(new Error('Resume timeout'));
465+
}, 2_000);
466+
467+
if (this.isConnected()) {
468+
clearTimeout(timeout);
469+
resolve();
470+
} else {
471+
this.dappClient.once('connected', () => {
472+
clearTimeout(timeout);
473+
resolve();
474+
});
475+
}
476+
});
477+
} catch (error) {
478+
return Promise.reject(
479+
new Error(`Failed to resume session: ${error.message}`),
480+
);
481+
}
482+
}
483+
452484
private async getCachedResponse(
453485
request: { jsonrpc: string; id: string } & TransportRequest,
454486
): Promise<TransportResponse | undefined> {
@@ -522,6 +554,10 @@ export class MWPTransport implements ExtendedTransport {
522554
return cachedWalletSession as TResponse;
523555
}
524556

557+
if (!this.isConnected()) {
558+
await this.attemptResumeSession();
559+
}
560+
525561
return new Promise<TResponse>((resolve, reject) => {
526562
const timeout = setTimeout(() => {
527563
this.rejectRequest(request.id, new TransportTimeoutError());

0 commit comments

Comments
 (0)