Merge pull request #460 from yemel/feature/reconnect-session

Feature/reconnect session
This commit is contained in:
Mario Colque 2014-05-23 22:31:19 +03:00
commit d02bd2c11a
5 changed files with 2682 additions and 2 deletions

View File

@ -730,7 +730,7 @@ on supported browsers please check <a href="http://www.webrtc.org/">http://www.w
<script src="lib/angular-route/angular-route.min.js"></script>
<script src="lib/angular-foundation/mm-foundation.min.js"></script>
<script src="lib/angular-foundation/mm-foundation-tpls.min.js"></script>
<script src="lib/peerjs/peer.js"></script>
<script src="lib/peer.js"></script> <!-- TODO Change this on new PeerJS version -->
<script src="lib/bitcore/browser/bundle.js"></script>
<script src="lib/crypto-js/rollups/sha256.js"></script>
<script src="lib/crypto-js/rollups/pbkdf2.js"></script>

View File

@ -83,7 +83,7 @@ angular.module('copay.signin').controller('SigninController',
};
function installStartupHandlers(wallet) {
wallet.network.on('error', function(err) {
wallet.on('connectionError', function(err) {
$scope.failure = true;
});
wallet.on('ready', function() {

View File

@ -32,6 +32,12 @@ function Wallet(opts) {
this.name = opts.name;
this.netKey = opts.netKey || SecureRandom.getRandomBuffer(8).toString('base64');
// Renew token every 24hs
if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) {
this.token = opts.token;
this.tokenTime = opts.tokenTime;
}
this.verbose = opts.verbose;
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
@ -166,6 +172,11 @@ Wallet.prototype._optsToObj = function() {
version: this.version,
};
if (this.token){
obj.token = this.token;
obj.tokenTime = new Date().getTime();
}
return obj;
};
@ -214,6 +225,9 @@ Wallet.prototype.netStart = function() {
self.log('[Wallet.js.132:openError:] GOT openError'); //TODO
self.emit('openError');
});
net.on('error', function(){
self.emit('connectionError'); // Bubble the error
});
net.on('close', function() {
self.emit('close');
});
@ -221,6 +235,7 @@ Wallet.prototype.netStart = function() {
var myId = self.getMyCopayerId();
var startOpts = {
copayerId: myId,
token: self.token,
maxPeers: self.totalCopayers,
netKey: this.netKey,
};
@ -230,6 +245,7 @@ Wallet.prototype.netStart = function() {
}
net.start(startOpts, function() {
self.emit('ready', net.getPeer());
self.token = net.peer.options.token;
setTimeout(function(){
console.log('[EMIT publicKeyRingUpdated:]'); //TODO
self.emit('publicKeyRingUpdated', true);
@ -237,6 +253,7 @@ Wallet.prototype.netStart = function() {
self.connectToAll();
console.log('[EMIT TxProposal]'); //TODO
self.emit('txProposalsUpdated');
self.store();
},10);
});
};
@ -305,6 +322,8 @@ Wallet.fromObj = function(o, storage, network, blockchain) {
Wallet.prototype.toEncryptedObj = function() {
var walletObj = this.toObj();
delete walletObj.opts.token;
delete walletObj.opts.tokenTime;
return this.storage.export(walletObj);
};

View File

@ -3,6 +3,7 @@ var imports = require('soop').imports();
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
var bitcore = require('bitcore');
var util = bitcore.util;
var extend = require('util')._extend;
/*
* Emits
* 'connect'
@ -327,6 +328,9 @@ Network.prototype.start = function(opts, openCallback) {
this.netKey = opts.netKey;
this.maxPeers = opts.maxPeers || this.maxPeers;
if (opts.token)
this.opts.token = opts.token;
if (!this.copayerId)
this.setCopayerId(opts.copayerId);

2657
lib/peer.js Normal file

File diff suppressed because it is too large Load Diff