mirror of https://github.com/BTCPrivate/copay.git
store
This commit is contained in:
parent
6e45aca517
commit
a2465eca74
|
@ -209,7 +209,7 @@ Identity.prototype.storeWallet = function(wallet, cb) {
|
||||||
* @param {Function} cb
|
* @param {Function} cb
|
||||||
*/
|
*/
|
||||||
Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) {
|
Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) {
|
||||||
identity.storeWallet(wallet,cb);
|
identity.storeWallet(wallet, cb);
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
|
|
||||||
|
@ -252,10 +252,13 @@ Identity.prototype.store = function(opts, cb) {
|
||||||
storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) {
|
storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
console.log('[Identity.js.255:opts:]', opts); //TODO
|
||||||
if (opts.noWallets)
|
if (opts.noWallets)
|
||||||
return cb();
|
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'));
|
if (!w) return cb(new Error('Could not decrypt'));
|
||||||
|
|
||||||
this._checkVersion(w.version);
|
this._checkVersion(w.version);
|
||||||
this.addWallet(w, function(err) {
|
this.addWallet(w);
|
||||||
if (err) return cb(err, null);
|
self.bindWallet(w);
|
||||||
self.wallets[w.getId()] = w;
|
self.storeWallet(w, function() {
|
||||||
self.bindWallet(w);
|
if (err) return cb(err);
|
||||||
self.store(null, function(err) {
|
|
||||||
|
self.store({
|
||||||
|
noWallets: true
|
||||||
|
}, function(err) {
|
||||||
return cb(err, w);
|
return cb(err, w);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -469,23 +475,24 @@ Identity.prototype.createWallet = function(opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var w = new walletClass(opts);
|
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);
|
if (err) return cb(err);
|
||||||
self.bindWallet(w);
|
|
||||||
w.netStart();
|
self.store({
|
||||||
return cb(err, w);
|
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);
|
||||||
preconditions.checkArgument(wallet.getId);
|
preconditions.checkArgument(wallet.getId);
|
||||||
preconditions.checkArgument(cb);
|
|
||||||
|
|
||||||
this.wallets[wallet.getId()] = wallet;
|
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';
|
err = 'walletFull';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (err)
|
return cb(err, w);
|
||||||
return cb(err);
|
|
||||||
|
|
||||||
self.store({
|
|
||||||
noWallets: true
|
|
||||||
}, function(err) {
|
|
||||||
return cb(err, w);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue