mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #111 from colkito/fix/validate-wallet-before-routing
Fix/validate wallet before routing
This commit is contained in:
commit
900d1d3a16
|
@ -174,7 +174,7 @@
|
|||
</script>
|
||||
|
||||
<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">
|
||||
<h3>I am </h3>
|
||||
<div class="row">
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.addresses').controller('AddressesController',
|
||||
function($scope, $rootScope, $location, Socket, controllerUtils) {
|
||||
function($scope, $rootScope, controllerUtils) {
|
||||
$scope.title = 'Home';
|
||||
$scope.oneAtATime = true;
|
||||
$scope.addrBalance = {};
|
||||
|
@ -9,21 +9,21 @@ angular.module('copay.addresses').controller('AddressesController',
|
|||
var w = $rootScope.wallet;
|
||||
|
||||
var _updateBalance = function () {
|
||||
controllerUtils.setSocketHandlers();
|
||||
|
||||
w.getBalance(function (balance, balanceByAddr, isMain) {
|
||||
if (balanceByAddr && Object.keys(balanceByAddr).length) {
|
||||
$scope.balanceByAddr = balanceByAddr;
|
||||
$rootScope.balanceByAddr = balanceByAddr;
|
||||
$scope.isMain = isMain;
|
||||
$scope.addrs = Object.keys(balanceByAddr);
|
||||
$scope.selectedAddr = $scope.addrs[0];
|
||||
$scope.$digest();
|
||||
$rootScope.$digest();
|
||||
}
|
||||
});
|
||||
var socket = Socket($scope);
|
||||
controllerUtils.handleTransactionByAddress($scope, _updateBalance);
|
||||
};
|
||||
|
||||
$scope.newAddr = function() {
|
||||
var a = w.generateAddress().toString();
|
||||
w.generateAddress();
|
||||
_updateBalance();
|
||||
};
|
||||
|
||||
|
@ -31,10 +31,6 @@ angular.module('copay.addresses').controller('AddressesController',
|
|||
$scope.selectedAddr = addr;
|
||||
};
|
||||
|
||||
|
||||
if (!$rootScope.wallet || !$rootScope.wallet.id) {
|
||||
$location.path('signin');
|
||||
}
|
||||
_updateBalance();
|
||||
w.on('refresh',_updateBalance);
|
||||
w.on('refresh', _updateBalance);
|
||||
});
|
||||
|
|
|
@ -1,14 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.backup').controller('BackupController',
|
||||
function($scope, $rootScope, $location, $window, $timeout, Socket, controllerUtils) {
|
||||
if (!$rootScope.wallet || !$rootScope.wallet.id) {
|
||||
$location.path('signin');
|
||||
}
|
||||
else {
|
||||
controllerUtils.handleTransactionByAddress($scope);
|
||||
}
|
||||
|
||||
function($scope, $rootScope, $location, $window, $timeout) {
|
||||
$scope.title = 'Backup';
|
||||
|
||||
var filename = $rootScope.wallet.id + '.json.aes';
|
||||
|
|
|
@ -24,9 +24,16 @@ angular.module('copay.header').controller('HeaderController',
|
|||
'link': '#/backup'
|
||||
}];
|
||||
|
||||
if (!$rootScope.wallet || !$rootScope.wallet.id) {
|
||||
$location.path('signin');
|
||||
}
|
||||
$rootScope.$watch('wallet', function(wallet) {
|
||||
if (wallet) {
|
||||
controllerUtils.setSocketHandlers();
|
||||
$rootScope.wallet.getBalance(function(balance) {
|
||||
$rootScope.$apply(function() {
|
||||
$rootScope.totalBalance = balance;
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$scope.isActive = function(item) {
|
||||
if (item.link && item.link.replace('#','') == $location.path()) {
|
||||
|
|
|
@ -1,17 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.peer').controller('PeerController',
|
||||
function($scope, $rootScope, $location, $routeParams, Socket, controllerUtils) {
|
||||
|
||||
$scope.init = function() {
|
||||
//Network.connect($rootScope.masterId);
|
||||
};
|
||||
|
||||
if (!$rootScope.wallet || !$rootScope.wallet.id) {
|
||||
$location.path('signin');
|
||||
}
|
||||
else {
|
||||
controllerUtils.handleTransactionByAddress($scope);
|
||||
}
|
||||
function($scope, $rootScope, $location, $routeParams) {
|
||||
});
|
||||
|
||||
|
|
|
@ -1,19 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.send').controller('SendController',
|
||||
function($scope, $rootScope, $location, Socket, controllerUtils) {
|
||||
function($scope, $rootScope, $location) {
|
||||
$scope.title = 'Send';
|
||||
|
||||
$scope.unitIds = ['BTC','mBTC'];
|
||||
$scope.selectedUnit = $scope.unitIds[0];
|
||||
|
||||
if (!$rootScope.wallet || !$rootScope.wallet.id) {
|
||||
$location.path('signin');
|
||||
}
|
||||
else {
|
||||
controllerUtils.handleTransactionByAddress($scope);
|
||||
}
|
||||
|
||||
$scope.submitForm = function(form) {
|
||||
if (form.$invalid) {
|
||||
$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.
|
||||
$rootScope.flashMessage = { message: 'Your transaction proposal has been sent successfully', type: 'success'};
|
||||
};
|
||||
|
||||
});
|
||||
|
|
|
@ -19,11 +19,12 @@ angular.module('copay.setup').controller('SetupController',
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
updateRCSelect($scope.totalCopayers);
|
||||
|
||||
$scope.$watch('totalCopayers', function(tc) {
|
||||
updateRCSelect(tc);
|
||||
})
|
||||
});
|
||||
|
||||
$scope.create = function(totalCopayers, requiredCopayers) {
|
||||
$scope.loading = true;
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.signin').controller('SigninController',
|
||||
function($scope, $rootScope, $location, walletFactory, controllerUtils) {
|
||||
|
||||
// var peerData = Storage.get($rootScope.walletId, 'peerData');
|
||||
// $rootScope.peerId = peerData ? peerData.peerId : null;
|
||||
$scope.loading = false;
|
||||
|
||||
$scope.walletIds = walletFactory.getWalletIds();
|
||||
|
||||
$scope.selectedWalletId = $scope.walletIds.length ? $scope.walletIds[0]:null;
|
||||
|
||||
$scope.create = function() {
|
||||
|
@ -25,7 +21,6 @@ console.log('[signin.js.23:walletId:]',walletId); //TODO
|
|||
$scope.join = function(secret) {
|
||||
$scope.loading = true;
|
||||
|
||||
|
||||
walletFactory.network.on('joinError', function() {
|
||||
controllerUtils.onErrorDigest($scope);
|
||||
});
|
||||
|
@ -35,29 +30,4 @@ console.log('[signin.js.33] joinCreateSession RETURN', w); //TODO
|
|||
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);
|
||||
// }
|
||||
});
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
var bitcore = require('bitcore');
|
||||
|
||||
angular.module('copay.transactions').controller('TransactionsController',
|
||||
function($scope, $rootScope, $location, Socket, controllerUtils) {
|
||||
function($scope, $rootScope, $location) {
|
||||
$scope.title = 'Transactions';
|
||||
|
||||
$scope.oneAtATime = true;
|
||||
|
@ -37,19 +37,6 @@ console.log('[transactions.js.10:_updateTxs:]'); //TODO
|
|||
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) {
|
||||
var w = $rootScope.wallet;
|
||||
w.sendTx(ntxid, function(txid) {
|
||||
|
|
34
js/routes.js
34
js/routes.js
|
@ -7,31 +7,40 @@ angular
|
|||
|
||||
$routeProvider
|
||||
.when('/', {
|
||||
templateUrl: 'signin.html'
|
||||
templateUrl: 'signin.html',
|
||||
validate: false
|
||||
})
|
||||
.when('/signin', {
|
||||
templateUrl: 'signin.html'
|
||||
templateUrl: 'signin.html',
|
||||
validate: false
|
||||
})
|
||||
.when('/setup', {
|
||||
templateUrl: 'setup.html'
|
||||
templateUrl: 'setup.html',
|
||||
validate: false
|
||||
})
|
||||
.when('/addresses', {
|
||||
templateUrl: 'addresses.html'
|
||||
templateUrl: 'addresses.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/join/:id', {
|
||||
templateUrl: 'join.html'
|
||||
templateUrl: 'join.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/peer', {
|
||||
templateUrl: 'peer.html'
|
||||
templateUrl: 'peer.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/transactions', {
|
||||
templateUrl: 'transactions.html'
|
||||
templateUrl: 'transactions.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/send', {
|
||||
templateUrl: 'send.html'
|
||||
templateUrl: 'send.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/backup', {
|
||||
templateUrl: 'backup.html'
|
||||
templateUrl: 'backup.html',
|
||||
validate: true
|
||||
})
|
||||
.otherwise({
|
||||
templateUrl: '404.html'
|
||||
|
@ -45,4 +54,11 @@ angular
|
|||
$locationProvider
|
||||
.html5Mode(false);
|
||||
//.hashPrefix('!');
|
||||
})
|
||||
.run(function($rootScope, $location) {
|
||||
$rootScope.$on('$routeChangeStart', function(event, next, current) {
|
||||
if ((!$rootScope.wallet || !$rootScope.wallet.id) && next.validate) {
|
||||
$location.path('signin');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -21,7 +21,6 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
|
|||
$rootScope.$digest();
|
||||
}
|
||||
|
||||
|
||||
root.setupUxHandlers = function(w) {
|
||||
w.on('badMessage', function(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
|
||||
};
|
||||
|
||||
root.handleTransactionByAddress = function(scope, cb) {
|
||||
var socket = Socket(scope);
|
||||
root.setSocketHandlers = function(cb) {
|
||||
Socket.removeAllListeners();
|
||||
|
||||
var addrs = $rootScope.wallet.getAddressesStr();
|
||||
for(var i=0;i<addrs.length;i++) {
|
||||
socket.emit('subscribe', addrs[i]);
|
||||
for(var i = 0; i < addrs.length; i++) {
|
||||
console.log('### SUBSCRIBE TO', addrs[i]);
|
||||
Socket.emit('subscribe', addrs[i]);
|
||||
}
|
||||
|
||||
addrs.forEach(function(addr) {
|
||||
socket.on(addr, function(txid) {
|
||||
Socket.on(addr, function(txid) {
|
||||
console.log('Received!', txid);
|
||||
$rootScope.wallet.getBalance(function(balance) {
|
||||
scope.$apply(function() {
|
||||
$rootScope.wallet.getBalance(function(balance, balanceByAddr) {
|
||||
$rootScope.$apply(function() {
|
||||
$rootScope.totalBalance = balance;
|
||||
$rootScope.balanceByAddr = balanceByAddr;
|
||||
});
|
||||
|
||||
console.log('New balance:', balance);
|
||||
if (typeof cb === 'function') return cb();
|
||||
});
|
||||
|
|
|
@ -1,72 +1,49 @@
|
|||
'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',
|
||||
function($rootScope) {
|
||||
var server = 'http://' + config.socket.host + ':' + config.socket.port;
|
||||
var socket = io.connect(server, {
|
||||
var listeners = [];
|
||||
var url = 'http://' + config.socket.host + ':' + config.socket.port;
|
||||
var socket = io.connect(url, {
|
||||
'reconnect': true,
|
||||
'reconnection delay': 500,
|
||||
});
|
||||
return function(scope) {
|
||||
var scopedSocket = new ScopedSocket(socket, $rootScope);
|
||||
scope.$on('$destroy', function() {
|
||||
scopedSocket.removeAllListeners();
|
||||
});
|
||||
socket.on('connect', function() {
|
||||
scopedSocket.removeAllListeners({
|
||||
skipConnect: true
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
},
|
||||
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 = [];
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue