add setFromObj and getEncryptedObj to storage classes

This is so that you can export the file from the browser and use the same file
in the wallet file from the command-line. I have made encryption work
equivalently between the browser and node.
This commit is contained in:
Ryan X. Charles 2014-04-17 18:04:56 -03:00
commit 69c5c3bc2e
8 changed files with 140 additions and 6 deletions

View file

@ -1,9 +1,11 @@
'use strict';
var chai = chai || require('chai');
var chai = require('chai');
var should = chai.should();
var Storage = Storage || require('../js/models/storage/File.js');
var sinon = sinon || require('sinon');
var Storage = require('../js/models/storage/File.js');
var sinon = require('sinon');
var crypto = require('crypto');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
describe('Storage/File', function() {
it('should exist', function() {
@ -135,6 +137,37 @@ describe('Storage/File', function() {
});
});
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 hex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(encrypted.toString()));
var storage = new Storage({password: 'password'});
storage.data['walletId'] = obj;
var enc = storage.getEncryptedObj('walletId');
enc.length.should.equal(96);
enc.slice(0,10).should.equal(hex.slice(0,10));
enc.slice(0,6).should.equal("53616c");
});
});
describe('#clearAll', function() {
it('should set data to {}', function() {

View file

@ -45,5 +45,17 @@ if (typeof process === 'undefined' || !process.version) {
JSON.stringify(obj2).should.equal(JSON.stringify(obj));
});
});
describe('#getEncryptedObj', function() {
it('should encrypt the wallet', function() {
localStorage.clear();
var storage = new LocalEncrypted({password: 'password'});
storage.set('walletId', 'test', 'testval');
var obj = {test:'testval'};
var encrypted = storage.getEncryptedObj('walletId');
encrypted.length.should.equal(96);
encrypted.slice(0,6).should.equal("53616c");
});
});
});
}

View file

@ -13,5 +13,15 @@ if (typeof process === 'undefined' || !process.version) {
var s = new LocalPlain();
should.exist(s);
});
describe('#setFromObj', function() {
it('should set keys from an object', function() {
localStorage.clear();
var obj = {test:'testval'};
var storage = new LocalPlain();
storage.setFromObj('walletId', obj);
storage.get('walletId', 'test').should.equal('testval');
});
});
});
}