mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #1234 from yemel/refactor/blockchain
Refactor/blockchain
This commit is contained in:
commit
ccbbe58fc1
|
@ -128,6 +128,19 @@ input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill, inpu
|
|||
-webkit-box-shadow: 0 0 0px 1000px white inset;
|
||||
}
|
||||
|
||||
.status {
|
||||
border: 1px solid #f0c36d;
|
||||
background-color: #f9edbe;
|
||||
position: absolute;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
left: 0;
|
||||
right: 0;
|
||||
top: 10px;
|
||||
width: 215px;
|
||||
padding: 7px;
|
||||
}
|
||||
|
||||
.join label,
|
||||
.open label,
|
||||
.setup label {
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<div class="page">
|
||||
<div class="off-canvas-wrap">
|
||||
<div class="inner-wrap">
|
||||
|
||||
<span class="status" ng-if="$root.reconnecting">Attempting to reconnect...</span>
|
||||
<nav class="tab-bar" ng-class="{'hide-tab-bar' : !$root.wallet ||
|
||||
!$root.wallet.isReady() || $root.wallet.isLocked}">
|
||||
<section class="left-small">
|
||||
|
|
|
@ -9,8 +9,7 @@ angular.module('copayApp.controllers').controller('AddressesController',
|
|||
$scope.loading = true;
|
||||
w.generateAddress(null, function() {
|
||||
$timeout(function() {
|
||||
controllerUtils.setSocketHandlers();
|
||||
controllerUtils.updateAddressList();
|
||||
controllerUtils.updateGlobalAddresses();
|
||||
$scope.loading = false;
|
||||
}, 1);
|
||||
});
|
||||
|
|
|
@ -57,9 +57,6 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
|
|||
return new Array(num);
|
||||
}
|
||||
|
||||
// Init socket handlers (with no wallet yet)
|
||||
controllerUtils.setSocketHandlers();
|
||||
|
||||
if ($rootScope.wallet) {
|
||||
$scope.$on('$idleWarn', function(a,countdown) {
|
||||
if (!(countdown%5))
|
||||
|
|
|
@ -109,7 +109,9 @@ angular.module('copayApp.controllers').controller('TransactionsController',
|
|||
var addresses = w.getAddressesStr();
|
||||
if (addresses.length > 0) {
|
||||
$scope.blockchain_txs = $scope.wallet.txCache || [];
|
||||
w.blockchain.getTransactions(addresses, function(txs) {
|
||||
w.blockchain.getTransactions(addresses, function(err, txs) {
|
||||
if (err) throw err;
|
||||
|
||||
$timeout(function() {
|
||||
$scope.blockchain_txs = [];
|
||||
for (var i = 0; i < txs.length; i++) {
|
||||
|
|
|
@ -1,72 +1,180 @@
|
|||
'use strict';
|
||||
|
||||
var util = require('util');
|
||||
var async = require('async');
|
||||
var request = require('request');
|
||||
var bitcore = require('bitcore');
|
||||
var coinUtil = bitcore.util;
|
||||
var io = require('socket.io-client');
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
var http;
|
||||
if (process.version) {
|
||||
http = require('http');
|
||||
/*
|
||||
This class lets interfaces with the blockchain, making general queries and
|
||||
subscribing to transactions on adressess and blocks.
|
||||
|
||||
Opts:
|
||||
- host
|
||||
- port
|
||||
- schema
|
||||
- reconnection (optional)
|
||||
- reconnectionDelay (optional)
|
||||
|
||||
Events:
|
||||
- tx: activity on subscribed address.
|
||||
- block: a new block that includes a subscribed address.
|
||||
- connect: the connection with the blockchain is ready.
|
||||
- disconnect: the connection with the blochckain is unavailable.
|
||||
*/
|
||||
|
||||
var Insight = function (opts) {
|
||||
this.status = this.STATUS.DISCONNECTED;
|
||||
this.subscribed = {};
|
||||
this.listeningBlocks = false;
|
||||
|
||||
preconditions.checkArgument(opts).shouldBeObject(opts)
|
||||
.checkArgument(opts.host)
|
||||
.checkArgument(opts.port)
|
||||
.checkArgument(opts.schema);
|
||||
|
||||
this.url = opts.schema + '://' + opts.host + ':' + opts.port;
|
||||
this.opts = {
|
||||
'reconnection': opts.reconnection || true,
|
||||
'reconnectionDelay': opts.reconnectionDelay || 1000,
|
||||
'secure': opts.schema === 'https'
|
||||
};
|
||||
|
||||
function Insight(opts) {
|
||||
opts = opts || {};
|
||||
this.host = opts.host || 'localhost';
|
||||
this.port = opts.port || '3001';
|
||||
this.schema = opts.schema || 'http';
|
||||
this.retryDelay = opts.retryDelay || 5000;
|
||||
}
|
||||
this.socket = this.getSocket(this.url, this.opts);
|
||||
|
||||
function _asyncForEach(array, fn, callback) {
|
||||
array = array.slice(0);
|
||||
// Emmit connection events
|
||||
var self = this;
|
||||
this.socket.on('connect', function() {
|
||||
self.status = self.STATUS.CONNECTED;
|
||||
self.subscribeToBlocks();
|
||||
self.emit('connect', 0);
|
||||
});
|
||||
|
||||
function processOne() {
|
||||
var item = array.pop();
|
||||
fn(item, function(result) {
|
||||
if (array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
this.socket.on('connect_error', function() {
|
||||
if (self.status != self.STATUS.CONNECTED) return;
|
||||
self.status = self.STATUS.DISCONNECTED;
|
||||
self.emit('disconnect');
|
||||
});
|
||||
|
||||
this.socket.on('connect_timeout', function() {
|
||||
if (self.status != self.STATUS.CONNECTED) return;
|
||||
self.status = self.STATUS.DISCONNECTED;
|
||||
self.emit('disconnect');
|
||||
});
|
||||
|
||||
this.socket.on('reconnect', function(attempt) {
|
||||
if (self.status != self.STATUS.DISCONNECTED) return;
|
||||
self.status = self.STATUS.CONNECTED;
|
||||
self.emit('connect', attempt);
|
||||
});
|
||||
}
|
||||
if (array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
|
||||
util.inherits(Insight, EventEmitter);
|
||||
|
||||
Insight.prototype.STATUS = {
|
||||
CONNECTED: 'connected',
|
||||
DISCONNECTED: 'disconnected',
|
||||
DESTROYED: 'destroyed'
|
||||
}
|
||||
|
||||
/** @private */
|
||||
Insight.prototype.subscribeToBlocks = function() {
|
||||
if (this.listeningBlocks || !this.socket.connected) return;
|
||||
|
||||
var self = this;
|
||||
this.socket.emit('subscribe', 'inv');
|
||||
this.socket.on('block', function(blockHash) {
|
||||
self.emit('block', blockHash);
|
||||
});
|
||||
this.listeningBlocks = true;
|
||||
}
|
||||
|
||||
/** @private */
|
||||
Insight.prototype.getSocket = function(url, opts) {
|
||||
return io(this.url, this.opts);
|
||||
}
|
||||
|
||||
/** @private */
|
||||
Insight.prototype.request = function(path, cb) {
|
||||
preconditions.checkArgument(path).shouldBeFunction(cb);
|
||||
request(this.url + path, cb);
|
||||
}
|
||||
|
||||
/** @private */
|
||||
Insight.prototype.requestPost = function(path, data, cb) {
|
||||
preconditions.checkArgument(path).checkArgument(data).shouldBeFunction(cb);
|
||||
request({method: "POST", url: this.url + path, json: data}, cb);
|
||||
}
|
||||
|
||||
Insight.prototype.destroy = function() {
|
||||
this.socket.destroy();
|
||||
this.subscribed = {};
|
||||
this.status = this.STATUS.DESTROYED;
|
||||
this.removeAllListeners();
|
||||
};
|
||||
|
||||
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': ''
|
||||
Insight.prototype.subscribe = function(addresses) {
|
||||
addresses = Array.isArray(addresses) ? addresses : [addresses];
|
||||
var self = this;
|
||||
|
||||
function handlerFor(self, address) {
|
||||
return function (txid) {
|
||||
// verify the address is still subscribed
|
||||
if (!self.subscribed[address]) return;
|
||||
self.emit('tx', {address: address, txid: txid});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
addresses.forEach(function(address) {
|
||||
preconditions.checkArgument(new bitcore.Address(address).isValid());
|
||||
|
||||
// skip already subscibed
|
||||
if (!self.subscribed[address]) {
|
||||
self.subscribed[address] = true;
|
||||
self.socket.emit('subscribe', address);
|
||||
self.socket.on(address, handlerFor(self, address));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// 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;
|
||||
Insight.prototype.getSubscriptions = function(addresses) {
|
||||
return Object.keys(this.subscribed);
|
||||
}
|
||||
return cb(err, ret);
|
||||
|
||||
Insight.prototype.unsubscribe = function(addresses) {
|
||||
addresses = Array.isArray(addresses) ? addresses : [addresses];
|
||||
var self = this;
|
||||
|
||||
addresses.forEach(function(address) {
|
||||
preconditions.checkArgument(new bitcore.Address(address).isValid());
|
||||
self.socket.removeEventListener(address);
|
||||
delete self.subscribed[address];
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype.unsubscribeAll = function() {
|
||||
this.unsubscribe(this.getSubscriptions());
|
||||
};
|
||||
|
||||
Insight.prototype.broadcast = function(rawtx, cb) {
|
||||
preconditions.checkArgument(rawtx);
|
||||
preconditions.shouldBeFunction(cb);
|
||||
|
||||
this.requestPost('/api/tx/send', {rawtx: rawtx}, function(err, res, body) {
|
||||
if (err || res.statusCode != 200) cb(err || res);
|
||||
cb(null, body.txid);
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype.getTransaction = function(txid, cb) {
|
||||
preconditions.shouldBeFunction(cb);
|
||||
this.request('/api/tx/' + txid, function(err, res, body) {
|
||||
if (err || res.statusCode != 200 || !body) return cb(err || res);
|
||||
cb(null, JSON.parse(body));
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -75,77 +183,53 @@ Insight.prototype.getTransactions = function(addresses, cb) {
|
|||
preconditions.shouldBeFunction(cb);
|
||||
|
||||
var self = this;
|
||||
if (!addresses || !addresses.length) return cb([]);
|
||||
if (!addresses.length) return cb(null, []);
|
||||
|
||||
var txids = [];
|
||||
var txs = [];
|
||||
|
||||
_asyncForEach(addresses, function(addr, callback) {
|
||||
var options = self._getOptions('GET', '/api/addr/' + addr);
|
||||
|
||||
self._request(options, function(err, res) {
|
||||
if (res && res.transactions) {
|
||||
var txids_tmp = res.transactions;
|
||||
for (var i = 0; i < txids_tmp.length; i++) {
|
||||
txids.push(txids_tmp[i]);
|
||||
}
|
||||
}
|
||||
callback();
|
||||
// Iterator: get a list of transaction ids for an address
|
||||
function getTransactionIds(address, next) {
|
||||
self.request('/api/addr/' + address, function(err, res, body) {
|
||||
if (err || res.statusCode != 200 || !body) return next(err || res);
|
||||
next(null, JSON.parse(body).transactions);
|
||||
});
|
||||
}, function() {
|
||||
var uniqueTxids = {};
|
||||
for (var k in txids) {
|
||||
uniqueTxids[txids[k]] = 1;
|
||||
}
|
||||
_asyncForEach(Object.keys(uniqueTxids), function(txid, callback2) {
|
||||
var options = self._getOptions('GET', '/api/tx/' + txid);
|
||||
self._request(options, function(err, res) {
|
||||
txs.push(res);
|
||||
callback2();
|
||||
|
||||
async.map(addresses, getTransactionIds, function then(err, txids) {
|
||||
if (err) return cb(err);
|
||||
|
||||
// txids it's a list of list, let's fix that:
|
||||
var txidsList = txids.reduce(function(a, r) {
|
||||
return r.concat(a);
|
||||
});
|
||||
}, function() {
|
||||
return cb(txs);
|
||||
|
||||
// Remove duplicated txids
|
||||
txidsList = txidsList.filter(function(elem, pos, self) {
|
||||
return self.indexOf(elem) == pos;
|
||||
});
|
||||
|
||||
// Now get the transactions for that list of txIds
|
||||
async.map(txidsList, self.getTransaction.bind(self), function then(err, txs) {
|
||||
if (err) return cb(err);
|
||||
cb(null, txs);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype.getUnspent = function(addresses, cb) {
|
||||
if (!addresses || !addresses.length) return cb(null, []);
|
||||
preconditions.shouldBeArray(addresses);
|
||||
preconditions.shouldBeFunction(cb);
|
||||
|
||||
var all = [];
|
||||
|
||||
var options = this._getOptions('POST', '/api/addrs/utxo', 'addrs=' + addresses.join(','));
|
||||
|
||||
var self = this;
|
||||
this._request(options, function(err, res) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
|
||||
return cb(null, all);
|
||||
this.requestPost('/api/addrs/utxo', {addrs: addresses.join(',')}, function(err, res, body) {
|
||||
if (err || res.statusCode != 200) return cb(err || res);
|
||||
cb(null, body);
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype.sendRawTransaction = function(rawtx, cb) {
|
||||
if (!rawtx) throw new Error('rawtx must be set');
|
||||
Insight.prototype.getActivity = function(addresses, cb) {
|
||||
preconditions.shouldBeArray(addresses);
|
||||
|
||||
var options = this._getOptions('POST', '/api/tx/send', 'rawtx=' + rawtx);
|
||||
this._request(options, function(err, res) {
|
||||
if (err) return cb();
|
||||
this.getTransactions(addresses, function then(err, txs) {
|
||||
if (err) return cb(err);
|
||||
|
||||
return cb(res.txid);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
|
@ -177,112 +261,4 @@ Insight.prototype.checkActivity = function(addresses, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Insight.prototype._requestNode = function(options, callback) {
|
||||
if (options.method === 'POST') {
|
||||
options.headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Length': options.data.length,
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
var req = http.request(options, function(response) {
|
||||
var ret, errTxt, e;
|
||||
if (response.statusCode == 200 || response.statusCode === 304) {
|
||||
response.on('data', function(chunk) {
|
||||
try {
|
||||
ret = JSON.parse(chunk);
|
||||
} catch (e2) {
|
||||
errTxt = 'CRITICAL: Wrong response from insight' + e2;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
errTxt = "INSIGHT ERROR:" + response.statusCode;
|
||||
console.log(errTxt);
|
||||
e = new Error(errTxt);
|
||||
return callback(e);
|
||||
}
|
||||
response.on('end', function() {
|
||||
if (errTxt) {
|
||||
console.log("INSIGHT ERROR:" + errTxt);
|
||||
e = new Error(errTxt);
|
||||
}
|
||||
return callback(e, ret);
|
||||
});
|
||||
response.on('error', function(e) {
|
||||
return callback(e, ret);
|
||||
});
|
||||
});
|
||||
|
||||
if (options.data) {
|
||||
req.write(options.data);
|
||||
}
|
||||
req.end();
|
||||
};
|
||||
|
||||
Insight.prototype._requestBrowser = function(options, callback) {
|
||||
var self = this;
|
||||
var request = new XMLHttpRequest();
|
||||
var url = (options.schema || '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);
|
||||
request.timeout = 5000;
|
||||
request.ontimeout = function() {
|
||||
setTimeout(function() {
|
||||
return self._request(options, callback);
|
||||
}, self.retryDelay);
|
||||
return callback(new Error('Insight request timeout'));
|
||||
};
|
||||
|
||||
|
||||
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: '+request.status;
|
||||
} 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);
|
||||
}
|
||||
return callback(e, ret);
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Insight;
|
||||
|
|
|
@ -193,14 +193,12 @@ Wallet.prototype._getKeyMap = function(txp) {
|
|||
Wallet.prototype._checkSentTx = function(ntxid, cb) {
|
||||
var txp = this.txProposals.get(ntxid);
|
||||
var tx = txp.builder.build();
|
||||
var txid = bitcore.util.formatHashFull(tx.getHash());
|
||||
|
||||
this.blockchain.checkSentTx(tx, function(err, txid) {
|
||||
var ret = false;
|
||||
if (txid) {
|
||||
txp.setSent(txid);
|
||||
ret = txid;
|
||||
}
|
||||
return cb(ret);
|
||||
this.blockchain.getTransaction(txid, function(err, tx) {
|
||||
if (err) return cb(false);
|
||||
txp.setSent(tx.txid);
|
||||
cb(ret);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -806,7 +804,7 @@ Wallet.prototype.sendTx = function(ntxid, cb) {
|
|||
this.log('Raw transaction: ', txHex);
|
||||
|
||||
var self = this;
|
||||
this.blockchain.sendRawTransaction(txHex, function(txid) {
|
||||
this.blockchain.broadcast(txHex, function(err, txid) {
|
||||
self.log('BITCOIND txid:', txid);
|
||||
if (txid) {
|
||||
self.txProposals.get(ntxid).setSent(txid);
|
||||
|
@ -1724,7 +1722,7 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
|
|||
// Optimize window to minimize the derivations.
|
||||
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
|
||||
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, copayerIndex);
|
||||
self.blockchain.checkActivity(addresses, function(err, actives) {
|
||||
self.blockchain.getActivity(addresses, function(err, actives) {
|
||||
if (err) throw err;
|
||||
|
||||
// Check for new activities in the newlly scanned addresses
|
||||
|
@ -1752,6 +1750,7 @@ Wallet.prototype.close = function() {
|
|||
this.log('## CLOSING');
|
||||
this.lock.release();
|
||||
this.network.cleanUp();
|
||||
this.blockchain.destroy();
|
||||
};
|
||||
|
||||
Wallet.prototype.getNetwork = function() {
|
||||
|
|
|
@ -35,6 +35,7 @@ Network.prototype.cleanUp = function() {
|
|||
this.criticalErr = '';
|
||||
this.removeAllListeners();
|
||||
if (this.socket) {
|
||||
this.socket.removeAllListeners();
|
||||
this.socket.disconnect();
|
||||
this.socket = null;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
var bitcore = require('bitcore');
|
||||
|
||||
angular.module('copayApp.services')
|
||||
.factory('controllerUtils', function($rootScope, $sce, $location, notification, $timeout, Socket, video, uriHandler) {
|
||||
.factory('controllerUtils', function($rootScope, $sce, $location, notification, $timeout, video, uriHandler) {
|
||||
var root = {};
|
||||
root.getVideoMutedStatus = function(copayer) {
|
||||
if (!$rootScope.videoInfo) return;
|
||||
|
@ -24,8 +24,6 @@ angular.module('copayApp.services')
|
|||
if ($rootScope.wallet)
|
||||
$rootScope.wallet.close();
|
||||
|
||||
Socket.removeAllListeners();
|
||||
|
||||
$rootScope.wallet = null;
|
||||
delete $rootScope['wallet'];
|
||||
|
||||
|
@ -68,7 +66,7 @@ angular.module('copayApp.services')
|
|||
uriHandler.register();
|
||||
$rootScope.unitName = config.unitName;
|
||||
$rootScope.txAlertCount = 0;
|
||||
$rootScope.insightError = 0;
|
||||
$rootScope.reconnecting = false;
|
||||
$rootScope.isCollapsed = true;
|
||||
$rootScope.$watch('txAlertCount', function(txAlertCount) {
|
||||
if (txAlertCount && txAlertCount > 0) {
|
||||
|
@ -100,11 +98,9 @@ angular.module('copayApp.services')
|
|||
|
||||
|
||||
root.startNetwork = function(w, $scope) {
|
||||
Socket.removeAllListeners();
|
||||
|
||||
root.setupRootVariables();
|
||||
root.installStartupHandlers(w, $scope);
|
||||
root.setSocketHandlers();
|
||||
root.updateGlobalAddresses();
|
||||
|
||||
var handlePeerVideo = function(err, peerID, url) {
|
||||
if (err) {
|
||||
|
@ -125,6 +121,8 @@ angular.module('copayApp.services')
|
|||
});
|
||||
w.on('ready', function(myPeerID) {
|
||||
$rootScope.wallet = w;
|
||||
root.setConnectionListeners($rootScope.wallet);
|
||||
|
||||
if ($rootScope.pendingPayment) {
|
||||
$location.path('send');
|
||||
} else {
|
||||
|
@ -135,7 +133,7 @@ angular.module('copayApp.services')
|
|||
});
|
||||
|
||||
w.on('publicKeyRingUpdated', function(dontDigest) {
|
||||
root.setSocketHandlers();
|
||||
root.updateGlobalAddresses();
|
||||
if (!dontDigest) {
|
||||
$rootScope.$digest();
|
||||
}
|
||||
|
@ -199,13 +197,7 @@ angular.module('copayApp.services')
|
|||
$rootScope.updatingBalance = true;
|
||||
|
||||
w.getBalance(function(err, balanceSat, balanceByAddrSat, safeBalanceSat) {
|
||||
if (err) {
|
||||
console.error('Error: ' + err.message); //TODO
|
||||
root._setCommError();
|
||||
return null;
|
||||
} else {
|
||||
root._clearCommError();
|
||||
}
|
||||
if (err) throw err;
|
||||
|
||||
var satToUnit = 1 / config.unitToSatoshi;
|
||||
var COIN = bitcore.util.COIN;
|
||||
|
@ -298,81 +290,53 @@ angular.module('copayApp.services')
|
|||
});
|
||||
}
|
||||
|
||||
var connectionLost = false;
|
||||
$rootScope.$watch('insightError', function(status) {
|
||||
if (!status) return;
|
||||
|
||||
// Reconnected
|
||||
if (status === -1) {
|
||||
if (!connectionLost) return; // Skip on first reconnect
|
||||
connectionLost = false;
|
||||
root.setConnectionListeners = function(wallet) {
|
||||
wallet.blockchain.on('connect', function(attempts) {
|
||||
if (attempts == 0) return;
|
||||
notification.success('Networking restored', 'Connection to Insight re-established');
|
||||
return;
|
||||
}
|
||||
|
||||
// Retry
|
||||
if (status == 1) return; // Skip the first try
|
||||
connectionLost = true;
|
||||
notification.error('Networking problem', 'Connection to Insight lost, reconnecting (attempt number ' + (status - 1) + ')');
|
||||
$rootScope.reconnecting = false;
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
});
|
||||
|
||||
root._setCommError = function(e) {
|
||||
if ($rootScope.insightError < 0)
|
||||
$rootScope.insightError = 0;
|
||||
$rootScope.insightError++;
|
||||
};
|
||||
wallet.blockchain.on('disconnect', function() {
|
||||
notification.error('Networking problem', 'Connection to Insight lost, trying to reconnect...');
|
||||
$rootScope.reconnecting = true;
|
||||
$rootScope.$digest();
|
||||
});
|
||||
|
||||
root._clearCommError = function(e) {
|
||||
if ($rootScope.insightError > 0)
|
||||
$rootScope.insightError = -1;
|
||||
else
|
||||
$rootScope.insightError = 0;
|
||||
};
|
||||
wallet.blockchain.on('tx', function(tx) {
|
||||
notification.funds('Funds received!', tx.address);
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
});
|
||||
|
||||
root.setSocketHandlers = function() {
|
||||
root.updateAddressList();
|
||||
if (!Socket.sysEventsSet) {
|
||||
Socket.sysOn('error', root._setCommError);
|
||||
Socket.sysOn('reconnect_error', root._setCommError);
|
||||
Socket.sysOn('reconnect_failed', root._setCommError);
|
||||
Socket.sysOn('connect', root._clearCommError);
|
||||
Socket.sysOn('reconnect', root._clearCommError);
|
||||
Socket.sysEventsSet = true;
|
||||
if (!$rootScope.wallet.spendUnconfirmed) {
|
||||
wallet.blockchain.on('block', function(block) {
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
root.updateGlobalAddresses = function() {
|
||||
if (!$rootScope.wallet) return;
|
||||
|
||||
var currentAddrs = Socket.getListeners();
|
||||
root.updateAddressList();
|
||||
var currentAddrs = $rootScope.wallet.blockchain.getSubscriptions();
|
||||
var allAddrs = $rootScope.addrInfos;
|
||||
|
||||
var newAddrs = [];
|
||||
for (var i in allAddrs) {
|
||||
var a = allAddrs[i];
|
||||
if (!currentAddrs[a.addressStr])
|
||||
newAddrs.push(a);
|
||||
if (!currentAddrs[a.addressStr] && !a.isChange)
|
||||
newAddrs.push(a.addressStr);
|
||||
}
|
||||
for (var i = 0; i < newAddrs.length; i++) {
|
||||
Socket.emit('subscribe', newAddrs[i].addressStr);
|
||||
}
|
||||
newAddrs.forEach(function(a) {
|
||||
Socket.on(a.addressStr, function(txid) {
|
||||
|
||||
if (!a.isChange)
|
||||
notification.funds('Funds received!', a.addressStr);
|
||||
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if (!$rootScope.wallet.spendUnconfirmed && !Socket.isListeningBlocks()) {
|
||||
Socket.emit('subscribe', 'inv');
|
||||
Socket.on('block', function(block) {
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
});
|
||||
}
|
||||
$rootScope.wallet.blockchain.subscribe(newAddrs);
|
||||
};
|
||||
return root;
|
||||
});
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('Socket',
|
||||
function($rootScope) {
|
||||
var listeners = [];
|
||||
var url = (config.socket.schema || 'http') + '://' + config.socket.host + ':' + config.socket.port;
|
||||
var opts = {
|
||||
'reconnection': true,
|
||||
'reconnectionDelay': config.socket.reconnectDelay || 500,
|
||||
'secure': config.socket.schema === 'https' ? true : false,
|
||||
};
|
||||
|
||||
var socket = io(url, opts);
|
||||
|
||||
return {
|
||||
on: function(event, callback) {
|
||||
var wrappedCallback = function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
callback.apply(socket, args);
|
||||
});
|
||||
};
|
||||
socket.on(event, wrappedCallback);
|
||||
if (event !== 'connect') {
|
||||
listeners.push({
|
||||
event: event,
|
||||
fn: wrappedCallback
|
||||
});
|
||||
}
|
||||
},
|
||||
sysOn: function(event, callback) {
|
||||
var wrappedCallback = function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
callback.apply(socket, args);
|
||||
});
|
||||
};
|
||||
socket.io.on(event, wrappedCallback);
|
||||
},
|
||||
getListeners: function() {
|
||||
var ret = {};
|
||||
|
||||
var addrList = listeners
|
||||
.filter(function(i) {
|
||||
return i.event != 'block';
|
||||
})
|
||||
.map(function(i) {
|
||||
return i.event;
|
||||
});
|
||||
|
||||
for (var i in addrList) {
|
||||
ret[addrList[i]] = 1;
|
||||
}
|
||||
return ret;
|
||||
},
|
||||
isListeningBlocks: function() {
|
||||
return listeners.filter(function(i) {
|
||||
return i.event == 'block';
|
||||
}).length > 0;
|
||||
},
|
||||
emit: function(event, data, callback) {
|
||||
socket.emit(event, data, function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
if (callback) {
|
||||
callback.apply(socket, args);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
removeAllListeners: function() {
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
var details = listeners[i];
|
||||
socket.removeAllListeners(details.event);
|
||||
}
|
||||
|
||||
listeners = [];
|
||||
}
|
||||
};
|
||||
});
|
|
@ -54,6 +54,7 @@ module.exports = function(config) {
|
|||
'test/lib/chai-should.js',
|
||||
'test/lib/chai-expect.js',
|
||||
'test/mocks/FakeWallet.js',
|
||||
'test/mocks/FakeBlockchainSocket.js',
|
||||
'test/mocks/FakePayProServer.js',
|
||||
|
||||
'test/mocha.conf.js',
|
||||
|
|
|
@ -11,10 +11,12 @@
|
|||
},
|
||||
"version": "0.4.7",
|
||||
"dependencies": {
|
||||
"browser-request": "^0.3.2",
|
||||
"mocha": "^1.18.2",
|
||||
"mocha-lcov-reporter": "0.0.1",
|
||||
"optimist": "^0.6.1",
|
||||
"preconditions": "^1.0.7",
|
||||
"request": "^2.40.0",
|
||||
"sinon": "1.9.1"
|
||||
},
|
||||
"scripts": {
|
||||
|
|
|
@ -6,8 +6,12 @@ function FakeBlockchain(opts) {
|
|||
opts = opts || {};
|
||||
}
|
||||
|
||||
FakeBlockchain.prototype.getTransaction = function(txid, cb) {
|
||||
return cb(null, {txid: txid});
|
||||
};
|
||||
|
||||
FakeBlockchain.prototype.getTransactions = function(addresses, cb) {
|
||||
return cb([]);
|
||||
return cb(null, []);
|
||||
};
|
||||
|
||||
|
||||
|
@ -15,6 +19,9 @@ FakeBlockchain.prototype.fixUnspent = function(u) {
|
|||
this.u = u;
|
||||
};
|
||||
|
||||
FakeBlockchain.prototype.destroy = function() {
|
||||
};
|
||||
|
||||
FakeBlockchain.prototype.getUnspent = function(addresses, cb) {
|
||||
return cb(null, this.u || [{
|
||||
'address': 'mji7zocy8QzYywQakwWf99w9bCT6orY1C1',
|
||||
|
@ -41,9 +48,9 @@ FakeBlockchain.prototype.getUnspent2 = function(addresses, cb) {
|
|||
}]);
|
||||
};
|
||||
|
||||
FakeBlockchain.prototype.sendRawTransaction = function(rawtx, cb) {
|
||||
FakeBlockchain.prototype.broadcast = function(rawtx, cb) {
|
||||
var txid = '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa';
|
||||
return cb(txid);
|
||||
return cb(null, txid);
|
||||
};
|
||||
|
||||
FakeBlockchain.prototype.checkSentTx = function (tx, cb) {
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
'use strict';
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var FakeSocket = function (url, opts) {
|
||||
var self = this;
|
||||
|
||||
self.connected = false;
|
||||
setTimeout(function() {
|
||||
self.connected = true;
|
||||
self.emit('connect');
|
||||
}, 0);
|
||||
}
|
||||
|
||||
var inherits = function(ctor, superCtor) {
|
||||
ctor.super_ = superCtor;
|
||||
ctor.prototype = Object.create(superCtor.prototype, {
|
||||
constructor: {
|
||||
value: ctor,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
inherits(FakeSocket, EventEmitter);
|
||||
|
||||
FakeSocket.prototype.removeEventListener = function() {
|
||||
return;
|
||||
}
|
||||
|
||||
FakeSocket.prototype.destroy = function() {
|
||||
this.connected = false;
|
||||
this.removeAllListeners();
|
||||
};
|
||||
|
||||
module.exports = FakeSocket;
|
|
@ -36,6 +36,10 @@ var FakeWallet = function() {
|
|||
return true;
|
||||
}
|
||||
};
|
||||
this.blockchain = {
|
||||
getSubscriptions: function(){ return []; },
|
||||
subscribe: function(){}
|
||||
};
|
||||
|
||||
this.privateKey = new FakePrivateKey();
|
||||
};
|
||||
|
|
|
@ -752,7 +752,7 @@ describe('Wallet model', function() {
|
|||
var utxo = createUTXO(w);
|
||||
w.blockchain.fixUnspent(utxo);
|
||||
w.createTx(toAddress, amountSatStr, null, function(ntxid) {
|
||||
sinon.stub(w.blockchain, 'sendRawTransaction').yields(undefined);
|
||||
sinon.stub(w.blockchain, 'broadcast').yields({statusCode: 303});
|
||||
var spyCheckSentTx = sinon.spy(w, '_checkSentTx');
|
||||
w.sendTx(ntxid, function () {});
|
||||
chai.expect(spyCheckSentTx.calledOnce).to.be.true;
|
||||
|
@ -823,7 +823,7 @@ describe('Wallet model', function() {
|
|||
});
|
||||
|
||||
var mockFakeActivity = function(f) {
|
||||
w.blockchain.checkActivity = function(addresses, cb) {
|
||||
w.blockchain.getActivity = function(addresses, cb) {
|
||||
var activity = new Array(addresses.length);
|
||||
for (var i = 0; i < addresses.length; i++) {
|
||||
var a1 = ADDRESSES_CHANGE.indexOf(addresses[i]);
|
||||
|
|
|
@ -91,7 +91,8 @@ describe('WalletFactory model', function() {
|
|||
},
|
||||
blockchain: {
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
port: 80,
|
||||
schema: 'https'
|
||||
},
|
||||
networkName: 'testnet',
|
||||
passphrase: 'test',
|
||||
|
|
|
@ -4,23 +4,13 @@ var chai = chai || require('chai');
|
|||
var should = chai.should();
|
||||
var sinon = require('sinon');
|
||||
var bitcore = bitcore || require('bitcore');
|
||||
try {
|
||||
var copay = require('copay'); //browser
|
||||
} catch (e) {
|
||||
var copay = require('../copay'); //node
|
||||
}
|
||||
var Buffer = bitcore.Buffer;
|
||||
var Insight = copay.Insight || require('../js/models/blockchain/Insight');
|
||||
var FakeSocket = require('./mocks/FakeBlockchainSocket');
|
||||
|
||||
var ID = '933bf321393459b7';
|
||||
var copayers = [
|
||||
'tpubD6NzVbkrYhZ4WeSS3M5axcR1EMYPeerA8GozBmYVLKSjriMXhse1C4kiLJMvaaDKRBaP7iSJJo5wMBh3JSYcMz1vrwXKKnAgtt4V4pfSEcq',
|
||||
'tpubD6NzVbkrYhZ4XPjvz7c2544jPBY2WKCJVCETEE68ykBLMcE7J3GVDGvmPEdzvTWWXxQsE25rm7f4J1ZNxzWhuR7iEhX1m4dS9HrYbg1ezUP',
|
||||
'tpubD6NzVbkrYhZ4YTRVfKf1tHgydyvoEWdsBRVCG6odCZdpY7nPZWxA26sLPtyHkquzHmgdAH8HpftobnJJUvcbi7MyHVqXmPLJCW9KCS6rkw8',
|
||||
'tpubD6NzVbkrYhZ4XDY86vJmcCUuUvbqujhM633a5ih8b6ngm1AsskGz3orGkjvbzcJNQUJSK9jqggRwSohq3LAigwWZ8uzGNrGZqCwaE95foAj',
|
||||
'tpubD6NzVbkrYhZ4XGHkbBTx4kU5w7RDb9hWXyK9tuEaYrY9SJUWBCUxrcMFkqBa6qAv11FNdVJ4MFxKdnKnjoBWDY6SwBtmP83gjFHTV5zz4RW'
|
||||
];
|
||||
var addresses = [
|
||||
var copay = copay || require('../copay');
|
||||
var Buffer = bitcore.Buffer;
|
||||
var Insight = copay.Insight;
|
||||
|
||||
var ADDRESSES = [
|
||||
'2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM',
|
||||
'2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb', // 41btc
|
||||
'2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x', // 50btc
|
||||
|
@ -31,7 +21,7 @@ var addresses = [
|
|||
'2N9EdxU3co5XKTyj3yhFBeU3qw3EM1rrgzE'
|
||||
];
|
||||
|
||||
var unspent = [{
|
||||
var UNSPENT = [{
|
||||
address: "2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb",
|
||||
txid: "d5597c6cf7f72507af63a4d5a2f9f84edb45fb42452cc8c514435b7a93158915",
|
||||
vout: 0,
|
||||
|
@ -49,104 +39,283 @@ var unspent = [{
|
|||
confirmations: 6728
|
||||
}];
|
||||
|
||||
var rawtx = '01000000010c2a03ed71ee18148e8c99c5ff66d5ffb75e5def46cdea2acc6f30103f33bfb5010000006a47304402207f960aeefdfad270dd77d1acca7af17d3a2e47e2059034ff5d6305cf63635e1d02202f061ee196cc4459cdecae6559beac696a9ecde9a17520849f319fa2a627e64f012103870465f9b4efb90b5d186a7a5eacd7081e601020dacd68d942e5918a56ed0bfcffffffff02a086010000000000ad532102a9495c64323cd8c3354dbf0b3400d830ee680da493acbccc3c2c356d1b20fabf21028233cf8bc6112ae2c36468bd447732c5586b52e1ba3284a2319cadfac6367f99210279fd856e5ed13ab6807e85ed7c0cd6f80613be042240fd731c43f5aba3dcae9821021380858a67a4f99eda52ce2d72c300911f9d3eb9d7a45102a2133f14f7b2dc14210215739b613ce42106a11ce433342c13c610bf68a1bc934f607ad7aeb4178e04cf55ae2044d200000000001976a9146917322f0010aaf7ec136a34b476dfc5eb7a331288ac00000000';
|
||||
|
||||
var FAKE_OPTS = {
|
||||
host: 'something.com',
|
||||
port: 123,
|
||||
schema: 'http'
|
||||
}
|
||||
|
||||
describe('Insight model', function() {
|
||||
|
||||
before(function() {
|
||||
sinon.stub(Insight.prototype, "getSocket", function() {
|
||||
return new FakeSocket();
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
Insight.prototype.getSocket.restore();
|
||||
});
|
||||
|
||||
it('should create an instance', function() {
|
||||
var i = new Insight();
|
||||
should.exist(i);
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
should.exist(blockchain);
|
||||
blockchain.url.should.be.equal('http://something.com:123');
|
||||
});
|
||||
|
||||
// Tests for Node
|
||||
if (typeof process !== 'undefined' && process.version) {
|
||||
it('should return array of unspent output', function(done) {
|
||||
var i = new Insight();
|
||||
|
||||
|
||||
var http = require('http');
|
||||
var request = {
|
||||
statusCode: 200
|
||||
};
|
||||
|
||||
request.on = function(event, cb) {
|
||||
if (event === 'error') return;
|
||||
if (event === 'data') return cb(JSON.stringify(unspent));
|
||||
return cb();
|
||||
};
|
||||
|
||||
var req = {};
|
||||
req.write = function() {};
|
||||
req.end = function() {};
|
||||
|
||||
|
||||
sinon
|
||||
.stub(http, 'request')
|
||||
.returns(req)
|
||||
.yields(request);
|
||||
|
||||
i.getUnspent(['2MuD5LnZSViZZYwZbpVsagwrH8WWvCztdmV', '2NBSLoMvsHsf2Uv3LA17zV4beH6Gze6RovA'], function(e, ret) {
|
||||
should.not.exist(e);
|
||||
ret.should.deep.equal(unspent);
|
||||
http.request.restore();
|
||||
it('should subscribe to inventory', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var emitSpy = sinon.spy(blockchain.socket, 'emit');
|
||||
blockchain.on('connect', function() {
|
||||
emitSpy.calledWith('subscribe', 'inv');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return txid', function(done) {
|
||||
var i = new Insight();
|
||||
|
||||
var http = require('http');
|
||||
var request = {
|
||||
statusCode: 200
|
||||
};
|
||||
|
||||
request.on = function(event, cb) {
|
||||
if (event === 'error') return;
|
||||
if (event === 'data') return cb('{ "txid": "1234" }');
|
||||
return cb();
|
||||
};
|
||||
|
||||
var req = {};
|
||||
req.write = function() {};
|
||||
req.end = function() {};
|
||||
|
||||
sinon
|
||||
.stub(http, 'request')
|
||||
.returns(req)
|
||||
.yields(request);
|
||||
|
||||
i.sendRawTransaction(rawtx, function(a) {
|
||||
should.exist(a);
|
||||
a.should.equal('1234');
|
||||
http.request.restore();
|
||||
it('should be able to destroy the instance', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.status.should.be.equal('disconnected');
|
||||
blockchain.on('connect', function() {
|
||||
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.getSubscriptions().length.should.equal(1);
|
||||
blockchain.destroy();
|
||||
blockchain.getSubscriptions().length.should.equal(0);
|
||||
blockchain.status.should.be.equal('destroyed');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should subscribe to an address', function() {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var emitSpy = sinon.spy(blockchain.socket, 'emit');
|
||||
|
||||
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.getSubscriptions().length.should.equal(1);
|
||||
emitSpy.calledWith('subscribe', 'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
});
|
||||
|
||||
it('should subscribe to an address once', function() {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
|
||||
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.getSubscriptions().length.should.equal(1);
|
||||
});
|
||||
|
||||
it('should subscribe to a list of addresses', function() {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var emitSpy = sinon.spy(blockchain.socket, 'emit');
|
||||
|
||||
blockchain.subscribe([
|
||||
'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM',
|
||||
'2NBBHBjB5sd7HFqKtout1L7d6dPhwJgP2j8'
|
||||
]);
|
||||
blockchain.getSubscriptions().length.should.equal(2);
|
||||
emitSpy.calledWith('subscribe', 'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
emitSpy.calledWith('subscribe', '2NBBHBjB5sd7HFqKtout1L7d6dPhwJgP2j8');
|
||||
});
|
||||
|
||||
it('should unsubscribe to an address', function() {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.getSubscriptions().length.should.equal(1);
|
||||
blockchain.unsubscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.getSubscriptions().length.should.equal(0);
|
||||
});
|
||||
|
||||
it('should unsubscribe to all addresses', function() {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.subscribe('2NBBHBjB5sd7HFqKtout1L7d6dPhwJgP2j8');
|
||||
blockchain.getSubscriptions().length.should.equal(2);
|
||||
|
||||
blockchain.unsubscribeAll('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||
blockchain.getSubscriptions().length.should.equal(0);
|
||||
});
|
||||
|
||||
it('should broadcast a raw transaction', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var rawtx = '01000000010c2a03ed71ee18148e8c99c5ff66d5ffb75e5def46cdea2acc6f30103f33bfb5010000006a47304402207f960aeefdfad270dd77d1acca7af17d3a2e47e2059034ff5d6305cf63635e1d02202f061ee196cc4459cdecae6559beac696a9ecde9a17520849f319fa2a627e64f012103870465f9b4efb90b5d186a7a5eacd7081e601020dacd68d942e5918a56ed0bfcffffffff02a086010000000000ad532102a9495c64323cd8c3354dbf0b3400d830ee680da493acbccc3c2c356d1b20fabf21028233cf8bc6112ae2c36468bd447732c5586b52e1ba3284a2319cadfac6367f99210279fd856e5ed13ab6807e85ed7c0cd6f80613be042240fd731c43f5aba3dcae9821021380858a67a4f99eda52ce2d72c300911f9d3eb9d7a45102a2133f14f7b2dc14210215739b613ce42106a11ce433342c13c610bf68a1bc934f607ad7aeb4178e04cf55ae2044d200000000001976a9146917322f0010aaf7ec136a34b476dfc5eb7a331288ac00000000';
|
||||
|
||||
sinon.stub(blockchain, "requestPost", function(url, data, cb) {
|
||||
url.should.be.equal('/api/tx/send');
|
||||
var res = {statusCode: 200};
|
||||
var body = {txid: 1234};
|
||||
setTimeout(function() {
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
blockchain.broadcast(rawtx, function(err, id) {
|
||||
id.should.be.equal(1234);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTransaction', function() {
|
||||
it('should get a transaction by id', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var txid = '123321';
|
||||
var tx = {txid: txid, more: 'something'};
|
||||
|
||||
sinon.stub(blockchain, "request", function(url, cb) {
|
||||
url.should.be.equal('/api/tx/' + txid);
|
||||
var res = {statusCode: 200};
|
||||
var body = JSON.stringify(tx);
|
||||
setTimeout(function() {
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
blockchain.getTransaction(txid, function(err, t) {
|
||||
chai.expect(err).to.be.null;
|
||||
t.should.be.an('object');
|
||||
t.txid.should.be.equal(tx.txid);
|
||||
t.more.should.be.equal(tx.more);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle a 404 error code', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var txid = '123321';
|
||||
|
||||
sinon.stub(blockchain, "request", function(url, cb) {
|
||||
url.should.be.equal('/api/tx/' + txid);
|
||||
var res = {statusCode: 404};
|
||||
var body = '';
|
||||
setTimeout(function() {
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
blockchain.getTransaction(txid, function(err, t) {
|
||||
chai.expect(t).to.be.undefined;
|
||||
chai.expect(err).not.be.null;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle a null response', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var txid = '123321';
|
||||
|
||||
sinon.stub(blockchain, "request", function(url, cb) {
|
||||
url.should.be.equal('/api/tx/' + txid);
|
||||
var res = {statusCode: 200};
|
||||
var body = null;
|
||||
setTimeout(function() {
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
blockchain.getTransaction(txid, function(err, t) {
|
||||
chai.expect(t).to.be.undefined;
|
||||
chai.expect(err).not.be.null;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle an empty response', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
var txid = '123321';
|
||||
|
||||
sinon.stub(blockchain, "request", function(url, cb) {
|
||||
url.should.be.equal('/api/tx/' + txid);
|
||||
var res = {statusCode: 200};
|
||||
var body = null;
|
||||
setTimeout(function() {
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
blockchain.getTransaction(txid, function(err, t) {
|
||||
chai.expect(t).to.be.undefined;
|
||||
chai.expect(err).not.be.null;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a set of transaction by addresses', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
|
||||
sinon.stub(blockchain, "request", function(url, cb) {
|
||||
var res = {statusCode: 200};
|
||||
|
||||
if (url == '/api/addr/2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM') {
|
||||
return setTimeout(function() {
|
||||
var body = JSON.stringify({transactions: [1, 2]});
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
if (url == '/api/addr/2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb') {
|
||||
return setTimeout(function() {
|
||||
var body = JSON.stringify({transactions: [3]});
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
setTimeout(function() {
|
||||
var body = JSON.stringify({txid: '123123'});
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
it('#checkActivity for innactive addreses', function(done) {
|
||||
var w = new Insight();
|
||||
w.getTransactions = function(addresses, cb) {
|
||||
cb([]);
|
||||
};
|
||||
var addresses = ['2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM', '2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb'];
|
||||
blockchain.getTransactions(addresses, function(err, txs) {
|
||||
chai.expect(err).to.be.null;
|
||||
txs.length.should.be.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
w.checkActivity(addresses, function(err, actives) {
|
||||
actives.length.should.equal(addresses.length);
|
||||
it('should get a list of unspent output', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
|
||||
sinon.stub(blockchain, "requestPost", function(url, data, cb) {
|
||||
url.should.be.equal('/api/addrs/utxo');
|
||||
data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb,2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x');
|
||||
setTimeout(function() {
|
||||
var res = {statusCode: 200};
|
||||
var body = UNSPENT;
|
||||
cb(null, res, body);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
blockchain.getUnspent(ADDRESSES.slice(0, 3), function(err, unspent) {
|
||||
chai.expect(err).to.be.null;
|
||||
unspent.length.should.be.equal(2);
|
||||
unspent[0].address.should.be.equal('2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb');
|
||||
unspent[1].address.should.be.equal('2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getActivity', function() {
|
||||
it('should get activity for an innactive address', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
|
||||
sinon.stub(blockchain, "getTransactions", function(addresses, cb) {
|
||||
cb(null, []);
|
||||
});
|
||||
|
||||
blockchain.getActivity(ADDRESSES, function(err, actives) {
|
||||
chai.expect(err).to.be.null;
|
||||
actives.length.should.equal(ADDRESSES.length);
|
||||
actives.filter(function(i) {
|
||||
return i
|
||||
}).length.should.equal(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('#checkActivity for active addreses', function(done) {
|
||||
var w = new Insight();
|
||||
w.getTransactions = function(addresses, cb) {
|
||||
cb([{
|
||||
|
||||
it('should get activity for active addresses', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
|
||||
sinon.stub(blockchain, "getTransactions", function(addresses, cb) {
|
||||
cb(null, [{
|
||||
vin: [{
|
||||
addr: '2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM'
|
||||
}],
|
||||
|
@ -169,62 +338,71 @@ describe('Insight model', function() {
|
|||
}
|
||||
}]
|
||||
}]);
|
||||
};
|
||||
});
|
||||
|
||||
w.checkActivity(addresses, function(err, actives) {
|
||||
actives.length.should.equal(addresses.length);
|
||||
blockchain.getActivity(ADDRESSES, function(err, actives) {
|
||||
chai.expect(err).to.be.null;
|
||||
actives.length.should.equal(ADDRESSES.length);
|
||||
actives.filter(function(i) {
|
||||
return i
|
||||
}).length.should.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Events', function() {
|
||||
it('should emmit event on a new block', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.on('connect', function() {
|
||||
blockchain.socket.emit('block', '12312312');
|
||||
});
|
||||
|
||||
it('should handle getTransaction null response', function(done) {
|
||||
var w = new Insight();
|
||||
w._request = sinon.stub().yields();
|
||||
w.getTransactions(['asdasd'], function(ret) {
|
||||
ret.length.should.equal(0);
|
||||
blockchain.on('block', function(blockid) {
|
||||
blockid.should.be.equal('12312312');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should emmit event on a transaction for subscried addresses', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.subscribe('2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY');
|
||||
blockchain.on('connect', function() {
|
||||
blockchain.socket.emit('2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY', '1123');
|
||||
});
|
||||
|
||||
|
||||
it('should handle getTransaction empty response', function(done) {
|
||||
var w = new Insight();
|
||||
w._request = sinon.stub().yields([]);
|
||||
w.getTransactions(['asdasd'], function(ret) {
|
||||
ret.length.should.equal(0);
|
||||
blockchain.on('tx', function(ev) {
|
||||
ev.address.should.be.equal('2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY');
|
||||
ev.txid.should.be.equal('1123');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe("#checkSentTx", function() {
|
||||
it('should return true if Tx is found', function(done) {
|
||||
var w = new Insight();
|
||||
w._request = sinon.stub().yields(null, {
|
||||
txid: "414142",
|
||||
it('should\'t emmit event on a transaction for non subscribed addresses', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.on('connect', function() {
|
||||
blockchain.socket.emit('2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY', '1123');
|
||||
setTimeout(function() { done(); }, 20);
|
||||
});
|
||||
var tx = function() {};
|
||||
tx.prototype.getHash = function(){return new Buffer('BAA')};
|
||||
w.checkSentTx(new tx(), function(err, ret) {
|
||||
should.not.exist(err);
|
||||
ret.should.equal('414142');
|
||||
|
||||
blockchain.on('tx', function(ev) {
|
||||
throw Error('should not call this event!');
|
||||
});
|
||||
});
|
||||
|
||||
it('should emmit event on connection', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.on('connect', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should return false if Tx is not found', function(done) {
|
||||
var w = new Insight();
|
||||
w._request = sinon.stub().yields(null, {
|
||||
txid: "414142",
|
||||
|
||||
it('should emmit event on disconnection', function(done) {
|
||||
var blockchain = new Insight(FAKE_OPTS);
|
||||
blockchain.on('connect', function() {
|
||||
blockchain.socket.emit('connect_error');
|
||||
});
|
||||
var tx = function() {};
|
||||
tx.prototype.getHash = function(){return new Buffer('ABC')};
|
||||
w.checkSentTx(new tx(), function(err, ret) {
|
||||
should.not.exist(err);
|
||||
ret.should.equal(false);
|
||||
blockchain.on('disconnect', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -18,6 +18,7 @@ describe('Network / Async', function() {
|
|||
fakeSocket.emit = function() {};
|
||||
fakeSocket.on = function() {};
|
||||
fakeSocket.disconnect = function() {};
|
||||
fakeSocket.removeAllListeners = function() {};
|
||||
n.createSocket = function() {
|
||||
return fakeSocket;
|
||||
};
|
||||
|
|
|
@ -18,56 +18,6 @@ describe('Check config', function() {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
describe("Unit: Socket Service", function() {
|
||||
beforeEach(angular.mock.module('copayApp.services'));
|
||||
|
||||
it('should contain a Socket service', inject(function(Socket) {
|
||||
expect(Socket).not.to.equal(null);
|
||||
}));
|
||||
|
||||
|
||||
it('Socket should support #on', inject(function(Socket) {
|
||||
expect(Socket.on).to.be.a('function');
|
||||
}));
|
||||
|
||||
|
||||
it('Socket should support #sysOn', inject(function(Socket) {
|
||||
expect(Socket.sysOn).to.be.a('function');
|
||||
}));
|
||||
|
||||
|
||||
it('Socket should add handlers with #on', inject(function(Socket) {
|
||||
Socket.on('a', function() {});
|
||||
Socket.on('b', function() {});
|
||||
Socket.sysOn('c', function() {});
|
||||
var ret = Socket.getListeners();
|
||||
expect(ret.a).to.be.equal(1);
|
||||
expect(ret.b).to.be.equal(1);
|
||||
expect(Object.keys(ret)).to.have.length(2);
|
||||
}));
|
||||
|
||||
it('Socket should support block event', inject(function(Socket) {
|
||||
expect(Socket.isListeningBlocks()).to.be.false;
|
||||
Socket.on('block', function() {});
|
||||
expect(Socket.isListeningBlocks()).to.be.true;
|
||||
Socket.removeAllListeners();
|
||||
expect(Socket.isListeningBlocks()).to.be.false;
|
||||
}));
|
||||
|
||||
it('Socket should support #removeAllListeners', inject(function(Socket) {
|
||||
Socket.on('a', function() {});
|
||||
Socket.on('b', function() {});
|
||||
Socket.sysOn('c', function() {});
|
||||
var ret = Socket.getListeners();
|
||||
expect(Object.keys(ret)).to.have.length(2);
|
||||
Socket.removeAllListeners();
|
||||
ret = Socket.getListeners();
|
||||
expect(Object.keys(ret)).to.have.length(0);
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe("Unit: Walletfactory Service", function() {
|
||||
beforeEach(angular.mock.module('copayApp.services'));
|
||||
it('should contain a walletFactory service', inject(function(walletFactory) {
|
||||
|
@ -112,39 +62,22 @@ describe("Unit: controllerUtils", function() {
|
|||
expect($rootScope.unitName).to.be.equal('bits');
|
||||
});
|
||||
}));
|
||||
describe("Unit: controllerUtils #setSocketHandlers", function() {
|
||||
describe("Unit: controllerUtils #updateGlobalAddresses", function() {
|
||||
|
||||
|
||||
it(' should call updateAddressList ', inject(function(controllerUtils, $rootScope) {
|
||||
$rootScope.wallet = new FakeWallet();
|
||||
var spy = sinon.spy(controllerUtils, 'updateAddressList');
|
||||
controllerUtils.setSocketHandlers();
|
||||
controllerUtils.updateGlobalAddresses();
|
||||
sinon.assert.callCount(spy, 1);
|
||||
}));
|
||||
|
||||
it('should update addresses', inject(function(controllerUtils, $rootScope) {
|
||||
$rootScope.wallet = new FakeWallet();
|
||||
var Waddr = Object.keys($rootScope.wallet.balanceByAddr)[0];
|
||||
controllerUtils.setSocketHandlers();
|
||||
controllerUtils.updateGlobalAddresses();
|
||||
expect($rootScope.addrInfos[0].address).to.be.equal(Waddr);;
|
||||
}));
|
||||
|
||||
it('should set System Event Handlers', inject(function(controllerUtils, $rootScope, Socket) {
|
||||
var spy = sinon.spy(Socket, 'sysOn');
|
||||
$rootScope.wallet = new FakeWallet();
|
||||
controllerUtils.setSocketHandlers();
|
||||
sinon.assert.callCount(spy, 5);
|
||||
['error', 'reconnect_error', 'reconnect_failed', 'connect', 'reconnect'].forEach(function(e) {
|
||||
sinon.assert.calledWith(spy, e);
|
||||
});
|
||||
}));
|
||||
|
||||
it('should set Address Event Handlers', inject(function(controllerUtils, $rootScope, Socket) {
|
||||
var spy = sinon.spy(Socket, 'on');
|
||||
$rootScope.wallet = new FakeWallet();
|
||||
var Waddr = Object.keys($rootScope.wallet.balanceByAddr)[0];
|
||||
controllerUtils.setSocketHandlers();
|
||||
sinon.assert.calledWith(spy, Waddr);
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
|
|
|
@ -40,6 +40,9 @@ var createBundle = function(opts) {
|
|||
b.require('bitcore/node_modules/browserify-buffertools/buffertools.js', {
|
||||
expose: 'buffertools'
|
||||
});
|
||||
b.require('browser-request', {
|
||||
expose: 'request'
|
||||
});
|
||||
|
||||
b.require('./copay', {
|
||||
expose: 'copay'
|
||||
|
@ -88,6 +91,9 @@ var createBundle = function(opts) {
|
|||
b.require('./test/mocks/FakeBlockchain', {
|
||||
expose: './mocks/FakeBlockchain'
|
||||
});
|
||||
b.require('./test/mocks/FakeBlockchainSocket', {
|
||||
expose: './mocks/FakeBlockchainSocket'
|
||||
});
|
||||
b.require('./test/mocks/FakeNetwork', {
|
||||
expose: './mocks/FakeNetwork'
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue