mv Passphrase logic to Storage
This commit is contained in:
parent
70d306242e
commit
74129a6923
4 changed files with 20 additions and 8 deletions
|
|
@ -113,7 +113,6 @@ Identity.create = function(email, password, opts, cb) {
|
||||||
Identity._createProfile(email, password, iden.storage, function(err, profile) {
|
Identity._createProfile(email, password, iden.storage, function(err, profile) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
iden.profile = profile;
|
iden.profile = profile;
|
||||||
console.log('[Identity.js.115:profile:]',profile); //TODO
|
|
||||||
|
|
||||||
if (opts.noWallets)
|
if (opts.noWallets)
|
||||||
cb(null, iden);
|
cb(null, iden);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
var preconditions = require('preconditions').singleton();
|
var preconditions = require('preconditions').singleton();
|
||||||
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
|
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
|
var Passphrase = require('./Passphrase');
|
||||||
var preconditions = require('preconditions').instance();
|
var preconditions = require('preconditions').instance();
|
||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
var CACHE_DURATION = 1000 * 60 * 5;
|
var CACHE_DURATION = 1000 * 60 * 5;
|
||||||
|
|
@ -17,11 +18,15 @@ var id = 0;
|
||||||
function Storage(opts) {
|
function Storage(opts) {
|
||||||
preconditions.checkArgument(opts);
|
preconditions.checkArgument(opts);
|
||||||
preconditions.checkArgument(opts.password);
|
preconditions.checkArgument(opts.password);
|
||||||
opts = opts || {};
|
|
||||||
|
|
||||||
this.wListCache = {};
|
this.wListCache = {};
|
||||||
this.__uniqueid = ++id;
|
this.__uniqueid = ++id;
|
||||||
this.setPassphrase(opts.password);
|
this.passphraseConfig = {
|
||||||
|
salt: opts.salt,
|
||||||
|
iterations: opts.iterations,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.setPassword(opts.password);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.db = opts.db || localStorage;
|
this.db = opts.db || localStorage;
|
||||||
|
|
@ -48,8 +53,15 @@ Storage.prototype.hasPassphrase = function() {
|
||||||
return pps[this.__uniqueid] ? true : false;
|
return pps[this.__uniqueid] ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.setPassphrase = function(password) {
|
|
||||||
pps[this.__uniqueid] = password;
|
Storage.prototype._setPassphrase = function(passphrase) {
|
||||||
|
pps[this.__uniqueid] = passphrase;
|
||||||
|
};
|
||||||
|
|
||||||
|
Storage.prototype.setPassword = function(password, config) {
|
||||||
|
var passphraseConfig = _.extend(this.passphraseConfig, config);
|
||||||
|
var p = new Passphrase(passphraseConfig);
|
||||||
|
this._setPassphrase(p.getBase64(password));
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage.prototype._encrypt = function(string) {
|
Storage.prototype._encrypt = function(string) {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ describe('Storage model', function() {
|
||||||
var s;
|
var s;
|
||||||
beforeEach(function(done) {
|
beforeEach(function(done) {
|
||||||
s = new Storage(requireMock('FakeLocalStorage').storageParams);
|
s = new Storage(requireMock('FakeLocalStorage').storageParams);
|
||||||
s.setPassphrase('mysupercoolpassword');
|
s.setPassword('mysupercoolpassword');
|
||||||
s.clearAll(done);
|
s.clearAll(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -415,13 +415,13 @@ describe('Storage model', function() {
|
||||||
|
|
||||||
describe('#decrypt', function() {
|
describe('#decrypt', function() {
|
||||||
it('should not be able to decrypt with wrong password', function() {
|
it('should not be able to decrypt with wrong password', function() {
|
||||||
s.setPassphrase('xxx');
|
s.setPassword('xxx');
|
||||||
var wo = s.decrypt(encryptedLegacy1);
|
var wo = s.decrypt(encryptedLegacy1);
|
||||||
should.not.exist(wo);
|
should.not.exist(wo);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to decrypt an old backup', function() {
|
it('should be able to decrypt an old backup', function() {
|
||||||
s.setPassphrase(legacyPassword1);
|
s._setPassphrase(legacyPassword1);
|
||||||
var wo = s.decrypt(encryptedLegacy1);
|
var wo = s.decrypt(encryptedLegacy1);
|
||||||
should.exist(wo);
|
should.exist(wo);
|
||||||
wo.opts.id.should.equal('48ba2f1ffdfe9708');
|
wo.opts.id.should.equal('48ba2f1ffdfe9708');
|
||||||
|
|
|
||||||
|
|
@ -32,4 +32,5 @@ module.exports.storageParams = {
|
||||||
password: '123',
|
password: '123',
|
||||||
db: new FakeLocalStorage(),
|
db: new FakeLocalStorage(),
|
||||||
sessionStorage: new FakeLocalStorage(),
|
sessionStorage: new FakeLocalStorage(),
|
||||||
|
iterations: 1,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue