Merge pull request #45 from eordano/fix/storageDontOverride

Storage: dont override if an item aready exists
This commit is contained in:
Matias Alejo Garcia 2014-10-28 18:23:09 -03:00
commit 1a53c0c504
6 changed files with 57 additions and 17 deletions

View file

@ -231,7 +231,9 @@ Identity.prototype.store = function(opts, cb) {
var self = this; var self = this;
opts = opts || {}; opts = opts || {};
self.storage.setItem(this.getId(), this.toObj(), function(err) { var storeFunction = opts.failIfExists ? self.storage.createItem : self.storage.setItem;
storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) {
if (err) return cb(err); if (err) return cb(err);
if (opts.noWallets) if (opts.noWallets)
@ -349,7 +351,7 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
iden.store(function(err) { iden.store(null, function(err) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
@ -402,10 +404,8 @@ Identity.prototype.bindWallet = function(w) {
* @param {PublicKeyRing=} opts.publicKeyRing * @param {PublicKeyRing=} opts.publicKeyRing
* @param {string} opts.nickname * @param {string} opts.nickname
* @param {string} opts.password * @param {string} opts.password
* @TODO: Figure out what is this parameter * @param {boolean} opts.spendUnconfirmed this.walletDefaults.spendUnconfirmed
* @param {?} opts.spendUnconfirmed this.walletDefaults.spendUnconfirmed ?? * @param {number} opts.reconnectDelay time in milliseconds
* @TODO: Figure out in what unit is this reconnect delay.
* @param {number} opts.reconnectDelay milliseconds?
* @param {number=} opts.version * @param {number=} opts.version
* @param {callback} opts.version * @param {callback} opts.version
* @return {Wallet} * @return {Wallet}
@ -467,11 +467,7 @@ Identity.prototype.createWallet = function(opts, cb) {
if (err) return cb(err); if (err) return cb(err);
self.bindWallet(w); self.bindWallet(w);
w.netStart(); w.netStart();
self.store({ return cb(err, w);
noWallets: true
}, function(err) {
return cb(err, w);
});
}); });
}; };
@ -539,7 +535,7 @@ Identity.prototype.deleteWallet = function(walletId, cb) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
self.store(cb); self.store(null, cb);
}); });
}; };
@ -654,7 +650,11 @@ Identity.prototype.joinWallet = function(opts, cb) {
err = 'walletFull'; err = 'walletFull';
} }
} }
return cb(err, w); self.store({
noWallets: true
}, function(err) {
return cb(err, w);
});
}); });
} }
}); });

View file

@ -8,9 +8,12 @@ function EncryptedInsightStorage(config) {
inherits(EncryptedInsightStorage, InsightStorage); inherits(EncryptedInsightStorage, InsightStorage);
EncryptedInsightStorage.prototype.getItem = function(name, callback) { EncryptedInsightStorage.prototype.getItem = function(name, callback) {
var key = cryptoUtil.kdfbinary(this.password + this.email); var key = cryptoUtil.kdf(this.password + this.email);
InsightStorage.prototype.getItem.apply(this, [name, InsightStorage.prototype.getItem.apply(this, [name,
function(err, body) { function(err, body) {
if (err) {
return callback(err);
}
var decryptedJson = cryptoUtil.decrypt(key, body); var decryptedJson = cryptoUtil.decrypt(key, body);
if (!decryptedJson) { if (!decryptedJson) {
return callback('Internal Error'); return callback('Internal Error');
@ -21,13 +24,13 @@ EncryptedInsightStorage.prototype.getItem = function(name, callback) {
}; };
EncryptedInsightStorage.prototype.setItem = function(name, value, callback) { EncryptedInsightStorage.prototype.setItem = function(name, value, callback) {
var key = cryptoUtil.kdfbinary(this.password + this.email); var key = cryptoUtil.kdf(this.password + this.email);
var record = cryptoUtil.encrypt(key, value); var record = cryptoUtil.encrypt(key, value);
InsightStorage.prototype.setItem.apply(this, [name, record, callback]); InsightStorage.prototype.setItem.apply(this, [name, record, callback]);
}; };
EncryptedInsightStorage.prototype.removeItem = function(name, callback) { EncryptedInsightStorage.prototype.removeItem = function(name, callback) {
var key = cryptoUtil.kdfbinary(this.password + this.email); var key = cryptoUtil.kdf(this.password + this.email);
InsightStorage.prototype.removeItem.apply(this, [name, callback]); InsightStorage.prototype.removeItem.apply(this, [name, callback]);
}; };

View file

@ -95,6 +95,16 @@ GoogleDrive.prototype._httpGet = function(theUrl) {
return xmlHttp.responseText; return xmlHttp.responseText;
} }
GoogleDrive.prototype.createItem = function(name, value, callback) {
this.getItem(name, function(err, retrieved) {
if (err || !retrieved) {
return this.setItem(name, value, callback);
} else {
return callback('EEXISTS');
}
});
};
GoogleDrive.prototype.getItem = function(k, cb) { GoogleDrive.prototype.getItem = function(k, cb) {
//console.log('[googleDrive.js.95:getItem:]', k); //TODO //console.log('[googleDrive.js.95:getItem:]', k); //TODO
var self = this; var self = this;

View file

@ -16,6 +16,17 @@ InsightStorage.prototype.setCredentials = function(email, password, opts) {
this.password = password; this.password = password;
}; };
InsightStorage.prototype.createItem = function(name, value, callback) {
var self = this;
this.getItem(name, function(err, retrieved) {
if (err || !retrieved) {
return self.setItem(name, value, callback);
} else {
return callback('EEXISTS');
}
});
};
InsightStorage.prototype.getItem = function(name, callback) { InsightStorage.prototype.getItem = function(name, callback) {
var key = cryptoUtil.kdf(this.password + this.email); var key = cryptoUtil.kdf(this.password + this.email);
var secret = cryptoUtil.kdf(key, this.password); var secret = cryptoUtil.kdf(key, this.password);

View file

@ -16,6 +16,16 @@ LocalStorage.prototype.getItem = function(k,cb) {
return cb(null, localStorage.getItem(k)); return cb(null, localStorage.getItem(k));
}; };
/**
* Same as setItem, but fails if an item already exists
*/
LocalStorage.prototype.createItem = function(name, value, callback) {
if (localStorage.getItem(name)) {
return callback('EEXISTS');
}
return this.setItem(name, value, callback);
};
LocalStorage.prototype.setItem = function(k,v,cb) { LocalStorage.prototype.setItem = function(k,v,cb) {
localStorage.setItem(k,v); localStorage.setItem(k,v);
return cb(); return cb();

View file

@ -29,7 +29,13 @@ angular.module('copayApp.services')
controllerUtils.onErrorDigest( controllerUtils.onErrorDigest(
scope, 'Could not create default wallet'); scope, 'Could not create default wallet');
} else { } else {
controllerUtils.bindProfile(scope, iden, wallet.id); iden.store({failIfExists: true}, function(err) {
if (err) {
controllerUtils.onErrorDigest(scope, 'User already exists!');
} else {
controllerUtils.bindProfile(scope, iden, wallet.id);
}
});
} }
scope.loading = false; scope.loading = false;
}); });