From 8f495e513f3decc5ff91f58011e4aaa1a143c406 Mon Sep 17 00:00:00 2001 From: freebenweintraub <297510873+freebenweintraub@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:36:40 -0700 Subject: [PATCH] fix: detect Rabby and MetaMask via their wallet connectors --- .../WalletConnectorButton.tsx | 25 ++----- .../wallet-connection/WalletConnectorList.tsx | 68 ++++------------- .../__tests__/WalletConnectorList.test.tsx | 74 +++++++++++++++++++ 3 files changed, 95 insertions(+), 72 deletions(-) create mode 100644 src/components/wallet-connection/__tests__/WalletConnectorList.test.tsx diff --git a/src/components/wallet-connection/WalletConnectorButton.tsx b/src/components/wallet-connection/WalletConnectorButton.tsx index db36f9820..1ea8d60d9 100644 --- a/src/components/wallet-connection/WalletConnectorButton.tsx +++ b/src/components/wallet-connection/WalletConnectorButton.tsx @@ -6,23 +6,14 @@ interface WalletConnectorButtonProps { connector?: Connector; walletName: string; walletIcon: string; - isInstalled: boolean; - onConnect: () => void; - disabled?: boolean; + onConnect?: () => void; } /** * Single wallet connector button with icon and loading state * Handles connection to a specific wallet connector */ -export function WalletConnectorButton({ - connector, - walletName, - walletIcon, - isInstalled, - onConnect, - disabled, -}: WalletConnectorButtonProps) { +export function WalletConnectorButton({ connector, walletName, walletIcon, onConnect }: WalletConnectorButtonProps) { const { connect, isPending, connectingWalletId } = useWalletConnection({ onSuccess: onConnect, }); @@ -30,11 +21,7 @@ export function WalletConnectorButton({ const isConnecting = connector && isPending && connectingWalletId === connector.id; const handleClick = () => { - // Only connect if wallet is installed and connector exists - if (!isInstalled || !connector) { - return; - } - connect(connector.id); + if (connector) connect(connector.id); }; return ( @@ -42,11 +29,9 @@ export function WalletConnectorButton({ variant="outline" size="xl" width="full" - className={`flex flex-row items-center justify-start gap-3 h-14 text-pinto-gray-5 hover:text-pinto-gray-5 disabled:opacity-50 disabled:cursor-not-allowed ${ - !isInstalled ? "opacity-50 cursor-not-allowed" : "" - }`} + className="flex flex-row items-center justify-start gap-3 h-14 text-pinto-gray-5 hover:text-pinto-gray-5 disabled:opacity-50 disabled:cursor-not-allowed" onClick={handleClick} - disabled={disabled || (isPending && !isConnecting)} + disabled={!connector || (isPending && !isConnecting)} >
void; } -interface WalletConfig { - id: string; - name: string; - icon: string; - checkInstalled?: () => boolean; -} - -const WALLET_ORDER: WalletConfig[] = [ +const WALLET_ORDER = [ { - id: "rabby", + id: "io.rabby", name: "Rabby Wallet", icon: rabbyIcon, - checkInstalled: () => typeof window !== "undefined" && window.ethereum?.isRabby === true, }, { - id: "metamask", + id: "io.metamask", + fallbackId: "io.metamask.flask", name: "MetaMask", icon: metamaskIcon, - checkInstalled: () => typeof window !== "undefined" && window.ethereum?.isMetaMask === true, }, { - id: "walletconnect", + id: "walletConnect", name: "WalletConnect", icon: walletconnectIcon, - checkInstalled: () => true, // WalletConnect is always available (QR code) }, { - id: "coinbase", + id: "coinbaseWalletSDK", name: "Coinbase Wallet", icon: coinbaseIcon, - checkInstalled: () => true, // Coinbase Wallet can use QR code if extension not installed }, ]; @@ -51,44 +39,20 @@ const WALLET_ORDER: WalletConfig[] = [ * Shows: Rabby, MetaMask, WalletConnect, Coinbase Wallet */ export function WalletConnectorList({ onConnect }: WalletConnectorListProps) { - const { connectors } = useConnect(); - - const orderedWallets = useMemo(() => { - return WALLET_ORDER.map((walletConfig) => { - // Find matching connector - const connector = connectors.find((c) => { - const idLower = c.id.toLowerCase(); - const nameLower = c.name.toLowerCase(); - const configIdLower = walletConfig.id.toLowerCase(); - - return ( - idLower.includes(configIdLower) || - nameLower.includes(configIdLower) || - (configIdLower === "walletconnect" && (c.type === "walletConnect" || c.type === "wallet_connect")) - ); - }); - - const isInstalled = walletConfig.checkInstalled?.() ?? false; - - return { - config: walletConfig, - connector, - isInstalled, - icon: walletConfig.icon, - }; - }); - }, [connectors]); + const connectors = useConnectors(); return ( <> - {orderedWallets.map(({ config, connector, isInstalled, icon }) => ( + {WALLET_ORDER.map(({ id, fallbackId, name, icon }) => ( connector.id === id) ?? + connectors.find((connector) => connector.id === fallbackId) + } + walletName={name} walletIcon={icon} - isInstalled={isInstalled} - onConnect={() => onConnect?.()} + onConnect={onConnect} /> ))} diff --git a/src/components/wallet-connection/__tests__/WalletConnectorList.test.tsx b/src/components/wallet-connection/__tests__/WalletConnectorList.test.tsx new file mode 100644 index 000000000..e4ced481e --- /dev/null +++ b/src/components/wallet-connection/__tests__/WalletConnectorList.test.tsx @@ -0,0 +1,74 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { renderToStaticMarkup } from "react-dom/server"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { http, WagmiProvider, createConfig } from "wagmi"; +import { base } from "wagmi/chains"; +import { injected } from "wagmi/connectors"; +import { WalletConnectorList } from "../WalletConnectorList"; + +vi.hoisted(() => { + vi.stubEnv("VITE_CHAINS", "8453"); +}); + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +function renderWalletButtons(announcedWallets: { rdns: string; name: string }[], isMetaMask = false) { + const browser = Object.assign(new EventTarget(), { + ethereum: { isBraveWallet: true, isMetaMask }, + }); + vi.stubGlobal("window", browser); + + browser.addEventListener("eip6963:requestProvider", () => { + for (const wallet of announcedWallets) { + browser.dispatchEvent( + new CustomEvent("eip6963:announceProvider", { + detail: { info: { ...wallet, uuid: crypto.randomUUID() }, provider: {} }, + }), + ); + } + }); + + const config = createConfig({ + chains: [base], + transports: { [base.id]: http() }, + connectors: [injected()], + storage: null, + }); + + const html = renderToStaticMarkup( + + + + + , + ); + + return html.match(/]*>.*?<\/button>/g) ?? []; +} + +describe("wallet extension availability", () => { + it.each(["io.metamask", "io.metamask.flask"])("enables Rabby and %s alongside a built-in wallet", (rdns) => { + const buttons = renderWalletButtons([ + { rdns: "io.rabby", name: "Rabby Wallet" }, + { rdns, name: "MetaMask" }, + ]); + + for (const name of ["Rabby Wallet", "MetaMask"]) { + const button = buttons.find((markup) => markup.includes(`>${name}`)); + expect(button).toBeDefined(); + expect(button).not.toContain('disabled=""'); + expect(button).not.toContain(" opacity-50 cursor-not-allowed"); + } + }); + + it("keeps unannounced extensions disabled even if another wallet sets isMetaMask", () => { + const buttons = renderWalletButtons([{ rdns: "com.brave.wallet", name: "Brave Wallet" }], true); + + for (const name of ["Rabby Wallet", "MetaMask"]) { + const button = buttons.find((markup) => markup.includes(`>${name}`)); + expect(button).toContain('disabled=""'); + } + }); +});