import { createBaseAccountSDK } from "@base-org/account";// Initialize the SDKconst provider = createBaseAccountSDK({ appName: "My App",}).getProvider();// 1 — get a fresh nonce (generate locally or prefetch from backend)const nonce = window.crypto.randomUUID().replace(/-/g, "");// OR prefetch from server// const nonce = await fetch("/auth/nonce").then((response) => response.text());// 2 — switch to Base Chainconst switchChainResponse = await provider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: "0x2105" }],});console.log("Switch chain response:", switchChainResponse);// 3 — connect and authenticatetry { const { accounts } = await provider.request({ method: "wallet_connect", params: [ { version: "1", capabilities: { signInWithEthereum: { nonce, chainId: "0x2105", // Base Mainnet - 8453 }, }, }, ], }); const { address } = accounts[0]; const { message, signature } = accounts[0].capabilities.signInWithEthereum; await fetch("/auth/verify", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ address, message, signature }), });} catch (error) { console.error("Failed to authenticate with Base Account:", error);}
위 코드를 Base Account 외에도 사용하는 경우, 모든 지갑이 새로운 wallet_connect
메서드를 아직 지원하지 않는다는 점에 유의하세요.
호출이 [method_not_supported]를 발생시키면 eth_requestAccounts와 personal_sign을 사용하도록 폴백하세요.
팝업
차단을 피하려면
사용자가 “Base로 로그인” 버튼을 누르기 전에 (예: 페이지 로드 시) 논스를 가져오거나 생성하세요.
보안을 위해, 백엔드가 모든 논스를 추적하고 재사용된 논스를 거부하기만 하면 됩니다. 논스의 출처는 관계없습니다.