diff --git a/js/models/Storage.js b/js/models/Storage.js index 3747971de..aa26ecbfb 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -330,12 +330,10 @@ Storage.prototype.getLastOpened = function(cb) { this.getGlobal('lastOpened', cb); }; -Storage.prototype.setFromObj = function(walletId, obj, cb) { +Storage.prototype.setFromObj = function(key, obj, cb) { + preconditions.checkArgument(key); preconditions.checkArgument(cb); - var self = this; - - var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : ''); - self._write(key, obj, function() { + this._write(key, obj, function() { return cb(); }); }; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index b9943a434..4477ef611 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -873,7 +873,10 @@ Wallet.prototype.keepAlive = function() { Wallet.prototype.store = function(cb) { var self = this; this.keepAlive(); - this.storage.setFromObj(this.id, this.toObj(), function(err) { + + var val = this.toObj(); + var key = 'wallet::' + this.id + ((val.opts && val.opts.name) ? '_' + obj.opts.name : ''); + this.storage.setFromObj(key, val, function(err) { log.debug('Wallet stored'); if (cb) cb(err); diff --git a/test/Storage.js b/test/Storage.js index a528044a8..e48de991d 100644 --- a/test/Storage.js +++ b/test/Storage.js @@ -177,8 +177,8 @@ describe('Storage model', function() { var w2 = { name: 'pepe' }; - s.setFromObj('1', w1, function() { - s.setFromObj('2', w2, function() { + s.setFromObj('wallet::1_wallet1', w1, function() { + s.setFromObj('wallet::2', w2, function() { s.getWallets2(function(ws) { ws[0].should.deep.equal({ id: '1', @@ -208,8 +208,8 @@ describe('Storage model', function() { name: 'pepe' }; - s.setFromObj('1', w1, function() { - s.setFromObj('2', w2, function() { + s.setFromObj('wallet::1_wallet1', w1, function() { + s.setFromObj('wallet::2', w2, function() { s._write('3::name', 'matias', function() { s._write('1::name', 'juan', function() { s.setGlobal('nameFor::3', 'wallet3', function() { @@ -280,8 +280,8 @@ describe('Storage model', function() { name: 'pepe' }; - s.setFromObj('1', w1, function() { - s.setFromObj('2', w2, function() { + s.setFromObj('wallet::1', w1, function() { + s.setFromObj('wallet::2', w2, function() { s.deleteWallet('3', function(err) { err.toString().should.include('WNOTFOUND'); done(); @@ -301,8 +301,8 @@ describe('Storage model', function() { name: 'pepe' }; - s.setFromObj('1', w1, function() { - s.setFromObj('2', w2, function() { + s.setFromObj('wallet::1', w1, function() { + s.setFromObj('wallet::2', w2, function() { s.deleteWallet('1', function(err) { should.not.exist(err); s.getWallets2(function(ws) { @@ -372,7 +372,7 @@ describe('Storage model', function() { describe('#setFromObj', function() { it('should store from an object as single key', function(done) { - s.setFromObj('id1', { + s.setFromObj('wallet::id1_nameid1', { 'key': 'val', 'opts': { 'name': 'nameid1'