Skip to content

Commit 0c7c5d0

Browse files
committed
fix: add provider URL to SIWE signature verification
1 parent 288a281 commit 0c7c5d0

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

web/netlify/functions/authUser.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import middy from "@middy/core";
22
import jsonBodyParser from "@middy/http-json-body-parser";
33
import { createClient } from "@supabase/supabase-js";
4+
import { ethers } from "ethers";
45
import * as jwt from "jose";
56
import { SiweMessage } from "siwe";
67

7-
import { ETH_SIGNATURE_REGEX, DEFAULT_CHAIN } from "consts/processEnvConsts";
8+
import { ETH_SIGNATURE_REGEX, DEFAULT_CHAIN, isProductionDeployment } from "consts/processEnvConsts";
89

910
import { netlifyUri, netlifyDeployUri, netlifyDeployPrimeUri } from "src/generatedNetlifyInfo.json";
1011
import { Database } from "src/types/supabase-notification";
@@ -73,9 +74,12 @@ const authUser = async (event) => {
7374
}
7475

7576
try {
76-
await siweMessage.verify({ signature, nonce: nonceData.nonce, time: new Date().toISOString() });
77+
const alchemyChain = isProductionDeployment() ? "arb-mainnet" : "arb-sepolia";
78+
const alchemyRpcURL = `https://${alchemyChain}.g.alchemy.com/v2/${process.env.ALCHEMY_FUNCTIONS_API_KEY}`;
79+
const provider = new ethers.providers.JsonRpcProvider(alchemyRpcURL);
80+
await siweMessage.verify({ signature, nonce: nonceData.nonce, time: new Date().toISOString() }, { provider });
7781
} catch (err) {
78-
throw new Error("Invalid signer");
82+
throw new Error("Invalid signer: " + JSON.stringify(err));
7983
}
8084

8185
const { error } = await supabase.from("user-nonce").delete().match({ address: lowerCaseAddress });

web/src/consts/chains.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { extractChain } from "viem";
2-
import { Chain, arbitrum, mainnet, arbitrumSepolia, gnosisChiado } from "viem/chains";
2+
import { Chain, arbitrum, mainnet, arbitrumSepolia, gnosis, gnosisChiado } from "viem/chains";
33

44
import { isProductionDeployment } from "./index";
55

66
export const DEFAULT_CHAIN = isProductionDeployment() ? arbitrum.id : arbitrumSepolia.id;
77

8+
// Read/Write
89
export const SUPPORTED_CHAINS: Record<number, Chain> = {
910
[isProductionDeployment() ? arbitrum.id : arbitrumSepolia.id]: isProductionDeployment() ? arbitrum : arbitrumSepolia,
1011
};
1112

13+
// Read Only
1214
export const QUERY_CHAINS: Record<number, Chain> = {
1315
[gnosisChiado.id]: gnosisChiado,
16+
[gnosis.id]: gnosis,
1417
[mainnet.id]: mainnet,
1518
};
1619

web/src/context/Web3Provider.tsx

+42-17
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,59 @@ import React from "react";
33
import { createWeb3Modal } from "@web3modal/wagmi/react";
44
import { type Chain } from "viem";
55
import { createConfig, fallback, http, WagmiProvider, webSocket } from "wagmi";
6-
import { mainnet, arbitrumSepolia, arbitrum, gnosisChiado } from "wagmi/chains";
6+
import { mainnet, arbitrumSepolia, arbitrum, gnosisChiado, gnosis, sepolia } from "wagmi/chains";
77
import { walletConnect } from "wagmi/connectors";
88

9-
import { ALL_CHAINS } from "consts/chains";
9+
import { ALL_CHAINS, DEFAULT_CHAIN } from "consts/chains";
1010
import { isProductionDeployment } from "consts/index";
1111

1212
import { lightTheme } from "styles/themes";
1313

14-
const projectId = import.meta.env.WALLETCONNECT_PROJECT_ID ?? "";
15-
export const alchemyApiKey = import.meta.env.ALCHEMY_API_KEY ?? "";
14+
const alchemyApiKey = import.meta.env.ALCHEMY_API_KEY ?? "";
15+
const isProduction = isProductionDeployment();
1616

17-
const chains = ALL_CHAINS as [Chain, ...Chain[]];
17+
// https://github.com/alchemyplatform/alchemy-sdk-js/blob/96b3f62/src/types/types.ts#L98-L119
18+
const alchemyToViemChain = {
19+
[arbitrum.id]: "arb-mainnet",
20+
[arbitrumSepolia.id]: "arb-sepolia",
21+
[mainnet.id]: "eth-mainnet",
22+
[sepolia.id]: "eth-sepolia",
23+
};
1824

1925
type AlchemyProtocol = "https" | "wss";
20-
type AlchemyChain = "arb-sepolia" | "eth-mainnet" | "arb";
21-
const alchemyURL = (protocol: AlchemyProtocol, chain: AlchemyChain) =>
22-
`${protocol}://${chain}.g.alchemy.com/v2/${alchemyApiKey}`;
23-
const alchemyTransport = (chain: AlchemyChain) =>
24-
fallback([webSocket(alchemyURL("wss", chain)), http(alchemyURL("https", chain))]);
25-
26-
const transports = {
27-
[isProductionDeployment() ? arbitrum.id : arbitrumSepolia.id]: isProductionDeployment()
28-
? alchemyTransport("arb")
29-
: alchemyTransport("arb-sepolia"),
30-
[mainnet.id]: alchemyTransport("eth-mainnet"),
31-
[gnosisChiado.id]: fallback([webSocket("wss://rpc.chiadochain.net/wss"), http("https://rpc.chiadochain.net")]),
26+
27+
// https://github.com/alchemyplatform/alchemy-sdk-js/blob/96b3f62/src/util/const.ts#L16-L18
28+
const alchemyURL = (protocol: AlchemyProtocol, chainId: number) =>
29+
`${protocol}://${alchemyToViemChain[chainId]}.g.alchemy.com/v2/${alchemyApiKey}`;
30+
31+
export const getChainRpcUrl = (protocol: AlchemyProtocol, chainId: number) => {
32+
return alchemyURL(protocol, chainId);
33+
};
34+
35+
export const getDefaultChainRpcUrl = (protocol: AlchemyProtocol) => {
36+
return getChainRpcUrl(protocol, DEFAULT_CHAIN);
37+
};
38+
39+
export const getTransports = () => {
40+
const alchemyTransport = (chain: Chain) =>
41+
fallback([http(alchemyURL("https", chain.id)), webSocket(alchemyURL("wss", chain.id))]);
42+
const defaultTransport = (chain: Chain) =>
43+
fallback([http(chain.rpcUrls.default?.http?.[0]), webSocket(chain.rpcUrls.default?.webSocket?.[0])]);
44+
45+
return {
46+
[isProduction ? arbitrum.id : arbitrumSepolia.id]: isProduction
47+
? alchemyTransport(arbitrum)
48+
: alchemyTransport(arbitrumSepolia),
49+
[isProduction ? gnosis.id : gnosisChiado.id]: isProduction
50+
? defaultTransport(gnosis)
51+
: defaultTransport(gnosisChiado),
52+
[mainnet.id]: alchemyTransport(mainnet), // Always enabled for ENS resolution
53+
};
3254
};
3355

56+
const chains = ALL_CHAINS as [Chain, ...Chain[]];
57+
const transports = getTransports();
58+
const projectId = import.meta.env.WALLETCONNECT_PROJECT_ID ?? "";
3459
const wagmiConfig = createConfig({
3560
chains,
3661
transports,

0 commit comments

Comments
 (0)