diff --git a/package.json b/package.json index 64309e6..a6455c1 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "ethers": "^5.6.9", "express": "^4.18.1", "greenlock-express": "^4.0.3", + "hsd": "^4.0.1", "jayson": "^3.6.6", "json-stable-stringify": "^1.0.1", "libskynet": "^0.0.48", @@ -33,6 +34,7 @@ "node-cron": "^3.0.1", "node-fetch": "^3.2.6", "random-access-memory": "^4.1.0", + "random-key": "^0.3.2", "sprintf-js": "^1.1.2", "xml2js": "^0.4.23" }, diff --git a/src/constant_vars.ts b/src/constant_vars.ts index 2fc372a..0df0f5a 100644 --- a/src/constant_vars.ts +++ b/src/constant_vars.ts @@ -24,6 +24,7 @@ export const POCKET_ACCOUNT_PUBLIC_KEY = export const POCKET_ACCOUNT_PRIVATE_KEY = process.env.POCKET_ACCOUNT_PRIVATE_KEY || false; +export const HSD_USE_EXTERNAL_NODE = process.env.HSD_USE_EXTERNAL_NODE || false; export const HSD_NETWORK_TYPE = process.env.HSD_NETWORK || "main"; export const HSD_HOST = process.env.HSD_HOST || "localhost"; export const HSD_PORT = Number(process.env.HSD_PORT) || 12037; diff --git a/src/rpc/handshake.ts b/src/rpc/handshake.ts index a9ee7a3..ac7e705 100644 --- a/src/rpc/handshake.ts +++ b/src/rpc/handshake.ts @@ -1,19 +1,58 @@ +import { createRequire } from "module"; import { RpcMethodList } from "./index.js"; +// @ts-ignore +import rand from "random-key"; +// @ts-ignore +import SPVNode from "hsd/lib/node/spvnode.js"; import { HSD_API_KEY, HSD_HOST, HSD_NETWORK_TYPE, HSD_PORT, -} from "../constant_vars.js"; + HSD_USE_EXTERNAL_NODE, +} from "../constants.js"; + +const require = createRequire(import.meta.url); const { NodeClient } = require("hs-client"); -const hnsClient = new NodeClient({ - network: HSD_NETWORK_TYPE, - host: HSD_HOST, - port: HSD_PORT, - apiKey: HSD_API_KEY, -}); +let hsdServer: SPVNode; + +let clientArgs = { + network: "main", + host: "127.0.0.1", + port: 12037, + apiKey: rand.generate(), +}; + +if (!HSD_USE_EXTERNAL_NODE) { + process.env.HSD_NO_DNS = "true"; + process.env.HSD_NO_RS = "true"; + process.env.HSD_HTTP_HOST = "127.0.0.1"; + process.env.HSD_API_KEY = clientArgs.apiKey; + hsdServer = new SPVNode({ + config: false, + argv: false, + env: true, + logFile: false, + logConsole: false, + logLevel: "info", + memory: false, + workers: true, + listen: false, + network: "main", + loader: require, + }); +} else { + clientArgs = { + network: HSD_NETWORK_TYPE, + host: HSD_HOST, + port: HSD_PORT, + apiKey: HSD_API_KEY, + }; +} + +const hnsClient = new NodeClient(clientArgs); export default { getnameresource: async function (args: any, context: object) {