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

@ -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);
};