From a28e98e85da89925c62ec51016ec54161db44dc3 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 12 Apr 2014 13:37:41 -0300 Subject: [PATCH] many bugs fix in peer connections, and ux --- index.html | 4 +- js/controllers/header.js | 4 ++ js/models/CopayPeer.js | 17 +++++++- js/models/PublicKeyRing.js | 1 + js/services/network.js | 79 +++++++++++++++++++------------------- 5 files changed, 61 insertions(+), 44 deletions(-) diff --git a/index.html b/index.html index 91fef6c63..371adfc4a 100644 --- a/index.html +++ b/index.html @@ -74,11 +74,11 @@

Open a Existing Wallet

- +

diff --git a/js/controllers/header.js b/js/controllers/header.js index 5ae7d90eb..161181d6a 100644 --- a/js/controllers/header.js +++ b/js/controllers/header.js @@ -35,8 +35,12 @@ angular.module('copay.header').controller('HeaderController', }; $scope.signout = function() { + console.log('[header.js.37:signout:]'); //TODO + Network.disconnect(function() { + console.log('[header.js.41] disconnect CB'); //TODO $location.path('signin'); + $rootScope.$digest(); }); }; diff --git a/js/models/CopayPeer.js b/js/models/CopayPeer.js index eecbca8e7..1517b2ae3 100644 --- a/js/models/CopayPeer.js +++ b/js/models/CopayPeer.js @@ -67,6 +67,7 @@ CopayPeer.prototype._showConnectedPeers = function() { }; CopayPeer.prototype._onClose = function(peerId) { + console.log('[CopayPeer.js.70] _onClose'); //TODO this.connectedPeers = CopayPeer._arrayRemove(peerId, this.connectedPeers); this._notify(); }; @@ -92,7 +93,7 @@ CopayPeer.prototype._onData = function(data, isInbound) { console.log('### ERROR ON DATA: "%s" ', data, isInbound, e); return; }; - console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender); + console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender, obj.data); switch(obj.data.type) { case 'peerList': @@ -156,13 +157,16 @@ CopayPeer.prototype._setupConnectionHandlers = function( }); dataConn.on('close', function() { - console.log('### CLOSE RECV FROM:', dataConn.peer); //TODO + if (self.closing) return; + console.log('### CLOSE RECV FROM:', dataConn.peer); + self._onClose(dataConn.peer); if (typeof closeCallback === 'function') closeCallback(); }); }; CopayPeer.prototype._notify = function(newPeer) { + console.log('[CopayPeer.js.168:_notify:]'); //TODO this._showConnectedPeers(); this.emit('networkChange', newPeer); }; @@ -182,6 +186,10 @@ CopayPeer.prototype._setupPeerHandlers = function(openCallback) { p.on('error', function(err) { console.log('### PEER ERROR:', err); + self.peer.disconnect(); + self.peer.destroy(); + self.peer = null; + this.emit('abort'); }); p.on('connection', function(dataConn) { @@ -266,9 +274,13 @@ CopayPeer.prototype.connectTo = function(peerId, openCallback, closeCallback ) { }; CopayPeer.prototype.disconnect = function(peerId, cb) { + console.log('[CopayPeer.js.268:disconnect:]'); //TODO var self = this; + self.closing = 1; this.send(null, { type: 'disconnect' }, function() { + +console.log('[CopayPeer.js.273] disconnect CB'); //TODO self.connectedPeers = []; self.peerId = null; if (self.peer) { @@ -276,6 +288,7 @@ CopayPeer.prototype.disconnect = function(peerId, cb) { self.peer.destroy(); self.peer = null; } + self.closing = 0; if (typeof cb === 'function') cb(); }); }; diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index ed5adc23a..6aea08e39 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -72,6 +72,7 @@ PublicKeyRing.fromObj = function (data) { var w = new PublicKeyRing(config); + w.id = data.id; w.requiredCopayers = data.requiredCopayers; w.totalCopayers = data.totalCopayers; w.addressIndex = data.addressIndex; diff --git a/js/services/network.js b/js/services/network.js index da5ea6b0d..3d4d771d2 100644 --- a/js/services/network.js +++ b/js/services/network.js @@ -110,13 +110,13 @@ console.log('[network.js.97:priv:]',priv); //TODO $rootScope.txProposals = new copay.TxProposals.fromObj(txp); $rootScope.PrivateKey = new copay.PrivateKey.fromObj(priv); //TODO secure - // Add our key JIC + // JIC: Add our key try { $rootScope.publicKeyRing.addCopayer( $rootScope.PrivateKey.getBIP32().extendedPublicKeyString() ); } catch (e) { -console.log('[network.js.103]', e); //TODO + console.log('NOT NECCESARY AN ERROR:', e); //TODO }; ret = true; } @@ -127,7 +127,8 @@ console.log('[network.js.103]', e); //TODO console.log('### CLOSING WALLET'); $rootScope.walletId = null; $rootScope.publicKeyRing = null; - //TODO + $rootScope.privateKey = null; + $rootScope.txProposals = null; }; var _checkWallet = function(walletId, allowChange) { @@ -137,9 +138,6 @@ console.log('[network.js.103]', e); //TODO return; if ($rootScope.walletId && $rootScope.walletId !== walletId) { - if (allowChange) - closeWallet(); - else throw new Error('message to wrong walletID'); } @@ -150,37 +148,9 @@ console.log('[network.js.103]', e); //TODO }; - var _handlePublicKeyRing = function(senderId, data, isInbound) { - var cp = $rootScope.cp; - _checkWallet(data.publicKeyRing.id, true); - var shouldSend = false; - - var recipients, pkr = $rootScope.publicKeyRing; - var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing); - if (pkr.merge(inPKR, true) && !data.isBroadcast) { - console.log('### BROADCASTING PKR'); - recipients = null; - shouldSend = true; - } - else if (isInbound && !data.isBroadcast) { - // always replying to connecting peer - console.log('### REPLYING PKR TO:', senderId); - recipients = senderId; - shouldSend = true; - } - - if (shouldSend) { - console.log('### SENDING PKR TO:', recipients); - cp.send( recipients, { - type: 'publicKeyRing', - publicKeyRing: $rootScope.publicKeyRing.toObj(), - }); - } - }; - var sendTxProposals = function(recipients) { var cp = $rootScope.cp; - console.log('### SENDING txProposals TO:', recipients||'All'); + console.log('### SENDING txProposals TO:', recipients||'All', $rootScope.txProposals); cp.send( recipients, { type: 'txProposals', txProposals: $rootScope.txProposals.toObj(), @@ -197,9 +167,36 @@ console.log('[network.js.103]', e); //TODO }); }; + var _handlePublicKeyRing = function(senderId, data, isInbound) { + var cp = $rootScope.cp; + _checkWallet(data.publicKeyRing.id); + var shouldSend = false; + + var recipients, pkr = $rootScope.publicKeyRing; + var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing); +console.log('[network.js.176:inPKR:]',inPKR); //TODO +console.log('[network.js.178:pkr:]',pkr); //TODO + if (pkr.merge(inPKR, true) && !data.isBroadcast) { + console.log('### BROADCASTING PKR'); + recipients = null; + shouldSend = true; + } + else if (isInbound && !data.isBroadcast) { + // always replying to connecting peer + console.log('### REPLYING PKR TO:', senderId); + recipients = senderId; + shouldSend = true; + } + + console.log('[network.js.189:shouldSend:]',shouldSend); //TODO + if (shouldSend) { + sendPublicKeyRing(recipients); + } + _refreshUx(); + }; var _handleTxProposals = function(senderId, data, isInbound) { var cp = $rootScope.cp; - _checkWallet(data.txProposals.walletId, false); + _checkWallet(data.txProposals.walletId); var shouldSend = false; console.log('RECV TXPROPOSAL:',data); //TODO @@ -220,7 +217,7 @@ console.log('[network.js.103]', e); //TODO } if (shouldSend) { - this.sendTxProposals(recipients); + sendTxProposals(recipients); } }; @@ -233,9 +230,11 @@ console.log('[network.js.103]', e); //TODO case 'txProposals': _handleTxProposals(senderId, data, isInbound); break; - + case 'abort': + disconnect(); + _refreshUx(); + break; } - _refreshUx(); }; var _setupHandlers = function () { var cp = $rootScope.cp; @@ -261,9 +260,9 @@ console.log('[network.js.103]', e); //TODO if ($rootScope.cp) { $rootScope.cp.disconnect(); } + closeWallet(); Storage.remove('peerData'); $rootScope.isLogged = false; - _refreshUx(); }; var connect = function(peerId, openCallback, failCallback) {