fix: support imToken in-app browser login
All checks were successful
Deploy to Frontend Servers / deploy (push) Successful in 1m2s
All checks were successful
Deploy to Frontend Servers / deploy (push) Successful in 1m2s
This commit is contained in:
@@ -15,7 +15,9 @@ export type EthereumProvider = {
|
||||
isMetaMask?: boolean;
|
||||
isTokenPocket?: boolean;
|
||||
isImToken?: boolean;
|
||||
selectedAddress?: string;
|
||||
providers?: EthereumProvider[];
|
||||
enable?: () => Promise<unknown[]>;
|
||||
request: <T = unknown>(args: {
|
||||
method: string;
|
||||
params?: unknown[];
|
||||
@@ -75,6 +77,8 @@ async function ensureBnbChain(ethereum: EthereumProvider): Promise<void> {
|
||||
async function requestInjectedAddress(
|
||||
ethereum: EthereumProvider,
|
||||
): Promise<string> {
|
||||
if (isAddress(ethereum.selectedAddress)) return ethereum.selectedAddress;
|
||||
|
||||
const existingAccounts: unknown[] = await ethereum
|
||||
.request<unknown[]>({ method: "eth_accounts" })
|
||||
.catch((): unknown[] => []);
|
||||
@@ -85,18 +89,25 @@ async function requestInjectedAddress(
|
||||
.request<unknown[]>({
|
||||
method: "eth_requestAccounts",
|
||||
})
|
||||
.catch((error: unknown): never => {
|
||||
throw normalizeWalletError(error);
|
||||
.catch(async (error: unknown): Promise<unknown[]> => {
|
||||
if (!ethereum.enable) throw normalizeWalletError(error);
|
||||
return ethereum.enable().catch((fallbackError: unknown): never => {
|
||||
throw normalizeWalletError(fallbackError || error);
|
||||
});
|
||||
});
|
||||
const requestedAddress = requestedAccounts.find(isAddress);
|
||||
if (!requestedAddress) throw new Error("walletNoAccount");
|
||||
return requestedAddress;
|
||||
if (requestedAddress) return requestedAddress;
|
||||
if (isAddress(ethereum.selectedAddress)) return ethereum.selectedAddress;
|
||||
throw new Error("walletNoAccount");
|
||||
}
|
||||
|
||||
export function getInjectedEthereum(): EthereumProvider | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
const maybeWindow = window as typeof window & { ethereum?: EthereumProvider };
|
||||
return maybeWindow.ethereum ?? null;
|
||||
const maybeWindow = window as typeof window & {
|
||||
ethereum?: EthereumProvider;
|
||||
web3?: { currentProvider?: EthereumProvider };
|
||||
};
|
||||
return maybeWindow.ethereum ?? maybeWindow.web3?.currentProvider ?? null;
|
||||
}
|
||||
|
||||
export function isTokenPocketBrowser(): boolean {
|
||||
@@ -168,7 +179,9 @@ export async function connectInjectedWallet(
|
||||
console.info("[wallet-login] injected account", address);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user