copay/js/models/blockchain/Insight.js

255 lines
5.9 KiB
JavaScript
Raw Normal View History

2014-04-14 13:17:56 -07:00
'use strict';
2014-05-12 13:41:15 -07:00
var imports = require('soop').imports();
var bitcore = require('bitcore');
2014-04-14 13:17:56 -07:00
function Insight(opts) {
opts = opts || {};
2014-04-15 14:23:35 -07:00
this.host = opts.host || 'localhost';
this.port = opts.port || '3001';
this.scheme = opts.scheme || 'http';
2014-06-05 08:18:54 -07:00
this.retryDelay = opts.retryDelay || 5000;
2014-04-14 13:17:56 -07:00
}
function _asyncForEach(array, fn, callback) {
array = array.slice(0);
2014-05-12 13:41:15 -07:00
2014-04-14 13:17:56 -07:00
function processOne() {
var item = array.pop();
fn(item, function(result) {
2014-05-12 13:41:15 -07:00
if (array.length > 0) {
setTimeout(processOne, 0); // schedule immediately
} else {
callback(); // Done!
}
});
2014-04-14 13:17:56 -07:00
}
2014-05-12 13:41:15 -07:00
if (array.length > 0) {
2014-04-14 13:17:56 -07:00
setTimeout(processOne, 0); // schedule immediately
} else {
callback(); // Done!
}
};
2014-05-12 13:41:15 -07:00
function removeRepeatedElements(ar) {
var ya = false,
v = "",
aux = [].concat(ar),
r = Array();
for (var i in aux) { //
v = aux[i];
ya = false;
for (var a in aux) {
if (v == aux[a]) {
if (ya == false) {
ya = true;
} else {
aux[a] = "";
}
}
}
}
for (var a in aux) {
if (aux[a] != "") {
r.push(aux[a]);
}
}
return r;
}
2014-04-14 13:17:56 -07:00
Insight.prototype.getTransactions = function(addresses, cb) {
var self = this;
2014-05-12 13:41:15 -07:00
if (!addresses || !addresses.length) return cb([]);
var txids = [];
var txs = [];
2014-05-12 13:41:15 -07:00
_asyncForEach(addresses, function(addr, callback) {
var options = {
host: self.host,
port: self.port,
scheme: self.scheme,
method: 'GET',
path: '/api/addr/' + addr,
2014-05-12 13:41:15 -07:00
headers: {
'Access-Control-Request-Headers': ''
}
};
2014-05-12 13:41:15 -07:00
self._request(options, function(err, res) {
var txids_tmp = res.transactions;
2014-05-12 13:41:15 -07:00
for (var i = 0; i < txids_tmp.length; i++) {
txids.push(txids_tmp[i]);
}
callback();
});
}, function() {
var clean_txids = removeRepeatedElements(txids);
_asyncForEach(clean_txids, function(txid, callback2) {
var options = {
host: self.host,
port: self.port,
scheme: self.scheme,
method: 'GET',
path: '/api/tx/' + txid,
2014-05-12 13:41:15 -07:00
headers: {
'Access-Control-Request-Headers': ''
}
};
self._request(options, function(err, res) {
txs.push(res);
callback2();
});
}, function() {
return cb(txs);
});
});
};
2014-04-18 09:20:35 -07:00
Insight.prototype.getUnspent = function(addresses, cb) {
if (!addresses || !addresses.length) return cb(null, []);
2014-04-14 13:17:56 -07:00
var all = [];
2014-05-12 14:47:17 -07:00
var options = {
2014-05-20 06:44:53 -07:00
host: this.host,
port: this.port,
scheme: this.scheme,
method: 'POST',
data: 'addrs=' + addresses.join(','),
path: '/api/addrs/utxo',
2014-05-12 14:47:17 -07:00
headers: {
'Access-Control-Request-Headers': ''
}
};
2014-05-12 13:41:15 -07:00
2014-06-05 08:18:54 -07:00
var self = this;
2014-05-20 06:44:53 -07:00
this._request(options, function(err, res) {
if (err) {
return cb(err);
}
2014-06-05 08:18:54 -07:00
2014-05-12 14:47:17 -07:00
if (res && res.length > 0) {
all = all.concat(res);
}
return cb(null, all);
2014-04-14 13:17:56 -07:00
});
};
Insight.prototype.sendRawTransaction = function(rawtx, cb) {
2014-06-09 07:19:38 -07:00
if (!rawtx) throw new Error('rawtx must be set');
2014-04-14 13:17:56 -07:00
var options = {
host: this.host,
port: this.port,
method: 'POST',
path: '/api/tx/send',
2014-05-12 13:41:15 -07:00
data: 'rawtx=' + rawtx,
headers: {
'Access-Control-Request-Headers': ''
2014-05-12 13:41:15 -07:00
}
2014-04-14 13:17:56 -07:00
};
2014-05-12 13:41:15 -07:00
this._request(options, function(err, res) {
2014-04-14 13:17:56 -07:00
if (err) return cb();
2014-04-20 16:24:24 -07:00
2014-04-14 13:17:56 -07:00
return cb(res.txid);
});
};
Insight.prototype._request = function(options, callback) {
2014-06-05 08:18:54 -07:00
var self = this;
if (typeof process === 'undefined' || !process.version) {
var request = new XMLHttpRequest();
// TODO: Normalize URL
var url = 'http://' + options.host;
if (options.port !== 80) {
url = url + ':' + options.port;
}
url = url + options.path;
if (options.data && options.method === 'GET') {
url = url + '?' + options.data;
}
request.open(options.method, url, true);
2014-06-05 08:18:54 -07:00
request.timeout = 5000;
request.ontimeout = function() {
2014-06-05 08:18:54 -07:00
setTimeout(function() {
2014-06-13 15:45:00 -07:00
return self._request(options, callback);
2014-06-05 08:18:54 -07:00
}, self.retryDelay);
return callback(new Error('Insight request timeout'));
};
request.onreadystatechange = function() {
2014-06-13 15:45:00 -07:00
if (request.readyState !== 4) return;
var ret, errTxt, e;
if (request.status === 200 || request.status === 304) {
try {
ret = JSON.parse(request.responseText);
} catch (e2) {
errTxt = 'CRITICAL: Wrong response from insight' + e2;
2014-04-14 13:17:56 -07:00
}
2014-06-13 15:45:00 -07:00
} else if (request.status >= 400 && request.status < 499) {
errTxt = 'CRITICAL: Bad request to insight. Probably wrong transaction to broadcast?.';
} else {
errTxt = 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText;
setTimeout(function() {
console.log('### Retrying Insight Request....');
return self._request(options, callback);
}, self.retryDelay);
}
if (errTxt) {
console.log("INSIGHT ERROR:", e);
e = new Error(errTxt);
}
2014-06-13 15:45:00 -07:00
return callback(e, ret);
};
if (options.method === 'POST') {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2014-04-14 13:17:56 -07:00
}
request.send(options.data || null);
2014-06-13 15:45:00 -07:00
} else {
var http = require('http');
var req = http.request(options, function(response) {
var ret;
if (response.statusCode == 200 || response.status === 304) {
response.on('data', function(chunk) {
try {
ret = JSON.parse(chunk);
} catch (e) {
return callback({
message: 'Wrong response from insight'
2014-05-12 13:41:15 -07:00
});
}
});
2014-05-12 13:41:15 -07:00
response.on('end', function() {
return callback(null, ret);
});
2014-05-12 13:41:15 -07:00
} else {
return callback({
2014-05-12 13:41:15 -07:00
message: 'Error ' + response.statusCode
});
}
});
if (options.data) {
req.write(options.data);
2014-04-14 13:17:56 -07:00
}
req.end();
2014-04-14 13:17:56 -07:00
}
};
2014-04-14 13:17:56 -07:00
module.exports = require('soop')(Insight);