fix: simplify wallet choices and use bnb chain
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { http, createConfig, WagmiProvider } from "wagmi";
|
||||
import { mainnet } from "wagmi/chains";
|
||||
import { bsc } from "wagmi/chains";
|
||||
|
||||
const projectId =
|
||||
import.meta.env.VITE_WALLETCONNECT_PROJECT_ID || "ark-library-dev-only";
|
||||
@@ -32,11 +32,11 @@ const connectors = connectorsForWallets(
|
||||
);
|
||||
|
||||
export const wagmiConfig = createConfig({
|
||||
chains: [mainnet],
|
||||
chains: [bsc],
|
||||
connectors,
|
||||
ssr: false,
|
||||
transports: {
|
||||
[mainnet.id]: http(),
|
||||
[bsc.id]: http(),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -1,159 +1,84 @@
|
||||
import { useConnectModal } from "@rainbow-me/rainbowkit";
|
||||
import { QRCodeSVG } from "qrcode.react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useAccount, useSignMessage } from "wagmi";
|
||||
import { useToast } from "../components/Toast";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useI18n } from "../i18n";
|
||||
import {
|
||||
createTokenPocketLoginRequest,
|
||||
fetchTokenPocketLoginResult,
|
||||
requestWalletNonce,
|
||||
verifyWalletSignature,
|
||||
type TokenPocketLoginRequest,
|
||||
} from "./api";
|
||||
import { openWalletDeepLink } from "./deepLinks";
|
||||
import { getInjectedWallet, type WalletKind } from "./injected";
|
||||
import { useWallet } from "./WalletProvider";
|
||||
|
||||
const pollIntervalMs = 1800;
|
||||
type ModalState = "idle" | "signing";
|
||||
|
||||
type ModalState = "idle" | "tpLoading" | "tpPolling" | "rainbowSigning";
|
||||
type WalletOption = {
|
||||
kind: WalletKind;
|
||||
labelKey: string;
|
||||
};
|
||||
|
||||
const walletOptions: WalletOption[] = [
|
||||
{ kind: "tokenPocket", labelKey: "walletTokenPocket" },
|
||||
{ kind: "metaMask", labelKey: "walletMetaMask" },
|
||||
{ kind: "imToken", labelKey: "walletImToken" },
|
||||
];
|
||||
|
||||
function isMobileDevice(): boolean {
|
||||
if (typeof navigator === "undefined") return false;
|
||||
const ua = navigator.userAgent || "";
|
||||
return (
|
||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile/i.test(
|
||||
ua,
|
||||
) ||
|
||||
(/Macintosh/i.test(ua) && navigator.maxTouchPoints > 1)
|
||||
);
|
||||
}
|
||||
|
||||
export function WalletLoginModal() {
|
||||
const { t } = useI18n();
|
||||
const { showToast } = useToast();
|
||||
const { closeLoginModal, completeLogin, loginModalOpen, signInInjected } =
|
||||
useWallet();
|
||||
const { openConnectModal } = useConnectModal();
|
||||
const { address, isConnected } = useAccount();
|
||||
const { signMessageAsync } = useSignMessage();
|
||||
const { closeLoginModal, loginModalOpen, signInInjected } = useWallet();
|
||||
const [state, setState] = useState<ModalState>("idle");
|
||||
const [error, setError] = useState("");
|
||||
const [tpRequest, setTpRequest] = useState<TokenPocketLoginRequest | null>(
|
||||
null,
|
||||
);
|
||||
const [rainbowPending, setRainbowPending] = useState(false);
|
||||
const rainbowSigningRef = useRef(false);
|
||||
const [mobileDevice, setMobileDevice] = useState(false);
|
||||
const [selectedWallet, setSelectedWallet] = useState<WalletKind | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!loginModalOpen) return;
|
||||
setMobileDevice(isMobileDevice());
|
||||
setSelectedWallet(null);
|
||||
setError("");
|
||||
}, [loginModalOpen]);
|
||||
|
||||
const close = () => {
|
||||
if (state === "tpLoading" || state === "rainbowSigning") return;
|
||||
if (state === "signing") return;
|
||||
closeLoginModal();
|
||||
setError("");
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!loginModalOpen || !tpRequest) return;
|
||||
if (state !== "tpPolling") return;
|
||||
|
||||
let cancelled = false;
|
||||
const abortController = new AbortController();
|
||||
const poll = async () => {
|
||||
try {
|
||||
const result = await fetchTokenPocketLoginResult(
|
||||
tpRequest.actionId,
|
||||
abortController.signal,
|
||||
);
|
||||
if (cancelled) return;
|
||||
if (result.status === "completed") {
|
||||
const verified = await verifyWalletSignature({
|
||||
address: result.address,
|
||||
message: result.message,
|
||||
signature: result.signature,
|
||||
});
|
||||
if (cancelled) return;
|
||||
completeLogin(verified.token, verified.wallet);
|
||||
showToast(t("walletLoginSuccess"));
|
||||
setState("idle");
|
||||
setTpRequest(null);
|
||||
return;
|
||||
}
|
||||
if (result.status === "expired" || result.status === "failed") {
|
||||
setState("idle");
|
||||
setError(result.error || t("walletTpExpired"));
|
||||
}
|
||||
} catch (err) {
|
||||
if (
|
||||
!cancelled &&
|
||||
!(err instanceof DOMException && err.name === "AbortError")
|
||||
) {
|
||||
setError(t("walletLoginFailed"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void poll();
|
||||
const timer = window.setInterval(() => void poll(), pollIntervalMs);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
abortController.abort();
|
||||
window.clearInterval(timer);
|
||||
};
|
||||
}, [completeLogin, loginModalOpen, state, tpRequest, t, showToast]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!rainbowPending || !isConnected || !address) return;
|
||||
if (rainbowSigningRef.current) return;
|
||||
rainbowSigningRef.current = true;
|
||||
setState("rainbowSigning");
|
||||
setError("");
|
||||
|
||||
void (async () => {
|
||||
try {
|
||||
const nonce = await requestWalletNonce(address);
|
||||
const signature = await signMessageAsync({ message: nonce.message });
|
||||
const verified = await verifyWalletSignature({
|
||||
address,
|
||||
message: nonce.message,
|
||||
signature,
|
||||
});
|
||||
completeLogin(verified.token, verified.wallet);
|
||||
showToast(t("walletLoginSuccess"));
|
||||
} catch (err) {
|
||||
const message =
|
||||
err instanceof Error ? err.message : t("walletLoginFailed");
|
||||
setError(message || t("walletLoginFailed"));
|
||||
showToast(t("walletLoginFailed"), "error");
|
||||
} finally {
|
||||
setState("idle");
|
||||
setRainbowPending(false);
|
||||
rainbowSigningRef.current = false;
|
||||
}
|
||||
})();
|
||||
}, [
|
||||
address,
|
||||
isConnected,
|
||||
rainbowPending,
|
||||
showToast,
|
||||
signMessageAsync,
|
||||
t,
|
||||
completeLogin,
|
||||
]);
|
||||
|
||||
if (!loginModalOpen) return null;
|
||||
|
||||
const startInjected = async () => {
|
||||
setError("");
|
||||
setState("idle");
|
||||
await signInInjected().catch(() => undefined);
|
||||
const walletName = (kind: WalletKind) => {
|
||||
if (kind === "tokenPocket") return t("walletTokenPocket");
|
||||
if (kind === "metaMask") return t("walletMetaMask");
|
||||
return t("walletImToken");
|
||||
};
|
||||
|
||||
const startTokenPocketQr = async () => {
|
||||
const chooseWallet = async (kind: WalletKind) => {
|
||||
setError("");
|
||||
setState("tpLoading");
|
||||
try {
|
||||
const req = await createTokenPocketLoginRequest();
|
||||
setTpRequest(req);
|
||||
setState("tpPolling");
|
||||
} catch {
|
||||
setState("idle");
|
||||
setError(t("walletTpQrFailed"));
|
||||
setSelectedWallet(kind);
|
||||
|
||||
const injectedWallet = getInjectedWallet(kind);
|
||||
if (!injectedWallet) {
|
||||
if (mobileDevice) {
|
||||
openWalletDeepLink(kind);
|
||||
return;
|
||||
}
|
||||
setError(
|
||||
t("walletInstallSelected").replace("{wallet}", walletName(kind)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
const startRainbowFallback = () => {
|
||||
setError("");
|
||||
setRainbowPending(true);
|
||||
openConnectModal?.();
|
||||
if (!openConnectModal) setError(t("walletRainbowUnavailable"));
|
||||
setState("signing");
|
||||
await signInInjected(kind)
|
||||
.catch((err) => {
|
||||
setError(err instanceof Error ? err.message : t("walletLoginFailed"));
|
||||
})
|
||||
.finally(() => setState("idle"));
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -186,97 +111,38 @@ export function WalletLoginModal() {
|
||||
</div>
|
||||
|
||||
<div className="mt-5 grid gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void startInjected()}
|
||||
className="rounded-2xl border border-ark-gold/40 bg-ark-gold/10 p-4 text-left transition hover:bg-ark-gold/15 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ark-gold/80"
|
||||
>
|
||||
<span className="block font-semibold text-ark-gold2">
|
||||
{t("walletInjected")}
|
||||
</span>
|
||||
<span className="mt-1 block text-sm text-neutral-400">
|
||||
{t("walletInjectedDesc")}
|
||||
</span>
|
||||
</button>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/[0.03] p-4">
|
||||
<div className="flex flex-col gap-3 md:flex-row md:items-start md:justify-between">
|
||||
<div>
|
||||
<h3 className="font-semibold text-white">
|
||||
{t("walletTokenPocketQr")}
|
||||
</h3>
|
||||
<p className="mt-1 text-sm leading-6 text-neutral-400">
|
||||
{t("walletTokenPocketQrDesc")}
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void startTokenPocketQr()}
|
||||
disabled={state === "tpLoading"}
|
||||
className="shrink-0 rounded-full bg-ark-gold px-4 py-2 text-sm font-semibold text-black transition hover:bg-ark-gold2 disabled:cursor-wait disabled:opacity-70"
|
||||
>
|
||||
{state === "tpLoading" ? t("loading") : t("walletGenerateQr")}
|
||||
</button>
|
||||
</div>
|
||||
{tpRequest ? (
|
||||
<div className="mt-4 grid place-items-center gap-3 rounded-2xl bg-white p-4 text-center">
|
||||
<QRCodeSVG value={tpRequest.qrUrl} size={196} level="M" />
|
||||
<p className="text-xs font-medium text-neutral-700">
|
||||
{t("walletQrUseAnotherDevice")}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
<p className="text-sm font-medium text-neutral-300">
|
||||
{mobileDevice ? t("walletChooseMobile") : t("walletChooseDesktop")}
|
||||
</p>
|
||||
<div className="grid gap-2">
|
||||
{walletOptions.map((option) => {
|
||||
const signingThis =
|
||||
state === "signing" && selectedWallet === option.kind;
|
||||
return (
|
||||
<button
|
||||
key={option.kind}
|
||||
type="button"
|
||||
onClick={() => void chooseWallet(option.kind)}
|
||||
disabled={state === "signing"}
|
||||
className="flex items-center justify-between rounded-2xl border border-white/10 bg-[#20202a] px-4 py-4 text-left text-base font-semibold text-neutral-100 transition hover:border-ark-gold/50 hover:bg-ark-gold/10 disabled:cursor-wait disabled:opacity-70"
|
||||
>
|
||||
<span>{t(option.labelKey)}</span>
|
||||
<span className="text-sm text-ark-gold">
|
||||
{signingThis
|
||||
? t("walletSigning")
|
||||
: mobileDevice
|
||||
? t("walletOpen")
|
||||
: t("walletConnect")}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2 sm:grid-cols-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openWalletDeepLink("tokenPocket")}
|
||||
className="rounded-2xl border border-white/10 bg-[#20202a] px-4 py-3 text-sm font-semibold text-neutral-100 transition hover:border-ark-gold/40"
|
||||
>
|
||||
{t("walletOpenTokenPocket")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openWalletDeepLink("metaMask")}
|
||||
className="rounded-2xl border border-white/10 bg-[#20202a] px-4 py-3 text-sm font-semibold text-neutral-100 transition hover:border-ark-gold/40"
|
||||
>
|
||||
{t("walletOpenMetaMask")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openWalletDeepLink("imToken")}
|
||||
className="rounded-2xl border border-white/10 bg-[#20202a] px-4 py-3 text-sm font-semibold text-neutral-100 transition hover:border-ark-gold/40"
|
||||
>
|
||||
{t("walletOpenImToken")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-dashed border-white/15 p-4">
|
||||
<div className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
|
||||
<div>
|
||||
<h3 className="font-semibold text-white">
|
||||
{t("walletRainbowFallback")}
|
||||
</h3>
|
||||
<p className="mt-1 text-sm leading-6 text-neutral-400">
|
||||
{t("walletRainbowFallbackDesc")}
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={startRainbowFallback}
|
||||
disabled={state === "rainbowSigning"}
|
||||
className="shrink-0 rounded-full border border-ark-gold/50 px-4 py-2 text-sm font-semibold text-ark-gold transition hover:bg-ark-gold/10 disabled:cursor-wait disabled:opacity-70"
|
||||
>
|
||||
{state === "rainbowSigning"
|
||||
? t("walletSigning")
|
||||
: t("walletOpenRainbow")}
|
||||
</button>
|
||||
</div>
|
||||
<p className="mt-3 text-xs leading-5 text-yellow-200/80">
|
||||
{t("walletNetworkWarning")}
|
||||
{!mobileDevice ? (
|
||||
<p className="rounded-2xl border border-white/10 bg-white/[0.03] px-4 py-3 text-xs leading-5 text-neutral-400">
|
||||
{t("walletDesktopHint")}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{error ? (
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { useToast } from "../components/Toast";
|
||||
import { useI18n } from "../i18n";
|
||||
import { fetchWalletMe } from "./api";
|
||||
import { signInWithInjectedWallet } from "./injected";
|
||||
import { signInWithInjectedWallet, type WalletKind } from "./injected";
|
||||
import { clearWalletToken, readWalletToken, writeWalletToken } from "./token";
|
||||
|
||||
type WalletStatus = "loading" | "loggedOut" | "loggedIn";
|
||||
@@ -22,7 +22,7 @@ type WalletContextValue = {
|
||||
loginModalOpen: boolean;
|
||||
openLoginModal: () => void;
|
||||
closeLoginModal: () => void;
|
||||
signInInjected: () => Promise<void>;
|
||||
signInInjected: (kind?: WalletKind) => Promise<void>;
|
||||
completeLogin: (token: string, wallet: string) => void;
|
||||
logout: () => void;
|
||||
};
|
||||
@@ -80,18 +80,21 @@ export function WalletProvider({ children }: { children: ReactNode }) {
|
||||
setLoginModalOpen(false);
|
||||
}, []);
|
||||
|
||||
const signInInjected = useCallback(async () => {
|
||||
try {
|
||||
const res = await signInWithInjectedWallet();
|
||||
completeLogin(res.token, res.wallet);
|
||||
showToast(t("walletLoginSuccess"));
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : t("walletLoginFailed");
|
||||
showToast(message || t("walletLoginFailed"), "error");
|
||||
throw error;
|
||||
}
|
||||
}, [completeLogin, showToast, t]);
|
||||
const signInInjected = useCallback(
|
||||
async (kind?: WalletKind) => {
|
||||
try {
|
||||
const res = await signInWithInjectedWallet(kind);
|
||||
completeLogin(res.token, res.wallet);
|
||||
showToast(t("walletLoginSuccess"));
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : t("walletLoginFailed");
|
||||
showToast(message || t("walletLoginFailed"), "error");
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[completeLogin, showToast, t],
|
||||
);
|
||||
|
||||
const logout = useCallback(() => {
|
||||
clearWalletToken();
|
||||
|
||||
@@ -12,7 +12,7 @@ export function walletDeepLink(
|
||||
switch (kind) {
|
||||
case "tokenPocket":
|
||||
return `tpdapp://open?params=${encodeURIComponent(
|
||||
JSON.stringify({ url: dappUrl, chain: "ETH" }),
|
||||
JSON.stringify({ url: dappUrl, chain: "BSC" }),
|
||||
)}`;
|
||||
case "metaMask":
|
||||
return `https://metamask.app.link/dapp/${encodeURIComponent(
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { requestWalletNonce, verifyWalletSignature } from "./api";
|
||||
|
||||
export type WalletKind = "tokenPocket" | "metaMask" | "imToken";
|
||||
|
||||
const bnbChainIdHex = "0x38";
|
||||
|
||||
export type EthereumProvider = {
|
||||
isMetaMask?: boolean;
|
||||
isTokenPocket?: boolean;
|
||||
isImToken?: boolean;
|
||||
providers?: EthereumProvider[];
|
||||
request: <T = unknown>(args: {
|
||||
method: string;
|
||||
params?: unknown[];
|
||||
@@ -13,13 +21,54 @@ export function getInjectedEthereum(): EthereumProvider | null {
|
||||
return maybeWindow.ethereum ?? null;
|
||||
}
|
||||
|
||||
export async function signInWithInjectedWallet(): Promise<{
|
||||
export function getInjectedWallet(kind?: WalletKind): EthereumProvider | null {
|
||||
const ethereum = getInjectedEthereum();
|
||||
if (!ethereum || !kind) return ethereum;
|
||||
const providers = ethereum.providers?.length
|
||||
? ethereum.providers
|
||||
: [ethereum];
|
||||
const match = providers.find((provider) => {
|
||||
if (kind === "metaMask") return provider.isMetaMask;
|
||||
if (kind === "tokenPocket") return provider.isTokenPocket;
|
||||
if (kind === "imToken") return provider.isImToken;
|
||||
return false;
|
||||
});
|
||||
return match ?? null;
|
||||
}
|
||||
|
||||
async function ensureBnbChain(ethereum: EthereumProvider): Promise<void> {
|
||||
try {
|
||||
await ethereum.request({
|
||||
method: "wallet_switchEthereumChain",
|
||||
params: [{ chainId: bnbChainIdHex }],
|
||||
});
|
||||
} catch (error) {
|
||||
const code = (error as { code?: number | string }).code;
|
||||
if (code !== 4902 && code !== "4902") throw error;
|
||||
await ethereum.request({
|
||||
method: "wallet_addEthereumChain",
|
||||
params: [
|
||||
{
|
||||
blockExplorerUrls: ["https://bscscan.com"],
|
||||
chainId: bnbChainIdHex,
|
||||
chainName: "BNB Smart Chain",
|
||||
nativeCurrency: { decimals: 18, name: "BNB", symbol: "BNB" },
|
||||
rpcUrls: ["https://bsc-dataseed.binance.org"],
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function signInWithInjectedWallet(kind?: WalletKind): Promise<{
|
||||
token: string;
|
||||
wallet: string;
|
||||
}> {
|
||||
const ethereum = getInjectedEthereum();
|
||||
const ethereum = getInjectedWallet(kind);
|
||||
if (!ethereum) throw new Error("No injected wallet found");
|
||||
|
||||
await ensureBnbChain(ethereum);
|
||||
|
||||
const accounts = await ethereum.request<string[]>({
|
||||
method: "eth_requestAccounts",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user