From 0bf2aecdcfaf0f125c8acd742945c5f98bbb2b0c Mon Sep 17 00:00:00 2001 From: Corey Phillips Date: Mon, 28 Dec 2020 17:01:11 -0500 Subject: [PATCH] Added pingServer check to connectToPeer in helpers/index.js. Updated subscribeAddress method in helpers/index.js. Updated _attemptToGetArray & _getTimeout in helpers/index.js. --- helpers/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/helpers/index.js b/helpers/index.js index e1ae09e..5519507 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -7,7 +7,7 @@ let electrumKeepAliveInterval = 60000; const _getTimeout = ({ arr = [], timeout = 1000 } = {}) => { try { if (!Array.isArray(arr)) arr = _attemptToGetArray(arr); - if (arr && Array.isArray(arr)) return arr.length * timeout; + if (arr && Array.isArray(arr)) return (arr.length * timeout)/2; return timeout || 1000; } catch { return timeout; @@ -17,7 +17,10 @@ const _getTimeout = ({ arr = [], timeout = 1000 } = {}) => { const _attemptToGetArray = (scriptHashes = []) => { try { if (Array.isArray(scriptHashes)) return scriptHashes; - if ("data" in scriptHashes) return scriptHashes.data; + if ("data" in scriptHashes) { + if (Array.isArray(scriptHashes.data)) return scriptHashes.data; + if (typeof scriptHashes.data === 'object') return Object.values(scriptHashes.data); + } return []; } catch { return []; } }; @@ -133,7 +136,12 @@ const connectToPeer = ({ port = 50002, host = "", protocol = "ssl", network = "b if (needToConnect) { clients.mainClient[network] = new ElectrumClient(port, host, protocol); connectionResponse = await promiseTimeout(1000, clients.mainClient[network].connect()); - if (!connectionResponse.error) { + if (connectionResponse.error) { + resolve(connectionResponse); + return; + } + const pingResponse = pingServer(); + if (!pingResponse.error) { try { //Clear/Remove Electrum's keep-alive message. clearInterval(electrumKeepAlive); @@ -301,7 +309,7 @@ const subscribeAddress = async ({ id = Math.random(), scriptHash = "", network = try { if (clients.mainClient[network] === false) await connectToRandomPeer(network, clients.peers[network]); if (clients.subscribedAddresses[network].length < 1) { - const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', (data => onReceive(data)))); + const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', (onReceive))); if (res.error) return { ...res, id, method: "subscribeAddress" }; } //Ensure this address is not already subscribed