From 2075357d16867ca79dc56c605eb8534665470e78 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 30 Mar 2015 15:34:05 -0300 Subject: [PATCH] extract block explorer --- lib/blockexplorer.js | 54 ++++++++++++++++++++++++++++++++++++++++++++ lib/server.js | 42 ++++++---------------------------- 2 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 lib/blockexplorer.js diff --git a/lib/blockexplorer.js b/lib/blockexplorer.js new file mode 100644 index 0000000..e163632 --- /dev/null +++ b/lib/blockexplorer.js @@ -0,0 +1,54 @@ +'use strict'; + +var _ = require('lodash'); +var $ = require('preconditions').singleton(); +var log = require('npmlog'); +log.debug = log.verbose; + +var Explorers = require('bitcore-explorers'); +var request = require('request'); + + +function BlockExplorer(opts) { + $.checkArgument(opts); + var provider = opts.provider || 'insight'; + var network = opts.network || 'livenet'; + + var url; + + switch (provider) { + case 'insight': + switch (network) { + default: + case 'livenet': + url = 'https://insight.bitpay.com:443'; + break; + case 'testnet': + url = 'https://test-insight.bitpay.com:443' + break; + } + var bc = new Explorers.Insight(url, network); + bc.getTransactions = _.bind(getTransactionsInsight, bc, url); + this.blockExplorer = bc; + break; + default: + throw new Error('Provider ' + provider + ' not supported'); + break; + }; +}; + + +function getTransactionsInsight(url, addresses, cb) { + request({ + method: "POST", + url: url + '/api/addrs/txs', + json: { + addrs: [].concat(addresses).join(',') + } + }, function(err, res, body) { + if (err || res.statusCode != 200) return cb(err || res); + return cb(null, body); + }); +}; + +module.exports = BlockExplorer; diff --git a/lib/server.js b/lib/server.js index 5be41c2..2c307b8 100644 --- a/lib/server.js +++ b/lib/server.js @@ -10,12 +10,12 @@ var Bitcore = WalletUtils.Bitcore; var PublicKey = Bitcore.PublicKey; var HDPublicKey = Bitcore.HDPublicKey; var Address = Bitcore.Address; -var Explorers = require('bitcore-explorers'); var ClientError = require('./clienterror'); var Utils = require('./utils'); var Storage = require('./storage'); var EventBroadcaster = require('./eventbroadcaster'); +var BlockExplorer = require('./blockexplorer'); var Wallet = require('./model/wallet'); var Copayer = require('./model/copayer'); @@ -336,42 +336,14 @@ WalletService.prototype.verifyMessageSignature = function(opts, cb) { WalletService.prototype._getBlockExplorer = function(provider, network) { - var url; - - function getTransactionsInsight(url, addresses, cb) { - var request = require('request'); - request({ - method: "POST", - url: url + '/api/addrs/txs', - json: { - addrs: [].concat(addresses).join(',') - } - }, function(err, res, body) { - if (err || res.statusCode != 200) return cb(err || res); - return cb(null, body); + if (!this.blockExplorer) { + this.blockexplorer = new BlockExplorer({ + provider: provider, + network: network, }); - }; - - if (this.blockExplorer) - return this.blockExplorer; - - switch (provider) { - default: ; - case 'insight': - switch (network) { - default: - case 'livenet': - url = 'https://insight.bitpay.com:443'; - break; - case 'testnet': - url = 'https://test-insight.bitpay.com:443' - break; - } - var bc = new Explorers.Insight(url, network); - bc.getTransactions = _.bind(getTransactionsInsight, bc, url); - return bc; - break; } + + return this.blockExplorer; }; /**