From ca0d557d943aab2e4a7decca5ccae436bc4f10d6 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Tue, 31 Jan 2023 02:13:37 +0530 Subject: [PATCH] btcOperator v1.1.1: getTx.hex - Added getTx.hex(txid): resolves the tx hex of the txid - Update: api responses are checked for response.ok - Fixed: getTx giving incorrect confirmation value for unconfirmed tx --- btcOperator.js | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/btcOperator.js b/btcOperator.js index beb2cd6..6925cec 100644 --- a/btcOperator.js +++ b/btcOperator.js @@ -1,17 +1,23 @@ -(function (EXPORTS) { //btcOperator v1.1.0 +(function (EXPORTS) { //btcOperator v1.1.1 /* BTC Crypto and API Operator */ const btcOperator = EXPORTS; //This library uses API provided by chain.so (https://chain.so/) const URL = "https://blockchain.info/"; - const fetch_api = btcOperator.fetch = function (api) { + const fetch_api = btcOperator.fetch = function (api, json_res = true) { return new Promise((resolve, reject) => { console.debug(URL + api); fetch(URL + api).then(response => { - response.json() - .then(result => result.error ? reject(result) : resolve(result)) - .catch(error => reject(error)) + if (response.ok) { + (json_res ? response.json() : response.text()) + .then(result => resolve(result)) + .catch(error => reject(error)) + } else { + response.json() + .then(result => reject(result)) + .catch(error => reject(error)) + } }).catch(error => reject(error)) }) }; @@ -739,23 +745,27 @@ btcOperator.getTx = txid => new Promise((resolve, reject) => { fetch_api(`rawtx/${txid}`).then(result => { - getLatestBlock().then(latest_block => { - resolve({ - block: result.block_height, - txid: result.hash, - time: result.time * 1000, - confirmations: latest_block - result.block_height, //calculate confirmations using latest block number as api doesnt relay it - size: result.size, - fee: util.Sat_to_BTC(result.fee), - inputs: result.inputs.map(i => Object({ address: i.prev_out.addr, value: util.Sat_to_BTC(i.prev_out.value) })), - total_input_value: util.Sat_to_BTC(result.inputs.reduce((a, i) => a + i.prev_out.value, 0)), - outputs: result.out.map(o => Object({ address: o.addr, value: util.Sat_to_BTC(o.value) })), - total_output_value: util.Sat_to_BTC(result.out.reduce((a, o) => a += o.value, 0)), - }) - }) + getLatestBlock().then(latest_block => resolve({ + block: result.block_height, + txid: result.hash, + time: result.time * 1000, + confirmations: result.block_height === null ? 0 : latest_block - result.block_height, //calculate confirmations using latest block number as api doesnt relay it + size: result.size, + fee: util.Sat_to_BTC(result.fee), + inputs: result.inputs.map(i => Object({ address: i.prev_out.addr, value: util.Sat_to_BTC(i.prev_out.value) })), + total_input_value: util.Sat_to_BTC(result.inputs.reduce((a, i) => a + i.prev_out.value, 0)), + outputs: result.out.map(o => Object({ address: o.addr, value: util.Sat_to_BTC(o.value) })), + total_output_value: util.Sat_to_BTC(result.out.reduce((a, o) => a += o.value, 0)), + })) }).catch(error => reject(error)) }); + btcOperator.getTx.hex = txid => new Promise((resolve, reject) => { + fetch_api(`rawtx/${txid}?format=hex`, false) + .then(result => resolve(result)) + .catch(error => reject(error)) + }) + btcOperator.getAddressData = address => new Promise((resolve, reject) => { fetch_api(`rawaddr/${address}`).then(data => { let details = {};