small refactor on rootScope

This commit is contained in:
Matias Alejo Garcia 2014-06-24 12:57:15 -03:00
commit 1abf1dc90d
4 changed files with 130 additions and 87 deletions

View file

@ -31,8 +31,8 @@ angular.module('copayApp.controllers').controller('AddressesController',
for (var i = 0; i < addrInfos.length; i++) { for (var i = 0; i < addrInfos.length; i++) {
var addrinfo = addrInfos[i]; var addrinfo = addrInfos[i];
$scope.addresses.push({ $scope.addresses.push({
'address' : addrinfo.address.toString(), 'address': addrinfo.addressStr,
'balance' : $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.address.toString()] : 0, 'balance': $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.addressStr] : 0,
'isChange': addrinfo.isChange 'isChange': addrinfo.isChange
}); });
} }
@ -41,5 +41,4 @@ angular.module('copayApp.controllers').controller('AddressesController',
$rootScope.receivedFund = null; $rootScope.receivedFund = null;
} }
} }
}); });

View file

@ -1,4 +1,3 @@
'use strict'; 'use strict';
@ -90,7 +89,9 @@ PublicKeyRing.prototype._checkKeys = function() {
}; };
PublicKeyRing.prototype._newExtendedPublicKey = function() { PublicKeyRing.prototype._newExtendedPublicKey = function() {
return new PrivateKey({networkName: this.network.name}) return new PrivateKey({
networkName: this.network.name
})
.deriveBIP45Branch() .deriveBIP45Branch()
.extendedPublicKeyString(); .extendedPublicKeyString();
}; };
@ -147,10 +148,13 @@ PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
var hk = this.copayersHK[i].derive(path); var hk = this.copayersHK[i].derive(path);
pubKeys[i] = hk.eckey.public; pubKeys[i] = hk.eckey.public;
} }
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');}); this.publicKeysCache[path] = pubKeys.map(function(pk) {
} return pk.toString('hex');
else { });
pubKeys = pubKeys.map(function(s){return new Buffer(s,'hex');}); } else {
pubKeys = pubKeys.map(function(s) {
return new Buffer(s, 'hex');
});
} }
@ -205,8 +209,10 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
var ret = []; var ret = [];
if (!opts.excludeChange) { if (!opts.excludeChange) {
for (var i = 0; i < this.indexes.getChangeIndex(); i++) { for (var i = 0; i < this.indexes.getChangeIndex(); i++) {
var a = this.getAddress(i, true);
ret.unshift({ ret.unshift({
address: this.getAddress(i, true), address: this.getAddress(i, true),
addressStr: a.toString(),
isChange: true isChange: true
}); });
} }
@ -214,8 +220,10 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
if (!opts.excludeMain) { if (!opts.excludeMain) {
for (var i = 0; i < this.indexes.getReceiveIndex(); i++) { for (var i = 0; i < this.indexes.getReceiveIndex(); i++) {
var a = this.getAddress(i, false);
ret.unshift({ ret.unshift({
address: this.getAddress(i,false), address: a,
addressStr: a.toString(),
isChange: false isChange: false
}); });
} }

View file

@ -14,6 +14,8 @@ angular.module('copayApp.services')
}; };
root.logout = function() { root.logout = function() {
Socket.removeAllListeners();
$rootScope.wallet = null; $rootScope.wallet = null;
delete $rootScope['wallet']; delete $rootScope['wallet'];
video.close(); video.close();
@ -70,10 +72,35 @@ angular.module('copayApp.services')
}); });
}; };
root.setupRootVariables = function() {
$rootScope.unitName = config.unitName;
$rootScope.txAlertCount = 0;
$rootScope.insightError = 0;
$rootScope.isCollapsed = true;
$rootScope.$watch('insightError', function(status) {
if (status === -1) {
$rootScope.$flashMessage = {
type: 'success',
message: 'Networking Restored :)',
};
$rootScope.insightError = 0;
}
});
$rootScope.$watch('txAlertCount', function(txAlertCount) {
if (txAlertCount && txAlertCount > 0) {
notification.info('New Transaction', ($rootScope.txAlertCount == 1) ? 'You have a pending transaction proposal' : 'You have ' + $rootScope.txAlertCount + ' pending transaction proposals', txAlertCount);
}
});
};
root.startNetwork = function(w, $scope) { root.startNetwork = function(w, $scope) {
Socket.removeAllListeners();
root.setupRootVariables();
root.installStartupHandlers(w, $scope); root.installStartupHandlers(w, $scope);
root.setSocketHandlers();
var handlePeerVideo = function(err, peerID, url) { var handlePeerVideo = function(err, peerID, url) {
if (err) { if (err) {
@ -103,8 +130,8 @@ angular.module('copayApp.services')
}); });
w.on('publicKeyRingUpdated', function(dontDigest) { w.on('publicKeyRingUpdated', function(dontDigest) {
root.setSocketHandlers();
root.updateAddressList(); root.updateAddressList();
root.setSocketHandlers();
if (!dontDigest) { if (!dontDigest) {
$rootScope.$digest(); $rootScope.$digest();
} }
@ -273,20 +300,23 @@ angular.module('copayApp.services')
if (!$rootScope.wallet) return; if (!$rootScope.wallet) return;
var currentAddrs = Socket.getListeners(); var currentAddrs = Socket.getListeners();
var addrs = $rootScope.wallet.getAddressesStr(); var allAddrs = $rootScope.addrInfos;
var newAddrs = []; var newAddrs = [];
for (var i in addrs) { for (var i in allAddrs) {
var a = addrs[i]; var a = allAddrs[i];
if (!currentAddrs[a]) if (!currentAddrs[a.addressStr])
newAddrs.push(a); newAddrs.push(a);
} }
for (var i = 0; i < newAddrs.length; i++) { for (var i = 0; i < newAddrs.length; i++) {
Socket.emit('subscribe', newAddrs[i]); Socket.emit('subscribe', newAddrs[i].addressStr);
} }
newAddrs.forEach(function(addr) { newAddrs.forEach(function(a) {
Socket.on(addr, function(txid) { Socket.on(a.addressStr, function(txid) {
$rootScope.receivedFund = [txid, addr];
if (!a.isChange)
notification.funds('Received fund', a.addressStr);
root.updateBalance(function() { root.updateBalance(function() {
$rootScope.$digest(); $rootScope.$digest();
}); });

View file

@ -38,8 +38,12 @@ angular.module('copayApp.services').factory('Socket',
var ret = {}; var ret = {};
var addrList = listeners var addrList = listeners
.filter(function(i) { return i.event != 'block'; }) .filter(function(i) {
.map(function(i) {return i.event;}); return i.event != 'block';
})
.map(function(i) {
return i.event;
});
for (var i in addrList) { for (var i in addrList) {
ret[addrList[i]] = 1; ret[addrList[i]] = 1;
@ -47,7 +51,9 @@ angular.module('copayApp.services').factory('Socket',
return ret; return ret;
}, },
isListeningBlocks: function() { isListeningBlocks: function() {
return listeners.filter(function(i) { return i.event == 'block'; }).length > 0; return listeners.filter(function(i) {
return i.event == 'block';
}).length > 0;
}, },
emit: function(event, data, callback) { emit: function(event, data, callback) {
socket.emit(event, data, function() { socket.emit(event, data, function() {
@ -62,7 +68,7 @@ angular.module('copayApp.services').factory('Socket',
removeAllListeners: function() { removeAllListeners: function() {
for (var i = 0; i < listeners.length; i++) { for (var i = 0; i < listeners.length; i++) {
var details = listeners[i]; var details = listeners[i];
socket.removeListener(details.event, details.fn); socket.removeAllListeners(details.event);
} }
listeners = []; listeners = [];