fix: support imToken in-app browser login
All checks were successful
Deploy to Frontend Servers / deploy (push) Successful in 1m2s

This commit is contained in:
TerryM
2026-06-06 00:45:24 +08:00
parent cbaa06f77d
commit 9c4b8a4df7

View File

@@ -15,7 +15,9 @@ export type EthereumProvider = {
isMetaMask?: boolean; isMetaMask?: boolean;
isTokenPocket?: boolean; isTokenPocket?: boolean;
isImToken?: boolean; isImToken?: boolean;
selectedAddress?: string;
providers?: EthereumProvider[]; providers?: EthereumProvider[];
enable?: () => Promise<unknown[]>;
request: <T = unknown>(args: { request: <T = unknown>(args: {
method: string; method: string;
params?: unknown[]; params?: unknown[];
@@ -75,6 +77,8 @@ async function ensureBnbChain(ethereum: EthereumProvider): Promise<void> {
async function requestInjectedAddress( async function requestInjectedAddress(
ethereum: EthereumProvider, ethereum: EthereumProvider,
): Promise<string> { ): Promise<string> {
if (isAddress(ethereum.selectedAddress)) return ethereum.selectedAddress;
const existingAccounts: unknown[] = await ethereum const existingAccounts: unknown[] = await ethereum
.request<unknown[]>({ method: "eth_accounts" }) .request<unknown[]>({ method: "eth_accounts" })
.catch((): unknown[] => []); .catch((): unknown[] => []);
@@ -85,18 +89,25 @@ async function requestInjectedAddress(
.request<unknown[]>({ .request<unknown[]>({
method: "eth_requestAccounts", method: "eth_requestAccounts",
}) })
.catch((error: unknown): never => { .catch(async (error: unknown): Promise<unknown[]> => {
throw normalizeWalletError(error); if (!ethereum.enable) throw normalizeWalletError(error);
return ethereum.enable().catch((fallbackError: unknown): never => {
throw normalizeWalletError(fallbackError || error);
});
}); });
const requestedAddress = requestedAccounts.find(isAddress); const requestedAddress = requestedAccounts.find(isAddress);
if (!requestedAddress) throw new Error("walletNoAccount"); if (requestedAddress) return requestedAddress;
return requestedAddress; if (isAddress(ethereum.selectedAddress)) return ethereum.selectedAddress;
throw new Error("walletNoAccount");
} }
export function getInjectedEthereum(): EthereumProvider | null { export function getInjectedEthereum(): EthereumProvider | null {
if (typeof window === "undefined") return null; if (typeof window === "undefined") return null;
const maybeWindow = window as typeof window & { ethereum?: EthereumProvider }; const maybeWindow = window as typeof window & {
return maybeWindow.ethereum ?? null; ethereum?: EthereumProvider;
web3?: { currentProvider?: EthereumProvider };
};
return maybeWindow.ethereum ?? maybeWindow.web3?.currentProvider ?? null;
} }
export function isTokenPocketBrowser(): boolean { export function isTokenPocketBrowser(): boolean {
@@ -168,7 +179,9 @@ export async function connectInjectedWallet(
console.info("[wallet-login] injected account", address); console.info("[wallet-login] injected account", address);
console.info("[wallet-login] ensuring BNB Chain (0x38)…"); console.info("[wallet-login] ensuring BNB Chain (0x38)…");
await ensureBnbChain(ethereum); await ensureBnbChain(ethereum).catch((error: unknown) => {
console.warn("[wallet-login] BNB Chain switch skipped", error);
});
return address; return address;
} }