From df6da15952775686167780289c7ffb1b82f47edb Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 19 Mar 2023 17:17:05 -0400 Subject: [PATCH] *Initial version --- LICENSE | 2 +- build.js | 9 +++++++ pkg/load-eth.json | 3 +++ src/index.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 13 ++++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 build.js create mode 100644 pkg/load-eth.json create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/LICENSE b/LICENSE index 2071b23..bd66f50 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2023 Hammer Technologies LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/build.js b/build.js new file mode 100644 index 0000000..ade97a3 --- /dev/null +++ b/build.js @@ -0,0 +1,9 @@ +import esbuild from "esbuild"; + +esbuild.buildSync({ + entryPoints: ["src/index.ts"], + outfile: "dist/eth.js", + format: "cjs", + bundle: true, + platform: "node", +}); diff --git a/pkg/load-eth.json b/pkg/load-eth.json new file mode 100644 index 0000000..6177b47 --- /dev/null +++ b/pkg/load-eth.json @@ -0,0 +1,3 @@ +{ + "plugins": ["eth"] +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..844a5e8 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,62 @@ +import type { Plugin, PluginAPI } from "@lumeweb/interface-relay"; +import fetch, { Request, RequestInit } from "node-fetch"; + +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(request); + + let req = new Request(url, request); + + const resp = await fetch(req); + + return (await resp.json()) as any; +} + +function sanitizeRequestArgs(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]; + } + }); +} + +const plugin: Plugin = { + name: "eth", + async plugin(api: PluginAPI): Promise { + api.registerMethod("execution_request", { + cacheable: false, + async handler(request: RequestInit): Promise { + return doFetch(EXECUTION_RPC_URL, request); + }, + }); + + api.registerMethod("consensus_request", { + cacheable: false, + async handler(request: RequestInit): Promise { + return doFetch(CONSENSUS_RPC_URL, request); + }, + }); + }, +}; + +export default plugin; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..09c3093 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "module": "esnext", + "target": "esnext", + "esModuleInterop": true, + "outDir": "dist", + "moduleResolution": "node" + }, + "include": ["src"], + "exclude": [ + "node_modules" + ] +}