88 lines
2.0 KiB
TypeScript
88 lines
2.0 KiB
TypeScript
import type { Plugin, PluginAPI } from "@lumeweb/interface-relay";
|
|
import fetch, { Request, RequestInit } from "node-fetch";
|
|
|
|
import { URL } from "url";
|
|
|
|
const EXECUTION_RPC_URL =
|
|
"https://g.w.lavanet.xyz:443/gateway/eth/rpc-http/f195d68175eb091ec1f71d00f8952b85";
|
|
|
|
const CONSENSUS_RPC_URL = "https://www.lightclientdata.org";
|
|
|
|
async function doFetch(url: string, request: RequestInit) {
|
|
sanitizeRequestArgs(url, request);
|
|
|
|
let req = new Request(url, {
|
|
...request,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
const resp = await fetch(req);
|
|
|
|
return (await resp.json()) as any;
|
|
}
|
|
|
|
function sanitizeRequestArgs(url: string, request: RequestInit) {
|
|
if (!request || typeof request !== "object") {
|
|
throw Error("invalid request");
|
|
}
|
|
|
|
[
|
|
"agent",
|
|
"hostname",
|
|
"referrer",
|
|
"referrerPolicy",
|
|
"compress",
|
|
"port",
|
|
"protocol",
|
|
"hostname",
|
|
"insecureHTTPParser",
|
|
"highWaterMark",
|
|
"size",
|
|
].forEach((element) => {
|
|
if (element in request) {
|
|
delete request[element];
|
|
}
|
|
});
|
|
}
|
|
|
|
interface ConsensusRequest extends RequestInit {
|
|
path: string;
|
|
}
|
|
interface ExecutionRequest {
|
|
method: string;
|
|
params: string;
|
|
}
|
|
|
|
const plugin: Plugin = {
|
|
name: "eth",
|
|
async plugin(api: PluginAPI): Promise<void> {
|
|
api.registerMethod("consensus_request", {
|
|
cacheable: false,
|
|
async handler(request: ConsensusRequest): Promise<object> {
|
|
const consensusUrl = new URL(CONSENSUS_RPC_URL);
|
|
|
|
let dummyUrl = new URL(
|
|
`http://dummy/${request.path.replace(/^\/+/, "")}`
|
|
);
|
|
consensusUrl.pathname = dummyUrl.pathname;
|
|
consensusUrl.search = dummyUrl.search;
|
|
|
|
return doFetch(consensusUrl.toString(), request);
|
|
},
|
|
});
|
|
api.registerMethod("execution_request", {
|
|
cacheable: false,
|
|
async handler(request: ExecutionRequest): Promise<object> {
|
|
return doFetch(EXECUTION_RPC_URL, {
|
|
method: "POST",
|
|
body: JSON.stringify(request),
|
|
});
|
|
},
|
|
});
|
|
},
|
|
};
|
|
|
|
export default plugin;
|