*Initial version

This commit is contained in:
Derrick Hammer 2022-08-28 13:07:45 -04:00
parent 58fcc4e09e
commit 18756c5533
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
6 changed files with 166 additions and 1 deletions

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2022 Lume Web Copyright (c) 2022 Hammer Technologies LLC
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

9
build.js Normal file
View File

@ -0,0 +1,9 @@
import esbuild from 'esbuild'
esbuild.buildSync({
entryPoints: ['src/index.ts'],
outfile: 'dist/handshake.js',
format: 'cjs',
bundle: true,
platform: "node"
})

9
build.sh Normal file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
node build.js
mkdir -p dist/build/Release
cp node_modules/bdb/build/Release/leveldown.node dist/build/Release/leveldown.node
cp node_modules/bcrypto/build/Release/bcrypto.node dist/build/Release/bcrypto.node
cp node_modules/goosig/build/Release/goosig.node dist/build/Release/goosig.node
cp node_modules/mrmr/build/Release/mrmr.node dist/build/Release/mrmr.node
cp node_modules/hsd/lib/covenants/names.db dist

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "@lumeweb/relay-plugin-handshake",
"type": "module",
"version": "0.1.0",
"scripts": {
"build": "bash build.sh"
},
"devDependencies": {
"@lumeweb/relay": "https://github.com/LumeWeb/relay.git",
"esbuild": "^0.15.5"
},
"dependencies": {
"hs-client": "^0.0.11",
"hsd": "^4.0.1",
"random-key": "^0.3.2"
}
}

116
src/index.ts Normal file
View File

@ -0,0 +1,116 @@
import type {
Plugin,
PluginAPI,
RPCResponse,
RPCRequest,
} from "@lumeweb/relay";
// @ts-ignore
import rand from "random-key";
// @ts-ignore
import SPVNode from "hsd/lib/node/spvnode.js";
// @ts-ignore
import { NodeClient } from "hs-client";
async function boot(config: any) {
let hsdServer: SPVNode;
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) => {
const timeout = setTimeout(() => {
console.error("Shutdown is taking a long time. Exiting.");
process.exit(3);
}, 5000);
timeout.unref();
try {
console.error("Shutting down...");
await hsdServer.close();
clearTimeout(timeout);
console.error((err as Error).stack);
process.exit(2);
} catch (e: any) {
console.error(
`Error occurred during shutdown: ${(e as Error).message}`
);
process.exit(3);
}
});
(async () => {
try {
await hsdServer.ensure();
await hsdServer.open();
await hsdServer.connect();
hsdServer.startSync();
} catch (e: any) {
console.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.config);
api.registerMethod("getnameresource", {
cacheable: true,
async handler(request: RPCRequest): Promise<RPCResponse> {
let resp;
try {
resp = await client.execute("getnameresource", request.data);
} 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;
},
});
},
};
export default plugin;

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"rootDir": "src",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
}
}