copay/js/models/blockchain/Insight.js

290 lines
7.2 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-08-05 12:25:02 -07:00
var coinUtil = bitcore.util;
2014-07-07 14:12:58 -07:00
var preconditions = require('preconditions').singleton();
2014-04-14 13:17:56 -07:00
2014-06-18 20:17:46 -07:00
var http;
if (process.version) {
http = require('http');
};
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';
2014-06-26 14:47:27 -07:00
this.schema = opts.schema || '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-06-26 14:47:27 -07:00
Insight.prototype._getOptions = function(method, path, data) {
return {
host: this.host,
port: this.port,
schema: this.schema,
method: method,
path: path,
data: data,
headers: {
'Access-Control-Request-Headers': ''
}
};
};
2014-08-05 12:25:02 -07:00
// This is vulneable to txid maneability
// TODO: if ret = false,
// check output address from similar transactions.
//
Insight.prototype.checkSentTx = function(tx, cb) {
var hash = coinUtil.formatHashFull(tx.getHash());
var options = this._getOptions('GET', '/api/tx/' + hash);
this._request(options, function(err, res) {
if (err) return cb(err);
var ret = false;
if (res && res.txid === hash) {
ret = hash;
}
return cb(err, ret);
});
};
Insight.prototype.getTransactions = function(addresses, cb) {
2014-07-07 14:12:58 -07:00
preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb);
2014-05-12 13:41:15 -07:00
2014-07-07 14:12:58 -07:00
var self = this;
if (!addresses || !addresses.length) return cb([]);
var txids = [];
var txs = [];
2014-05-12 13:41:15 -07:00
_asyncForEach(addresses, function(addr, callback) {
2014-06-26 14:47:27 -07:00
var options = self._getOptions('GET', '/api/addr/' + addr);
2014-07-07 14:46:12 -07:00
self._request(options, function(err, res) {
2014-07-07 14:46:12 -07:00
if (res && res.transactions) {
2014-07-07 14:12:58 -07:00
var txids_tmp = res.transactions;
for (var i = 0; i < txids_tmp.length; i++) {
txids.push(txids_tmp[i]);
}
}
callback();
});
}, function() {
2014-08-05 13:18:02 -07:00
var uniqueTxids = {};
for (var k in txids) {
2014-08-06 11:30:47 -07:00
uniqueTxids[txids[k]] = 1;
2014-08-05 13:18:02 -07:00
}
_asyncForEach(Object.keys(uniqueTxids), function(txid, callback2) {
2014-06-26 14:47:27 -07:00
var options = self._getOptions('GET', '/api/tx/' + txid);
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-06-26 14:47:27 -07:00
var options = this._getOptions('POST', '/api/addrs/utxo', 'addrs=' + addresses.join(','));
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-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
2014-06-26 14:47:27 -07:00
var options = this._getOptions('POST', '/api/tx/send', 'rawtx=' + rawtx);
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);
});
};
2014-06-19 12:34:37 -07:00
Insight.prototype.checkActivity = function(addresses, cb) {
if (!addresses) throw new Error('address must be set');
this.getTransactions(addresses, function onResult(txs) {
var flatArray = function(xss) {
return xss.reduce(function(r, xs) {
return r.concat(xs);
}, []);
};
var getInputs = function(t) {
return t.vin.map(function(vin) {
return vin.addr
});
};
var getOutputs = function(t) {
return flatArray(
t.vout.map(function(vout) {
2014-08-06 11:30:47 -07:00
return vout.scriptPubKey.addresses;
})
);
};
var activityMap = new Array(addresses.length);
var activeAddress = flatArray(txs.map(function(t) {
return getInputs(t).concat(getOutputs(t));
}));
activeAddress.forEach(function(addr) {
var index = addresses.indexOf(addr);
if (index != -1) activityMap[index] = true;
});
cb(null, activityMap);
});
};
2014-06-18 20:17:46 -07:00
Insight.prototype._requestNode = function(options, callback) {
if (options.method === 'POST') {
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': options.data.length,
};
2014-06-18 20:17:46 -07:00
}
2014-06-18 20:17:46 -07:00
var req = http.request(options, function(response) {
var ret, errTxt, e;
if (response.statusCode == 200 || response.statusCode === 304) {
response.on('data', function(chunk) {
2014-06-13 15:45:00 -07:00
try {
2014-06-18 20:17:46 -07:00
ret = JSON.parse(chunk);
2014-06-13 15:45:00 -07:00
} catch (e2) {
errTxt = 'CRITICAL: Wrong response from insight' + e2;
2014-04-14 13:17:56 -07:00
}
2014-06-18 20:17:46 -07:00
});
} else {
errTxt = "INSIGHT ERROR:" + response.statusCode;
console.log(errTxt);
e = new Error(errTxt);
return callback(e);
}
response.on('end', function() {
2014-06-13 15:45:00 -07:00
if (errTxt) {
2014-06-18 20:17:46 -07:00
console.log("INSIGHT ERROR:" + errTxt);
2014-06-13 15:45:00 -07:00
e = new Error(errTxt);
}
2014-06-13 15:45:00 -07:00
return callback(e, ret);
2014-06-18 20:17:46 -07:00
});
response.on('error', function(e) {
return callback(e, ret);
});
});
2014-06-18 20:17:46 -07:00
if (options.data) {
req.write(options.data);
}
req.end();
};
2014-06-18 20:17:46 -07:00
Insight.prototype._requestBrowser = function(options, callback) {
2014-07-07 15:38:45 -07:00
var self = this;
2014-06-18 20:17:46 -07:00
var request = new XMLHttpRequest();
2014-06-26 14:47:27 -07:00
var url = (options.schema || 'http') + '://' + options.host;
2014-06-18 20:17:46 -07:00
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);
request.timeout = 5000;
request.ontimeout = function() {
setTimeout(function() {
return self._request(options, callback);
}, self.retryDelay);
return callback(new Error('Insight request timeout'));
};
2014-07-07 15:38:45 -07:00
2014-06-18 20:17:46 -07:00
request.onreadystatechange = function() {
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;
}
} 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() {
return self._request(options, callback);
}, self.retryDelay);
}
if (errTxt) {
console.log("INSIGHT ERROR:", e);
e = new Error(errTxt);
2014-04-14 13:17:56 -07:00
}
2014-06-18 20:17:46 -07:00
return callback(e, ret);
};
2014-06-18 20:17:46 -07:00
if (options.method === 'POST') {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
request.send(options.data || null);
};
Insight.prototype._request = function(options, callback) {
if (typeof process === 'undefined' || !process.version) {
this._requestBrowser(options, callback);
} else {
this._requestNode(options, callback);
2014-04-14 13:17:56 -07:00
}
};
2014-04-14 13:17:56 -07:00
module.exports = require('soop')(Insight);