From 23e6971e768ded6e03f0242599542e9ab83bc018 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 21 Aug 2014 19:22:25 -0400 Subject: [PATCH] remove File class --- js/models/storage/File.js | 149 ---------------------------- test/test.storage.File.js | 204 -------------------------------------- util/build.js | 3 - 3 files changed, 356 deletions(-) delete mode 100644 js/models/storage/File.js delete mode 100644 test/test.storage.File.js diff --git a/js/models/storage/File.js b/js/models/storage/File.js deleted file mode 100644 index ec0880145..000000000 --- a/js/models/storage/File.js +++ /dev/null @@ -1,149 +0,0 @@ -'use strict'; -var fs = require('fs'); -var CryptoJS = require('node-cryptojs-aes').CryptoJS; - -var passwords = []; - -function Storage(opts) { - opts = opts || {}; - - this.data = {}; - passwords[0] = opts.password; -} - -Storage.prototype._encrypt = function(string) { - var encrypted = CryptoJS.AES.encrypt(string, passwords[0]); - var encryptedBase64 = encrypted.toString(); - return encryptedBase64; -}; - -Storage.prototype._encryptObj = function(obj) { - var string = JSON.stringify(obj); - return this._encrypt(string); -}; - -Storage.prototype._decrypt = function(base64) { - var decrypted = CryptoJS.AES.decrypt(base64, passwords[0]); - var decryptedStr = decrypted.toString(CryptoJS.enc.Utf8); - return decryptedStr; -}; - -Storage.prototype._decryptObj = function(base64) { - var decryptedStr = this._decrypt(base64); - return JSON.parse(decryptedStr); -}; - -Storage.prototype.load = function(walletId, callback) { - var self = this; - fs.readFile(walletId, function(err, base64) { - if (typeof base64 !== 'string') - base64 = base64.toString(); - var data = self._decryptObj(base64); - - if (err) return callback(err); - - try { - this.data[walletId] = JSON.parse(data); - } catch (err) { - if (callback) - return callback(err); - } - - if (callback) - return callback(null); - }); -}; - -Storage.prototype.save = function(walletId, callback) { - var obj = this.data[walletId]; - var encryptedBase64 = this._encryptObj(obj); - - //TODO: update to use a queue to ensure that saves are made sequentially - fs.writeFile(walletId, encryptedBase64, function(err) { - if (callback) - return callback(err); - }); -}; - -Storage.prototype._read = function(k) { - var split = k.split('::'); - var walletId = split[0]; - var key = split[1]; - return this.data[walletId][key]; -}; - -Storage.prototype._write = function(k, v, callback) { - var split = k.split('::'); - var walletId = split[0]; - var key = split[1]; - if (!this.data[walletId]) - this.data[walletId] = {}; - this.data[walletId][key] = v; - this.save(walletId, callback); -}; - -// get value by key -Storage.prototype.getGlobal = function(k) { - return this._read(k); -}; - -// set value for key -Storage.prototype.setGlobal = function(k, v, callback) { - this._write(k, v, callback); -}; - -// remove value for key -Storage.prototype.removeGlobal = function(k, callback) { - var split = k.split('::'); - var walletId = split[0]; - var key = split[1]; - delete this.data[walletId][key]; - this.save(walletId, callback); -}; - -Storage.prototype._key = function(walletId, k) { - return walletId + '::' + k; -}; - -// get value by key -Storage.prototype.get = function(walletId, k) { - return this.getGlobal(this._key(walletId, k)); -}; - -// set value for key -Storage.prototype.set = function(walletId, k, v, callback) { - this.setGlobal(this._key(walletId, k), v, callback); -}; - -// remove value for key -Storage.prototype.remove = function(walletId, k, callback) { - this.removeGlobal(this._key(walletId, k), callback); -}; - -Storage.prototype.getWalletIds = function() { - return []; -}; - -Storage.prototype.setFromObj = function(walletId, obj, callback) { - this.data[walletId] = obj; - this.save(walletId, callback); -}; - -Storage.prototype.setFromEncryptedObj = function(walletId, base64, callback) { - var obj = this._decryptObj(base64); - this.setFromObj(walletId, obj, callback); -}; - -Storage.prototype.getEncryptedObj = function(walletId) { - var encryptedBase64 = this._encryptObj(this.data[walletId]); - - return encryptedBase64; -}; - -// remove all values -Storage.prototype.clearAll = function(callback) { - this.data = {}; - this.save(callback); -}; - -module.exports = Storage; diff --git a/test/test.storage.File.js b/test/test.storage.File.js deleted file mode 100644 index ae1c516a2..000000000 --- a/test/test.storage.File.js +++ /dev/null @@ -1,204 +0,0 @@ -'use strict'; - -var chai = chai || require('chai'); -var should = chai.should(); -var Storage = require('../js/models/storage/File'); -var sinon = require('sinon'); -var CryptoJS = require('node-cryptojs-aes').CryptoJS; - -var mock = require('mock-fs'); - -describe('Storage/File', function() { - it('should exist', function() { - should.exist(Storage); - }); - - var mockFS = function() { - var obj = { - "test": "test" - }; - var encryptedStr = CryptoJS.AES.encrypt(JSON.stringify(obj), 'password').toString(); - mock({ - 'myfilename': encryptedStr - }); - }; - - describe('#load', function(done) { - it('should call fs.readFile', function(done) { - mockFS(); - var storage = new Storage({ - password: 'password' - }); - storage.load('myfilename', function(err) { - mock.restore(); - done(); - }); - }); - }); - - describe('#save', function(done) { - it('should call fs.writeFile', function(done) { - mockFS(); - var storage = new Storage({ - password: 'password' - }); - storage.save('myfilename', function(err) { - mock.restore(); - done(); - }); - }); - }); - - describe('#_read', function() { - it('should return the value of a key', function() { - var storage = new Storage(); - storage.data = { - 'walletId': { - 'test': 'data' - } - }; - storage._read('walletId::test').should.equal('data'); - }); - }); - - describe('#_write', function() { - it('should save the value of a key and then run save', function(done) { - var storage = new Storage(); - storage.save = function(walletId, callback) { - storage.data[walletId]['key'].should.equal('value'); - callback(); - }; - storage._write('walletId::key', 'value', function() { - done(); - }); - }); - }); - - describe('#getGlobal', function() { - it('should call storage._read', function() { - var storage = new Storage(); - storage.data = { - 'walletId': { - 'test': 'test' - } - }; - storage._read = sinon.spy(); - storage.getGlobal('walletId::test'); - storage._read.calledOnce.should.equal(true); - }); - }); - - describe('#setGlobal', function() { - it('should store a global key', function(done) { - var storage = new Storage(); - storage.save = function(walletId, callback) { - storage.data[walletId]['key'].should.equal('value'); - callback(); - }; - storage.setGlobal('walletId::key', 'value', function() { - done(); - }); - }); - }); - - describe('#removeGlobal', function() { - it('should remove a global key', function(done) { - var storage = new Storage(); - storage.data = { - 'walletId': { - 'key': 'value' - } - }; - storage.save = function(walletId, callback) { - should.not.exist(storage.data[walletId]['key']); - callback(); - }; - storage.removeGlobal('walletId::key', function() { - done(); - }); - }); - }); - - describe('#_key', function() { - it('should merge the wallet id and item key', function() { - var storage = new Storage(); - storage._key('wallet', 'key').should.equal('wallet::key'); - }); - }); - - describe('#get', function() { - it('should call getGlobal with the correct key', function() { - var storage = new Storage(); - storage.getGlobal = sinon.spy(); - storage.get('wallet', 'key'); - storage.getGlobal.calledOnce.should.equal(true); - storage.getGlobal.calledWith('wallet::key').should.equal(true); - }); - }); - - describe('#set', function() { - it('should call setGlobal with the correct key', function() { - var storage = new Storage(); - storage.setGlobal = sinon.spy(); - storage.set('wallet', 'key'); - storage.setGlobal.calledOnce.should.equal(true); - storage.setGlobal.calledWith('wallet::key').should.equal(true); - }); - }); - - describe('#remove', function() { - it('should call removeGlobal with the correct key', function() { - var storage = new Storage(); - storage.removeGlobal = sinon.spy(); - storage.remove('wallet', 'key'); - storage.removeGlobal.calledOnce.should.equal(true); - storage.removeGlobal.calledWith('wallet::key').should.equal(true); - }); - }); - - describe('#setFromObj', function() { - it('should set this object for a wallet', function(done) { - var obj = { - test: 'testval' - }; - var storage = new Storage(); - storage.save = function(walletId, callback) { - callback(); - }; - storage.setFromObj('walletId', obj, function() { - storage.data.walletId.test.should.equal('testval'); - done(); - }); - }); - }); - - describe('#getEncryptedObj', function() { - it('should give an encrypted object', function() { - var obj = { - test: 'testval' - }; - var data = JSON.stringify(obj); - var encrypted = CryptoJS.AES.encrypt(data, 'password'); - var base64 = encrypted.toString(); - - var storage = new Storage({ - password: 'password' - }); - storage.data['walletId'] = obj; - - var enc = storage.getEncryptedObj('walletId'); - //enc.length.should.equal(96); - enc.length.should.be.greaterThan(10); - enc.slice(0, 10).should.equal(base64.slice(0, 10)); - //enc.slice(0,6).should.equal("53616c"); - }); - }); - - describe('#clearAll', function() { - it('should set data to {}', function() { - - }); - }); - -}); - diff --git a/util/build.js b/util/build.js index 5495a04a2..db14b847d 100644 --- a/util/build.js +++ b/util/build.js @@ -59,9 +59,6 @@ var createBundle = function(opts) { b.require('./js/models/core/HDPath', { expose: '../js/models/core/HDPath' }); - b.require('./js/models/storage/File', { - expose: '../js/models/storage/File' - }); b.require('./config', { expose: '../config' });