UNPKG

darkpay-insight-apiz

Version:

A Darkpay blockchain REST and web socket API service for Bitcore Node.

174 lines (154 loc) 4.02 kB
'use strict'; var Common = require('./common'); function StatusController(node) { this.node = node; this.common = new Common({log: this.node.log}); } StatusController.prototype.show = function(req, res) { var self = this; var option = req.query.q; switch(option) { case 'getDifficulty': this.getDifficulty(function(err, result) { if (err) { return self.common.handleErrors(err, res); } res.jsonp(result); }); break; case 'getLastBlockHash': res.jsonp(this.getLastBlockHash()); break; case 'getBestBlockHash': this.getBestBlockHash(function(err, result) { if (err) { return self.common.handleErrors(err, res); } res.jsonp(result); }); break; case 'getNetworkInfo': this.getNetworkInfo(function(err, result) { if (err) { return self.common.handleErrors(err, res); } res.jsonp({ info: result }); }); case 'getBlockchainInfo': this.getBlockchainInfo(function(err, result) { if (err) { return self.common.handleErrors(err, res); } res.jsonp({ info: result }); }); default: this.getInfo(function(err, result) { if (err) { return self.common.handleErrors(err, res); } res.jsonp({ info: result }); }); } }; StatusController.prototype.getInfo = function(callback) { var self = this; this.node.services.bitcoind.getBlockchainInfo(function(err, resultbc) { if (err) { return callback(err); } self.node.services.bitcoind.getNetworkInfo(function(err, resultn) { if (err) { return callback(err); } var info = { version: resultn.version, protocolversion: resultn.protocolVersion, blocks: resultbc.blocks, moneysupply: resultbc.moneysupply, timeoffset: resultn.timeOffset, connections: resultn.connections, proxy: resultn.proxy, difficulty: resultbc.difficulty, testnet: resultbc.testnet, relayfee: resultn.relayFee, network: resultbc.chain, errors: resultbc.warnings }; callback(null, info); }); }); }; StatusController.prototype.getLastBlockHash = function() { var hash = this.node.services.bitcoind.tiphash; return { syncTipHash: hash, lastblockhash: hash }; }; StatusController.prototype.getBestBlockHash = function(callback) { this.node.services.bitcoind.getBestBlockHash(function(err, hash) { if (err) { return callback(err); } callback(null, { bestblockhash: hash }); }); }; StatusController.prototype.getDifficulty = function(callback) { this.node.services.bitcoind.getBlockchainInfo(function(err, info) { if (err) { return callback(err); } callback(null, { difficulty: info.difficulty }); }); }; StatusController.prototype.sync = function(req, res) { var self = this; var status = 'syncing'; this.node.services.bitcoind.isSynced(function(err, synced) { if (err) { return self.common.handleErrors(err, res); } if (synced) { status = 'finished'; } self.node.services.bitcoind.syncPercentage(function(err, percentage) { if (err) { return self.common.handleErrors(err, res); } var info = { status: status, blockChainHeight: self.node.services.bitcoind.height, syncPercentage: Math.round(percentage), height: self.node.services.bitcoind.height, error: null, type: 'bitcore node' }; res.jsonp(info); }); }); }; // Hard coded to make insight ui happy, but not applicable StatusController.prototype.peer = function(req, res) { res.jsonp({ connected: true, host: '127.0.0.1', port: null }); }; StatusController.prototype.version = function(req, res) { var pjson = require('../package.json'); res.jsonp({ version: pjson.version }); }; module.exports = StatusController;