From a8f1bfa4d837067367e71602d735576d831f7b2f Mon Sep 17 00:00:00 2001 From: Arseniy Klempner Date: Wed, 12 Nov 2025 14:15:30 -0800 Subject: [PATCH] fix: only allow linea sepolia --- packages/rln/src/utils/walletClient.ts | 27 +++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/rln/src/utils/walletClient.ts b/packages/rln/src/utils/walletClient.ts index 86cb947d8e..bde294c276 100644 --- a/packages/rln/src/utils/walletClient.ts +++ b/packages/rln/src/utils/walletClient.ts @@ -5,11 +5,11 @@ import { PublicClient, WalletClient } from "viem"; -import { type Chain, lineaSepolia } from "viem/chains"; +import { lineaSepolia } from "viem/chains"; -export const createViemClientFromWindow = async ( - chain: Chain = lineaSepolia -): Promise => { +export const createViemClientFromWindow = async (): Promise< + WalletClient & PublicClient +> => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const ethereum = (window as any).ethereum; @@ -23,9 +23,26 @@ export const createViemClientFromWindow = async ( const rpcClient = createWalletClient({ account, - chain, + chain: lineaSepolia, transport: custom(ethereum) }).extend(publicActions); + // Ensure wallet is connected to Linea Sepolia + try { + await rpcClient.switchChain({ id: lineaSepolia.id }); + } catch (error: unknown) { + // This error code indicates that the chain has not been added to the wallet + if ( + typeof error === "object" && + error !== null && + "code" in error && + error.code === 4902 + ) { + await rpcClient.addChain({ chain: lineaSepolia }); + } else { + throw error; + } + } + return rpcClient; };