add store/restore passphrasse methods

This commit is contained in:
Matias Alejo Garcia 2014-10-14 09:58:12 -03:00
parent d9cd2e95d4
commit f1ae8f9c33
5 changed files with 32 additions and 7 deletions

View File

@ -295,14 +295,17 @@ Identity.prototype.close = function(cb) {
* @return {Wallet}
*/
Identity.prototype.importWallet = function(base64, password, skipFields, cb) {
preconditions.checkArgument(password);
preconditions.checkArgument(cb);
this.storage.savePassphrase();
this.storage.setPassword(password);
var obj = this.storage.decrypt(base64);
if (!obj) return false;
this.storage.restorePassphrase();
if (!obj) return false;
var w = Identity._walletFromObj(obj, this.storage, this.networkOpts, this.blockchainOpts);
console.log('[Identity.js.307:Identity:]',w); //TODO
this._checkVersion(w.version);
this.addWallet(w, function(err) {
if (err) return cb(err);

View File

@ -45,8 +45,24 @@ Storage.prototype._getPassphrase = function() {
throw new Error('NOPASSPHRASE: No passphrase set');
return pps[this.__uniqueid];
}
};
Storage.prototype.savePassphrase = function() {
if (!pps[this.__uniqueid])
throw new Error('NOPASSPHRASE: No passphrase set');
this.savedPassphrase = this.savedPassphrase || {};
this.savedPassphrase[this.__uniqueid] = pps[this.__uniqueid];
};
Storage.prototype.restorePassphrase = function() {
if (!this.savedPassphrase[this.__uniqueid])
throw new Error('NOSTOREDPASSPHRASE: No stored passphrase');
pps[this.__uniqueid] = this.savedPassphrase[this.__uniqueid];
this.savedPassphrase[this.__uniqueid] = undefined;
};
Storage.prototype.hasPassphrase = function() {
return pps[this.__uniqueid] ? true : false;

View File

@ -32,6 +32,8 @@ describe('Identity model', function() {
beforeEach(function(done) {
storage = sinon.stub();
storage.getItem = sinon.stub();
storage.savePassphrase = sinon.spy();
storage.restorePassphrase = sinon.spy();
storage.setPassword = sinon.spy();
storage.hasPassphrase = sinon.stub().returns(true);
storage.getSessionId = sinon.spy();
@ -127,7 +129,7 @@ describe('Identity model', function() {
describe('#open', function(done) {
beforeEach(function() {
storage.getFirst = sinon.stub().yields('wallet1234');
storage.getFirst = sinon.stub().yields(null, 'wallet1234');
profile.listWallets = sinon.stub().returns([{id:'walletid'}]);
Identity._openProfile = sinon.stub().callsArgWith(3, null, profile);
Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet);
@ -230,7 +232,7 @@ describe('Identity model', function() {
beforeEach(function() {
iden.migrateWallet = sinon.stub().yields(null);
storage.setPassword = sinon.spy();
storage.getFirst = sinon.stub().yields('wallet1234');
storage.getFirst = sinon.stub().yields(null, 'wallet1234');
var wallet = sinon.stub();
wallet.store = sinon.stub().yields(null);
@ -256,6 +258,7 @@ describe('Identity model', function() {
beforeEach(function() {
iden.migrateWallet = sinon.stub().yields(null);
storage.getFirst = sinon.stub().yields(null, 'wallet1234');
});
it('should create wallet from encrypted object', function(done) {
@ -266,9 +269,12 @@ describe('Identity model', function() {
wallet.getId = sinon.stub().returns('ID123');
Identity._walletFromObj = sinon.stub().returns(wallet);
Identity._walletRead = sinon.stub().yields(null,wallet);
iden.importWallet("encrypted object", "xxx", [], function(err) {
iden.openWallet('ID123', function(err, w) {
iden.storage.savePassphrase.calledOnce.should.equal(true);
iden.storage.restorePassphrase.calledOnce.should.equal(true);
should.not.exist(err);
should.exist(w);
done();

View File

@ -22,7 +22,7 @@ describe('Storage model', function() {
var s2 = new Storage(requireMock('FakeLocalStorage').storageParams);
(function() {
var params = _.clone(requireMock('FakeLocalStorage').storageParams);
params.password = undefined;
params.passphrase = '1234';
new Storage(params);
}).should.throw('Illegal Argument');
});

View File

@ -32,7 +32,7 @@ module.exports.storageParams = {
password: '123',
db: new FakeLocalStorage(),
sessionStorage: new FakeLocalStorage(),
passphrase: {
passphraseConfig: {
iterations: 1,
},
};