Skip to content

Commit

Permalink
0.12.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dkackman committed Sep 4, 2023
1 parent da8867e commit e87f70d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/chia-daemon-tests/daemon.live.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("chia-daemon", () => {
expect(error).to.equal(true);
expect(connected).to.equal(false);
});
it("should return true on valid connection", async function () {
it("should return true on valid connection _DEBUG_", async function () {
const chia = new ChiaDaemon(valid_connection, "tests");
let error = false;
chia.on("socket-error", (e) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/chia-daemon/chia_daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ export default class ChiaDaemon extends EventEmitter {
throw new Error("Already connected");
}

this.emit("connecting", this.connection.daemonAddress);
this.emit("connecting", this.connection.serviceAddress);

// the lifetime of the websocket is between connect and disconnect
const ws = new WebSocket(
this.connection.daemonAddress,
this.connection.serviceAddress,
this.connection.createClientOptions()
);

Expand Down
26 changes: 21 additions & 5 deletions packages/chia-daemon/connection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { readFileSync } from "fs";
import untildify from "./untildify.js";

/**
* Encapsulates the specific details needed to connect to a chia service.
*/
export default class Connection {
/**
*
* @param {string} service - The service name - see ServiceNames
* @param {string} host - The host (ip or hostname) of the chia service.
* @param {number} port - The port of the chia service.
* @param {string} key_path - The path to the key file for the service (can have ~).
* @param {string} cert_path - The path to the cert file for the service (can have ~).
* @param {number} timeout_seconds - The timeout in seconds for the connection.
*/
constructor(service, host, port, key_path, cert_path, timeout_seconds) {
this.service = service;
this.host = host;
this.host = host;
this.port = port;
this.key_path = key_path;
this.cert_path = cert_path;
this.timeout_seconds = timeout_seconds;
}

/**
* @returns {object} The options for the WebSocket
* or Https connection. This includes reading the
* cert and key files from disk
*/
createClientOptions() {
return {
rejectUnauthorized: false,
Expand All @@ -21,11 +37,11 @@ export default class Connection {
};
}

get daemonAddress() {
return `wss://${this.host}:${this.port}`;
}

/** formatted address */
get serviceAddress() {
if (this.service === "daemon") {
return `wss://${this.host}:${this.port}`;
}
return `https://${this.host}:${this.port}`;
}
}
36 changes: 28 additions & 8 deletions packages/chia-daemon/connection_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const ServiceNames = {
DataLayer: "data_layer",
};

export const ServicePorts = {
/**
* The default ports for the services. (https://docs.chia.net/rpc)
*/
export const DefaultServicePorts = {
daemon: 55400,
full_node: 8555,
wallet: 9256,
Expand All @@ -21,22 +24,39 @@ export const ServicePorts = {
data_layer: 8562,
};

/**
* Creates a connection to a service.
* @param {string} serviceName - The service for the command. One of the ServiceNames constants.
* @param {string} host - The host (ip or hostname) of the chia service.
* @param {string} root - The path to the chia root directory. (can also be a path to the ssl directory)
* @param {int} timeoutSeconds - The timeout in seconds for the connection.
* @param {object} portMap - The port map for the services. Defaults to DefaultServicePorts.
* @returns {Connection} The connection object
*/
export function createConnection(
serviceName,
host,
chiaRoot,
timeoutSeconds = 30
root,
timeoutSeconds = 30,
portMap = DefaultServicePorts
) {
if (chiaRoot === undefined) {
chiaRoot = getChiaRoot();
// if the user didn't specify a root, try to find it
if (root === undefined) {
root = getChiaRoot();
}

// if the root isn't an ssl path, assume it's the
// chia root and append the ssl path
if (!root.endsWith("/config/ssl")) {
root = `${root}/config/ssl`;
}

return new Connection(
serviceName,
host,
ServicePorts[serviceName],
`${chiaRoot}/config/ssl/${serviceName}/private_${serviceName}.key`,
`${chiaRoot}/config/ssl/${serviceName}/private_${serviceName}.crt`,
portMap[serviceName],
`${root}/${serviceName}/private_${serviceName}.key`,
`${root}/${serviceName}/private_${serviceName}.crt`,
timeoutSeconds
);
}
6 changes: 4 additions & 2 deletions packages/chia-daemon/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import ChiaDaemon from "./chia_daemon.js";
import { ChiaHttps, createHttpsService } from "./chia_https.js";
import Connection from "./connection.js";
import { localDaemonConnection } from "./chia_daemon.js";
import { getPayloadDescriptor, makePayload } from "./payload_generator.js";
import createRpcProxy from "./rpc_proxy.js";
import loadUIConfig from "./config.js";
import { createConnection, ServicePorts } from "./connection_factory.js";
import { createConnection, DefaultServicePorts } from "./connection_factory.js";

export {
ChiaDaemon,
ChiaHttps,
Connection,
localDaemonConnection,
getPayloadDescriptor,
makePayload,
createRpcProxy,
loadUIConfig,
createConnection,
createHttpsService,
ServicePorts,
DefaultServicePorts,
};
2 changes: 1 addition & 1 deletion packages/chia-daemon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chia-daemon",
"version": "0.12.0",
"version": "0.12.1",
"description": "A JS daemon client for chia rpc with dynamically generated end points",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e87f70d

Please sign in to comment.