mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #342 from maraoz/optimize/events
Optimize events emitted by Wallet.js and WebRTC.js
This commit is contained in:
commit
be83729223
|
@ -11,7 +11,6 @@ angular.module('copay.addresses').controller('AddressesController',
|
|||
w.generateAddress();
|
||||
controllerUtils.updateBalance(function() {
|
||||
$scope.loading = false;
|
||||
$rootScope.$digest();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -46,7 +46,6 @@ angular.module('copay.header').controller('HeaderController',
|
|||
var w = $rootScope.wallet;
|
||||
w.connectToAll();
|
||||
controllerUtils.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ var Builder = bitcore.TransactionBuilder;
|
|||
var http = require('http');
|
||||
var EventEmitter = imports.EventEmitter || require('events').EventEmitter;
|
||||
var copay = copay || require('../../../copay');
|
||||
var SecureRandom = bitcore.SecureRandom;
|
||||
var Base58Check = bitcore.Base58.base58Check;
|
||||
var SecureRandom = bitcore.SecureRandom;
|
||||
var Base58Check = bitcore.Base58.base58Check;
|
||||
|
||||
function Wallet(opts) {
|
||||
var self = this;
|
||||
|
@ -49,12 +49,16 @@ Wallet.getRandomId = function() {
|
|||
return r;
|
||||
};
|
||||
|
||||
Wallet.prototype.seedCopayer = function(pubKey) {
|
||||
this.seededCopayerId = pubKey;
|
||||
};
|
||||
|
||||
Wallet.prototype.connectToAll = function() {
|
||||
var all = this.publicKeyRing.getAllCopayerIds();
|
||||
this.network.connectToCopayers(all);
|
||||
if (this.firstCopayerId) {
|
||||
this.sendWalletReady(this.firstCopayerId);
|
||||
this.firstCopayerId = null;
|
||||
if (this.seededCopayerId) {
|
||||
this.sendWalletReady(this.seededCopayerId);
|
||||
this.seededCopayerId = null;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -75,13 +79,13 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
|
|||
recipients = null;
|
||||
this.sendPublicKeyRing(recipients);
|
||||
}
|
||||
this.emit('publicKeyRingUpdated', this.publicKeyRing);
|
||||
this.emit('refresh', this.publicKeyRing);
|
||||
this.store();
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
|
||||
this.log('RECV TXPROPOSAL:', data);
|
||||
this.log('RECV TXPROPOSAL:', data);
|
||||
|
||||
var recipients;
|
||||
var inTxp = copay.TxProposals.fromObj(data.txProposals);
|
||||
|
@ -121,15 +125,18 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
|||
}
|
||||
};
|
||||
|
||||
Wallet.prototype._handleNetworkChange = function(newCopayerId) {
|
||||
Wallet.prototype._handleConnect = function(newCopayerId) {
|
||||
if (newCopayerId) {
|
||||
this.log('#### Setting new COPAYER:', newCopayerId);
|
||||
this.sendWalletId(newCopayerId);
|
||||
this.emit('peer', this.network.peerFromCopayer(newCopayerId));
|
||||
}
|
||||
this.emit('refresh');
|
||||
var peerID = this.network.peerFromCopayer(newCopayerId)
|
||||
this.emit('connect', peerID);
|
||||
};
|
||||
|
||||
Wallet.prototype._handleDisconnect = function(peerID) {
|
||||
this.emit('disconnect', peerID);
|
||||
};
|
||||
|
||||
Wallet.prototype._optsToObj = function() {
|
||||
var obj = {
|
||||
|
@ -156,9 +163,9 @@ Wallet.prototype.getMyCopayerId = function() {
|
|||
|
||||
|
||||
Wallet.prototype.getSecret = function() {
|
||||
var i = new Buffer(this.getMyCopayerId(),'hex');
|
||||
var k = new Buffer(this.netKey,'base64');
|
||||
var b = Buffer.concat([i,k]);
|
||||
var i = new Buffer(this.getMyCopayerId(), 'hex');
|
||||
var k = new Buffer(this.netKey, 'base64');
|
||||
var b = Buffer.concat([i, k]);
|
||||
var str = Base58Check.encode(b);
|
||||
return str;
|
||||
};
|
||||
|
@ -167,7 +174,7 @@ Wallet.prototype.getSecret = function() {
|
|||
Wallet.decodeSecret = function(secretB) {
|
||||
var secret = Base58Check.decode(secretB);
|
||||
var netKeyBuf = secret.slice(-8);
|
||||
var pubKeyBuf = secret.slice(0,33);
|
||||
var pubKeyBuf = secret.slice(0, 33);
|
||||
return {
|
||||
pubKey: pubKeyBuf.toString('hex'),
|
||||
netKey: netKeyBuf.toString('base64'),
|
||||
|
@ -182,9 +189,9 @@ Wallet.prototype.netStart = function() {
|
|||
var self = this;
|
||||
var net = this.network;
|
||||
net.removeAllListeners();
|
||||
net.on('networkChange', self._handleNetworkChange.bind(self));
|
||||
net.on('connect', self._handleConnect.bind(self));
|
||||
net.on('disconnect', self._handleDisconnect.bind(self));
|
||||
net.on('data', self._handleData.bind(self));
|
||||
net.on('open', function() {}); // TODO
|
||||
net.on('openError', function() {
|
||||
self.log('[Wallet.js.132:openError:] GOT openError'); //TODO
|
||||
self.emit('openError');
|
||||
|
@ -206,7 +213,7 @@ Wallet.prototype.netStart = function() {
|
|||
|
||||
net.start(startOpts, function() {
|
||||
self.connectToAll();
|
||||
self.emit('created', net.getPeer());
|
||||
self.emit('ready', net.getPeer());
|
||||
self.emit('refresh');
|
||||
});
|
||||
};
|
||||
|
@ -231,17 +238,10 @@ Wallet.prototype.getRegisteredPeerIds = function() {
|
|||
return this.registeredPeerIds;
|
||||
};
|
||||
|
||||
Wallet.prototype.store = function(isSync) {
|
||||
Wallet.prototype.store = function() {
|
||||
var wallet = this.toObj();
|
||||
this.storage.setFromObj(this.id, wallet);
|
||||
|
||||
if (isSync) {
|
||||
this.log('Wallet stored.'); //TODO
|
||||
} else {
|
||||
this.log('Wallet stored. REFRESH Emitted'); //TODO
|
||||
this.emit('refresh');
|
||||
}
|
||||
|
||||
this.log('Wallet stored');
|
||||
};
|
||||
|
||||
Wallet.prototype.toObj = function() {
|
||||
|
@ -257,14 +257,14 @@ Wallet.prototype.toObj = function() {
|
|||
};
|
||||
|
||||
Wallet.fromObj = function(o, storage, network, blockchain) {
|
||||
var opts = JSON.parse(JSON.stringify(o.opts));
|
||||
var opts = JSON.parse(JSON.stringify(o.opts));
|
||||
opts.publicKeyRing = copay.PublicKeyRing.fromObj(o.publicKeyRing);
|
||||
opts.txProposals = copay.TxProposals.fromObj(o.txProposals);
|
||||
opts.privateKey = copay.PrivateKey.fromObj(o.privateKey);
|
||||
opts.txProposals = copay.TxProposals.fromObj(o.txProposals);
|
||||
opts.privateKey = copay.PrivateKey.fromObj(o.privateKey);
|
||||
|
||||
opts.storage = storage;
|
||||
opts.network = network;
|
||||
opts.blockchain = blockchain;
|
||||
opts.storage = storage;
|
||||
opts.network = network;
|
||||
opts.blockchain = blockchain;
|
||||
var w = new Wallet(opts);
|
||||
return w;
|
||||
};
|
||||
|
@ -291,7 +291,6 @@ Wallet.prototype.sendWalletReady = function(recipients) {
|
|||
type: 'walletReady',
|
||||
walletId: this.id,
|
||||
});
|
||||
this.emit('walletReady');
|
||||
};
|
||||
|
||||
Wallet.prototype.sendWalletId = function(recipients) {
|
||||
|
@ -319,7 +318,7 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
|
|||
Wallet.prototype.generateAddress = function(isChange) {
|
||||
var addr = this.publicKeyRing.generateAddress(isChange);
|
||||
this.sendPublicKeyRing();
|
||||
this.store(true);
|
||||
this.store();
|
||||
return addr;
|
||||
};
|
||||
|
||||
|
@ -346,7 +345,8 @@ Wallet.prototype.reject = function(ntxid) {
|
|||
|
||||
txp.rejectedBy[myId] = Date.now();
|
||||
this.sendTxProposals();
|
||||
this.store(true);
|
||||
this.store();
|
||||
this.emit('refresh');
|
||||
};
|
||||
|
||||
|
||||
|
@ -363,14 +363,14 @@ Wallet.prototype.sign = function(ntxid) {
|
|||
var before = b.signaturesAdded;
|
||||
b.sign(keys);
|
||||
|
||||
var ret = false;
|
||||
if (b.signaturesAdded > before) {
|
||||
txp.signedBy[myId] = Date.now();
|
||||
this.sendTxProposals();
|
||||
this.store(true);
|
||||
ret = true;
|
||||
this.store();
|
||||
this.emit('refresh');
|
||||
return true;
|
||||
}
|
||||
return ret;
|
||||
return false;
|
||||
};
|
||||
|
||||
Wallet.prototype.sendTx = function(ntxid, cb) {
|
||||
|
|
|
@ -193,7 +193,7 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
|
|||
data.opts.passphrase = passphrase;
|
||||
data.opts.id = data.walletId;
|
||||
var w = self.create(data.opts);
|
||||
w.firstCopayerId = s.pubKey;
|
||||
w.seedCopayer(s.pubKey);
|
||||
return cb(null, w);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -5,7 +5,7 @@ var bitcore = require('bitcore');
|
|||
var util = bitcore.util;
|
||||
/*
|
||||
* Emits
|
||||
* 'networkChange'
|
||||
* 'connect'
|
||||
* when network layout has change (new/lost peers, etc)
|
||||
*
|
||||
* 'data'
|
||||
|
@ -115,9 +115,9 @@ Network.prototype._deletePeer = function(peerId) {
|
|||
this.connectedPeers = Network._arrayRemove(peerId, this.connectedPeers);
|
||||
};
|
||||
|
||||
Network.prototype._onClose = function(peerId) {
|
||||
this._deletePeer(peerId);
|
||||
this._notifyNetworkChange();
|
||||
Network.prototype._onClose = function(peerID) {
|
||||
this._deletePeer(peerID);
|
||||
this.emit('disconnect', peerID);
|
||||
};
|
||||
|
||||
Network.prototype.connectToCopayers = function(copayerIds) {
|
||||
|
@ -128,8 +128,7 @@ Network.prototype.connectToCopayers = function(copayerIds) {
|
|||
if (this.allowedCopayerIds && !this.allowedCopayerIds[copayerId]) {
|
||||
console.log('### IGNORING STRANGE COPAYER:', copayerId);
|
||||
this._deletePeer(this.peerFromCopayer(copayerId));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
console.log('### CONNECTING TO:', copayerId);
|
||||
self.connectTo(copayerId);
|
||||
}
|
||||
|
@ -148,6 +147,7 @@ Network.prototype._addConnectedCopayer = function(copayerId, isInbound) {
|
|||
var peerId = this.peerFromCopayer(copayerId);
|
||||
this._addCopayerMap(peerId,copayerId);
|
||||
Network._arrayPushOnce(peerId, this.connectedPeers);
|
||||
this.emit('connect', copayerId);
|
||||
};
|
||||
|
||||
Network.prototype._onData = function(encStr, isInbound, peerId) {
|
||||
|
@ -178,8 +178,6 @@ Network.prototype._onData = function(encStr, isInbound, peerId) {
|
|||
console.log('#### Peer sent hello. Setting it up.'); //TODO
|
||||
this._addConnectedCopayer(payload.copayerId, isInbound);
|
||||
this._setInboundPeerAuth(peerId, true);
|
||||
this._notifyNetworkChange( isInbound ? payload.copayerId : null);
|
||||
this.emit('open');
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -225,8 +223,8 @@ Network.prototype._setupConnectionHandlers = function(dataConn, toCopayerId) {
|
|||
|
||||
// The connecting peer send hello
|
||||
if(toCopayerId) {
|
||||
self._addConnectedCopayer(toCopayerId);
|
||||
self._sendHello(toCopayerId);
|
||||
self._addConnectedCopayer(toCopayerId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -251,10 +249,6 @@ Network.prototype._setupConnectionHandlers = function(dataConn, toCopayerId) {
|
|||
});
|
||||
};
|
||||
|
||||
Network.prototype._notifyNetworkChange = function(newCopayerId) {
|
||||
this.emit('networkChange', newCopayerId);
|
||||
};
|
||||
|
||||
Network.prototype._setupPeerHandlers = function(openCallback) {
|
||||
var self = this;
|
||||
var p = this.peer;
|
||||
|
@ -428,7 +422,7 @@ Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) {
|
|||
|
||||
this.allowedCopayerIds={};
|
||||
for(var i in allowedCopayerIdsArray) {
|
||||
this.allowedCopayerIds[ allowedCopayerIdsArray[i] ] = 1;
|
||||
this.allowedCopayerIds[ allowedCopayerIdsArray[i] ] = true;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -10,8 +10,6 @@ angular.module('copay.controllerUtils')
|
|||
var vi = $rootScope.videoInfo[copayer]
|
||||
if (!vi) return;
|
||||
|
||||
//alert($rootScope.wallet.getOnlinePeerIDs());
|
||||
//alert(copayer);
|
||||
if ($rootScope.wallet.getOnlinePeerIDs().indexOf(copayer) === -1) {
|
||||
// peer disconnected, remove his video
|
||||
delete $rootScope.videoInfo[copayer]
|
||||
|
@ -74,25 +72,25 @@ angular.module('copay.controllerUtils')
|
|||
message: 'Received wrong message from peer id:' + peerId
|
||||
};
|
||||
});
|
||||
w.on('created', function(myPeerID) {
|
||||
w.on('ready', function(myPeerID) {
|
||||
video.setOwnPeer(myPeerID, w, handlePeerVideo);
|
||||
$rootScope.wallet = w;
|
||||
$location.path('addresses');
|
||||
});
|
||||
w.on('refresh', function() {
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
$rootScope.$digest();
|
||||
});
|
||||
w.on('publicKeyRingUpdated', function() {
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
w.on('refresh', function() {
|
||||
root.updateBalance();
|
||||
$rootScope.$digest();
|
||||
});
|
||||
w.on('openError', root.onErrorDigest);
|
||||
w.on('peer', function(peerID) {
|
||||
video.callPeer(peerID, handlePeerVideo);
|
||||
w.on('connect', function(peerID) {
|
||||
if (peerID) {
|
||||
video.callPeer(peerID, handlePeerVideo);
|
||||
}
|
||||
$rootScope.$digest();
|
||||
});
|
||||
w.on('disconnect', function(peerID) {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
w.on('close', root.onErrorDigest);
|
||||
w.netStart();
|
||||
|
@ -111,6 +109,7 @@ angular.module('copay.controllerUtils')
|
|||
$rootScope.balanceByAddr = balanceByAddr;
|
||||
$rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString();
|
||||
$rootScope.loading = false;
|
||||
$rootScope.$digest();
|
||||
if (cb) cb();
|
||||
});
|
||||
w.getBalance(true, function(balance) {
|
||||
|
@ -134,9 +133,7 @@ angular.module('copay.controllerUtils')
|
|||
addrs.forEach(function(addr) {
|
||||
Socket.on(addr, function(txid) {
|
||||
console.log('Received!', txid);
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
root.updateBalance();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue