From 97882f8e3b99dc1b56732bd0b135b0591f44c380 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 31 Oct 2014 17:57:56 -0300 Subject: [PATCH 1/6] rm unnecessary save --- js/models/Identity.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index abae78376..a63db37c4 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -390,13 +390,6 @@ Identity.prototype.bindWallet = function(w) { w.on('txProposalEvent', function() { Identity.storeWalletDebounced(self, w); }); - w.on('ready', function() { - self.store({ - noWallets: true - }, function() { - Identity.storeWalletDebounced(self, w); - }); - }); w.on('addressBookUpdated', function() { Identity.storeWalletDebounced(self, w); }); From 96c886776225ee457479ebabfab67ad3dd1e77bf Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 31 Oct 2014 18:14:14 -0300 Subject: [PATCH 2/6] clean logs and remove unnecesary code --- js/models/Async.js | 6 +++--- js/models/Identity.js | 4 ++-- js/models/Insight.js | 3 +-- js/models/Wallet.js | 9 +++------ 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/js/models/Async.js b/js/models/Async.js index 1d31c01e8..0e9d0c522 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -202,12 +202,12 @@ Network.prototype._onMessage = function(enc) { this._deletePeer(sender); return; } - log.debug('Async: receiving ' + JSON.stringify(payload)); if (this.ignoreMessageFromTs && this.ignoreMessageFromTs === enc.ts) { - log.debug('Ignoring message from ', enc.ts); + log.debug('Ignoring trailing message. Ts:', enc.ts); return; } + log.debug('Async: receiving ' + JSON.stringify(payload)); var self = this; switch (payload.type) { @@ -352,7 +352,7 @@ Network.prototype.start = function(opts, openCallback) { }; Network.prototype.createSocket = function() { - log.debug('Async: Connecting to socket:', this.url, this.socketOptions); + log.debug('Async: Connecting to socket:', this.url); return io.connect(this.url, this.socketOptions); }; diff --git a/js/models/Identity.js b/js/models/Identity.js index a63db37c4..4d4ffb221 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -158,7 +158,7 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback return callback(error); } try { - log.debug('## OPENING Wallet: ' + walletId); + log.debug('## OPENING Wallet:', self.getName(), walletId); if (_.isString(walletData)) { walletData = JSON.parse(walletData); } @@ -376,7 +376,7 @@ Identity.prototype.bindWallet = function(w) { var self = this; self.wallets[w.getId()] = w; - log.debug('Binding wallet ' + w.getName()); + log.debug('Binding wallet:' + w.getName()); w.on('txProposalsUpdated', function() { Identity.storeWalletDebounced(self, w); diff --git a/js/models/Insight.js b/js/models/Insight.js index 39761a8de..2a07a56d7 100644 --- a/js/models/Insight.js +++ b/js/models/Insight.js @@ -124,7 +124,7 @@ Insight.prototype.subscribeToBlocks = function() { /** @private */ Insight.prototype._getSocketIO = function(url, opts) { - log.debug('Insight: Connecting to socket:', this.url, this.opts); + log.debug('Insight: Connecting to socket:', this.url); return io(this.url, this.opts); }; @@ -200,7 +200,6 @@ Insight.prototype.subscribe = function(addresses) { var self = this; function handlerFor(self, address) { -console.log('HANDLER [Insight.js.150:address:]',address); //TODO return function(txid) { // verify the address is still subscribed if (!self.subscribed[address]) return; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 1ecf098db..8ef0b9092 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -116,7 +116,8 @@ function Wallet(opts) { inherits(Wallet, events.EventEmitter); Wallet.prototype.emitAndKeepAlive = function(args) { - log.debug('Wallet Emitting:', arguments); + var args = Array.prototype.slice.call(arguments); + log.debug('Wallet Emitting:', args); this.keepAlive(); this.emit.apply(this, arguments); }; @@ -810,7 +811,7 @@ Wallet.prototype._setBlockchainListeners = function() { var self = this; self.blockchain.removeAllListeners(); - log.debug('Setting Blockchain listeners for', this.getId()); + log.debug('Setting Blockchain listeners for', this.getName()); self.blockchain.on('reconnect', function(attempts) { log.debug('Wallet:' + self.id + 'blockchain reconnect event'); self.emitAndKeepAlive('insightReconnected'); @@ -888,12 +889,8 @@ Wallet.prototype.netStart = function() { log.debug('Wallet:' + self.id + ' Starting networking: ' + startOpts.copayerId); net.start(startOpts, function() { - log.debug('Wallet:' + self.id + ' Networking ready:', net.copayerId); self._setBlockchainListeners(); self.emitAndKeepAlive('ready', net.getPeer()); - setTimeout(function() { - self._newAddresses(true); - }, 0); }); }; From 6e45aca51778f7cb4ff7c9ceab5a1d6978ffe8d9 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 31 Oct 2014 18:17:44 -0300 Subject: [PATCH 3/6] fix logs --- js/models/Identity.js | 2 +- js/models/Wallet.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index 4d4ffb221..7ba2eaeb9 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -158,7 +158,7 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback return callback(error); } try { - log.debug('## OPENING Wallet:', self.getName(), walletId); + log.info('## OPENING Wallet:', walletId); if (_.isString(walletData)) { walletData = JSON.parse(walletData); } diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 8ef0b9092..ac3f1981d 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -868,7 +868,7 @@ Wallet.prototype.netStart = function() { if (net.started) { - log.debug('Wallet:' + self.id + ' Wallet networking was ready') + log.debug('Wallet:' + self.getName() + ': Wallet networking was ready') self.emitAndKeepAlive('ready', net.getPeer()); return; } From a2465eca7428233e438afb6315245547d99297ac Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 31 Oct 2014 18:53:50 -0300 Subject: [PATCH 4/6] store --- js/models/Identity.js | 50 ++++++++--------- js/models/WalletLock.js | 116 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 25 deletions(-) create mode 100644 js/models/WalletLock.js diff --git a/js/models/Identity.js b/js/models/Identity.js index 7ba2eaeb9..5ef822fe5 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -209,7 +209,7 @@ Identity.prototype.storeWallet = function(wallet, cb) { * @param {Function} cb */ Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) { - identity.storeWallet(wallet,cb); + identity.storeWallet(wallet, cb); }, 3000); @@ -252,10 +252,13 @@ Identity.prototype.store = function(opts, cb) { storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) { if (err) return cb(err); + console.log('[Identity.js.255:opts:]', opts); //TODO if (opts.noWallets) return cb(); - async.map(self.wallets, self.storeWallet, cb); + async.each(_.values(self.wallets), function(wallet, in_cb) { + self.storeWallet(wallet, in_cb); + }, cb); }); }; @@ -312,11 +315,14 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) { if (!w) return cb(new Error('Could not decrypt')); this._checkVersion(w.version); - this.addWallet(w, function(err) { - if (err) return cb(err, null); - self.wallets[w.getId()] = w; - self.bindWallet(w); - self.store(null, function(err) { + this.addWallet(w); + self.bindWallet(w); + self.storeWallet(w, function() { + if (err) return cb(err); + + self.store({ + noWallets: true + }, function(err) { return cb(err, w); }); }); @@ -469,23 +475,24 @@ Identity.prototype.createWallet = function(opts, cb) { var self = this; var w = new walletClass(opts); - this.addWallet(w, function(err) { + this.addWallet(w); + self.bindWallet(w); + w.netStart(); + self.storeWallet(w, function() { if (err) return cb(err); - self.bindWallet(w); - w.netStart(); - return cb(err, w); + + self.store({ + noWallets: true + }, function(err) { + return cb(err, w); + }); }); }; -Identity.prototype.addWallet = function(wallet, cb) { +Identity.prototype.addWallet = function(wallet) { preconditions.checkArgument(wallet); preconditions.checkArgument(wallet.getId); - preconditions.checkArgument(cb); - this.wallets[wallet.getId()] = wallet; - - // TODO (eordano): Consider not saving automatically after this - this.storage.setItem(wallet.getStorageKey(), wallet.toObj(), cb); }; /** @@ -655,14 +662,7 @@ Identity.prototype.joinWallet = function(opts, cb) { err = 'walletFull'; } } - if (err) - return cb(err); - - self.store({ - noWallets: true - }, function(err) { - return cb(err, w); - }); + return cb(err, w); }); } }); diff --git a/js/models/WalletLock.js b/js/models/WalletLock.js new file mode 100644 index 000000000..77514d7e0 --- /dev/null +++ b/js/models/WalletLock.js @@ -0,0 +1,116 @@ +'use strict'; + +var preconditions = require('preconditions').singleton(); + +function WalletLock(storage, walletId, timeoutMin) { + preconditions.checkArgument(storage); + preconditions.checkArgument(walletId); + + this.storage = storage; + this.timeoutMin = timeoutMin || 5; + this.key = WalletLock._keyFor(walletId); +} + +WalletLock.prototype.getSessionId = function(cb) { + preconditions.checkArgument(cb); + var self = this; + + self.sessionStorage.getItem('sessionId', function(sessionId) { + if (sessionId) + return cb(sessionId); + + sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex'); + self.sessionStorage.setItem('sessionId', sessionId, function() { + return cb(sessionId); + }); + }); +}; + + +WalletLock.prototype.init = function(cb) { + preconditions.checkArgument(cb); + var self = this; + + self.storage.getSessionId(function(sid) { + preconditions.checkState(sid); + + self.sessionId = sid; + cb(); + }); +}; + +WalletLock._keyFor = function(walletId) { + return 'lock' + '::' + walletId; +}; + +WalletLock.prototype._isLockedByOther = function(cb) { + var self = this; + + this.storage.getGlobal(this.key, function(json) { + var wl = json ? JSON.parse(json) : null; + if (!wl || !wl.expireTs) + return cb(false); + + var expiredSince = Date.now() - wl.expireTs; + if (expiredSince >= 0) + return cb(false); + + var isMyself = wl.sessionId === self.sessionId; + + if (isMyself) + return cb(false); + + // Seconds remainding + return cb(parseInt(-expiredSince / 1000)); + }); +}; + + +WalletLock.prototype._setLock = function(cb) { + preconditions.checkArgument(cb); + preconditions.checkState(this.sessionId); + var self = this; + + this.storage.setGlobal(this.key, { + sessionId: this.sessionId, + expireTs: Date.now() + this.timeoutMin * 60 * 1000, + }, function() { + + cb(null); + }); +}; + + +WalletLock.prototype._doKeepAlive = function(cb) { + preconditions.checkArgument(cb); + preconditions.checkState(this.sessionId); + + var self = this; + + this._isLockedByOther(function(t) { + if (t) + return cb(new Error('LOCKED: Wallet is locked for ' + t + ' srcs')); + + self._setLock(cb); + }); +}; + + + +WalletLock.prototype.keepAlive = function(cb) { + var self = this; + + if (!self.sessionId) { + return self.init(self._doKeepAlive.bind(self, cb)); + }; + + return this._doKeepAlive(cb); +}; + + +WalletLock.prototype.release = function(cb) { + this.storage.removeGlobal(this.key, cb); +}; + + +module.exports = WalletLock; From 7578758e17e2cca5254b859af30874c6bcd125b1 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 31 Oct 2014 18:56:11 -0300 Subject: [PATCH 5/6] . --- js/models/Identity.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index 5ef822fe5..0a3caba25 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -317,7 +317,7 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) { this._checkVersion(w.version); this.addWallet(w); self.bindWallet(w); - self.storeWallet(w, function() { + self.storeWallet(w, function(err) { if (err) return cb(err); self.store({ @@ -478,7 +478,7 @@ Identity.prototype.createWallet = function(opts, cb) { this.addWallet(w); self.bindWallet(w); w.netStart(); - self.storeWallet(w, function() { + self.storeWallet(w, function(err) { if (err) return cb(err); self.store({ From 89562e9612a853beb0271b0b7dd978cf591c825f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 31 Oct 2014 19:11:16 -0300 Subject: [PATCH 6/6] fix tests --- test/Identity.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/Identity.js b/test/Identity.js index d19d4788a..0fb900f29 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -121,14 +121,15 @@ describe('Identity model', function() { }); describe('Identity.create()', function() { - it('should create', function() { + it('should create and store identity', function() { var args = createIdentity(); args.blockchain.on = sinon.stub(); - var old = Identity.prototype.createWallet; + sinon.stub(Identity.prototype, 'store').yields(null); Identity.create(args.params, function(err, iden) { should.not.exist(err); should.exist(iden); should.exist(iden.wallets); + Identity.prototype.store.calledOnce.should.be.true; }); }); });