Merge pull request #111 from colkito/fix/validate-wallet-before-routing

Fix/validate wallet before routing
This commit is contained in:
Gustavo Maximiliano Cortez 2014-04-21 11:50:54 -03:00
commit 900d1d3a16
12 changed files with 101 additions and 169 deletions

View File

@ -174,7 +174,7 @@
</script> </script>
<script type="text/ng-template" id="peer.html"> <script type="text/ng-template" id="peer.html">
<div class="row" ng-controller="PeerController" ng-init="init()"> <div class="row" ng-controller="PeerController">
<div class="large-6 columns p70r line-dashed-v"> <div class="large-6 columns p70r line-dashed-v">
<h3>I am </h3> <h3>I am </h3>
<div class="row"> <div class="row">

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copay.addresses').controller('AddressesController', angular.module('copay.addresses').controller('AddressesController',
function($scope, $rootScope, $location, Socket, controllerUtils) { function($scope, $rootScope, controllerUtils) {
$scope.title = 'Home'; $scope.title = 'Home';
$scope.oneAtATime = true; $scope.oneAtATime = true;
$scope.addrBalance = {}; $scope.addrBalance = {};
@ -9,21 +9,21 @@ angular.module('copay.addresses').controller('AddressesController',
var w = $rootScope.wallet; var w = $rootScope.wallet;
var _updateBalance = function () { var _updateBalance = function () {
controllerUtils.setSocketHandlers();
w.getBalance(function (balance, balanceByAddr, isMain) { w.getBalance(function (balance, balanceByAddr, isMain) {
if (balanceByAddr && Object.keys(balanceByAddr).length) { if (balanceByAddr && Object.keys(balanceByAddr).length) {
$scope.balanceByAddr = balanceByAddr; $rootScope.balanceByAddr = balanceByAddr;
$scope.isMain = isMain; $scope.isMain = isMain;
$scope.addrs = Object.keys(balanceByAddr); $scope.addrs = Object.keys(balanceByAddr);
$scope.selectedAddr = $scope.addrs[0]; $scope.selectedAddr = $scope.addrs[0];
$scope.$digest(); $rootScope.$digest();
} }
}); });
var socket = Socket($scope);
controllerUtils.handleTransactionByAddress($scope, _updateBalance);
}; };
$scope.newAddr = function() { $scope.newAddr = function() {
var a = w.generateAddress().toString(); w.generateAddress();
_updateBalance(); _updateBalance();
}; };
@ -31,10 +31,6 @@ angular.module('copay.addresses').controller('AddressesController',
$scope.selectedAddr = addr; $scope.selectedAddr = addr;
}; };
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
_updateBalance(); _updateBalance();
w.on('refresh',_updateBalance); w.on('refresh', _updateBalance);
}); });

View File

