chia-agent 14.0.0-beta.0 → 14.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,60 @@
3
3
  ## [14.0.0]
4
4
  ### Breaking change
5
5
  - When RPC API responds with `success: false`, its `Promise` now does `reject`. (Previously it does `resolve`)
6
+ ### Changed
7
+ - Loosened a type of `agent` to call RPC APIs. RPC APIs can be invoked with `agent` which just implements
8
+ `sendMessage` method depicted as below.
9
+ ```typescript
10
+ export interface APIAgent {
11
+ sendMessage<M extends unknown>(
12
+ destination: string,
13
+ command: string,
14
+ data?: Record<string, unknown>,
15
+ ): Promise<M>;
16
+ }
17
+ ```
18
+ ### Added
19
+ - Added connectivity options for `RPCAgent`.
20
+ - `keepAlive` (default: `true`)
21
+ - `keepAliveMsecs` (default: `1000`)
22
+ - `maxSockets` (default: `Infinity`)
23
+ - `timeout` (default: `undefined`)
24
+ ```typescript
25
+ // Usage
26
+ const {RPCAgent} = require("chia-agent");
27
+ const {get_plots} = require("chia-agent/api/rpc");
28
+
29
+ const agent = new RPCAgent({
30
+ service: "harvester",
31
+ keepAlive: true,
32
+ keepAliveMsecs: 3000,
33
+ maxSockets: 1, // Avoid to set `1` if your requests may be sent in parallel.
34
+ timeout: 5000,
35
+ });
36
+ const res = await get_plots(agent);
37
+ ```
38
+ - Added `httpsAgent`, `httpAgent` option for `RPCAgent`.
39
+ You can now configure and inject your own `require('https').Agent` into `RPCAgent`.
40
+ ```typescript
41
+ // Usage
42
+ const {Agent: HttpsAgent} = require("https"); // or const {Agent: HttpAgent} = require('http');
43
+ const {RPCAgent} = require("chia-agent");
44
+ const {get_plots} = require("chia-agent/api/rpc");
45
+
46
+ const httpsAgent = new HttpsAgent({
47
+ host: "localhost",
48
+ port: 8560,
49
+ ca: ...,
50
+ cert: ...,
51
+ key: ...,
52
+ rejectUnauthorized: false,
53
+ });
54
+ const agent = new RPCAgent({httpsAgent: httpsAgent}); // `new RPCAgent({httpAgent: httpAgent});` is also allowed.
55
+ const res = await get_plots(agent);
56
+ ```
57
+
58
+ ### Fixed
59
+ - Fixed an issue where some of the RPC Pool APIs did not handle request parameters correctly.
6
60
 
7
61
  ## [13.2.0]
8
62
  ### Added
@@ -0,0 +1,3 @@
1
+ export interface APIAgent {
2
+ sendMessage<M extends unknown>(destination: string, command: string, data?: Record<string, unknown>): Promise<M>;
3
+ }
package/agent/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,25 +1,25 @@
1
1
  import { str, uint64 } from "../../chia/types/_python_types_";
2
- import { TRPCAgent } from "../../../rpc";
2
+ import type { RPCAgent } from "../../../rpc";
3
3
  import { ErrorResponse, GetFarmerResponse, GetPoolInfoResponse, PostFarmerRequest, PostFarmerResponse, PostPartialRequest, PostPartialResponse, PutFarmerRequest, PutFarmerResponse } from "../../chia/protocols/pool_protocol";
4
4
  import { FarmerRecord } from "../../chia/pool/store";
5
5
  export declare type TPoolInfoResponse = GetPoolInfoResponse;
6
- export declare function pool_info(agent: TRPCAgent): Promise<GetPoolInfoResponse>;
6
+ export declare function pool_info(agent: RPCAgent): Promise<GetPoolInfoResponse>;
7
7
  export declare type TGetFarmerRequest = {
8
8
  launcher_id: str;
9
9
  authentication_token: str;
10
10
  signature: str;
11
11
  };
12
12
  export declare type TGetFarmerResponse = GetFarmerResponse;
13
- export declare function get_farmer(agent: TRPCAgent, data: TGetFarmerRequest): Promise<GetFarmerResponse>;
13
+ export declare function get_farmer(agent: RPCAgent, data: TGetFarmerRequest): Promise<GetFarmerResponse>;
14
14
  export declare type TPostFarmerRequest = PostFarmerRequest;
15
15
  export declare type TPostFarmerResponse = PostFarmerResponse | ErrorResponse;
