translate addr module
This commit is contained in:
parent
391898a06e
commit
55ce16b552
|
@ -44,6 +44,7 @@ function BlockChainExplorer(opts) {
|
|||
url: url,
|
||||
apiPrefix: opts.apiPrefix,
|
||||
userAgent: opts.userAgent,
|
||||
translateAddresses: opts.translateAddresses,
|
||||
});
|
||||
default:
|
||||
throw new Error('Provider ' + provider + ' not supported.');
|
||||
|
|
|
@ -8,6 +8,7 @@ log.debug = log.verbose;
|
|||
var io = require('socket.io-client');
|
||||
var requestList = require('./request-list');
|
||||
var Common = require('../common');
|
||||
var AddressTranslator = require('../addresstranslator');
|
||||
var Constants = Common.Constants,
|
||||
Defaults = Common.Defaults,
|
||||
Utils = Common.Utils;
|
||||
|
@ -18,14 +19,16 @@ function Insight(opts) {
|
|||
$.checkArgument(Utils.checkValueInCollection(opts.coin, Constants.COINS));
|
||||
$.checkArgument(opts.url);
|
||||
|
||||
this.apiPrefix = opts.apiPrefix || '/api';
|
||||
this.apiPrefix = _.isUndefined(opts.apiPrefix)? '/api' : opts.apiPrefix;
|
||||
this.coin = opts.coin || Defaults.COIN;
|
||||
this.network = opts.network || 'livenet';
|
||||
this.hosts = opts.url;
|
||||
this.userAgent = opts.userAgent || 'bws';
|
||||
this.requestQueue = async.queue(this._doRequest.bind(this), Defaults.INSIGHT_REQUEST_POOL_SIZE);
|
||||
}
|
||||
this.shouldTranslateAddresses = _.isUndefined(opts.translateAddresses) ? this.coin == 'bch' : opts.translateAddresses;
|
||||
|
||||
this.requestQueue = async.queue(this._doRequest.bind(this), Defaults.INSIGHT_REQUEST_POOL_SIZE);
|
||||
|
||||
}
|
||||
|
||||
var _parseErr = function(err, res) {
|
||||
if (err) {
|
||||
|
@ -36,6 +39,46 @@ var _parseErr = function(err, res) {
|
|||
return "Error querying the blockchain";
|
||||
};
|
||||
|
||||
|
||||
// Translate Request Address query
|
||||
Insight.prototype.translateQueryAddresses = function(addresses) {
|
||||
if (!this.shouldTranslateAddresses) return addresses;
|
||||
|
||||
// It is called 'translatedInput' from Cxxx to 1xxx because the
|
||||
// module was created for insight-api
|
||||
return AddressTranslator.translateInput(addresses);
|
||||
};
|
||||
|
||||
|
||||
// Translate Result Address
|
||||
Insight.prototype.translateResultAddresses = function(addresses) {
|
||||
if (!this.shouldTranslateAddresses) return addresses;
|
||||
|
||||
// It is called 'translateOutput' from 1xxx to Cxxx because the
|
||||
// module was created for insight-api
|
||||
return AddressTranslator.translateOutput(addresses);
|
||||
};
|
||||
|
||||
|
||||
Insight.prototype.translateTx = function(tx) {
|
||||
if (!this.shouldTranslateAddresses) return tx;
|
||||
|
||||
_.each(tx.input, function(x){
|
||||
if (x.addr) {
|
||||
x.addr = AddressTranslator.translateOutput(x.addr);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
_.each(tx.output, function(x){
|
||||
if (x.scriptPubKey && x.scriptPubKey.addresses) {
|
||||
x.scriptPubKey.addresses = AddressTranslator.translateOutput(x.scriptPubKey.addresses);
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
Insight.prototype._doRequest = function(args, cb) {
|
||||
var opts = {
|
||||
hosts: this.hosts,
|
||||
|
@ -49,7 +92,6 @@ Insight.prototype._doRequest = function(args, cb) {
|
|||
// s= s.substr(0,100) + '...';
|
||||
log.debug('', 'Insight Q: %s', s);
|
||||
|
||||
console.log('[insight.js.51]'); //TODO
|
||||
requestList(_.defaults(args, opts), cb);
|
||||
};
|
||||
|
||||
|
@ -61,17 +103,25 @@ Insight.prototype.getConnectionInfo = function() {
|
|||
* Retrieve a list of unspent outputs associated with an address or set of addresses
|
||||
*/
|
||||
Insight.prototype.getUtxos = function(addresses, cb) {
|
||||
var self = this;
|
||||
|
||||
var url = this.url + this.apiPrefix + '/addrs/utxo';
|
||||
var args = {
|
||||
method: 'POST',
|
||||
path: this.apiPrefix + '/addrs/utxo',
|
||||
json: {
|
||||
addrs: _.uniq([].concat(addresses)).join(',')
|
||||
addrs: this.translateQueryAddresses(_.uniq([].concat(addresses))).join(',')
|
||||
},
|
||||
};
|
||||
|
||||
this.requestQueue.push(args, function(err, res, unspent) {
|
||||
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
|
||||
|
||||
if (self.shouldTranslateAddresses) {
|
||||
_.each(unspent, function(x) {
|
||||
x.address = self.translateResultAddresses(x.address);
|
||||
});
|
||||
}
|
||||
return cb(null, unspent);
|
||||
});
|
||||
};
|
||||
|
@ -95,6 +145,7 @@ Insight.prototype.broadcast = function(rawTx, cb) {
|
|||
};
|
||||
|
||||
Insight.prototype.getTransaction = function(txid, cb) {
|
||||
var self = this;
|
||||
var args = {
|
||||
method: 'GET',
|
||||
path: this.apiPrefix + '/tx/' + txid,
|
||||
|
@ -106,11 +157,16 @@ Insight.prototype.getTransaction = function(txid, cb) {
|
|||
if (err || res.statusCode !== 200)
|
||||
return cb(_parseErr(err, res));
|
||||
|
||||
self.translateTx(tx);
|
||||
|
||||
return cb(null, tx);
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype.getTransactions = function(addresses, from, to, cb) {
|
||||
var self = this;
|
||||
|
||||
|
||||
var qs = [];
|
||||
var total;
|
||||
if (_.isNumber(from)) qs.push('from=' + from);
|
||||
|
@ -125,7 +181,7 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
|
|||
method: 'POST',
|
||||
path: this.apiPrefix + '/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : ''),
|
||||
json: {
|
||||
addrs: _.uniq([].concat(addresses)).join(',')
|
||||
addrs: this.translateQueryAddresses(_.uniq([].concat(addresses))).join(',')
|
||||
},
|
||||
timeout: 120000,
|
||||
};
|
||||
|
@ -145,6 +201,12 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
|
|||
// NOTE: Whenever Insight breaks communication with bitcoind, it returns invalid data but no error code.
|
||||
if (!_.isArray(txs) || (txs.length != _.compact(txs).length)) return cb(new Error('Could not retrieve transactions from blockchain. Request was:' + JSON.stringify(args)));
|
||||
|
||||
if (self.shouldTranslateAddresses) {
|
||||
_.each(txs, function(tx){
|
||||
self.translateTx(tx);
|
||||
});
|
||||
}
|
||||
|
||||
return cb(null, txs, total);
|
||||
});
|
||||
};
|
||||
|
@ -154,7 +216,7 @@ Insight.prototype.getAddressActivity = function(address, cb) {
|
|||
|
||||
var args = {
|
||||
method: 'GET',
|
||||
path: self.apiPrefix + '/addr/' + address,
|
||||
path: self.apiPrefix + '/addr/' + this.translateQueryAddresses(address),
|
||||
json: true,
|
||||
};
|
||||
|
||||
|
@ -163,6 +225,8 @@ Insight.prototype.getAddressActivity = function(address, cb) {
|
|||
if (err || res.statusCode !== 200)
|
||||
return cb(_parseErr(err, res));
|
||||
|
||||
// note: result.addrStr is not translated, but not used.
|
||||
|
||||
var nbTxs = result.unconfirmedTxApperances + result.txApperances;
|
||||
return cb(null, nbTxs > 0);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue