Skip to content

Commit 08d3f9e

Browse files
Fix account selection on page load (#15)
* Fix Sign In * Use es2023 * Fix conflict with Solflare MetaMask connector * Lint
1 parent 91b51b6 commit 08d3f9e

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/wallet.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class MetamaskWalletAccount extends ReadonlyWalletAccount {
5959
export class MetamaskWallet implements Wallet {
6060
readonly #listeners: { [E in StandardEventsNames]?: StandardEventsListeners[E][] } = {};
6161
readonly version = '1.0.0' as const;
62-
readonly name = 'MetaMask' as const;
62+
readonly name = 'MetaMask' as const;
6363
readonly icon = metamaskIcon;
6464
readonly chains: SolanaChain[] = [SOLANA_MAINNET_CHAIN, SOLANA_DEVNET_CHAIN, SOLANA_TESTNET_CHAIN];
6565
readonly scope = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
@@ -137,15 +137,19 @@ export class MetamaskWallet implements Wallet {
137137

138138
#connect = async (): Promise<StandardConnectOutput> => {
139139
if (!this.accounts.length) {
140-
const existingSession = await this.client.getSession();
141-
142-
this.client.onNotification((data: any) => {
143-
if (data?.params?.notification?.method === 'metamask_accountsChanged') {
144-
this.#handleAccountsChangedEvent(data);
145-
}
140+
// Setup accountsChanged listener. Returns promise that resolves when first accountsChanged event is received
141+
const firstEventPromise = new Promise<void>((resolve) => {
142+
const handleFirstEvent = (data: any) => {
143+
if (data?.params?.notification?.method === 'metamask_accountsChanged') {
144+
this.#handleAccountsChangedEvent(data);
145+
resolve();
146+
}
147+
};
148+
this.client.onNotification(handleFirstEvent);
146149
});
147150

148-
// If there's no existing accounts for this session scope, create a new one
151+
const existingSession = await this.client.getSession();
152+
149153
const session: SessionData | undefined = existingSession?.sessionScopes[this.scope]?.accounts?.length
150154
? existingSession
151155
: await this.client.createSession({
@@ -162,9 +166,23 @@ export class MetamaskWallet implements Wallet {
162166

163167
const accounts = session?.sessionScopes[this.scope]?.accounts;
164168

165-
if (!accounts?.length || accounts?.[0] === undefined) {
166-
throw new Error('No accounts found in MetaMask session');
167-
}
169+
// Fallback if first event doesn't arrive within a reasonable amount of time
170+
await new Promise<void>((resolve, reject) => {
171+
const timeout = setTimeout(() => {
172+
console.warn('No accountsChanged event received, using first account from session');
173+
if (accounts?.[0]) {
174+
this.#account = this.#getAccountFromAddress(accounts[0].slice(this.scope.length + 1));
175+
resolve();
176+
} else {
177+
reject(new Error('No accounts available to use from session'));
178+
}
179+
}, 2000);
180+
181+
firstEventPromise.then(() => {
182+
clearTimeout(timeout);
183+
resolve();
184+
});
185+
});
168186
}
169187

170188
return { accounts: this.accounts };

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"esModuleInterop": true,
44
"forceConsistentCasingInFileNames": true,
5-
"lib": ["ES2020", "DOM"],
5+
"lib": ["ES2023", "DOM"],
66
"module": "CommonJS",
77
"moduleResolution": "node",
88
"noEmit": true,

0 commit comments

Comments
 (0)