diff --git a/helpers/index.js b/helpers/index.js index 29e874f..36460ef 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -440,6 +440,28 @@ const broadcastTransaction = ({ rawTx = [], id = Math.random(), network = "", ti }); }; +/** + * Returns header hex of the provided height and network. + * @param {Number} [height] + * @param {Number} [id] + * @param {"bitcoin" | "bitcoinTestnet"} network + * @param {Number | undefined} [timeout] + * @return {Promise<{id: Number, error: boolean, method: "getHeader", data: string, network: "bitcoin" | "bitcoinTestnet"}>} + */ +const getHeader = ({ height = 0, id = Math.random(), network = "", timeout = undefined } = {}) => { + const method = "getHeader"; + return new Promise(async (resolve) => { + try { + if (clients.mainClient[network] === false) await connectToRandomPeer(network, clients.peers[network]); + if (!timeout) timeout = _getTimeout(); + const { error, data } = await promiseTimeout(timeout, clients.mainClient[network].blockchainBlock_getBlockHeader(height)); + resolve({ id, error, method, data, network }); + } catch (e) { + resolve({ id, error: true, method, data: e, network }); + } + }); +} + module.exports = { start, stop, @@ -455,5 +477,6 @@ module.exports = { subscribeAddress, getFeeEstimate, broadcastTransaction, - getConnectedPeer + getConnectedPeer, + getHeader, }; diff --git a/lib/electrum_client.js b/lib/electrum_client.js index 0f2ba89..d30a8e6 100644 --- a/lib/electrum_client.js +++ b/lib/electrum_client.js @@ -125,13 +125,6 @@ class ElectrumClient extends Client{ } catch (e) { return { error: true, data: e }; } - } - blockchainBlock_getHeader(height){ - try { - return this.request('blockchain.block.get_header', [height]); - } catch (e) { - return { error: true, data: e }; - } } blockchainBlock_getBlockHeader(height){ try { @@ -140,13 +133,6 @@ class ElectrumClient extends Client{ return { error: true, data: e }; } } - blockchainBlock_getChunk(index){ - try { - return this.request('blockchain.block.get_chunk', [index]); - } catch (e) { - return { error: true, data: e }; - } - } blockchainEstimatefee(number){ try { return this.request('blockchain.estimatefee', [number]);