From e2570bc51f181f3fc98db373aad7c999d8b50dc1 Mon Sep 17 00:00:00 2001 From: Dan Janosik Date: Thu, 3 May 2018 13:21:35 -0400 Subject: [PATCH] switch to "bitcoin-core" npm package instead of deprecated "bitcoin" package thanks @nkostoulas for the recommendation --- app.js | 17 +++++++---- app/rpcApi.js | 61 +++++++++++++++++++------------------ package.json | 3 +- routes/baseActionsRouter.js | 16 +++++----- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/app.js b/app.js index cccff53..e45532a 100755 --- a/app.js +++ b/app.js @@ -14,7 +14,7 @@ var simpleGit = require('simple-git'); var utils = require("./app/utils.js"); var moment = require("moment"); var Decimal = require('decimal.js'); -var bitcoin = require("bitcoin"); +var bitcoinCore = require("bitcoin-core"); var pug = require("pug"); var momentDurationFormat = require("moment-duration-format"); var rpcApi = require("./app/rpcApi.js"); @@ -77,6 +77,11 @@ function refreshExchangeRate() { app.runOnStartup = function() { + global.env = env; + global.coinConfig = coins[env.coin]; + + console.log("Running RPC Explorer for coin: " + global.coinConfig.name); + if (global.exchangeRate == null) { refreshExchangeRate(); } @@ -98,17 +103,17 @@ app.use(function(req, res, next) { req.session.port = env.bitcoind.port; req.session.username = env.bitcoind.rpc.username; - global.client = new bitcoin.Client({ + global.client = new bitcoinCore({ host: env.bitcoind.host, port: env.bitcoind.port, - user: env.bitcoind.rpc.username, - pass: env.bitcoind.rpc.password, + username: env.bitcoind.rpc.username, + password: env.bitcoind.rpc.password, timeout: 5000 }); } - res.locals.env = env; - res.locals.coinConfig = coins[env.coin]; + res.locals.env = global.env; + res.locals.coinConfig = global.coinConfig; res.locals.host = req.session.host; res.locals.port = req.session.port; diff --git a/app/rpcApi.js b/app/rpcApi.js index a666c86..569ab8a 100644 --- a/app/rpcApi.js +++ b/app/rpcApi.js @@ -15,7 +15,7 @@ function getGenesisCoinbaseTransactionId() { function getBlockchainInfo() { return new Promise(function(resolve, reject) { - client.cmd('getblockchaininfo', function(err, result, resHeaders) { + client.command('getblockchaininfo', function(err, result, resHeaders) { if (err) { console.log("Error 3207fh0f: " + err); @@ -31,7 +31,7 @@ function getBlockchainInfo() { function getNetworkInfo() { return new Promise(function(resolve, reject) { - client.cmd('getnetworkinfo', function(err, result, resHeaders) { + client.command('getnetworkinfo', function(err, result, resHeaders) { if (err) { console.log("Error 239r7ger7gy: " + err); @@ -47,7 +47,7 @@ function getNetworkInfo() { function getNetTotals() { return new Promise(function(resolve, reject) { - client.cmd('getnettotals', function(err, result, resHeaders) { + client.command('getnettotals', function(err, result, resHeaders) { if (err) { console.log("Error as07uthf40ghew: " + err); @@ -63,7 +63,7 @@ function getNetTotals() { function getMempoolInfo() { return new Promise(function(resolve, reject) { - client.cmd('getmempoolinfo', function(err, result, resHeaders) { + client.command('getmempoolinfo', function(err, result, resHeaders) { if (err) { console.log("Error 23407rhwe07fg: " + err); @@ -79,7 +79,7 @@ function getMempoolInfo() { function getUptimeSeconds() { return new Promise(function(resolve, reject) { - client.cmd('uptime', function(err, result, resHeaders) { + client.command('uptime', function(err, result, resHeaders) { if (err) { console.log("Error 3218y6gr3986sdd: " + err); @@ -95,7 +95,7 @@ function getUptimeSeconds() { function getMempoolStats() { return new Promise(function(resolve, reject) { - client.cmd('getrawmempool', true, function(err, result, resHeaders) { + client.command('getrawmempool', true, function(err, result, resHeaders) { if (err) { console.log("Error 428thwre0ufg: " + err); @@ -181,7 +181,7 @@ function getBlockByHeight(blockHeight) { return new Promise(function(resolve, reject) { var client = global.client; - client.cmd('getblockhash', blockHeight, function(err, result, resHeaders) { + client.command('getblockhash', blockHeight, function(err, result, resHeaders) { if (err) { console.log("Error 0928317yr3w: " + err); @@ -190,7 +190,7 @@ function getBlockByHeight(blockHeight) { return; } - client.cmd('getblock', result, function(err2, result2, resHeaders2) { + client.command('getblock', result, function(err2, result2, resHeaders2) { if (err2) { console.log("Error 320fh7e0hg: " + err2); @@ -213,31 +213,36 @@ function getBlocksByHeight(blockHeights) { for (var i = 0; i < blockHeights.length; i++) { batch.push({ method: 'getblockhash', - params: [ blockHeights[i] ] + parameters: [ blockHeights[i] ] }); } var blockHashes = []; - client.cmd(batch, function(err, result, resHeaders) { - blockHashes.push(result); + client.command(batch).then((responses) => { + responses.forEach((item) => { + blockHashes.push(item); + }); if (blockHashes.length == batch.length) { var batch2 = []; for (var i = 0; i < blockHashes.length; i++) { batch2.push({ method: 'getblock', - params: [ blockHashes[i] ] + parameters: [ blockHashes[i] ] }); } var blocks = []; - client.cmd(batch2, function(err2, result2, resHeaders2) { - if (err2) { + client.command(batch2).then((responses2) => { + //console.log(responses2); + if (false) { console.log("Error 138ryweufdf: " + err2); - } - blocks.push(result2); - if (blocks.length == batch2.length) { + } else { + responses2.forEach((item) => { + blocks.push(item); + }); + resolve(blocks); } }); @@ -252,7 +257,7 @@ function getBlockByHash(blockHash) { return new Promise(function(resolve, reject) { var client = global.client; - client.cmd('getblock', blockHash, function(err, result, resHeaders) { + client.command('getblock', blockHash, function(err, result, resHeaders) { if (err) { console.log("Error 0u2fgewue: " + err); @@ -296,7 +301,7 @@ function getRawTransaction(txid) { return; } - client.cmd('getrawtransaction', txid, 1, function(err, result, resHeaders) { + client.command('getrawtransaction', txid, 1, function(err, result, resHeaders) { if (err) { console.log("Error 329813yre823: " + err); @@ -344,7 +349,7 @@ function getRawTransactions(txids) { if (txid) { requests.push({ method: 'getrawtransaction', - params: [ txid, 1 ] + parameters: [ txid, 1 ] }); } } @@ -378,14 +383,12 @@ function executeBatchesSequentiallyInternal(batchId, batches, currentIndex, accu var count = batches[currentIndex].length; - client.cmd(batches[currentIndex], function(err, result, resHeaders) { - if (err) { - console.log("Error f83024hf4: " + err); - } - - accumulatedResults.push(result); + client.command(batches[currentIndex]).then(function(results) { + results.forEach((item) => { + accumulatedResults.push(item); count--; + }); if (count == 0) { executeBatchesSequentiallyInternal(batchId, batches, currentIndex + 1, accumulatedResults, resultFunc); @@ -397,7 +400,7 @@ function getBlockData(rpcClient, blockHash, txLimit, txOffset) { console.log("getBlockData: " + blockHash); return new Promise(function(resolve, reject) { - client.cmd('getblock', blockHash, function(err2, result2, resHeaders2) { + client.command('getblock', blockHash, function(err2, result2, resHeaders2) { if (err2) { console.log("Error 3017hfwe0f: " + err2); @@ -440,7 +443,7 @@ function getBlockData(rpcClient, blockHash, txLimit, txOffset) { function getHelp() { return new Promise(function(resolve, reject) { - client.cmd('help', function(err, result, resHeaders) { + client.command('help', function(err, result, resHeaders) { if (err) { console.log("Error 32907th429ghf: " + err); @@ -477,7 +480,7 @@ function getHelp() { function getRpcMethodHelp(methodName) { return new Promise(function(resolve, reject) { - client.cmd('help', methodName, function(err, result, resHeaders) { + client.command('help', methodName, function(err, result, resHeaders) { if (err) { console.log("Error 237hwerf07wehg: " + err); diff --git a/package.json b/package.json index 1b0e600..b2d8848 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "dependencies": { "body-parser": "~1.18.2", "cookie-parser": "~1.4.3", - "bitcoin": "3.0.1", + "bitcoin-core": "2.0.0", "crypto-js": "3.1.9-1", "debug": "~2.6.0", "decimal.js":"7.2.3", @@ -17,7 +17,6 @@ "jstransformer-markdown-it": "^2.0.0", "moment": "^2.21.0", "moment-duration-format": "2.2.2", - "monk": "^4.0.0", "morgan": "~1.9.0", "pug": "2.0.1", "request": "2.85.0", diff --git a/routes/baseActionsRouter.js b/routes/baseActionsRouter.js index 6d16966..d876bf7 100644 --- a/routes/baseActionsRouter.js +++ b/routes/baseActionsRouter.js @@ -4,7 +4,7 @@ var util = require('util'); var moment = require('moment'); var utils = require('./../app/utils'); var env = require("./../app/env"); -var bitcoin = require("bitcoin"); +var bitcoinCore = require("bitcoin-core"); var rpcApi = require("./../app/rpcApi"); router.get("/", function(req, res) { @@ -122,11 +122,11 @@ router.post("/connect", function(req, res) { req.session.port = port; req.session.username = username; - var client = new bitcoin.Client({ + var client = new bitcoinCore({ host: host, port: port, - user: username, - pass: password, + username: username, + password: password, timeout: 30000 }); @@ -322,7 +322,7 @@ router.get("/block-height/:blockHeight", function(req, res) { res.locals.offset = offset; res.locals.paginationBaseUrl = "/block-height/" + blockHeight; - client.cmd('getblockhash', blockHeight, function(err, result, resHeaders) { + client.command('getblockhash', blockHeight, function(err, result, resHeaders) { if (err) { // TODO handle RPC error return console.log(err); @@ -388,7 +388,7 @@ router.get("/tx/:transactionId", function(req, res) { rpcApi.getRawTransaction(txid).then(function(rawTxResult) { res.locals.result.getrawtransaction = rawTxResult; - client.cmd('getblock', rawTxResult.blockhash, function(err3, result3, resHeaders3) { + client.command('getblock', rawTxResult.blockhash, function(err3, result3, resHeaders3) { res.locals.result.getblock = result3; var txids = []; @@ -449,7 +449,7 @@ router.post("/rpc-terminal", function(req, res) { return; } - client.cmd([{method:cmd, params:parsedParams}], function(err, result, resHeaders) { + client.command([{method:cmd, parameters:parsedParams}], function(err, result, resHeaders) { console.log("Result[1]: " + JSON.stringify(result, null, 4)); console.log("Error[2]: " + JSON.stringify(err, null, 4)); console.log("Headers[3]: " + JSON.stringify(resHeaders, null, 4)); @@ -539,7 +539,7 @@ router.get("/rpc-browser", function(req, res) { console.log("Executing RPC '" + req.query.method + "' with params: [" + argValues + "]"); - client.cmd([{method:req.query.method, params:argValues}], function(err3, result3, resHeaders3) { + client.command([{method:req.query.method, parameters:argValues}], function(err3, result3, resHeaders3) { console.log("RPC Response: err=" + err3 + ", result=" + result3 + ", headers=" + resHeaders3); if (err3) {