bitcore-wallet-service/lib/blockchainexplorer.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-03-30 11:34:05 -07:00
'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');
var io = require('socket.io-client');
2015-03-30 11:34:05 -07:00
2015-03-30 16:16:51 -07:00
function BlockChainExplorer(opts) {
2015-03-30 11:34:05 -07:00
$.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;
}
2015-03-31 08:04:02 -07:00
var explorer = new Explorers.Insight(url, network);
explorer.getTransactions = _.bind(getTransactionsInsight, explorer, url);
explorer.initSocket = _.bind(initSocketInsight, explorer, url);
return explorer;
2015-03-30 11:34:05 -07:00
default:
throw new Error('Provider ' + provider + ' not supported');
};
};
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);
});
};
function initSocketInsight(url) {
var socket = io.connect(url, {});
return socket;
};
2015-03-30 16:16:51 -07:00
module.exports = BlockChainExplorer;