Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { Connection, ConnectionEvents, ConnectParams } from './types';
import { type Scope, TransportType } from '../../domain';
import { EventEmitter } from '../../domain/events';
import { DefaultTransport } from '../transports/default';

/**
* Strategy that talks to the in-page MetaMask provider (extension or in-app
* webview) over the standard `window.postMessage` transport.
*
* In contrast to MWP, this connection is also used in a "passive" mode while
* the SDK is idle so that the SDK can observe `wallet_sessionChanged` events
* emitted by the extension without an explicit user connect action.
*/
export class DefaultConnection
extends EventEmitter<ConnectionEvents>
implements Connection
{
readonly type = TransportType.Browser;

readonly #transport: DefaultTransport;

#notificationUnsubscribe: (() => void) | undefined;

// eslint-disable-next-line no-restricted-syntax -- Constructors can't use hash names; factory is preferred
private constructor(transport: DefaultTransport) {
super();
this.#transport = transport;
this.#notificationUnsubscribe = transport.onNotification((data) => {
this.emit('notification', data);
});
}

/**
* Factory used by `MultichainClient` to construct a fresh connection.
*
* @returns A new `DefaultConnection` with a ready-to-use transport.
*/
static create(): DefaultConnection {
return new DefaultConnection(new DefaultTransport());
}

get transport(): DefaultTransport {
return this.#transport;
}

isConnected(): boolean {
return this.#transport.isConnected();
}

/**
* Connect against the in-page MetaMask provider, opening or reusing a CAIP
* session as needed.
*
* @param params - Standard connect parameters.
*/
async connect(params: ConnectParams): Promise<void> {
await this.#transport.connect(params);
}

/**
* Initialise the transport's message listener and emit an initial
* `wallet_sessionChanged` event without performing a `wallet_createSession`
* request. Used to keep a passive listener open when the SDK is idle.
*
* Mirrors the prior `MultichainClient` behaviour of catching init errors
* with `console.error('Passive init failed:', ...)`.
*/
async initPassive(): Promise<void> {
try {
await this.#transport.init();
} catch (error) {
console.error('Passive init failed:', error);
}
}

async disconnect(scopes: Scope[] = []): Promise<void> {
await this.#transport.disconnect(scopes);
}

/**
* Releases the notification subscription. The DefaultTransport itself has
* no explicit teardown beyond this; the underlying `window` message
* listener is shared across `DefaultTransport` instances and is safe to
* leave in place.
*/
async dispose(): Promise<void> {
this.#notificationUnsubscribe?.();
this.#notificationUnsubscribe = undefined;
}
}
10 changes: 10 additions & 0 deletions packages/connect-multichain/src/multichain/connections/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { DefaultConnection } from './default-connection';
export { MwpConnection } from './mwp-connection';
export type {
Connection,
ConnectionContext,
ConnectionEvents,
ConnectParams,
MwpConnectFlow,
MwpConnectParams,
} from './types';
Loading
Loading