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