*Rewrite to use new trustless proxy method
ci/woodpecker/push/woodpecker Pipeline is pending
Details
ci/woodpecker/push/woodpecker Pipeline is pending
Details
This commit is contained in:
parent
010518a084
commit
bd7a343ed6
|
@ -19,9 +19,9 @@
|
|||
"vite": "^4.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lumeweb/libhyperproxy": "https://git.lumeweb.com/LumeWeb/libhyperproxy.git",
|
||||
"@lumeweb/relay-plugin-rollup-preset": "https://git.lumeweb.com/LumeWeb/relay-plugin-rollup-preset.git",
|
||||
"@lumeweb/relay-types": "https://git.lumeweb.com/LumeWeb/relay-types.git",
|
||||
"hs-client": "^0.0.11",
|
||||
"hsd": "https://github.com/LumeWeb/hsd.git#spv-namestate",
|
||||
"object-merger": "^1.0.3",
|
||||
"random-key": "^0.3.2"
|
||||
|
|
125
src/index.ts
125
src/index.ts
|
@ -2,38 +2,15 @@ import type { Plugin, PluginAPI } from "@lumeweb/relay-types";
|
|||
// @ts-ignore
|
||||
import rand from "random-key";
|
||||
// @ts-ignore
|
||||
import SPVNode from "hsd/lib/node/spvnode.js";
|
||||
// @ts-ignore
|
||||
import { NodeClient } from "hs-client";
|
||||
import FullNode from "hsd/lib/node/fullnode.js";
|
||||
import { Proxy, Socket } from "@lumeweb/libhyperproxy";
|
||||
|
||||
async function boot(api: PluginAPI) {
|
||||
let hsdServer: SPVNode;
|
||||
let server: FullNode;
|
||||
let api: PluginAPI;
|
||||
|
||||
const { config } = api;
|
||||
const PROTOCOL = "lumeweb.proxy.handshake";
|
||||
|
||||
let clientArgs = {
|
||||
network: "main",
|
||||
host: "127.0.0.1",
|
||||
port: 12037,
|
||||
apiKey: rand.generate(),
|
||||
};
|
||||
|
||||
if (!config.bool("hsd-use-external-node")) {
|
||||
hsdServer = new SPVNode({
|
||||
config: false,
|
||||
argv: false,
|
||||
env: true,
|
||||
noDns: true,
|
||||
memory: false,
|
||||
httpHost: "127.0.0.1",
|
||||
apiKey: clientArgs.apiKey,
|
||||
logFile: false,
|
||||
logConsole: true,
|
||||
logLevel: "info",
|
||||
workers: true,
|
||||
network: "main",
|
||||
});
|
||||
hsdServer.on("abort", async (err: any) => {
|
||||
async function abort(err: any) {
|
||||
const timeout = setTimeout(() => {
|
||||
api.logger.error("Shutdown is taking a long time. Exiting.");
|
||||
process.exit(3);
|
||||
|
@ -43,70 +20,72 @@ async function boot(api: PluginAPI) {
|
|||
|
||||
try {
|
||||
api.logger.error("Shutting down...");
|
||||
await hsdServer.close();
|
||||
await server.close();
|
||||
clearTimeout(timeout);
|
||||
api.logger.error((err as Error).stack);
|
||||
process.exit(2);
|
||||
} catch (e: any) {
|
||||
api.logger.error(
|
||||
`Error occurred during shutdown: ${(e as Error).message}`
|
||||
);
|
||||
api.logger.error(`Error occurred during shutdown: ${(e as Error).message}`);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
async function boot(api: PluginAPI) {
|
||||
const { pluginConfig: config } = api;
|
||||
|
||||
const apiKey = rand.generate();
|
||||
|
||||
if (!config.bool("external")) {
|
||||
server = new FullNode({
|
||||
config: false,
|
||||
argv: false,
|
||||
env: true,
|
||||
noDns: false,
|
||||
memory: false,
|
||||
httpHost: "127.0.0.1",
|
||||
apiKey,
|
||||
logFile: true,
|
||||
logConsole: true,
|
||||
logLevel: "info",
|
||||
workers: true,
|
||||
network: "main",
|
||||
bip37: true,
|
||||
});
|
||||
server.on("abort", abort);
|
||||
|
||||
api.logger.info("API Key %s", apiKey);
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await hsdServer.ensure();
|
||||
await hsdServer.open();
|
||||
await hsdServer.connect();
|
||||
await server.ensure();
|
||||
await server.open();
|
||||
await server.connect();
|
||||
|
||||
hsdServer.startSync();
|
||||
server.startSync();
|
||||
} catch (e: any) {
|
||||
api.logger.error((e as Error).stack);
|
||||
}
|
||||
})();
|
||||
} else {
|
||||
clientArgs = {
|
||||
network: config.str("hsd-network-type"),
|
||||
host: config.str("hsd-host"),
|
||||
port: config.uint("hsd-port"),
|
||||
apiKey: config.str("hsd-api-key"),
|
||||
};
|
||||
}
|
||||
|
||||
return new NodeClient(clientArgs);
|
||||
}
|
||||
|
||||
const plugin: Plugin = {
|
||||
name: "handshake",
|
||||
async plugin(api: PluginAPI): Promise<void> {
|
||||
const client = await boot(api);
|
||||
|
||||
api.registerMethod("getnameresource", {
|
||||
cacheable: true,
|
||||
async handler(name: string): Promise<any> {
|
||||
let resp;
|
||||
try {
|
||||
resp = await client.execute("getnameresource", name);
|
||||
} catch (e: any) {
|
||||
e = e as Error;
|
||||
const eType = e.type.toLowerCase();
|
||||
const eMessage = e.message.toLowerCase();
|
||||
|
||||
if (
|
||||
eType === "rpcerror" &&
|
||||
eMessage.includes("chain is not synced")
|
||||
) {
|
||||
throw new Error("NOT_READY");
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
return resp;
|
||||
async plugin(_api: PluginAPI): Promise<void> {
|
||||
api = _api;
|
||||
await boot(api);
|
||||
const proxy = new Proxy({
|
||||
swarm: api.swarm,
|
||||
protocol: PROTOCOL,
|
||||
});
|
||||
api.swarm.join(api.util.crypto.createHash(PROTOCOL));
|
||||
api.protocols.register(PROTOCOL, (peer: any, muxer: any) => {
|
||||
proxy.handlePeer({
|
||||
peer,
|
||||
muxer,
|
||||
onopen(socket: Socket) {
|
||||
server.pool.server.emit("connection", socket);
|
||||
},
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue