Skip to content

Commit a8f1bfa

Browse files
committed
fix: only allow linea sepolia
1 parent 31b8d32 commit a8f1bfa

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

packages/rln/src/utils/walletClient.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
PublicClient,
66
WalletClient
77
} from "viem";
8-
import { type Chain, lineaSepolia } from "viem/chains";
8+
import { lineaSepolia } from "viem/chains";
99

10-
export const createViemClientFromWindow = async (
11-
chain: Chain = lineaSepolia
12-
): Promise<WalletClient & PublicClient> => {
10+
export const createViemClientFromWindow = async (): Promise<
11+
WalletClient & PublicClient
12+
> => {
1313
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1414
const ethereum = (window as any).ethereum;
1515

@@ -23,9 +23,26 @@ export const createViemClientFromWindow = async (
2323

2424
const rpcClient = createWalletClient({
2525
account,
26-
chain,
26+
chain: lineaSepolia,
2727
transport: custom(ethereum)
2828
}).extend(publicActions);
2929

30+
// Ensure wallet is connected to Linea Sepolia
31+
try {
32+
await rpcClient.switchChain({ id: lineaSepolia.id });
33+
} catch (error: unknown) {
34+
// This error code indicates that the chain has not been added to the wallet
35+
if (
36+
typeof error === "object" &&
37+
error !== null &&
38+
"code" in error &&
39+
error.code === 4902
40+
) {
41+
await rpcClient.addChain({ chain: lineaSepolia });
42+
} else {
43+
throw error;
44+
}
45+
}
46+
3047
return rpcClient;
3148
};

0 commit comments

Comments
 (0)