diff --git a/helpers/clients.js b/helpers/clients.js index 3d55584..423bd88 100644 --- a/helpers/clients.js +++ b/helpers/clients.js @@ -17,20 +17,24 @@ class Clients { bitcoin: [], bitcoinTestnet: [] }; + this.subscribedHeaders = { + bitcoin: false, + bitcoinTestnet: false, + }; } - + updateNetwork(network) { this.network = network; } - + updateMainClient(mainClient) { this.mainClient = mainClient; } - + updatePeer(peer) { this.peer = peer; } - + } module.exports = new Clients(); diff --git a/helpers/index.js b/helpers/index.js index ca3fa35..e1ae09e 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -286,12 +286,12 @@ const getPeers = ({ id = Math.random(), network = "" } = {}) => { const subscribeHeader = async ({ id = "subscribeHeader", network = "", onReceive = () => null } = {}) => { try { if (clients.mainClient[network] === false) await connectToRandomPeer(network, clients.peers[network]); - clients.mainClient[network].subscribe.on('blockchain.headers.subscribe', (data) => { - console.log("Received header."); - console.log(data); - onReceive(data) - }); - return { id, error: false, method: "subscribeHeader", data: "Subscribed", network }; + if (clients.subscribedHeaders[network] === true) return { id, error: false, method: "subscribeHeader", data: 'Already Subscribed.', network }; + const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.headers.subscribe', (onReceive))); + if (res.error) return { ...res, id, method: "subscribeHeader" }; + const response = await promiseTimeout(10000, clients.mainClient[network].blockchainHeaders_subscribe()); + if (!response.error) clients.subscribedHeaders[network] = true; + return { ...response, id, method: "subscribeHeader" }; } catch (e) { return { id, error: true, method: "subscribeHeader", data: e, network }; } @@ -300,10 +300,12 @@ const subscribeHeader = async ({ id = "subscribeHeader", network = "", onReceive const subscribeAddress = async ({ id = Math.random(), scriptHash = "", network = "bitcoin", onReceive = (data) => console.log(data) } = {}) => { 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)))); + if (res.error) return { ...res, id, method: "subscribeAddress" }; + } //Ensure this address is not already subscribed - if (clients.subscribedAddresses[network].includes(scriptHash)) return { id, error: false, method: "subscribeAddress", data: "" }; - const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', (onReceive))); - if (res.error) return { ...res, id, method: "subscribeAddress" }; + if (clients.subscribedAddresses[network].includes(scriptHash)) return { id, error: false, method: "subscribeAddress", data: "Already Subscribed." }; const response = await promiseTimeout(10000, clients.mainClient[network].blockchainScripthash_subscribe(scriptHash)); if (!response.error) clients.subscribedAddresses[network].push(scriptHash); return { ...response, id, method: "subscribeAddress" }; diff --git a/lib/client.js b/lib/client.js index 22b3d2b..ce90a03 100644 --- a/lib/client.js +++ b/lib/client.js @@ -139,29 +139,21 @@ class Client { } onMessage(body, n) { - const msg = JSON.parse(body); - if (msg instanceof Array) { - this.response(msg); - } else { - if (msg.id !== void 0) { - try { - const msg = JSON.parse(body); - if (msg instanceof Array) { - this.response(msg); - } else { - this.subscribe.emit(msg.method, msg.params); - if (msg.id !== void 0) { - this.response(msg); - } else { - this.subscribe.emit(msg.method, msg.params); - } - } - } catch (error) { - this.conn.end(); - this.conn.destroy(); - this.onClose(error); + try { + const msg = JSON.parse(body); + if (msg instanceof Array) { + this.response(msg); + } else { + if (msg.id !== void 0) { + this.response(msg); + } else { + this.subscribe.emit(msg.method, msg.params); } } + } catch (error) { + this.conn.end(); + this.conn.destroy(); + this.onClose(error); } } @@ -169,6 +161,7 @@ class Client { } onClose(){ + this.status = 0; Object.keys(this.callback_message_queue).forEach((key) => { this.callback_message_queue[key](new Error('close connect')) delete this.callback_message_queue[key] @@ -180,7 +173,7 @@ class Client { } onError(e){ - //console.log('OnError:' + e); + console.log('OnError:' + e); } }