0xMiden / 0xMiden/wallet-adapter

MidenFiSignerProvider autoConnect opens extension popup on first visit, no silent reconnect path

Aperta
#85 0 commenti 1 reazione 0 assegnatari Vedi su GitHub
Lingua principale
TypeScript
Stelle
2
Fork
9
Merge medio
25m
PR unite (30g)
1

Descrizione

## Summary

`MidenFiSignerProvider`'s `autoConnect={true}` does not provide a MetaMask-style silent reconnect for previously approved sessions. It opens the extension popup on **every page load**, including the first visit when the user has never clicked Connect — making the prop unusable as a persistence mechanism. `autoConnect={false}` (the default) means the user has to manually click Connect on every page refresh / fresh tab.

## Repro

1. App wraps children with `` (single MidenWalletAdapter, no `walletsProp`)
2. Open the app for the first time in a clean profile (no localStorage `walletName`)
3. MidenFi extension popup opens immediately on mount — before the user has ever shown intent to connect

## Root cause

In `MidenFiSignerProvider.js` (current behaviour, paraphrased):

```js
const [name, setName] = useLocalStorage('walletName', null);

// useEffect ①
useEffect(() => {
if (!name && wallets.length === 1) {
setName(wallets[0].adapter.name); // ← auto-sets even on first visit
}
}, [name, wallets, setName]);

// useEffect ②
useEffect(() => {
if (!autoConnect || connected || readyState !== Installed) return;
adapter.connect(...); // ← triggers extension popup
}, [autoConnect, adapter, readyState, ...]);
```

Effect ① auto-selects `MidenFi` the moment the dynamic SDK import resolves (the only adapter). Effect ② then sees `autoConnect=true` + adapter Installed + not yet connected, and calls `adapter.connect()`, which forwards to `window.midenWallet.connect()`. The extension shows its approval UI even when no prior session existed.

The intent of `autoConnect` (per the prop comment: "Auto-connect to previously selected wallet on mount") cannot be satisfied this way, because the auto-`setName` causes every freshly-mounted page to look like "previously selected".

## What ConnectKit / wagmi + MetaMask do (reference)

- MetaMask injects `window.ethereum.selectedAddress` populated for already-approved origins
- wagmi's `eth_requestAccounts` returns silently when there is an approved session
- Net effect: the user clicks "Connect" once. Subsequent visits / refreshes silently re-establish the address with no popup. Manual `disconnect` revokes our app's session in the connector store but the extension's site approval persists for the next manual reconnect

`@miden-sdk/miden-wallet-adapter-miden`'s `MidenWalletAdapter` has no equivalent — there's no `selectedAddress`-equivalent property exposed on `window.midenWallet`, and `wallet.connect()` always invokes the UI flow even when an approved session exists in the extension.

## What we tried in userland

Workaround attempt (failed): wrap `` with a custom component that tracks our own consent flag in localStorage and calls `connect()` from a `useEffect` only when the flag is set. Two problems with this:

1. The extension still opens the popup every time `connect()` is called — there's no silent path
2. `useEffect(... , [wallet])` where `wallet` comes from `useMidenFiWallet()` fires repeatedly because the provider re-renders frequently (the adapter's `readyStateChange` listener is invoked on every adapter event), causing either popup loops or React error #310 from churn

## Proposed API additions (any one of these would unblock us)

### Option A — `WalletAdapter.silentConnect()` / `tryEagerConnect()`

A method that asks the extension "do you have an approved session for this origin? if so, return the address; otherwise, fail silently without showing UI". Wagmi's `connector.connect({chainId, isReconnecting: true})` follows this shape.

### Option B — Pre-`connect()` `WalletAdapter.address`

Expose the active wallet address on `window.midenWallet.address` (or a similar method) BEFORE `connect()` is called, populated only for origins the user has previously approved. The adapter could surface this as a `WalletAdapter.cachedAddress`, and `MidenFiSignerProvider` could short-circuit autoConnect to just read it.

### Option C — Auto-`setName` only after a manual connect

In `MidenFiSignerProvider`, defer the auto-set of `name` until `connect()` has succeeded at least once on this origin. That way `autoConnect={true}` is a real opt-in: it only fires when the user has previously consented in this browser. Fresh visitors see nothing.

This is the most localised fix and doesn't require any change to the extension or `MidenWalletAdapter` — it's an adjustment of the React provider's bootstrap logic.

## Impact

For our app (a Miden-native DeFi frontend), the lack of MetaMask-style silent reconnect is the most-complained-about UX issue: every navigation between routes within an SPA "feels" like a disconnect (technically the providers persist; the connection state doesn't), and users expect the same single-click experience as their EVM wallet.

Happy to draft a PR for Option C against `wallet-adapter-react` if that direction sounds right — it's a 5-line patch.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.