Skip to content

Commit dd1b004

Browse files
authored
add a typed chainId field to the WalletAdapter interface (#249)
1 parent dfc77d7 commit dd1b004

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎src/adapters/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ export interface WalletAdapter {
4343
* Disconnects the wallet session if applicable.
4444
*/
4545
disconnect?(): Promise<void>;
46+
chainId?: string;
4647
}

‎src/adapters/walletconnect.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface WalletConnectAdapterOptions {
3131
* Must be one of: 'stellar:pubnet', 'stellar:testnet', 'stellar:local'.
3232
* Defaults to 'stellar:pubnet'.
3333
*/
34-
chainId?: string;
34+
chainId?: string | undefined;
3535
/** DApp metadata for WalletConnect modal/handshake */
3636
metadata?: WalletConnectAppMetadata;
3737
/** Optional pre-existing WalletConnect SignClient or provider instance */
@@ -60,7 +60,7 @@ const SUPPORTED_CAIP2_CHAINS: Readonly<Record<string, string>> = {
6060
*/
6161
export class WalletConnectAdapter implements WalletAdapter {
6262
private readonly projectId?: string | undefined;
63-
private readonly chainId: string;
63+
public readonly chainId: string;
6464
private readonly metadata?: WalletConnectAppMetadata | undefined;
6565
private client: WalletConnectSignClient | null;
6666
private session: WalletConnectSession | null;

‎src/client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function assertWalletNetworkMatch(
3636
wallet: WalletAdapter,
3737
expectedNetwork: string,
3838
): void {
39-
const raw = (wallet as unknown as { chainId?: unknown }).chainId;
39+
const raw = wallet.chainId;
4040
if (raw === undefined || raw === null) return;
4141

4242
const caip2 = String(raw);

‎src/relayer/WebSocketRelayer.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ export class WebSocketRelayer {
9393
if (this.isDestroyed) {
9494
throw new Error('WebSocketRelayer has been destroyed');
9595
}
96+
return this.connectPromise ?? (this.connectPromise = (async () => {
97+
try {
98+
await this.acquireLock();
99+
if (this.ws && this.ws.readyState === 1) return;
100+
await this.establishConnection();
101+
} finally {
102+
this.connectPromise = null;
103+
this.releaseLock();
104+
}
105+
})());
96106
this.reconnectEnabled = true;
97107
if (this.connectPromise) {
98108
return this.connectPromise;
@@ -132,17 +142,18 @@ export class WebSocketRelayer {
132142

133143
return new Promise((resolve, reject) => {
134144
try {
135-
let ws: WebSocket;
145+
let wsInstance: any;
136146
try {
137-
ws = new WebSocketCtor(this.url);
147+
wsInstance = new (WebSocketCtor as any)(this.url);
138148
} catch {
139-
ws = (WebSocketCtor as unknown as (url: string) => WebSocket)(this.url);
149+
// Fallback for mocks that are plain functions.
150+
wsInstance = (WebSocketCtor as any)(this.url);
140151
}
152+
const ws = wsInstance;
141153
if (!ws) {
142154
resolve();
143155
return;
144156
}
145-
146157
let settled = false;
147158

148159
ws.onopen = () => {
@@ -164,6 +175,7 @@ export class WebSocketRelayer {
164175
settled = true;
165176
reject(new Error(`WebSocket connection closed before opening: ${this.url}`));
166177
}
178+
if (!settled && !this.isDestroyed) {
167179
this.emitStateChange('disconnected');
168180
if (!this.isDestroyed && this.reconnectEnabled) {
169181
this.attemptReconnect();

0 commit comments

Comments
 (0)