16
- export declare function post_farmer(agent: TRPCAgent, data: TPostFarmerRequest): Promise<TPostFarmerResponse>;
16
+ export declare function post_farmer(agent: RPCAgent, data: TPostFarmerRequest): Promise<TPostFarmerResponse>;
17
17
  export declare type TPutFarmerRequest = PutFarmerRequest;
18
18
  export declare type TPutFarmerResponse = PutFarmerResponse | ErrorResponse;
19
- export declare function put_farmer(agent: TRPCAgent, data: TPutFarmerRequest): Promise<TPutFarmerResponse>;
19
+ export declare function put_farmer(agent: RPCAgent, data: TPutFarmerRequest): Promise<TPutFarmerResponse>;
20
20
  export declare type TPartialRequest = PostPartialRequest;
21
21
  export declare type TPartialResponse = PostPartialResponse | ErrorResponse;
22
- export declare function partial(agent: TRPCAgent, data: TPartialRequest): Promise<TPartialResponse>;
22
+ export declare function partial(agent: RPCAgent, data: TPartialRequest): Promise<TPartialResponse>;
23
23
  export declare type TLoginRequest = {
24
24
  launcher_id: str;
25
25
  authentication_token: uint64;
@@ -29,4 +29,4 @@ export declare type TLoginResponse = {
29
29
  farmer_record: FarmerRecord;
30
30
  recent_partials: Array<[uint64, uint64]>;
31
31
  } | {};
32
- export declare function login(agent: TRPCAgent, data: TLoginRequest): Promise<TLoginResponse>;
32
+ export declare function login(agent: RPCAgent, data: TLoginRequest): Promise<TLoginResponse>;
@@ -18,19 +18,19 @@ function pool_info(agent) {
18
18
  exports.pool_info = pool_info;
19
19
  function get_farmer(agent, data) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
- return agent.request("GET", "farmer");
21
+ return agent.request("GET", "farmer", data);
22
22
  });
23
23
  }
24
24
  exports.get_farmer = get_farmer;
25
25
  function post_farmer(agent, data) {
26
26
  return __awaiter(this, void 0, void 0, function* () {
27
- return agent.request("POST", "farmer");
27
+ return agent.request("POST", "farmer", data);
28
28
  });
29
29
  }
30
30
  exports.post_farmer = post_farmer;
31
31
  function put_farmer(agent, data) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
- return agent.request("PUT", "farmer");
33
+ return agent.request("PUT", "farmer", data);
34
34
  });
35
35
  }
36
36
  exports.put_farmer = put_farmer;
package/api/types.d.ts CHANGED
@@ -11,7 +11,7 @@ export declare type GetMessageType<O extends string, C extends string, D> = {
11
11
  request_id: string;
12
12
  destination: string;
13
13
  };
14
- export declare type ResType<T extends TRPCAgent | TDaemon, A, D> = T extends TRPCAgent ? A : D;
14
+ export declare type ResType<T extends TRPCAgent | TDaemon, A, D> = T extends TDaemon ? D : A;
15
15
  export declare const wallet_ui_service = "wallet_ui";
16
16
  export declare const metrics_service = "metrics";
17
17
  export declare type TConnectionGeneral = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chia-agent",
3
- "version": "14.0.0-beta.0",
3
+ "version": "14.0.0-beta.2",
4
4
  "author": "ChiaMineJP <admin@chiamine.jp>",
5
5
  "description": "chia rpc/websocket client library",
6
6
  "license": "MIT",
package/rpc/index.d.ts CHANGED
@@ -2,11 +2,18 @@
2
2
  import { Agent as HttpsAgent } from "https";
3
3
  import { Agent as HttpAgent } from "http";
4
4
  import { TConfig } from "../config/index";
5
+ import { APIAgent } from "../agent/index";
5
6
  declare type TDestination = "farmer" | "harvester" | "full_node" | "wallet" | "data_layer" | "daemon" | "pool";
6
7
  export declare function getConnectionInfoFromConfig(destination: TDestination, config: TConfig): {
7
8
  hostname: string;
8
9
  port: number;
9
10
  };