@ -1,14 +1,7 @@
'use strict'; 'use strict';
angular.module('copay.backup').controller('BackupController', angular.module('copay.backup').controller('BackupController',
function($scope, $rootScope, $location, $window, $timeout, Socket, controllerUtils) { function($scope, $rootScope, $location, $window, $timeout) {
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
else {
controllerUtils.handleTransactionByAddress($scope);
}
$scope.title = 'Backup'; $scope.title = 'Backup';
var filename = $rootScope.wallet.id + '.json.aes'; var filename = $rootScope.wallet.id + '.json.aes';

View File

@ -24,9 +24,16 @@ angular.module('copay.header').controller('HeaderController',
'link': '#/backup' 'link': '#/backup'
}]; }];
if (!$rootScope.wallet || !$rootScope.wallet.id) { $rootScope.$watch('wallet', function(wallet) {
$location.path('signin'); if (wallet) {
} controllerUtils.setSocketHandlers();
$rootScope.wallet.getBalance(function(balance) {
$rootScope.$apply(function() {
$rootScope.totalBalance = balance;
});
});
}
});
$scope.isActive = function(item) { $scope.isActive = function(item) {
if (item.link && item.link.replace('#','') == $location.path()) { if (item.link && item.link.replace('#','') == $location.path()) {

View File

@ -1,17 +1,6 @@
'use strict'; 'use strict';
angular.module('copay.peer').controller('PeerController', angular.module('copay.peer').controller('PeerController',
function($scope, $rootScope, $location, $routeParams, Socket, controllerUtils) { function($scope, $rootScope, $location, $routeParams) {
$scope.init = function() {
//Network.connect($rootScope.masterId);
};
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
else {
controllerUtils.handleTransactionByAddress($scope);
}
}); });

View File

@ -1,19 +1,12 @@
'use strict'; 'use strict';
angular.module('copay.send').controller('SendController', angular.module('copay.send').controller('SendController',
function($scope, $rootScope, $location, Socket, controllerUtils) { function($scope, $rootScope, $location) {
$scope.title = 'Send'; $scope.title = 'Send';
$scope.unitIds = ['BTC','mBTC']; $scope.unitIds = ['BTC','mBTC'];
$scope.selectedUnit = $scope.unitIds[0]; $scope.selectedUnit = $scope.unitIds[0];
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
else {
controllerUtils.handleTransactionByAddress($scope);
}
$scope.submitForm = function(form) { $scope.submitForm = function(form) {
if (form.$invalid) { if (form.$invalid) {
$rootScope.flashMessage = { message: 'You can not send a proposal transaction. Please, try again', type: 'error'}; $rootScope.flashMessage = { message: 'You can not send a proposal transaction. Please, try again', type: 'error'};
@ -37,5 +30,4 @@ angular.module('copay.send').controller('SendController',
// TODO: check if createTx has an error. // TODO: check if createTx has an error.
$rootScope.flashMessage = { message: 'Your transaction proposal has been sent successfully', type: 'success'}; $rootScope.flashMessage = { message: 'Your transaction proposal has been sent successfully', type: 'success'};
}; };
}); });

View File

@ -19,11 +19,12 @@ angular.module('copay.setup').controller('SetupController',
} }
} }
}; };
updateRCSelect($scope.totalCopayers); updateRCSelect($scope.totalCopayers);
$scope.$watch('totalCopayers', function(tc) { $scope.$watch('totalCopayers', function(tc) {
updateRCSelect(tc); updateRCSelect(tc);
}) });
$scope.create = function(totalCopayers, requiredCopayers) { $scope.create = function(totalCopayers, requiredCopayers) {
$scope.loading = true; $scope.loading = true;

View File

@ -1,13 +1,9 @@
'use strict';
angular.module('copay.signin').controller('SigninController', angular.module('copay.signin').controller('SigninController',
function($scope, $rootScope, $location, walletFactory, controllerUtils) { function($scope, $rootScope, $location, walletFactory, controllerUtils) {
// var peerData = Storage.get($rootScope.walletId, 'peerData');
// $rootScope.peerId = peerData ? peerData.peerId : null;
$scope.loading = false; $scope.loading = false;
$scope.walletIds = walletFactory.getWalletIds(); $scope.walletIds = walletFactory.getWalletIds();
$scope.selectedWalletId = $scope.walletIds.length ? $scope.walletIds[0]:null; $scope.selectedWalletId = $scope.walletIds.length ? $scope.walletIds[0]:null;
$scope.create = function() { $scope.create = function() {
@ -25,7 +21,6 @@ console.log('[signin.js.23:walletId:]',walletId); //TODO
$scope.join = function(secret) { $scope.join = function(secret) {
$scope.loading = true; $scope.loading = true;
walletFactory.network.on('joinError', function() { walletFactory.network.on('joinError', function() {
controllerUtils.onErrorDigest($scope); controllerUtils.onErrorDigest($scope);
}); });
@ -35,29 +30,4 @@ console.log('[signin.js.33] joinCreateSession RETURN', w); //TODO
controllerUtils.setupUxHandlers(w); controllerUtils.setupUxHandlers(w);
}); });
}; };
//
// if (cid) {
// var w = walletFactory.(walletId);
//TODO
// Network.init(null, function() {
// Network.connect(cid,
// function() {
// $location.path('peer');
// $rootScope.$digest();
// }, function() {
// $rootScope.flashMessage = { message: 'Connection refussed', type: 'error'};
// $location.path('home');
// $rootScope.$digest();
// });
// });
// }
// if (peerData && peerData.peerId && peerData.connectedPeers.length > 0) {
// $rootScope.peerId = peerData.peerId;
// $scope.join(peerData.connectedPeers);
// }
}); });

View File

@ -2,7 +2,7 @@
var bitcore = require('bitcore'); var bitcore = require('bitcore');
angular.module('copay.transactions').controller('TransactionsController', angular.module('copay.transactions').controller('TransactionsController',
function($scope, $rootScope, $location, Socket, controllerUtils) { function($scope, $rootScope, $location) {
$scope.title = 'Transactions'; $scope.title = 'Transactions';
$scope.oneAtATime = true; $scope.oneAtATime = true;
@ -37,19 +37,6 @@ console.log('[transactions.js.10:_updateTxs:]'); //TODO
w.once('txProposalsUpdated',_updateTxs); w.once('txProposalsUpdated',_updateTxs);
}; };
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
else {
_updateTxs();
var w = $rootScope.wallet;
var socket = Socket($scope);
socket.on('connect', controllerUtils.handleTransactionByAddress($scope));
}
$scope.send = function (ntxid) { $scope.send = function (ntxid) {
var w = $rootScope.wallet; var w = $rootScope.wallet;
w.sendTx(ntxid, function(txid) { w.sendTx(ntxid, function(txid) {

View File

@ -7,31 +7,40 @@ angular
$routeProvider $routeProvider
.when('/', { .when('/', {
templateUrl: 'signin.html' templateUrl: 'signin.html',
validate: false
}) })
.when('/signin', { .when('/signin', {
templateUrl: 'signin.html' templateUrl: 'signin.html',
validate: false
}) })
.when('/setup', { .when('/setup', {
templateUrl: 'setup.html' templateUrl: 'setup.html',
validate: false
}) })
.when('/addresses', { .when('/addresses', {
templateUrl: 'addresses.html' templateUrl: 'addresses.html',
validate: true
}) })
.when('/join/:id', { .when('/join/:id', {
templateUrl: 'join.html' templateUrl: 'join.html',
validate: true
}) })
.when('/peer', { .when('/peer', {
templateUrl: 'peer.html' templateUrl: 'peer.html',
validate: true
}) })
.when('/transactions', { .when('/transactions', {
templateUrl: 'transactions.html' templateUrl: 'transactions.html',
validate: true
}) })
.when('/send', { .when('/send', {
templateUrl: 'send.html' templateUrl: 'send.html',
validate: true
}) })
.when('/backup', { .when('/backup', {
templateUrl: 'backup.html' templateUrl: 'backup.html',
validate: true
}) })
.otherwise({ .otherwise({
templateUrl: '404.html' templateUrl: '404.html'
@ -45,4 +54,11 @@ angular
$locationProvider $locationProvider
.html5Mode(false); .html5Mode(false);
//.hashPrefix('!'); //.hashPrefix('!');
})
.run(function($rootScope, $location) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if ((!$rootScope.wallet || !$rootScope.wallet.id) && next.validate) {
$location.path('signin');
}
});
}); });

View File

@ -21,7 +21,6 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
$rootScope.$digest(); $rootScope.$digest();
} }
root.setupUxHandlers = function(w) { root.setupUxHandlers = function(w) {
w.on('badMessage', function(peerId) { w.on('badMessage', function(peerId) {
$rootScope.flashMessage = {type:'error', message: 'Received wrong message from peer id:' + peerId}; $rootScope.flashMessage = {type:'error', message: 'Received wrong message from peer id:' + peerId};
@ -48,19 +47,24 @@ console.log('[controllerUtils.js.45] CALLING NETSTART FROM setupUxHandlers'); //
console.log('[controllerUtils.js.45] setupUxHandlers END'); //TODO console.log('[controllerUtils.js.45] setupUxHandlers END'); //TODO
}; };
root.handleTransactionByAddress = function(scope, cb) { root.setSocketHandlers = function(cb) {
var socket = Socket(scope); Socket.removeAllListeners();
var addrs = $rootScope.wallet.getAddressesStr(); var addrs = $rootScope.wallet.getAddressesStr();
for(var i=0;i<addrs.length;i++) { for(var i = 0; i < addrs.length; i++) {
socket.emit('subscribe', addrs[i]); console.log('### SUBSCRIBE TO', addrs[i]);
Socket.emit('subscribe', addrs[i]);
} }
addrs.forEach(function(addr) { addrs.forEach(function(addr) {
socket.on(addr, function(txid) { Socket.on(addr, function(txid) {
console.log('Received!', txid); console.log('Received!', txid);
$rootScope.wallet.getBalance(function(balance) { $rootScope.wallet.getBalance(function(balance, balanceByAddr) {
scope.$apply(function() { $rootScope.$apply(function() {
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
}); });
console.log('New balance:', balance); console.log('New balance:', balance);
if (typeof cb === 'function') return cb(); if (typeof cb === 'function') return cb();
}); });

View File

@ -1,72 +1,49 @@
'use strict'; 'use strict';
var ScopedSocket = function(socket, $rootScope) {
this.socket = socket;
this.$rootScope = $rootScope;
this.listeners = [];
};
ScopedSocket.prototype.removeAllListeners = function(opts) {
if (!opts) opts = {};
for (var i = 0; i < this.listeners.length; i++) {
var details = this.listeners[i];
if (opts.skipConnect && details.event === 'connect') {
continue;
}
this.socket.removeListener(details.event, details.fn);
}
this.listeners = [];
};
ScopedSocket.prototype.on = function(event, callback) {
var socket = this.socket;
var $rootScope = this.$rootScope;
var wrapped_callback = function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(socket, args);
});
};
socket.on(event, wrapped_callback);
this.listeners.push({
event: event,
fn: wrapped_callback
});
};
ScopedSocket.prototype.emit = function(event, data, callback) {
var socket = this.socket;
var $rootScope = this.$rootScope;
socket.emit(event, data, function() {
var args = arguments;
$rootScope.$apply(function() {
if (callback) {
callback.apply(socket, args);
}
});
});
};
angular.module('copay.socket').factory('Socket', angular.module('copay.socket').factory('Socket',
function($rootScope) { function($rootScope) {
var server = 'http://' + config.socket.host + ':' + config.socket.port; var listeners = [];
var socket = io.connect(server, { var url = 'http://' + config.socket.host + ':' + config.socket.port;
var socket = io.connect(url, {
'reconnect': true, 'reconnect': true,
'reconnection delay': 500, 'reconnection delay': 500,
}); });
return function(scope) {
var scopedSocket = new ScopedSocket(socket, $rootScope); return {
scope.$on('$destroy', function() { on: function(event, callback) {
scopedSocket.removeAllListeners(); var wrappedCallback = function() {
}); var args = arguments;
socket.on('connect', function() { $rootScope.$apply(function() {
scopedSocket.removeAllListeners({ callback.apply(socket, args);
skipConnect: true });
};
socket.on(event, wrappedCallback);
if (event !== 'connect') {
listeners.push({
event: event,
fn: wrappedCallback
});
}
},
emit: function(event, data, callback) {
socket.emit(event, data, function() {
var args = arguments;
$rootScope.$apply(function() {
if (callback) {
callback.apply(socket, args);
}
});
}); });
}); },
return scopedSocket; removeAllListeners: function() {
for (var i = 0; i < listeners.length; i++) {
var details = listeners[i];
socket.removeListener(details.event, details.fn);
}
listeners = [];
}
}; };
}); });