import { requestWalletNonce, verifyWalletSignature } from "./api"; export type EthereumProvider = { request: (args: { method: string; params?: unknown[]; }) => Promise; }; export function getInjectedEthereum(): EthereumProvider | null { if (typeof window === "undefined") return null; const maybeWindow = window as typeof window & { ethereum?: EthereumProvider }; return maybeWindow.ethereum ?? null; } export async function signInWithInjectedWallet(): Promise<{ token: string; wallet: string; }> { const ethereum = getInjectedEthereum(); if (!ethereum) throw new Error("No injected wallet found"); const accounts = await ethereum.request({ method: "eth_requestAccounts", }); const address = accounts[0]; if (!address) throw new Error("No wallet account returned"); const nonce = await requestWalletNonce(address); const signature = await ethereum.request({ method: "personal_sign", params: [nonce.message, address], }); return verifyWalletSignature({ address, message: nonce.message, signature, }); }