From 0f37ea1929b994b1abc98ee925314102e5671063 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 23 Sep 2014 10:16:26 -0300 Subject: [PATCH 1/4] Added Storage#readWallet and used this from WalletFactory --- js/models/Storage.js | 17 +++++ js/models/WalletFactory.js | 8 +-- test/test.Storage.js | 137 ++++++++++++++++++++++--------------- test/test.WalletFactory.js | 6 +- 4 files changed, 105 insertions(+), 63 deletions(-) diff --git a/js/models/Storage.js b/js/models/Storage.js index 35cf1f976..39f6bd07f 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -140,6 +140,23 @@ Storage.prototype._readHelper = function(walletId, k, cb) { }); }; +Storage.prototype.readWallet = function(walletId, cb) { + var self = this; + this.storage.allKeys(function(allKeys) { + var obj = {}; + var keys = _.filter(allKeys, function(k) { + if (k.indexOf(walletId + '::') === 0) return true; + }); + var count = keys.length; + _.each(keys, function(k) { + self._read(k, function(v) { + obj[k.split('::')[1]] = v; + if (--count === 0) return cb(obj); + }) + }); + }); +}; + Storage.prototype.getMany = function(walletId, keys, cb) { preconditions.checkArgument(cb); diff --git a/js/models/WalletFactory.js b/js/models/WalletFactory.js index 7b3d4d103..55216f968 100644 --- a/js/models/WalletFactory.js +++ b/js/models/WalletFactory.js @@ -153,10 +153,10 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) { err; var obj = {}; - this.storage.getMany(walletId, Wallet.PERSISTED_PROPERTIES, function(ret) { - for (var ii in ret) { - obj[ii] = ret[ii]; - } + this.storage.readWallet(walletId, function(ret) { + _.each(Wallet.PERSISTED_PROPERTIES, function(p) { + obj[p] = ret[p]; + }); if (!_.any(_.values(obj))) return cb(new Error('Wallet not found')); diff --git a/test/test.Storage.js b/test/test.Storage.js index 559dae141..03f5e7916 100644 --- a/test/test.Storage.js +++ b/test/test.Storage.js @@ -1,6 +1,7 @@ 'use strict'; var chai = chai || require('chai'); var sinon = require('sinon'); +var _ = require('underscore'); var should = chai.should(); var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; var copay = copay || require('../copay'); @@ -11,65 +12,65 @@ var timeStamp = Date.now(); describe('Storage model', function() { - var s; - beforeEach(function() { - s = new Storage(require('./mocks/FakeLocalStorage').storageParams); - s.setPassphrase('mysupercoolpassword'); - s.storage.clear(); - s.sessionStorage.clear(); - }); + var s; + beforeEach(function() { + s = new Storage(require('./mocks/FakeLocalStorage').storageParams); + s.setPassphrase('mysupercoolpassword'); + s.storage.clear(); + s.sessionStorage.clear(); + }); - it('should create an instance', function() { - var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams); - should.exist(s2); + it('should create an instance', function() { + var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams); + should.exist(s2); + }); + it('should fail when encrypting without a password', function() { + var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams); + (function() { + s2.set(fakeWallet, timeStamp, 1, function() {}); + }).should.throw('NOPASSPHRASE'); + }); + it('should be able to encrypt and decrypt', function(done) { + s._write(fakeWallet + timeStamp, 'value', function() { + s._read(fakeWallet + timeStamp, function(v) { + v.should.equal('value'); + done(); + }); }); - it('should fail when encrypting without a password', function() { - var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams); - (function() { - s2.set(fakeWallet, timeStamp, 1, function() {}); - }).should.throw('NOPASSPHRASE'); + }); + it('should be able to set a value', function(done) { + s.set(fakeWallet, timeStamp, 1, function() { + done(); }); - it('should be able to encrypt and decrypt', function(done) { - s._write(fakeWallet + timeStamp, 'value', function() { - s._read(fakeWallet + timeStamp, function(v) { - v.should.equal('value'); + }); + var getSetData = [ + 1, 1000, -15, -1000, + 0.1, -0.5, -0.5e-10, Math.PI, + 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', + '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { + x: 1, + y: 2 + }, { + x: 'hi', + y: null + }, { + a: {}, + b: [], + c: [1, 2, 'hi'] + }, + null + ]; + getSetData.forEach(function(obj) { + it('should be able to set a value and get it for ' + JSON.stringify(obj), function(done) { + s.set(fakeWallet, timeStamp, obj, function() { + s.get(fakeWallet, timeStamp, function(obj2) { + JSON.stringify(obj2).should.equal(JSON.stringify(obj)); done(); }); }); }); - it('should be able to set a value', function(done) { - s.set(fakeWallet, timeStamp, 1, function() { - done(); - }); - }); - var getSetData = [ - 1, 1000, -15, -1000, - 0.1, -0.5, -0.5e-10, Math.PI, - 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', - '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { - x: 1, - y: 2 - }, { - x: 'hi', - y: null - }, { - a: {}, - b: [], - c: [1, 2, 'hi'] - }, - null - ]; - getSetData.forEach(function(obj) { - it('should be able to set a value and get it for ' + JSON.stringify(obj), function(done) { - s.set(fakeWallet, timeStamp, obj, function() { - s.get(fakeWallet, timeStamp, function(obj2) { - JSON.stringify(obj2).should.equal(JSON.stringify(obj)); - done(); - }); - }); - }); - }); + }); describe('#export', function() { it('should export the encrypted wallet', function(done) { @@ -177,9 +178,9 @@ describe('Storage model', function() { var orig = s.getName.bind(s); s.getName = function(wid, cb) { - setTimeout(function() { + setTimeout(function() { orig(wid, cb); - },1); + }, 1); }; s.getWallets(function(ws) { @@ -197,8 +198,8 @@ describe('Storage model', function() { }); }); }); - }); - + }); + describe('#deleteWallet', function() { it('should fail to delete a unexisting wallet', function(done) { s.set('1', "hola", 'juan', function() { @@ -230,6 +231,30 @@ describe('Storage model', function() { }); }); + describe('#readWallet', function() { + it('should read wallet', function(done) { + var data = { + 'id1::a': 'x', + 'id1::b': 'y', + 'id2::c': 'z', + }; + s.storage.allKeys = sinon.stub().yields(_.keys(data)); + sinon.stub(s, '_read', function(k, cb) { + return cb(data[k]); + }); + s.readWallet('id1', function(w) { + w.should.exist; + w.hasOwnProperty('a').should.be.true; + w.hasOwnProperty('b').should.be.true; + w.hasOwnProperty('c').should.be.false; + w.a.should.equal('x'); + w.b.should.equal('y'); + s._read.restore(); + done(); + }); + }); + }); + describe('#setFromObj', function() { it('set localstorage from an object', function(done) { s.setFromObj('id1', { @@ -286,7 +311,7 @@ describe('Storage model', function() { var wo = s.import(encryptedLegacy1); should.not.exist(wo); }); - + it('should be able to decrypt an old backup', function() { s.setPassphrase(legacyPassword1); var wo = s.import(encryptedLegacy1); diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index ff30c525c..63a392ac0 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -246,7 +246,7 @@ describe('WalletFactory model', function() { describe('#read', function() { it('should fail to read unexisting wallet', function(done) { - wf.storage.getMany = sinon.stub().yields({}); + wf.storage.readWallet = sinon.stub().yields({}); wf.read('id', [], function(err, w) { should.not.exist(w); @@ -258,7 +258,7 @@ describe('WalletFactory model', function() { }); }); it('should fail to read broken wallet', function(done) { - wf.storage.getMany = sinon.stub().yields({ + wf.storage.readWallet = sinon.stub().yields({ 'opts': 1 }); wf.read('id', [], function(err, w) { @@ -272,7 +272,7 @@ describe('WalletFactory model', function() { }); it('should read existing wallet', function(done) { var wf = new WalletFactory(config, '0.0.1'); - wf.storage.getMany = sinon.stub().yields({ + wf.storage.readWallet = sinon.stub().yields({ 'opts': 1 }); wf.fromObj = sinon.stub().returns('ok'); From cd3f2b8b90f12bc9661d3f74650cc990cbc37ce6 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 23 Sep 2014 14:54:09 -0300 Subject: [PATCH 2/4] Added methods for storing/reading from sinlge key-value pair --- js/models/Storage.js | 68 +++++++++++++++++++- js/models/WalletFactory.js | 48 ++++++++++++-- test/test.Storage.js | 126 ++++++++++++++++++++++++++++++++++++- test/test.WalletFactory.js | 1 - 4 files changed, 231 insertions(+), 12 deletions(-) diff --git a/js/models/Storage.js b/js/models/Storage.js index 39f6bd07f..9b5fb22f1 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -94,7 +94,6 @@ Storage.prototype.getGlobal = function(k, cb) { // set value for key Storage.prototype.setGlobal = function(k, v, cb) { preconditions.checkArgument(cb); - this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v, cb); }; @@ -147,6 +146,7 @@ Storage.prototype.readWallet = function(walletId, cb) { var keys = _.filter(allKeys, function(k) { if (k.indexOf(walletId + '::') === 0) return true; }); + if (keys.length === 0) return cb(null); var count = keys.length; _.each(keys, function(k) { self._read(k, function(v) { @@ -157,6 +157,19 @@ Storage.prototype.readWallet = function(walletId, cb) { }); }; +Storage.prototype.readWallet2 = function(walletId, cb) { + var self = this; + this.storage.allKeys(function(allKeys) { + var keys = _.filter(allKeys, function(k) { + if ((k === 'wallet::' + walletId) || k.indexOf('wallet::' + walletId) === 0) return true; + }); + if (keys.length !== 1) throw new Error('WALLETNOTFOUND'); + self._read(keys[0], function(v) { + return cb(v); + }) + }); +}; + Storage.prototype.getMany = function(walletId, keys, cb) { preconditions.checkArgument(cb); @@ -256,6 +269,28 @@ Storage.prototype.getWallets = function(cb) { }); }; +Storage.prototype.getWallets2 = function(cb) { + var self = this; + var re = /wallet::([^_]+)(_?(.*))/; + + this.storage.allKeys(function(allKeys) { + var wallets = _.compact(_.map(allKeys, function(key) { + if (key.indexOf('wallet::') !== 0) + return null; + var match = key.match(re); + if (match.length != 4) + return null; + return { + id: match[1], + name: match[3] ? match[3] : undefined, + }; + })); + + return cb(wallets); + }); +}; + + Storage.prototype.deleteWallet = function(walletId, cb) { preconditions.checkArgument(walletId); preconditions.checkArgument(cb); @@ -288,17 +323,32 @@ Storage.prototype.deleteWallet = function(walletId, cb) { } }); +}; +Storage.prototype.deleteWallet2 = function(walletId, cb) { + preconditions.checkArgument(walletId); + preconditions.checkArgument(cb); + var self = this; + this.getWallets2(function(wallets) { + var w = _.findWhere(wallets, { + id: walletId + }); + if (!w) + return cb(new Error('WNOTFOUND: Wallet not found')); + self.removeGlobal('wallet::' + walletId + (w.name ? '_' + w.name : ''), function() { + return cb(); + }); + }); }; Storage.prototype.setLastOpened = function(walletId, cb) { this.setGlobal('lastOpened', walletId, cb); -} +}; Storage.prototype.getLastOpened = function(cb) { this.getGlobal('lastOpened', cb); -} +}; //obj contains keys to be set Storage.prototype.setFromObj = function(walletId, obj, cb) { @@ -319,6 +369,17 @@ Storage.prototype.setFromObj = function(walletId, obj, cb) { } }; +Storage.prototype.setFromObj2 = function(walletId, obj, cb) { + preconditions.checkArgument(cb); + var self = this; + + var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : ''); + self._write(key, obj, function() { + return cb(); + }); +}; + + // remove all values Storage.prototype.clearAll = function(cb) { this.storage.clear(cb); @@ -334,4 +395,5 @@ Storage.prototype.export = function(obj) { return this._encrypt(string); }; + module.exports = Storage; diff --git a/js/models/WalletFactory.js b/js/models/WalletFactory.js index 55216f968..b2b62e35b 100644 --- a/js/models/WalletFactory.js +++ b/js/models/WalletFactory.js @@ -142,6 +142,39 @@ WalletFactory.prototype.import = function(base64, passphrase, skipFields) { return w; }; +WalletFactory.prototype.migrateWallet = function(walletId, passphrase, cb) { + var self = this; + + self.storage.setPassphrase(passphrase); + self.storage.setFromObj = self.storage.setFromObj2; + + var reconfigureStorage = function() { + self.storage.getWallets = self.storage.getWallets2; + self.storage.readWallet = self.storage.readWallet2; + self.storage.deleteWallet = self.storage.deleteWallet2; + self.storage.get = function() { + throw 'DO NOT USE' + }; + self.storage.set = function() { + throw 'DO NOT USE' + }; + }; + + self.read(walletId, null, function(err, wallet) { + if (err) return cb(err); + + wallet.store(); + self.storage.deleteWallet(walletId, function() { + self.storage.removeGlobal('nameFor::' + walletId, function() { + reconfigureStorage(); + return cb(); + }); + }); + }); + +}; + + /** * @desc Retrieve a wallet from storage * @param {string} walletId - the wallet id @@ -153,7 +186,9 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) { err; var obj = {}; + console.log('aáaa'); this.storage.readWallet(walletId, function(ret) { + console.log('bbb'); _.each(Wallet.PERSISTED_PROPERTIES, function(p) { obj[p] = ret[p]; }); @@ -301,12 +336,15 @@ WalletFactory.prototype.open = function(walletId, passphrase, cb) { preconditions.checkArgument(cb); var self = this; self.storage.setPassphrase(passphrase); - self.read(walletId, null, function(err, w) { - if (err) return cb(err); - w.store(function(err) { - self.storage.setLastOpened(walletId, function() { - return cb(err, w); + self.migrateWallet(walletId, passphrase, function() { + self.read(walletId, null, function(err, w) { + if (err) return cb(err); + + w.store(function(err) { + self.storage.setLastOpened(walletId, function() { + return cb(err, w); + }); }); }); }); diff --git a/test/test.Storage.js b/test/test.Storage.js index 03f5e7916..be1dbafce 100644 --- a/test/test.Storage.js +++ b/test/test.Storage.js @@ -200,6 +200,35 @@ describe('Storage model', function() { }); }); + describe('#getWallets2', function() { + it('should retreive wallets from storage', function(done) { + var w1 = { + name: 'juan', + opts: { + name: 'wallet1' + } + }; + var w2 = { + name: 'pepe' + }; + s.setFromObj2('1', w1, function() { + s.setFromObj2('2', w2, function() { + s.getWallets2(function(ws) { + ws[0].should.deep.equal({ + id: '1', + name: 'wallet1', + }); + ws[1].should.deep.equal({ + id: '2', + name: undefined + }); + done(); + }); + }); + }); + }); + }); + describe('#deleteWallet', function() { it('should fail to delete a unexisting wallet', function(done) { s.set('1', "hola", 'juan', function() { @@ -231,6 +260,54 @@ describe('Storage model', function() { }); }); + describe('#deleteWallet2', function() { + it('should fail to delete a unexisting wallet', function(done) { + var w1 = { + name: 'juan', + opts: { + name: 'wallet1' + } + }; + var w2 = { + name: 'pepe' + }; + + s.setFromObj2('1', w1, function() { + s.setFromObj2('2', w2, function() { + s.deleteWallet2('3', function(err) { + err.toString().should.include('WNOTFOUND'); + done(); + }); + }); + }); + }); + + it('should delete a wallet', function(done) { + var w1 = { + name: 'juan', + opts: { + name: 'wallet1' + } + }; + var w2 = { + name: 'pepe' + }; + + s.setFromObj2('1', w1, function() { + s.setFromObj2('2', w2, function() { + s.deleteWallet2('1', function(err) { + should.not.exist(err); + s.getWallets2(function(ws) { + ws.length.should.equal(1); + ws[0].id.should.equal('2'); + done(); + }); + }); + }); + }); + }); + }); + describe('#readWallet', function() { it('should read wallet', function(done) { var data = { @@ -255,6 +332,34 @@ describe('Storage model', function() { }); }); + describe('#readWallet2', function() { + it('should read wallet', function(done) { + var data = { + 'wallet::id1_wallet1': { + a: 'x', + b: 'y' + }, + 'wallet::id2': { + c: 'z' + }, + }; + s.storage.allKeys = sinon.stub().yields(_.keys(data)); + sinon.stub(s, '_read', function(k, cb) { + return cb(data[k]); + }); + s.readWallet2('id1', function(w) { + w.should.exist; + w.hasOwnProperty('a').should.be.true; + w.hasOwnProperty('b').should.be.true; + w.hasOwnProperty('c').should.be.false; + w.a.should.equal('x'); + w.b.should.equal('y'); + s._read.restore(); + done(); + }); + }); + }); + describe('#setFromObj', function() { it('set localstorage from an object', function(done) { s.setFromObj('id1', { @@ -271,6 +376,24 @@ describe('Storage model', function() { }); }); + describe('#setFromObj2', function() { + it('should store from an object as single key', function(done) { + s.setFromObj2('id1', { + 'key': 'val', + 'opts': { + 'name': 'nameid1' + }, + }, function() { + s._read('wallet::id1_nameid1', function(r) { + r.should.exist; + r.key.should.exist; + r.key.should.equal('val'); + r.opts.name.should.equal('nameid1'); + done(); + }); + }); + }); + }); describe('#globals', function() { it('should set, get and remove keys', function(done) { @@ -335,11 +458,8 @@ describe('Storage model', function() { wo.publicKeyRing.copayersExtPubKeys[0].should.equal('tpubD9SGoP7CXsqSKTiQxCZSCpicDcophqnE4yuqjfw5M9tAR3fSjT9GDGwPEUFCN7SSmRKGDLZgKQePYFaLWyK32akeSan45TNTd8sgef9Ymh6'); wo.privateKey.extendedPrivateKeyString.should.equal('tprv8ZgxMBicQKsPfQCscb7CtJKzixxcVSyrCVcfr3WCFbtT8kYTzNubhjQ5R7AuYJgPCcSH4R8T34YVxeohKGhAB9wbB4eFBbQFjUpjGCqptHm'); wo.privateKey.networkName.should.equal('testnet'); - - }); }); - }); var legacyPassword1 = '1DUpLRbuVpgLkcEY8gY8iod/SmA7+OheGZJ9PtvmTlvNE0FkEWpCKW9STdzXYJqbn0wiAapE4ojHNYj2hjYYAQ=='; diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 63a392ac0..71f8bac10 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -549,7 +549,6 @@ describe('WalletFactory model', function() { }); }); - }); From 861c1882df165066184895603c19032048839716 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 23 Sep 2014 16:01:02 -0300 Subject: [PATCH 3/4] Removed unused functions and refactored tests --- js/models/Storage.js | 90 +++++-------------- js/models/WalletFactory.js | 72 +++++++++------ test/test.Storage.js | 178 ++++++++++++++++++------------------- test/test.WalletFactory.js | 29 +++++- 4 files changed, 182 insertions(+), 187 deletions(-) diff --git a/js/models/Storage.js b/js/models/Storage.js index 9b5fb22f1..79c5c117b 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -122,16 +122,6 @@ Storage.prototype.setSessionId = function(sessionId, cb) { this.sessionStorage.setItem('sessionId', sessionId, cb); }; -Storage.prototype._key = function(walletId, k) { - return walletId + '::' + k; -}; -// get value by key -Storage.prototype.get = function(walletId, k, cb) { - preconditions.checkArgument(walletId, k, cb); - this._read(this._key(walletId, k), cb); -}; - - Storage.prototype._readHelper = function(walletId, k, cb) { var wk = this._key(walletId, k); this._read(wk, function(v) { @@ -139,33 +129,34 @@ Storage.prototype._readHelper = function(walletId, k, cb) { }); }; -Storage.prototype.readWallet = function(walletId, cb) { +Storage.prototype.readWallet_Old = function(walletId, cb) { var self = this; this.storage.allKeys(function(allKeys) { var obj = {}; var keys = _.filter(allKeys, function(k) { if (k.indexOf(walletId + '::') === 0) return true; }); - if (keys.length === 0) return cb(null); + if (keys.length === 0) return cb(new Error('Wallet ' + walletId + ' not found')); var count = keys.length; _.each(keys, function(k) { self._read(k, function(v) { obj[k.split('::')[1]] = v; - if (--count === 0) return cb(obj); + if (--count === 0) return cb(null, obj); }) }); }); }; -Storage.prototype.readWallet2 = function(walletId, cb) { +Storage.prototype.readWallet = function(walletId, cb) { var self = this; this.storage.allKeys(function(allKeys) { var keys = _.filter(allKeys, function(k) { if ((k === 'wallet::' + walletId) || k.indexOf('wallet::' + walletId) === 0) return true; }); - if (keys.length !== 1) throw new Error('WALLETNOTFOUND'); + if (keys.length === 0) return cb(new Error('Wallet ' + walletId + ' not found')); self._read(keys[0], function(v) { - return cb(v); + if (_.isNull(v)) return cb(new Error('Could not decrypt wallet data')); + return cb(null, v); }) }); }; @@ -189,32 +180,7 @@ Storage.prototype.getMany = function(walletId, keys, cb) { } }; -// set value for key -Storage.prototype.set = function(walletId, k, v, cb) { - preconditions.checkArgument(walletId && k && cb); - - if (_.isUndefined(v)) return cb(); - - this._write(this._key(walletId, k), v, cb); -}; - -// remove value for key -Storage.prototype.remove = function(walletId, k, cb) { - preconditions.checkArgument(walletId && k && cb); - this.removeGlobal(this._key(walletId, k), cb); -}; - -Storage.prototype.setName = function(walletId, name, cb) { - preconditions.checkArgument(walletId && name && cb); - this.setGlobal('nameFor::' + walletId, name, cb); -}; - -Storage.prototype.getName = function(walletId, cb) { - preconditions.checkArgument(walletId && cb); - this.getGlobal('nameFor::' + walletId, cb); -}; - -Storage.prototype.getWalletIds = function(cb) { +Storage.prototype._getWalletIds = function(cb) { preconditions.checkArgument(cb); var walletIds = []; var uniq = {}; @@ -225,7 +191,7 @@ Storage.prototype.getWalletIds = function(cb) { if (split.length == 2) { var walletId = split[0]; - if (!walletId || walletId === 'nameFor' || walletId === 'lock') + if (!walletId || walletId === 'nameFor' || walletId === 'lock' || walletId === 'wallet') continue; if (typeof uniq[walletId] === 'undefined') { @@ -238,7 +204,7 @@ Storage.prototype.getWalletIds = function(cb) { }); }; -Storage.prototype.getWallets = function(cb) { +Storage.prototype.getWallets_Old = function(cb) { preconditions.checkArgument(cb); if (this.wListCache.ts > Date.now()) @@ -247,14 +213,14 @@ Storage.prototype.getWallets = function(cb) { var wallets = []; var self = this; - this.getWalletIds(function(ids) { + this._getWalletIds(function(ids) { var l = ids.length, i = 0; if (!l) return cb([]); _.each(ids, function(id) { - self.getName(id, function(name) { + self.getGlobal('nameFor::' + id, function(name) { wallets.push({ id: id, name: name, @@ -290,8 +256,17 @@ Storage.prototype.getWallets2 = function(cb) { }); }; +Storage.prototype.getWallets = function(cb) { + var self = this; + self.getWallets2(function(wallets1) { + self.getWallets_Old(function(wallets2) { + return cb(wallets1.concat(wallets2)); + }); + }) +}; -Storage.prototype.deleteWallet = function(walletId, cb) { + +Storage.prototype.deleteWallet_Old = function(walletId, cb) { preconditions.checkArgument(walletId); preconditions.checkArgument(cb); var err; @@ -325,7 +300,7 @@ Storage.prototype.deleteWallet = function(walletId, cb) { }); }; -Storage.prototype.deleteWallet2 = function(walletId, cb) { +Storage.prototype.deleteWallet = function(walletId, cb) { preconditions.checkArgument(walletId); preconditions.checkArgument(cb); @@ -350,29 +325,10 @@ Storage.prototype.getLastOpened = function(cb) { this.getGlobal('lastOpened', cb); }; -//obj contains keys to be set Storage.prototype.setFromObj = function(walletId, obj, cb) { preconditions.checkArgument(cb); var self = this; - var l = Object.keys(obj).length, - i = 0; - for (var k in obj) { - self.set(walletId, k, obj[k], function() { - if (++i == l) { - if (obj.opts.name) - self.setName(walletId, obj.opts.name, cb); - else - return cb(); - } - }); - } -}; - -Storage.prototype.setFromObj2 = function(walletId, obj, cb) { - preconditions.checkArgument(cb); - var self = this; - var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : ''); self._write(key, obj, function() { return cb(); diff --git a/js/models/WalletFactory.js b/js/models/WalletFactory.js index b2b62e35b..12f84086e 100644 --- a/js/models/WalletFactory.js +++ b/js/models/WalletFactory.js @@ -146,28 +146,18 @@ WalletFactory.prototype.migrateWallet = function(walletId, passphrase, cb) { var self = this; self.storage.setPassphrase(passphrase); - self.storage.setFromObj = self.storage.setFromObj2; - - var reconfigureStorage = function() { - self.storage.getWallets = self.storage.getWallets2; - self.storage.readWallet = self.storage.readWallet2; - self.storage.deleteWallet = self.storage.deleteWallet2; - self.storage.get = function() { - throw 'DO NOT USE' - }; - self.storage.set = function() { - throw 'DO NOT USE' - }; - }; - - self.read(walletId, null, function(err, wallet) { + self.read_Old(walletId, null, function(err, wallet) { if (err) return cb(err); - wallet.store(); - self.storage.deleteWallet(walletId, function() { - self.storage.removeGlobal('nameFor::' + walletId, function() { - reconfigureStorage(); - return cb(); + wallet.store(function(err) { + if (err) return cb(err); + + self.storage.deleteWallet_Old(walletId, function(err) { + if (err) return cb(err); + + self.storage.removeGlobal('nameFor::' + walletId, function() { + return cb(); + }); }); }); }); @@ -186,9 +176,9 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) { err; var obj = {}; - console.log('aáaa'); - this.storage.readWallet(walletId, function(ret) { - console.log('bbb'); + this.storage.readWallet(walletId, function(err, ret) { + if (err) return cb(err); + _.each(Wallet.PERSISTED_PROPERTIES, function(p) { obj[p] = ret[p]; }); @@ -212,6 +202,36 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) { }); }; +WalletFactory.prototype.read_Old = function(walletId, skipFields, cb) { + var self = this, + err; + var obj = {}; + + this.storage.readWallet_Old(walletId, function(err, ret) { + if (err) return cb(err); + + _.each(Wallet.PERSISTED_PROPERTIES, function(p) { + obj[p] = ret[p]; + }); + + if (!_.any(_.values(obj))) + return cb(new Error('Wallet not found')); + + var w, err; + obj.id = walletId; + try { + w = self.fromObj(obj, skipFields); + } catch (e) { + if (e && e.message && e.message.indexOf('MISSOPTS')) { + err = new Error('Could not read: ' + walletId); + } else { + err = e; + } + w = null; + } + return cb(err, w); + }); +}; /** * @desc This method instantiates a wallet. Usefull for stubbing. @@ -368,10 +388,10 @@ WalletFactory.prototype.getWallets = function(cb) { * @return {?} the result of the callback */ WalletFactory.prototype.delete = function(walletId, cb) { - var s = this.storage; - s.deleteWallet(walletId, function(err) { + var self = this; + self.storage.deleteWallet(walletId, function(err) { if (err) return cb(err); - s.setLastOpened(null, function(err) { + self.storage.setLastOpened(null, function(err) { return cb(err); }); }); diff --git a/test/test.Storage.js b/test/test.Storage.js index be1dbafce..97fd90282 100644 --- a/test/test.Storage.js +++ b/test/test.Storage.js @@ -28,7 +28,7 @@ describe('Storage model', function() { it('should fail when encrypting without a password', function() { var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams); (function() { - s2.set(fakeWallet, timeStamp, 1, function() {}); + s2._write(fakeWallet + timeStamp, 1, function() {}); }).should.throw('NOPASSPHRASE'); }); it('should be able to encrypt and decrypt', function(done) { @@ -40,7 +40,7 @@ describe('Storage model', function() { }); }); it('should be able to set a value', function(done) { - s.set(fakeWallet, timeStamp, 1, function() { + s._write(fakeWallet + timeStamp, 1, function() { done(); }); }); @@ -63,8 +63,8 @@ describe('Storage model', function() { ]; getSetData.forEach(function(obj) { it('should be able to set a value and get it for ' + JSON.stringify(obj), function(done) { - s.set(fakeWallet, timeStamp, obj, function() { - s.get(fakeWallet, timeStamp, function(obj2) { + s._write(fakeWallet + timeStamp, obj, function() { + s._read(fakeWallet + timeStamp, function(obj2) { JSON.stringify(obj2).should.equal(JSON.stringify(obj)); done(); }); @@ -74,7 +74,7 @@ describe('Storage model', function() { describe('#export', function() { it('should export the encrypted wallet', function(done) { - s.set(fakeWallet, timeStamp, 'testval', function() { + s._write(fakeWallet + timeStamp, 'testval', function() { var obj = { test: 'testval' }; @@ -85,28 +85,11 @@ describe('Storage model', function() { }); }); - describe('#remove', function() { - it('should remove an item', function(done) { - s.set('1', "hola", 'juan', function() { - s.get('1', 'hola', function(v) { - v.should.equal('juan'); - s.remove('1', 'hola', function() { - s.get('1', 'hola', function(v) { - should.not.exist(v); - done(); - }); - }); - }) - }) - }); - }); - - - describe('#getWalletIds', function() { + describe('#_getWalletIds', function() { it('should get wallet ids', function(done) { - s.set('1', "hola", 'juan', function() { - s.set('2', "hola", 'juan', function() { - s.getWalletIds(function(v) { + s._write('1::hola', 'juan', function() { + s._write('2::hola', 'juan', function() { + s._getWalletIds(function(v) { v.should.deep.equal(['1', '2']); done(); }); @@ -115,17 +98,6 @@ describe('Storage model', function() { }); }); - describe('#getName #setName', function() { - it('should get/set names', function(done) { - s.setName(1, 'hola', function() { - s.getName(1, function(v) { - v.should.equal('hola'); - done(); - }); - }); - }); - }); - describe('#getLastOpened #setLastOpened', function() { it('should get/set last opened', function() { s.setLastOpened('hey', function() { @@ -150,13 +122,13 @@ describe('Storage model', function() { }); } - describe('#getWallets', function() { - it('should retreive wallets from storage', function(done) { - s.set('1', "hola", 'juan', function() { - s.set('2', "hola", 'juan', function() { - s.setName(1, 'hola', function() { + describe('#getWallets_Old', function() { + it('should retrieve wallets from storage', function(done) { + s._write('1::hola', 'juan', function() { + s._write('2::hola', 'juan', function() { + s.setGlobal('nameFor::1', 'hola', function() { - s.getWallets(function(ws) { + s.getWallets_Old(function(ws) { ws[0].should.deep.equal({ id: '1', name: 'hola', @@ -171,19 +143,19 @@ describe('Storage model', function() { }); }); }); - it('should retreive wallets from storage (with delay)', function(done) { - s.set('1', "hola", 'juan', function() { - s.set('2', "hola", 'juan', function() { - s.setName(1, 'hola', function() { + it('should retrieve wallets from storage (with delay)', function(done) { + s._write('1::hola', 'juan', function() { + s._write('2::hola', 'juan', function() { + s.setGlobal('nameFor::1', 'hola', function() { - var orig = s.getName.bind(s); - s.getName = function(wid, cb) { + var orig = s.getGlobal.bind(s); + s.getGlobal = function(k, cb) { setTimeout(function() { - orig(wid, cb); + orig(k, cb); }, 1); }; - s.getWallets(function(ws) { + s.getWallets_Old(function(ws) { ws[0].should.deep.equal({ id: '1', name: 'hola', @@ -201,7 +173,7 @@ describe('Storage model', function() { }); describe('#getWallets2', function() { - it('should retreive wallets from storage', function(done) { + it('should retrieve wallets from storage', function(done) { var w1 = { name: 'juan', opts: { @@ -211,8 +183,8 @@ describe('Storage model', function() { var w2 = { name: 'pepe' }; - s.setFromObj2('1', w1, function() { - s.setFromObj2('2', w2, function() { + s.setFromObj('1', w1, function() { + s.setFromObj('2', w2, function() { s.getWallets2(function(ws) { ws[0].should.deep.equal({ id: '1', @@ -229,11 +201,51 @@ describe('Storage model', function() { }); }); - describe('#deleteWallet', function() { + + describe('#getWallets', function() { + it('should retrieve wallets from storage both new and old format', function(done) { + var w1 = { + name: 'juan', + opts: { + name: 'wallet1' + } + }; + var w2 = { + name: 'pepe' + }; + + s.setFromObj('1', w1, function() { + s.setFromObj('2', w2, function() { + s._write('3::name', 'matias', function() { + s.setGlobal('nameFor::3', 'wallet3', function() { + s.getWallets(function(ws) { + ws.length.should.equal(3); + ws[0].should.deep.equal({ + id: '1', + name: 'wallet1', + }); + ws[1].should.deep.equal({ + id: '2', + name: undefined + }); + ws[2].should.deep.equal({ + id: '3', + name: 'wallet3', + }); + done(); + }); + }); + }) + }); + }); + }); + }); + + describe('#deleteWallet_Old', function() { it('should fail to delete a unexisting wallet', function(done) { - s.set('1', "hola", 'juan', function() { - s.set('2', "hola", 'juan', function() { - s.deleteWallet('3', function(err) { + s._write('1::hola', 'juan', function() { + s._write('2::hola', 'juan', function() { + s.deleteWallet_Old('3', function(err) { err.toString().should.include('WNOTFOUND'); done(); }); @@ -242,11 +254,11 @@ describe('Storage model', function() { }); it('should delete a wallet', function(done) { - s.set('1', "hola", 'juan', function() { - s.set('2', "hola", 'juan', function() { - s.deleteWallet('1', function(err) { + s._write('1::hola', 'juan', function() { + s._write('2::hola', 'juan', function() { + s.deleteWallet_Old('1', function(err) { should.not.exist(err); - s.getWallets(function(ws) { + s.getWallets_Old(function(ws) { ws.length.should.equal(1); ws[0].should.deep.equal({ id: '2', @@ -260,7 +272,7 @@ describe('Storage model', function() { }); }); - describe('#deleteWallet2', function() { + describe('#deleteWallet', function() { it('should fail to delete a unexisting wallet', function(done) { var w1 = { name: 'juan', @@ -272,9 +284,9 @@ describe('Storage model', function() { name: 'pepe' }; - s.setFromObj2('1', w1, function() { - s.setFromObj2('2', w2, function() { - s.deleteWallet2('3', function(err) { + s.setFromObj('1', w1, function() { + s.setFromObj('2', w2, function() { + s.deleteWallet('3', function(err) { err.toString().should.include('WNOTFOUND'); done(); }); @@ -293,9 +305,9 @@ describe('Storage model', function() { name: 'pepe' }; - s.setFromObj2('1', w1, function() { - s.setFromObj2('2', w2, function() { - s.deleteWallet2('1', function(err) { + s.setFromObj('1', w1, function() { + s.setFromObj('2', w2, function() { + s.deleteWallet('1', function(err) { should.not.exist(err); s.getWallets2(function(ws) { ws.length.should.equal(1); @@ -308,7 +320,7 @@ describe('Storage model', function() { }); }); - describe('#readWallet', function() { + describe('#readWallet_Old', function() { it('should read wallet', function(done) { var data = { 'id1::a': 'x', @@ -319,7 +331,8 @@ describe('Storage model', function() { sinon.stub(s, '_read', function(k, cb) { return cb(data[k]); }); - s.readWallet('id1', function(w) { + s.readWallet_Old('id1', function(err, w) { + should.not.exist(err); w.should.exist; w.hasOwnProperty('a').should.be.true; w.hasOwnProperty('b').should.be.true; @@ -332,7 +345,7 @@ describe('Storage model', function() { }); }); - describe('#readWallet2', function() { + describe('#readWallet', function() { it('should read wallet', function(done) { var data = { 'wallet::id1_wallet1': { @@ -347,7 +360,8 @@ describe('Storage model', function() { sinon.stub(s, '_read', function(k, cb) { return cb(data[k]); }); - s.readWallet2('id1', function(w) { + s.readWallet('id1', function(err, w) { + should.not.exist(err); w.should.exist; w.hasOwnProperty('a').should.be.true; w.hasOwnProperty('b').should.be.true; @@ -361,24 +375,8 @@ describe('Storage model', function() { }); describe('#setFromObj', function() { - it('set localstorage from an object', function(done) { - s.setFromObj('id1', { - 'key': 'val', - 'opts': { - 'name': 'nameid1' - }, - }, function() { - s.get('id1', 'key', function(v) { - v.should.equal('val'); - done(); - }); - }); - }); - }); - - describe('#setFromObj2', function() { it('should store from an object as single key', function(done) { - s.setFromObj2('id1', { + s.setFromObj('id1', { 'key': 'val', 'opts': { 'name': 'nameid1' diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 71f8bac10..b20511800 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -40,7 +40,6 @@ describe('WalletFactory model', function() { wf.storage.setLastOpened = sinon.stub().yields(null); - var w = sinon.stub(); w.store = sinon.stub().yields(null); @@ -246,7 +245,7 @@ describe('WalletFactory model', function() { describe('#read', function() { it('should fail to read unexisting wallet', function(done) { - wf.storage.readWallet = sinon.stub().yields({}); + wf.storage.readWallet = sinon.stub().yields(null, {}); wf.read('id', [], function(err, w) { should.not.exist(w); @@ -258,7 +257,7 @@ describe('WalletFactory model', function() { }); }); it('should fail to read broken wallet', function(done) { - wf.storage.readWallet = sinon.stub().yields({ + wf.storage.readWallet = sinon.stub().yields(null, { 'opts': 1 }); wf.read('id', [], function(err, w) { @@ -272,7 +271,7 @@ describe('WalletFactory model', function() { }); it('should read existing wallet', function(done) { var wf = new WalletFactory(config, '0.0.1'); - wf.storage.readWallet = sinon.stub().yields({ + wf.storage.readWallet = sinon.stub().yields(null, { 'opts': 1 }); wf.fromObj = sinon.stub().returns('ok'); @@ -298,6 +297,7 @@ describe('WalletFactory model', function() { var s1 = sinon.stub(); s1.store = sinon.stub().yields(null); wf.read = sinon.stub().yields(null, s1); + wf.migrateWallet = sinon.stub().yields(null); wf.storage.setLastOpened = sinon.stub().yields(null); wf.open('dummy', 'xxx', function(err, w) { @@ -314,6 +314,7 @@ describe('WalletFactory model', function() { var s1 = sinon.stub(); s1.store = sinon.stub().yields(null); wf.read = sinon.stub().yields(null, s1); + wf.migrateWallet = sinon.stub().yields(null); wf.storage.setLastOpened = sinon.stub().yields(null); wf.open('dummy', 'xxx', function(err, w) { @@ -331,6 +332,7 @@ describe('WalletFactory model', function() { var s1 = sinon.stub(); s1.store = sinon.stub().yields(null); wf.read = sinon.stub().yields(null, s1); + wf.migrateWallet = sinon.stub().yields(null); wf.storage.setLastOpened = sinon.stub().yields(null); wf.open('dummy', 'xxx', function(err, w) { @@ -346,6 +348,7 @@ describe('WalletFactory model', function() { var s1 = sinon.stub(); s1.store = sinon.stub().yields(null); wf.read = sinon.stub().yields(null, s1); + wf.migrateWallet = sinon.stub().yields(null); wf.storage.setLastOpened = sinon.stub().yields(null); wf.open('dummy', 'xxx', function(err, w) { @@ -354,6 +357,24 @@ describe('WalletFactory model', function() { done(); }); }); + it('should call #migrateWallet', function(done) { + var wf = new WalletFactory(config, '0.0.1'); + wf.storage.setPassphrase = sinon.spy(); + + var s1 = sinon.stub(); + s1.store = sinon.stub().yields(null); + wf.read = sinon.stub().yields(null, s1); + wf.migrateWallet = sinon.stub().yields(null); + wf.storage.deleteWallet_Old = sinon.stub().yields(null); + wf.storage.removeGlobal = sinon.stub().yields(null); + wf.storage.setLastOpened = sinon.stub().yields(null); + + wf.open('dummy', 'xxx', function(err, w) { + wf.migrateWallet.calledOnce.should.equal(true); + wf.migrateWallet.getCall(0).args[0].should.equal('dummy'); + done(); + }); + }); }); describe('#create', function() { From 48c3633ecef566d02e1cdfcdfecdf8aea9acffa3 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 24 Sep 2014 09:39:17 -0300 Subject: [PATCH 4/4] Wallets in both formats only get listed once --- js/models/Storage.js | 9 +++++++-- test/test.Storage.js | 32 +++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/js/models/Storage.js b/js/models/Storage.js index 79c5c117b..3747971de 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -258,9 +258,14 @@ Storage.prototype.getWallets2 = function(cb) { Storage.prototype.getWallets = function(cb) { var self = this; - self.getWallets2(function(wallets1) { + self.getWallets2(function(wallets) { self.getWallets_Old(function(wallets2) { - return cb(wallets1.concat(wallets2)); + var ids = _.pluck(wallets, 'id'); + _.each(wallets2, function(w) { + if (!_.contains(ids, w.id)) + wallets.push(w); + }); + return cb(wallets); }); }) }; diff --git a/test/test.Storage.js b/test/test.Storage.js index 97fd90282..6e76213e3 100644 --- a/test/test.Storage.js +++ b/test/test.Storage.js @@ -217,22 +217,24 @@ describe('Storage model', function() { s.setFromObj('1', w1, function() { s.setFromObj('2', w2, function() { s._write('3::name', 'matias', function() { - s.setGlobal('nameFor::3', 'wallet3', function() { - s.getWallets(function(ws) { - ws.length.should.equal(3); - ws[0].should.deep.equal({ - id: '1', - name: 'wallet1', + s._write('1::name', 'juan', function() { + s.setGlobal('nameFor::3', 'wallet3', function() { + s.getWallets(function(ws) { + ws.length.should.equal(3); + ws[0].should.deep.equal({ + id: '1', + name: 'wallet1', + }); + ws[1].should.deep.equal({ + id: '2', + name: undefined + }); + ws[2].should.deep.equal({ + id: '3', + name: 'wallet3', + }); + done(); }); - ws[1].should.deep.equal({ - id: '2', - name: undefined - }); - ws[2].should.deep.equal({ - id: '3', - name: 'wallet3', - }); - done(); }); }); })