|
1 | 1 | import { computed, type ComputedRef, type Ref, ref, watch } from "vue"; |
2 | 2 |
|
3 | 3 | import { useStorage } from "@vueuse/core"; |
4 | | -import { JsonRpcProvider } from "ethers"; |
| 4 | +import { FetchRequest, JsonRpcProvider, makeError } from "ethers"; |
5 | 5 |
|
6 | 6 | import useEnvironmentConfig from "./useEnvironmentConfig"; |
7 | 7 | import { DEFAULT_NETWORK } from "./useRuntimeConfig"; |
@@ -31,6 +31,59 @@ export type Context = { |
31 | 31 | isGatewaySettlementChain: (chainId: number | null) => boolean; |
32 | 32 | }; |
33 | 33 |
|
| 34 | +// Prividium authorizes every RPC call against the caller, and only the explorer API session |
| 35 | +// holds the user's token, so RPC calls go through the API instead of straight to the RPC. |
| 36 | +// The session cookie is set on the API origin and ethers does not send cross-origin |
| 37 | +// credentials, so the request is made with a fetch that includes them. |
| 38 | +function getRpcRequest(network: NetworkConfig) { |
| 39 | + if (!network.prividium) { |
| 40 | + return network.rpcUrl; |
| 41 | + } |
| 42 | + |
| 43 | + const request = new FetchRequest(`${network.apiUrl}/rpc`); |
| 44 | + // Overriding getUrlFunc replaces ethers' own fetch, which arms `req.timeout` and forwards |
| 45 | + // cancellation, so both are reproduced here to keep requests bounded and abortable. |
| 46 | + request.getUrlFunc = async (req, signal) => { |
| 47 | + const controller = new AbortController(); |
| 48 | + let abortError: Error | null = null; |
| 49 | + const timer = setTimeout(() => { |
| 50 | + abortError = makeError("request timeout", "TIMEOUT"); |
| 51 | + controller.abort(); |
| 52 | + }, req.timeout); |
| 53 | + signal?.addListener(() => { |
| 54 | + abortError = makeError("request cancelled", "CANCELLED"); |
| 55 | + controller.abort(); |
| 56 | + }); |
| 57 | + |
| 58 | + let response: Response; |
| 59 | + try { |
| 60 | + response = await fetch(req.url, { |
| 61 | + method: req.method, |
| 62 | + headers: req.headers, |
| 63 | + body: req.body, |
| 64 | + credentials: "include", |
| 65 | + signal: controller.signal, |
| 66 | + }); |
| 67 | + } catch (error) { |
| 68 | + throw abortError ?? error; |
| 69 | + } finally { |
| 70 | + clearTimeout(timer); |
| 71 | + } |
| 72 | + |
| 73 | + const headers: Record<string, string> = {}; |
| 74 | + response.headers.forEach((value, key) => { |
| 75 | + headers[key.toLowerCase()] = value; |
| 76 | + }); |
| 77 | + return { |
| 78 | + statusCode: response.status, |
| 79 | + statusMessage: response.statusText, |
| 80 | + headers, |
| 81 | + body: new Uint8Array(await response.arrayBuffer()), |
| 82 | + }; |
| 83 | + }; |
| 84 | + return request; |
| 85 | +} |
| 86 | + |
34 | 87 | let l2Provider: JsonRpcProvider | null; |
35 | 88 | export default (): Context => { |
36 | 89 | const environmentConfig = useEnvironmentConfig(); |
@@ -78,7 +131,7 @@ export default (): Context => { |
78 | 131 |
|
79 | 132 | function getL2Provider() { |
80 | 133 | if (!l2Provider) { |
81 | | - l2Provider = new JsonRpcProvider(currentNetwork.value.rpcUrl, currentNetwork.value.l2ChainId, { |
| 134 | + l2Provider = new JsonRpcProvider(getRpcRequest(currentNetwork.value), currentNetwork.value.l2ChainId, { |
82 | 135 | staticNetwork: true, |
83 | 136 | }); |
84 | 137 | } |
|
0 commit comments