11
+ export declare function getConf(configPath?: string): TConfig;
12
+ export declare function loadCertFilesFromConfig(config: TConfig): {
13
+ clientCert: Buffer;
14
+ clientKey: Buffer;
15
+ caCert: Buffer;
16
+ };
10
17
  export declare type TRPCAgentProps = {
11
18
  protocol: "https";
12
19
  host: string;
@@ -15,41 +22,52 @@ export declare type TRPCAgentProps = {
15
22
  client_cert?: string | Buffer;
16
23
  client_key?: string | Buffer;
17
24
  skip_hostname_verification?: boolean;
25
+ keepAlive?: boolean;
26
+ keepAliveMsecs?: number;
27
+ maxSockets?: number;
28
+ timeout?: number;
18
29
  } | {
19
30
  protocol: "https";
20
31
  host: string;
21
32
  port: number;
22
33
  configPath: string;
23
34
  skip_hostname_verification?: boolean;
35
+ keepAlive?: boolean;
36
+ keepAliveMsecs?: number;
37
+ maxSockets?: number;
38
+ timeout?: number;
24
39
  } | {
25
40
  protocol: "http";
26
41
  host: string;
27
42
  port: number;
43
+ keepAlive?: boolean;
44
+ keepAliveMsecs?: number;
45
+ maxSockets?: number;
46
+ timeout?: number;
28
47
  } | {
29
48
  service: TDestination;
30
49
  host?: string;
31
50
  port?: number;
32
51
  configPath?: string;
33
52
  skip_hostname_verification?: boolean;
53
+ keepAlive?: boolean;
54
+ keepAliveMsecs?: number;
55
+ maxSockets?: number;
56
+ timeout?: number;
57
+ } | {
58
+ httpsAgent: HttpsAgent;
59
+ skip_hostname_verification?: boolean;
60
+ } | {
61
+ httpAgent: HttpAgent;
62
+ skip_hostname_verification?: boolean;
34
63
  };
35
- export declare class RPCAgent {
64
+ export declare class RPCAgent implements APIAgent {
36
65
  protected _protocol: "http" | "https";
37
- protected _hostname: string;
38
- protected _port: number;
39
- protected _caCert?: string | Buffer;
40
- protected _clientCert?: string | Buffer;
41
- protected _clientKey?: string | Buffer;
42
66
  protected _agent: HttpsAgent | HttpAgent;
43
67
  protected _skip_hostname_verification: boolean;
44
68
  constructor(props: TRPCAgentProps);
45
- protected _getConfig(configPath?: string): TConfig;
46
- protected _loadCertFilesFromConfig(config: TConfig): {
47
- clientCert: Buffer;
48
- clientKey: Buffer;
49
- caCert: Buffer;
50
- };
51
69
  sendMessage<M extends unknown>(destination: string, command: string, data?: Record<string, unknown>): Promise<M>;
52
70
  request<R>(method: string, path: string, data?: any): Promise<R>;
53
71
  }
54
- export declare type TRPCAgent = InstanceType<typeof RPCAgent>;
72
+ export declare type TRPCAgent = APIAgent;
55
73
  export {};
package/rpc/index.js CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.RPCAgent = exports.getConnectionInfoFromConfig = void 0;
12
+ exports.RPCAgent = exports.loadCertFilesFromConfig = exports.getConf = exports.getConnectionInfoFromConfig = void 0;
13
13
  const https_1 = require("https");
14
14
  const http_1 = require("http");
15
15
  const fs_1 = require("fs");
@@ -60,100 +60,135 @@ function getConnectionInfoFromConfig(destination, config) {
60
60
  return { hostname, port };
61
61
  }
62
62
  exports.getConnectionInfoFromConfig = getConnectionInfoFromConfig;
63
+ function getConf(configPath) {
64
+ configPath = configPath || index_1.configPath;
65
+ if (!fs_1.existsSync(configPath)) {
66
+ logger_1.getLogger().error(`chia config file does not exist at: ${configPath}`);
67
+ throw new Error("chia config file Not Found.");
68
+ }
69
+ return index_1.getConfig(configPath);
70
+ }
71
+ exports.getConf = getConf;
72
+ function loadCertFilesFromConfig(config) {
73
+ const clientCertPath = index_1.resolveFromChiaRoot([config["/daemon_ssl/private_crt"]]);
74
+ const clientKeyPath = index_1.resolveFromChiaRoot([config["/daemon_ssl/private_key"]]);
75
+ const caCertPath = index_1.resolveFromChiaRoot([config["/private_ssl_ca/crt"]]);
76
+ logger_1.getLogger().debug(`Loading client cert file from ${clientCertPath}`);
77
+ logger_1.getLogger().debug(`Loading client key file from ${clientKeyPath}`);
78
+ logger_1.getLogger().debug(`Loading ca cert file from ${caCertPath}`);
79
+ const getCertOrKey = (path) => {
80
+ if (!fs_1.existsSync(path)) {
81
+ logger_1.getLogger().error(`ssl crt/key does not exist at: ${path}`);
82
+ throw new Error(`crt/key Not Found at ${path}`);
83
+ }
84
+ return fs_1.readFileSync(path);
85
+ };
86
+ const clientCert = getCertOrKey(clientCertPath);
87
+ const clientKey = getCertOrKey(clientKeyPath);
88
+ const caCert = getCertOrKey(caCertPath);
89
+ return { clientCert, clientKey, caCert };
90
+ }
91
+ exports.loadCertFilesFromConfig = loadCertFilesFromConfig;
63
92
  const userAgent = "chia-agent/1.0.0";
64
93
  class RPCAgent {
65
94
  constructor(props) {
66
- this._caCert = "";
67
- this._clientCert = "";
68
- this._clientKey = "";
69
95
  this._skip_hostname_verification = false;
70
- if ("protocol" in props) {
96
+ if ("httpsAgent" in props) {
97
+ this._protocol = "https";
98
+ this._agent = props.httpsAgent;
99
+ this._skip_hostname_verification = Boolean(props.skip_hostname_verification);
100
+ }
101
+ else if ("httpAgent" in props) {
102
+ this._protocol = "http";
103
+ this._agent = props.httpAgent;
104
+ this._skip_hostname_verification = Boolean(props.skip_hostname_verification);
105
+ }
106
+ else if ("protocol" in props) {
71
107
  this._protocol = props.protocol;
72
- this._hostname = props.host;
73
- this._port = props.port;
108
+ const { host, port } = props;
109
+ let clientCert;
110
+ let clientKey;
111
+ let caCert;
112
+ const keepAlive = props.keepAlive !== false;
113
+ const keepAliveMsecs = typeof props.keepAliveMsecs === "number" && props.keepAliveMsecs > 0 ?
114
+ props.keepAliveMsecs : 1000;
115
+ const maxSockets = typeof props.maxSockets === "number" && props.maxSockets > 0 ?
116
+ props.maxSockets : Infinity;
117
+ const timeout = typeof props.timeout === "number" && props.timeout > 0 ?
118
+ props.timeout : undefined;
74
119
  if (props.protocol === "https") {
75
120
  if ("configPath" in props) {
76
- const config = this._getConfig(props.configPath);
77
- const certs = this._loadCertFilesFromConfig(config);
78
- this._clientCert = certs.clientCert;
79
- this._clientKey = certs.clientKey;
80
- this._caCert = certs.caCert;
121
+ const config = getConf(props.configPath);
122
+ const certs = loadCertFilesFromConfig(config);
123
+ clientCert = certs.clientCert;
124
+ clientKey = certs.clientKey;
125
+ caCert = certs.caCert;
81
126
  this._skip_hostname_verification = Boolean(props.skip_hostname_verification);
82
127
  }
83
128
  else {
84
- this._caCert = props.ca_cert;
85
- this._clientCert = props.client_cert;
86
- this._clientKey = props.client_key;
129
+ ({ client_cert: clientCert, client_key: clientKey, ca_cert: caCert } = props);
87
130
  this._skip_hostname_verification = Boolean(props.skip_hostname_verification);
88
131
  }
89
132
  this._agent = new https_1.Agent({
90
- host: this._hostname,
91
- port: this._port,
92
- ca: this._caCert,
93
- cert: this._clientCert,
94
- key: this._clientKey,
95
- rejectUnauthorized: Boolean(this._caCert) && this._hostname !== "localhost",
133
+ host,
134
+ port,
135
+ ca: caCert,
136
+ cert: clientCert,
137
+ key: clientKey,
138
+ rejectUnauthorized: Boolean(caCert) && host !== "localhost",
139
+ keepAlive,
140
+ keepAliveMsecs,
141
+ maxSockets,
142
+ timeout,
96
143
  });
97
144
  }
98
145
  else {
99
- this._agent = new http_1.Agent();
146
+ this._agent = new http_1.Agent({
147
+ keepAlive,
148
+ keepAliveMsecs,
149
+ maxSockets,
150
+ timeout,
151
+ });
100
152
  }
101
153
  }
102
154
  else {
103
155
  this._protocol = "https";
104
- const config = this._getConfig("configPath" in props ? props.configPath : undefined);
156
+ let host;
157
+ let port;
158
+ const keepAlive = props.keepAlive !== false;
159
+ const keepAliveMsecs = typeof props.keepAliveMsecs === "number" && props.keepAliveMsecs > 0 ?
160
+ props.keepAliveMsecs : 1000;
161
+ const maxSockets = typeof props.maxSockets === "number" && props.maxSockets > 0 ?
162
+ props.maxSockets : Infinity;
163
+ const timeout = typeof props.timeout === "number" && props.timeout > 0 ?
164
+ props.timeout : undefined;
165
+ const config = getConf("configPath" in props ? props.configPath : undefined);
105
166
  if (props.host && typeof props.port === "number") {
106
- this._hostname = props.host;
107
- this._port = props.port;
167
+ ({ host, port } = props);
108
168
  }
109
169
  else {
110
- const { hostname, port } = getConnectionInfoFromConfig(props.service, config);
111
- this._hostname = props.host ? props.host : hostname;
112
- this._port = typeof props.port === "number" ? props.port : port;
170
+ const info = getConnectionInfoFromConfig(props.service, config);
171
+ host = props.host ? props.host : info.hostname;
172
+ port = typeof props.port === "number" ? props.port : info.port;
113
173
  }
114
- logger_1.getLogger().debug(`Picked ${this._hostname}:${this._port} for ${props.service}`);
115
- const certs = this._loadCertFilesFromConfig(config);
116
- this._clientCert = certs.clientCert;
117
- this._clientKey = certs.clientKey;
118
- this._caCert = certs.caCert;
174
+ logger_1.getLogger().debug(`Picked ${host}:${port} for ${props.service}`);
175
+ const certs = loadCertFilesFromConfig(config);
176
+ const { clientCert, clientKey, caCert } = certs;
119
177
  this._skip_hostname_verification = Boolean(props.skip_hostname_verification);
120
178
  this._agent = new https_1.Agent({
121
- host: this._hostname,
122
- port: this._port,
123
- ca: this._caCert,
124
- cert: this._clientCert,
125
- key: this._clientKey,
126
- rejectUnauthorized: Boolean(this._caCert) && this._hostname !== "localhost",
179
+ host: host,
180
+ port: port,
181
+ ca: caCert,
182
+ cert: clientCert,
183
+ key: clientKey,
184
+ rejectUnauthorized: Boolean(caCert) && host !== "localhost",
185
+ keepAlive,
186
+ keepAliveMsecs,
187
+ maxSockets,
188
+ timeout,
127
189
  });
128
190
  }
129
191
  }
130
- _getConfig(configPath) {
131
- configPath = configPath || index_1.configPath;
132
- if (!fs_1.existsSync(configPath)) {
133
- logger_1.getLogger().error(`chia config file does not exist at: ${configPath}`);
134
- throw new Error("chia config file Not Found.");
135
- }
136
- return index_1.getConfig(configPath);
137
- }
138
- _loadCertFilesFromConfig(config) {
139
- const clientCertPath = index_1.resolveFromChiaRoot([config["/daemon_ssl/private_crt"]]);
140
- const clientKeyPath = index_1.resolveFromChiaRoot([config["/daemon_ssl/private_key"]]);
141
- const caCertPath = index_1.resolveFromChiaRoot([config["/private_ssl_ca/crt"]]);
142
- logger_1.getLogger().debug(`Loading client cert file from ${clientCertPath}`);
143
- logger_1.getLogger().debug(`Loading client key file from ${clientKeyPath}`);
144
- logger_1.getLogger().debug(`Loading ca cert file from ${caCertPath}`);
145
- const getCertOrKey = (path) => {
146
- if (!fs_1.existsSync(path)) {
147
- logger_1.getLogger().error(`ssl crt/key does not exist at: ${path}`);
148
- throw new Error(`crt/key Not Found at ${path}`);
149
- }
150
- return fs_1.readFileSync(path);
151
- };
152
- const clientCert = getCertOrKey(clientCertPath);
153
- const clientKey = getCertOrKey(clientKeyPath);
154
- const caCert = getCertOrKey(caCertPath);
155
- return { clientCert, clientKey, caCert };
156
- }
157
192
  sendMessage(destination, command, data) {
158
193
  return __awaiter(this, void 0, void 0, function* () {
159
194
  // parameter `destination` is not used because target rpc server is determined by url.
@@ -168,9 +203,6 @@ class RPCAgent {
168
203
  const pathname = `/${path.replace(/^\/+/, "")}`;
169
204
  const METHOD = method.toUpperCase();
170
205
  const options = {
171
- protocol: this._protocol + ":",
172
- hostname: this._hostname,
173
- port: `${this._port}`,
174
206
  path: pathname,
175
207
  method: METHOD,
176
208
  agent: this._agent,