Skip to content
Merged
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
25 changes: 5 additions & 20 deletions src/components/wallet-connection/WalletConnectorButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,32 @@ 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,
});

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 (
<Button
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)}
>
<div className="w-6 h-6 flex items-center justify-center flex-shrink-0 relative">
<img
Expand Down
68 changes: 16 additions & 52 deletions src/components/wallet-connection/WalletConnectorList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { useMemo } from "react";
import { useConnect } from "wagmi";
import type { Connector } from "wagmi";
import { useConnectors } from "wagmi";
import { WalletConnectorButton } from "./WalletConnectorButton";

import coinbaseIcon from "@/assets/wallets/coinbase.png";
Expand All @@ -12,37 +10,27 @@ interface WalletConnectorListProps {
onConnect?: () => 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
},
];

Expand All @@ -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 }) => (
<WalletConnectorButton
key={config.id}
connector={connector}
walletName={config.name}
key={id}
connector={
connectors.find((connector) => connector.id === id) ??
connectors.find((connector) => connector.id === fallbackId)
}
walletName={name}
walletIcon={icon}
isInstalled={isInstalled}
onConnect={() => onConnect?.()}
onConnect={onConnect}
/>
))}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
<WagmiProvider config={config} reconnectOnMount={false}>
<QueryClientProvider client={new QueryClient()}>
<WalletConnectorList />
</QueryClientProvider>
</WagmiProvider>,
);

return html.match(/<button[^>]*>.*?<\/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}</span>`));
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}</span>`));
expect(button).toContain('disabled=""');
}
});
});
Loading