fix walletLock test

This commit is contained in:
Matias Alejo Garcia 2014-09-08 10:46:57 -03:00
commit f1e6d6cfc6
8 changed files with 269 additions and 153 deletions

View file

@ -99,17 +99,22 @@ Storage.prototype.setGlobal = function(k, v, cb) {
// remove value for key
Storage.prototype.removeGlobal = function(k, cb) {
preconditions.checkArgument(cb);
this.storage.removeItem(k, cb);
};
Storage.prototype.getSessionId = function() {
var sessionId = this.sessionStorage.getItem('sessionId');
if (!sessionId) {
Storage.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');
this.sessionStorage.setItem('sessionId', sessionId);
}
return sessionId;
self.sessionStorage.setItem('sessionId', sessionId, function(){
return cb(sessionId);
});
});
};
Storage.prototype._key = function(walletId, k) {
@ -117,14 +122,15 @@ Storage.prototype._key = function(walletId, k) {
};
// get value by key
Storage.prototype.get = function(walletId, k, cb) {
preconditions.checkArgument(walletId, k, cb);
this._read(this._key(walletId, k), cb);
};
Storage.prototype._readHelper = function(walletId, k, cb) {
var wk = this._key(walletId, k);
this._read(wk, function(v){
return cb(v,k);
this._read(wk, function(v) {
return cb(v, k);
});
};
@ -133,15 +139,15 @@ Storage.prototype.getMany = function(walletId, keys, cb) {
var self = this;
var ret = {};
console.log('[Storage.js.142:keys:]',keys); //TODO
var l = keys.length, i=0;
var l = keys.length,
i = 0;
for (var ii in keys) {
this._readHelper(walletId, keys[ii], function(v, k) {
ret[k] = v;
if (++i == l) {
return cb(ret);
return cb(ret);
}
});
}
@ -202,7 +208,8 @@ Storage.prototype.getWallets = function(cb) {
var self = this;
this.getWalletIds(function(ids) {
var l = ids.length, i=0;
var l = ids.length,
i = 0;
if (!l)
return cb([]);
@ -221,21 +228,29 @@ Storage.prototype.getWallets = function(cb) {
});
};
Storage.prototype.deleteWallet = function(walletId) {
Storage.prototype.deleteWallet = function(walletId, cb) {
preconditions.checkArgument(walletId);
preconditions.checkArgument(cb);
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
for (var i = 0; i < this.storage.length; i++) {
var key = this.storage.key(i);
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
this.storage.allKeys(function(allKeys) {
for (var key in allKeys) {
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
};
}
}
});
var l = toDelete.length,
i = 0;
for (var i in toDelete) {
this.removeGlobal(i);
this.removeGlobal(i, function() {
if (++i == l)
return cb();
});
}
};
@ -255,7 +270,6 @@ Storage.prototype.setFromObj = function(walletId, obj, cb) {
var l = Object.keys(obj).length,
i = 0;
for (var k in obj) {
console.log('[Storage.js.247]', k, i, l); //TODO
self.set(walletId, k, obj[k], function() {
if (++i == l) {
self.setName(walletId, obj.opts.name, cb);

View file

@ -182,6 +182,8 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) {
* @return {Wallet}
*/
WalletFactory.prototype.create = function(opts, cb) {
preconditions.checkArgument(cb);
opts = opts || {};
opts.networkName = opts.networkName || 'testnet';
@ -229,6 +231,7 @@ WalletFactory.prototype.create = function(opts, cb) {
opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version;
console.log('[WalletFactory.js.165]'); //TODO
var w = new Wallet(opts);
var self = this;
w.store(function() {
@ -266,6 +269,7 @@ WalletFactory.prototype._checkVersion = function(inVersion) {
* @return
*/
WalletFactory.prototype.open = function(walletId, passphrase, cb) {
preconditions.checkArgument(cb);
var self = this,
err;
self.storage._setPassphrase(passphrase);
@ -334,6 +338,7 @@ WalletFactory.prototype.decodeSecret = function(secret) {
* @param {walletCreationCallback} cb - a callback
*/
WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphrase, privateHex, cb) {
preconditions.checkArgument(cb);
var self = this;
var decodedSecret = this.decodeSecret(secret);
if (!decodedSecret || !decodedSecret.networkName || !decodedSecret.pubKey) {

View file

@ -6,13 +6,24 @@ function WalletLock(storage, walletId, timeoutMin) {
preconditions.checkArgument(storage);
preconditions.checkArgument(walletId);
this.sessionId = storage.getSessionId();
this.storage = storage;
this.timeoutMin = timeoutMin || 5;
this.key = WalletLock._keyFor(walletId);
// this._setLock(function() {});
}
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;
};
@ -22,18 +33,27 @@ WalletLock.prototype._isLockedByOther = function(cb) {
this.storage.getGlobal(this.key, function(json) {
var wl = json ? JSON.parse(json) : null;
var t = wl ? (Date.now() - wl.expireTs) : false;
// is not locked?
if (!wl || t > 0 || wl.sessionId === self.sessionId)
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(-t / 1000.));
return cb(parseInt(-expiredSince / 1000));
});
};
WalletLock.prototype._setLock = function(cb) {
preconditions.checkArgument(cb);
preconditions.checkState(this.sessionId);
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
@ -44,20 +64,33 @@ WalletLock.prototype._setLock = function(cb) {
};
WalletLock.prototype.keepAlive = function(cb) {
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('Wallet is already open. Close it to proceed or wait ' + t + ' seconds if you close it already'));
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);
};