diff --git a/CHANGELOG.md b/CHANGELOG.md index 66a318b..8a03c83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.0.1-alpha.98](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.97...v0.0.1-alpha.98) (2024-09-26) + + +### Features + +* allow override of the ssl location for fullnode peers ([a16f720](https://github.com/DIG-Network/dig-chia-sdk/commit/a16f720126a10f54dd55cc0454952701e0cae317)) + ### [0.0.1-alpha.97](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.96...v0.0.1-alpha.97) (2024-09-26) diff --git a/package-lock.json b/package-lock.json index fe3d9e1..beacc39 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.97", + "version": "0.0.1-alpha.98", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.97", + "version": "0.0.1-alpha.98", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.25", diff --git a/package.json b/package.json index 1372858..f90a195 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.97", + "version": "0.0.1-alpha.98", "description": "", "type": "commonjs", "main": "./dist/index.js", diff --git a/src/blockchain/FullNodePeer.ts b/src/blockchain/FullNodePeer.ts index 4522636..d30790c 100644 --- a/src/blockchain/FullNodePeer.ts +++ b/src/blockchain/FullNodePeer.ts @@ -29,15 +29,26 @@ export class FullNodePeer { private static deprioritizedIps: Set = new Set(); // New set for deprioritized IPs static { - FullNodePeer.memoizedFetchNewPeerIPs = memoize(FullNodePeer.fetchNewPeerIPs); + FullNodePeer.memoizedFetchNewPeerIPs = memoize( + FullNodePeer.fetchNewPeerIPs + ); } private constructor(peer: Peer) { this.peer = peer; } - public static async connect(): Promise { - const peer = await FullNodePeer.getBestPeer(); + /** + * Connect to the best peer, optionally using custom certificate and key file paths. + * @param {string} [certFilePath] - Optional custom certificate file path. + * @param {string} [keyFilePath] - Optional custom key file path. + * @returns {Promise} The connected peer. + */ + public static async connect( + certFilePath?: string, + keyFilePath?: string + ): Promise { + const peer = await FullNodePeer.getBestPeer(certFilePath, keyFilePath); return new FullNodePeer(peer).peer; } @@ -132,7 +143,9 @@ export class FullNodePeer { } } } catch (error: any) { - console.error(`Failed to resolve IPs from ${DNS_HOST}: ${error.message}`); + console.error( + `Failed to resolve IPs from ${DNS_HOST}: ${error.message}` + ); } } throw new Error("No reachable IPs found in any DNS records."); @@ -178,7 +191,9 @@ export class FullNodePeer { const timeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => { FullNodePeer.cachedPeer = null; - reject(new Error("Operation timed out. Reconnecting to a new peer.")); + reject( + new Error("Operation timed out. Reconnecting to a new peer.") + ); }, 60000); // 1 minute }); @@ -197,12 +212,17 @@ export class FullNodePeer { return result; } catch (error: any) { // If the error is WebSocket-related or timeout, reset the peer - if (error.message.includes("WebSocket") || error.message.includes("Operation timed out")) { + if ( + error.message.includes("WebSocket") || + error.message.includes("Operation timed out") + ) { FullNodePeer.cachedPeer = null; // @ts-ignore FullNodePeer.memoizedFetchNewPeerIPs.cache.clear(); FullNodePeer.deprioritizedIps.clear(); - console.info(`Fullnode Peer error, reconnecting to a new peer...`); + console.info( + `Fullnode Peer error, reconnecting to a new peer...` + ); const newPeer = await FullNodePeer.getBestPeer(); return (newPeer as any)[prop](...args); } @@ -215,7 +235,10 @@ export class FullNodePeer { }); } - private static async getBestPeer(): Promise { + private static async getBestPeer( + certFilePath?: string, + keyFilePath?: string + ): Promise { const now = Date.now(); if ( @@ -225,12 +248,29 @@ export class FullNodePeer { return FullNodePeer.cachedPeer.peer; } - const sslFolder = path.resolve(os.homedir(), ".dig", "ssl"); - const certFile = path.join(sslFolder, "public_dig.crt"); - const keyFile = path.join(sslFolder, "public_dig.key"); + let certFile: string; + let keyFile: string; + + // If certFilePath or keyFilePath is provided, ensure both are provided + if (certFilePath && keyFilePath) { + certFile = certFilePath; + keyFile = keyFilePath; - if (!fs.existsSync(sslFolder)) { - fs.mkdirSync(sslFolder, { recursive: true }); + if (!fs.existsSync(certFile)) { + throw new Error(`Certificate file not found: ${certFile}`); + } + if (!fs.existsSync(keyFile)) { + throw new Error(`Key file not found: ${keyFile}`); + } + } else { + // Use default paths if no custom paths are provided + const sslFolder = path.resolve(os.homedir(), ".dig", "ssl"); + certFile = path.join(sslFolder, "public_dig.crt"); + keyFile = path.join(sslFolder, "public_dig.key"); + + if (!fs.existsSync(sslFolder)) { + throw new Error(`SSL folder not found: ${sslFolder}`); + } } new Tls(certFile, keyFile); @@ -258,7 +298,9 @@ export class FullNodePeer { ); return FullNodePeer.createPeerProxy(peer); } catch (error: any) { - console.error(`Failed to create peer for IP ${ip}: ${error.message}`); + console.error( + `Failed to create peer for IP ${ip}: ${error.message}` + ); return null; } } @@ -299,7 +341,9 @@ export class FullNodePeer { (height, index) => height === highestPeak && !FullNodePeer.deprioritizedIps.has(peerIPs[index]) && // Exclude deprioritized IPs - (peerIPs[index] === LOCALHOST || peerIPs[index] === trustedNodeIp || peerIPs[index] === CHIA_NODES_HOST) + (peerIPs[index] === LOCALHOST || + peerIPs[index] === trustedNodeIp || + peerIPs[index] === CHIA_NODES_HOST) ); // If LOCALHOST, TRUSTED_NODE_IP, or CHIA_NODES_HOST don't have the highest peak, select any peer with the highest peak