From e7486c7fe09f25f1fc12744465f02b64ebfd0452 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 12 May 2014 17:41:15 -0300 Subject: [PATCH] blockchain API changes --- js/models/blockchain/Insight.js | 124 +++++++++++++++++--------------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/js/models/blockchain/Insight.js b/js/models/blockchain/Insight.js index 7fa1b275f..7c6d3acc4 100644 --- a/js/models/blockchain/Insight.js +++ b/js/models/blockchain/Insight.js @@ -1,7 +1,7 @@ 'use strict'; -var imports = require('soop').imports(); -var bitcore = require('bitcore'); +var imports = require('soop').imports(); +var bitcore = require('bitcore'); function Insight(opts) { opts = opts || {}; @@ -12,55 +12,58 @@ function Insight(opts) { function _asyncForEach(array, fn, callback) { array = array.slice(0); + function processOne() { var item = array.pop(); fn(item, function(result) { - if(array.length > 0) { - setTimeout(processOne, 0); // schedule immediately - } else { - callback(); // Done! - } - }); + if (array.length > 0) { + setTimeout(processOne, 0); // schedule immediately + } else { + callback(); // Done! + } + }); } - if(array.length > 0) { + if (array.length > 0) { setTimeout(processOne, 0); // schedule immediately } else { callback(); // Done! } }; -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; +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; } Insight.prototype.getTransactions = function(addresses, cb) { var self = this; - + if (!addresses || !addresses.length) return cb([]); var txids = []; var txs = []; - + _asyncForEach(addresses, function(addr, callback) { var options = { host: self.host, @@ -69,12 +72,14 @@ Insight.prototype.getTransactions = function(addresses, cb) { method: 'GET', path: '/api/addr/' + addr, - headers: { 'Access-Control-Request-Headers' : '' } + headers: { + 'Access-Control-Request-Headers': '' + } }; - + self._request(options, function(err, res) { var txids_tmp = res.transactions; - for(var i=0; i 0) { + if (res && res.length > 0) { all = all.concat(res); - } + } callback(); }); }, function() { @@ -137,14 +146,16 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) { port: this.port, method: 'POST', path: '/api/tx/send', - data: 'rawtx='+rawtx, - headers: { 'content-type' : 'application/x-www-form-urlencoded' } + data: 'rawtx=' + rawtx, + headers: { + 'content-type': 'application/x-www-form-urlencoded' + } }; - this._request(options, function(err,res) { -console.log('[Insight.js.73:err:]',err); //TODO + this._request(options, function(err, res) { + console.log('[Insight.js.73:err:]', err); //TODO if (err) return cb(); -console.log('[Insight.js.74]', res); //TODO + console.log('[Insight.js.74]', res); //TODO return cb(res.txid); }); }; @@ -171,8 +182,7 @@ Insight.prototype._request = function(options, callback) { if (request.readyState === 4) { if (request.status === 200) { return callback(null, JSON.parse(request.responseText)); - } - else { + } else { return callback({ message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText }); @@ -195,17 +205,20 @@ Insight.prototype._request = function(options, callback) { try { ret = JSON.parse(chunk); } catch (e) { - callback({message: "Wrong response from insight"}); + callback({ + message: "Wrong response from insight" + }); return; } }); - response.on('end', function () { - callback(undefined, ret); + response.on('end', function() { + callback(undefined, ret); return; }); - } - else { - callback({message: 'Error ' + response.statusCode}); + } else { + callback({ + message: 'Error ' + response.statusCode + }); return; } }); @@ -218,4 +231,3 @@ Insight.prototype._request = function(options, callback) { module.exports = require('soop')(Insight); -