blockchain API changes

This commit is contained in:
Manuel Araoz 2014-05-12 17:41:15 -03:00
commit e7486c7fe0

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var imports = require('soop').imports(); var imports = require('soop').imports();
var bitcore = require('bitcore'); var bitcore = require('bitcore');
function Insight(opts) { function Insight(opts) {
opts = opts || {}; opts = opts || {};
@ -12,55 +12,58 @@ function Insight(opts) {
function _asyncForEach(array, fn, callback) { function _asyncForEach(array, fn, callback) {
array = array.slice(0); array = array.slice(0);
function processOne() { function processOne() {
var item = array.pop(); var item = array.pop();
fn(item, function(result) { fn(item, function(result) {
if(array.length > 0) { if (array.length > 0) {
setTimeout(processOne, 0); // schedule immediately setTimeout(processOne, 0); // schedule immediately
} else { } else {
callback(); // Done! callback(); // Done!
} }
}); });
} }
if(array.length > 0) { if (array.length > 0) {
setTimeout(processOne, 0); // schedule immediately setTimeout(processOne, 0); // schedule immediately
} else { } else {
callback(); // Done! callback(); // Done!
} }
}; };
function removeRepeatedElements(ar){ function removeRepeatedElements(ar) {
var ya=false,v="",aux=[].concat(ar),r=Array(); var ya = false,
for (var i in aux){ // v = "",
v=aux[i]; aux = [].concat(ar),
ya=false; r = Array();
for (var a in aux){ for (var i in aux) { //
if (v==aux[a]){ v = aux[i];
if (ya==false){ ya = false;
ya=true; for (var a in aux) {
} if (v == aux[a]) {
else{ if (ya == false) {
aux[a]=""; ya = true;
} } else {
} aux[a] = "";
} }
} }
for (var a in aux){ }
if (aux[a]!=""){ }
r.push(aux[a]); for (var a in aux) {
} if (aux[a] != "") {
} r.push(aux[a]);
return r; }
}
return r;
} }
Insight.prototype.getTransactions = function(addresses, cb) { Insight.prototype.getTransactions = function(addresses, cb) {
var self = this; var self = this;
if (!addresses || !addresses.length) return cb([]); if (!addresses || !addresses.length) return cb([]);
var txids = []; var txids = [];
var txs = []; var txs = [];
_asyncForEach(addresses, function(addr, callback) { _asyncForEach(addresses, function(addr, callback) {
var options = { var options = {
host: self.host, host: self.host,
@ -69,12 +72,14 @@ Insight.prototype.getTransactions = function(addresses, cb) {
method: 'GET', method: 'GET',
path: '/api/addr/' + addr, path: '/api/addr/' + addr,
headers: { 'Access-Control-Request-Headers' : '' } headers: {
'Access-Control-Request-Headers': ''
}
}; };
self._request(options, function(err, res) { self._request(options, function(err, res) {
var txids_tmp = res.transactions; var txids_tmp = res.transactions;
for(var i=0; i<txids_tmp.length; i++) { for (var i = 0; i < txids_tmp.length; i++) {
txids.push(txids_tmp[i]); txids.push(txids_tmp[i]);
} }
callback(); callback();
@ -88,7 +93,9 @@ Insight.prototype.getTransactions = function(addresses, cb) {
scheme: self.scheme, scheme: self.scheme,
method: 'GET', method: 'GET',
path: '/api/tx/' + txid, path: '/api/tx/' + txid,
headers: { 'Access-Control-Request-Headers' : '' } headers: {
'Access-Control-Request-Headers': ''
}
}; };
self._request(options, function(err, res) { self._request(options, function(err, res) {
txs.push(res); txs.push(res);
@ -102,7 +109,7 @@ Insight.prototype.getTransactions = function(addresses, cb) {
Insight.prototype.getUnspent = function(addresses, cb) { Insight.prototype.getUnspent = function(addresses, cb) {
var self = this; var self = this;
if (!addresses || !addresses.length) return cb([]); if (!addresses || !addresses.length) return cb([]);
var all = []; var all = [];
@ -115,13 +122,15 @@ Insight.prototype.getUnspent = function(addresses, cb) {
method: 'GET', method: 'GET',
path: '/api/addr/' + addr + '/utxo', path: '/api/addr/' + addr + '/utxo',
headers: { 'Access-Control-Request-Headers' : '' } headers: {
'Access-Control-Request-Headers': ''
}
}; };
self._request(options, function(err, res) { self._request(options, function(err, res) {
if (res && res.length > 0) { if (res && res.length > 0) {
all = all.concat(res); all = all.concat(res);
} }
callback(); callback();
}); });
}, function() { }, function() {
@ -137,14 +146,16 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
port: this.port, port: this.port,
method: 'POST', method: 'POST',
path: '/api/tx/send', path: '/api/tx/send',
data: 'rawtx='+rawtx, data: 'rawtx=' + rawtx,
headers: { 'content-type' : 'application/x-www-form-urlencoded' } headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}; };
this._request(options, function(err,res) { this._request(options, function(err, res) {
console.log('[Insight.js.73:err:]',err); //TODO console.log('[Insight.js.73:err:]', err); //TODO
if (err) return cb(); if (err) return cb();
console.log('[Insight.js.74]', res); //TODO console.log('[Insight.js.74]', res); //TODO
return cb(res.txid); return cb(res.txid);
}); });
}; };
@ -171,8 +182,7 @@ Insight.prototype._request = function(options, callback) {
if (request.readyState === 4) { if (request.readyState === 4) {
if (request.status === 200) { if (request.status === 200) {
return callback(null, JSON.parse(request.responseText)); return callback(null, JSON.parse(request.responseText));
} } else {
else {
return callback({ return callback({
message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText
}); });
@ -195,17 +205,20 @@ Insight.prototype._request = function(options, callback) {
try { try {
ret = JSON.parse(chunk); ret = JSON.parse(chunk);
} catch (e) { } catch (e) {
callback({message: "Wrong response from insight"}); callback({
message: "Wrong response from insight"
});
return; return;
} }
}); });
response.on('end', function () { response.on('end', function() {
callback(undefined, ret); callback(undefined, ret);
return; return;
}); });
} } else {
else { callback({
callback({message: 'Error ' + response.statusCode}); message: 'Error ' + response.statusCode
});
return; return;
} }
}); });
@ -218,4 +231,3 @@ Insight.prototype._request = function(options, callback) {
module.exports = require('soop')(Insight); module.exports = require('soop')(Insight);