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