@dignetwork/dig-sdk 0.0.1-alpha.97 → 0.0.1-alpha.98

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.
@@ -5,7 +5,13 @@ export declare class FullNodePeer {
5
5
  private peer;
6
6
  private static deprioritizedIps;
7
7
  private constructor();
8
- static connect(): Promise<Peer>;
8
+ /**
9
+ * Connect to the best peer, optionally using custom certificate and key file paths.
10
+ * @param {string} [certFilePath] - Optional custom certificate file path.
11
+ * @param {string} [keyFilePath] - Optional custom key file path.
12
+ * @returns {Promise<Peer>} The connected peer.
13
+ */
14
+ static connect(certFilePath?: string, keyFilePath?: string): Promise<Peer>;
9
15
  private static isPortReachable;
10
16
  private static isValidIpAddress;
11
17
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"FullNodePeer.d.ts","sourceRoot":"","sources":["../../src/blockchain/FullNodePeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAqBpD,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAC,UAAU,CAAkD;IAC3E,OAAO,CAAC,MAAM,CAAC,uBAAuB,CAA0B;IAChE,OAAO,CAAC,IAAI,CAAO;IACnB,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAA0B;IAMzD,OAAO;WAIa,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAK5C,OAAO,CAAC,MAAM,CAAC,eAAe;IAiB9B,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAM/B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,kBAAkB;mBASZ,eAAe;mBA2Df,UAAU;IA2B/B,OAAO,CAAC,MAAM,CAAC,eAAe;mBAkDT,WAAW;IAuGzB,OAAO,IAAI,IAAI;WAIF,mBAAmB,CACrC,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,OAAO,CAAC;CAwBpB"}
1
+ {"version":3,"file":"FullNodePeer.d.ts","sourceRoot":"","sources":["../../src/blockchain/FullNodePeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAqBpD,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAC,UAAU,CAAkD;IAC3E,OAAO,CAAC,MAAM,CAAC,uBAAuB,CAA0B;IAChE,OAAO,CAAC,IAAI,CAAO;IACnB,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAA0B;IAQzD,OAAO;IAIP;;;;;OAKG;WACiB,OAAO,CACzB,YAAY,CAAC,EAAE,MAAM,EACrB,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC;IAKhB,OAAO,CAAC,MAAM,CAAC,eAAe;IAiB9B,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAM/B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,kBAAkB;mBASZ,eAAe;mBA6Df,UAAU;IA2B/B,OAAO,CAAC,MAAM,CAAC,eAAe;mBAyDT,WAAW;IA+HzB,OAAO,IAAI,IAAI;WAIF,mBAAmB,CACrC,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,OAAO,CAAC;CAwBpB"}
@@ -30,8 +30,14 @@ class FullNodePeer {
30
30
  constructor(peer) {
31
31
  this.peer = peer;
32
32
  }
33
- static async connect() {
34
- const peer = await FullNodePeer.getBestPeer();
33
+ /**
34
+ * Connect to the best peer, optionally using custom certificate and key file paths.
35
+ * @param {string} [certFilePath] - Optional custom certificate file path.
36
+ * @param {string} [keyFilePath] - Optional custom key file path.
37
+ * @returns {Promise<Peer>} The connected peer.
38
+ */
39
+ static async connect(certFilePath, keyFilePath) {
40
+ const peer = await FullNodePeer.getBestPeer(certFilePath, keyFilePath);
35
41
  return new FullNodePeer(peer).peer;
36
42
  }
37
43
  static isPortReachable(host, port, timeout = CONNECTION_TIMEOUT) {
@@ -158,7 +164,8 @@ class FullNodePeer {
158
164
  }
159
165
  catch (error) {
160
166
  // If the error is WebSocket-related or timeout, reset the peer
161
- if (error.message.includes("WebSocket") || error.message.includes("Operation timed out")) {
167
+ if (error.message.includes("WebSocket") ||
168
+ error.message.includes("Operation timed out")) {
162
169
  FullNodePeer.cachedPeer = null;
163
170
  // @ts-ignore
164
171
  FullNodePeer.memoizedFetchNewPeerIPs.cache.clear();
@@ -175,17 +182,33 @@ class FullNodePeer {
175
182
  },
176
183
  });
177
184
  }
178
- static async getBestPeer() {
185
+ static async getBestPeer(certFilePath, keyFilePath) {
179
186
  const now = Date.now();
180
187
  if (FullNodePeer.cachedPeer &&
181
188
  now - FullNodePeer.cachedPeer.timestamp < CACHE_DURATION) {
182
189
  return FullNodePeer.cachedPeer.peer;
183
190
  }
184
- const sslFolder = path_1.default.resolve(os_1.default.homedir(), ".dig", "ssl");
185
- const certFile = path_1.default.join(sslFolder, "public_dig.crt");
186
- const keyFile = path_1.default.join(sslFolder, "public_dig.key");
187
- if (!fs_1.default.existsSync(sslFolder)) {
188
- fs_1.default.mkdirSync(sslFolder, { recursive: true });
191
+ let certFile;
192
+ let keyFile;
193
+ // If certFilePath or keyFilePath is provided, ensure both are provided
194
+ if (certFilePath && keyFilePath) {
195
+ certFile = certFilePath;
196
+ keyFile = keyFilePath;
197
+ if (!fs_1.default.existsSync(certFile)) {
198
+ throw new Error(`Certificate file not found: ${certFile}`);
199
+ }
200
+ if (!fs_1.default.existsSync(keyFile)) {
201
+ throw new Error(`Key file not found: ${keyFile}`);
202
+ }
203
+ }
204
+ else {
205
+ // Use default paths if no custom paths are provided
206
+ const sslFolder = path_1.default.resolve(os_1.default.homedir(), ".dig", "ssl");
207
+ certFile = path_1.default.join(sslFolder, "public_dig.crt");
208
+ keyFile = path_1.default.join(sslFolder, "public_dig.key");
209
+ if (!fs_1.default.existsSync(sslFolder)) {
210
+ throw new Error(`SSL folder not found: ${sslFolder}`);
211
+ }
189
212
  }
190
213
  new chia_server_coin_1.Tls(certFile, keyFile);
191
214
  const peerIPs = await FullNodePeer.getPeerIPs();
@@ -230,7 +253,9 @@ class FullNodePeer {
230
253
  // Prioritize LOCALHOST, TRUSTED_NODE_IP, and CHIA_NODES_HOST if they have the highest peak height
231
254
  let bestPeerIndex = validHeights.findIndex((height, index) => height === highestPeak &&
232
255
  !FullNodePeer.deprioritizedIps.has(peerIPs[index]) && // Exclude deprioritized IPs
233
- (peerIPs[index] === LOCALHOST || peerIPs[index] === trustedNodeIp || peerIPs[index] === CHIA_NODES_HOST));
256
+ (peerIPs[index] === LOCALHOST ||
257
+ peerIPs[index] === trustedNodeIp ||
258
+ peerIPs[index] === CHIA_NODES_HOST));
234
259
  // If LOCALHOST, TRUSTED_NODE_IP, or CHIA_NODES_HOST don't have the highest peak, select any peer with the highest peak
235
260
  if (bestPeerIndex === -1) {
236
261
  bestPeerIndex = validHeights.findIndex((height) => height === highestPeak);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.97",
3
+ "version": "0.0.1-alpha.98",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",