diff --git a/js/models/Profile.js b/js/models/Profile.js index 367af685d..22747ca29 100644 --- a/js/models/Profile.js +++ b/js/models/Profile.js @@ -45,7 +45,7 @@ Profile.create = function(email, password, storage, cb) { Profile.any = function(storage, cb) { - storage.getFirst(Profile.key(''), function(err, v, k) { + storage.getFirst(Profile.key(''), { onlyKey: true}, function(err, v, k) { return cb(k ? true : false); }); }; diff --git a/js/models/Storage.js b/js/models/Storage.js index e0f771e85..0927c51be 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -175,13 +175,21 @@ Storage.prototype.get = function(key, cb) { }) }; -Storage.prototype.getFirst = function(prefix, cb) { +Storage.prototype.getFirst = function(prefix, opts, cb) { + opts = opts || {}; + var self = this; this.db.allKeys(function(allKeys) { var keys = _.filter(allKeys, function(k) { if ((k === prefix) || k.indexOf(prefix) === 0) return true; }); - if (keys.length === 0) return cb(new Error('not found')); + + if (keys.length === 0) + return cb(new Error('not found')); + + if (opts.onlyKey) + return cb(null, null, keys[0]); + self._read(keys[0], function(v) { if (_.isNull(v)) return cb(new Error('Could not decrypt data'), null, keys[0]); return cb(null, v, keys[0]); @@ -206,7 +214,7 @@ Storage.prototype.delete = function(key, cb) { Storage.prototype.deletePrefix = function(prefix, cb) { var self = this; - this.getFirst(prefix, function(err, v, k) { + this.getFirst(prefix, {}, function(err, v, k) { if (err || !v) return cb(err); self.delete(k, function(err) { diff --git a/js/models/Wallet.js b/js/models/Wallet.js index bc86c2de1..7e01394dd 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -175,7 +175,7 @@ Wallet.key = function(str) { Wallet.any = function(storage, cb) { - storage.getFirst(Wallet.key(''), function(err, v, k) { + storage.getFirst(Wallet.key(''), { onlyKey: true}, function(err, v, k) { return cb(k ? true : false); }); }; @@ -259,7 +259,7 @@ Wallet.read = function(walletId, readOpts, cb) { err; var obj = {}; - storage.getFirst(Wallet.key(walletId), function(err, ret) { + storage.getFirst(Wallet.key(walletId), {}, function(err, ret) { if (err) return cb(err); if (!ret) diff --git a/test/Storage.js b/test/Storage.js index a9545cfd7..33bf30d75 100644 --- a/test/Storage.js +++ b/test/Storage.js @@ -347,7 +347,7 @@ describe('Storage model', function() { sinon.stub(s, '_read', function(k, cb) { return cb(data[k]); }); - s.getFirst('wallet::id1', function(err, w) { + s.getFirst('wallet::id1', {}, function(err, w) { should.not.exist(err); w.should.exist; w.hasOwnProperty('a').should.be.true;